Back to index

lightning-sunbird  0.9+nobinonly
write_png.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2003 USC, Information Sciences Institute
00003  *
00004  * Permission to use, copy, modify, distribute, and sell this software
00005  * and its documentation for any purpose is hereby granted without
00006  * fee, provided that the above copyright notice appear in all copies
00007  * and that both that copyright notice and this permission notice
00008  * appear in supporting documentation, and that the name of the
00009  * University of Southern California not be used in advertising or
00010  * publicity pertaining to distribution of the software without
00011  * specific, written prior permission. The University of Southern
00012  * California makes no representations about the suitability of this
00013  * software for any purpose.  It is provided "as is" without express
00014  * or implied warranty.
00015  *
00016  * THE UNIVERSITY OF SOUTHERN CALIFORNIA DISCLAIMS ALL WARRANTIES WITH
00017  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
00018  * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF
00019  * SOUTHERN CALIFORNIA BE LIABLE FOR ANY SPECIAL, INDIRECT OR
00020  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
00021  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00022  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00023  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00024  *
00025  * Author: Carl D. Worth <cworth@cworth.org>
00026  */
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <png.h>
00031 
00032 #include "write_png.h"
00033 
00034 static void
00035 unpremultiply_data (png_structp png, png_row_infop row_info, png_bytep data)
00036 {
00037     int i;
00038 
00039     for (i = 0; i < row_info->rowbytes; i += 4) {
00040         unsigned char *b = &data[i];
00041         unsigned int pixel;
00042         unsigned char alpha;
00043 
00044        memcpy (&pixel, b, sizeof (unsigned int));
00045        alpha = (pixel & 0xff000000) >> 24;
00046         if (alpha == 0) {
00047            b[0] = b[1] = b[2] = b[3] = 0;
00048        } else {
00049             b[0] = (((pixel & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
00050             b[1] = (((pixel & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
00051             b[2] = (((pixel & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
00052            b[3] = alpha;
00053        }
00054     }
00055 }
00056 
00057 void
00058 write_png_argb32 (char *buffer, FILE *file,
00059                 int width, int height, int stride)
00060 {
00061     int i;
00062     png_struct *png;
00063     png_info *info;
00064     png_byte **rows;
00065     png_color_16 white;
00066     
00067     rows = malloc (height * sizeof(png_byte*));
00068 
00069     for (i = 0; i < height; i++) {
00070        rows[i] = buffer + i * stride;
00071     }
00072 
00073     png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00074     info = png_create_info_struct (png);
00075 
00076     png_init_io (png, file);
00077     png_set_IHDR (png, info,
00078                 width, height, 8,
00079                 PNG_COLOR_TYPE_RGB_ALPHA, 
00080                 PNG_INTERLACE_NONE,
00081                 PNG_COMPRESSION_TYPE_DEFAULT,
00082                 PNG_FILTER_TYPE_DEFAULT);
00083 
00084     white.red = 0xff;
00085     white.blue = 0xff;
00086     white.green = 0xff;
00087     png_set_bKGD (png, info, &white);
00088 
00089     png_set_write_user_transform_fn (png, unpremultiply_data);
00090     png_set_bgr (png);
00091 
00092     png_write_info (png, info);
00093     png_write_image (png, rows);
00094     png_write_end (png, info);
00095 
00096     png_destroy_write_struct (&png, &info);
00097 
00098     free (rows);
00099 }