io-png.c 4.44 KB
Newer Older
1 2
/* GdkPixbuf library - JPEG image loader
 *
3
 * Copyright (C) 1999 Mark Crichton
4 5 6 7
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Mark Crichton <crichton@gimp.org>
 *          Federico Mena-Quintero <federico@gimp.org>
8
 *
9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
20 21 22
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
23
 */
24

25 26 27
#include <config.h>
#include <stdio.h>
#include <png.h>
28 29 30 31 32 33 34 35 36 37
#include "gdk-pixbuf.h"



/* Destroy notification function for the libart pixbuf */
static void
free_buffer (gpointer user_data, gpointer data)
{
	free (data);
}
38 39

/* Shared library entry point */
40 41
GdkPixbuf *
image_load (FILE *f)
42
{
43 44
	png_structp png_ptr;
	png_infop info_ptr, end_info;
45
	gint i, depth, ctype, inttype, passes, bpp;
46 47
	png_uint_32 w, h, x, y;
	png_bytepp rows;
48
	guchar *pixels, *temp, *rowdata;
49

50
	png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	if (!png_ptr)
		return NULL;

	info_ptr = png_create_info_struct (png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct (&png_ptr, NULL, NULL);
		return NULL;
	}

	end_info = png_create_info_struct (png_ptr);
	if (!end_info) {
		png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
		return NULL;
	}

	if (setjmp (png_ptr->jmpbuf)) {
		png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
		return NULL;
	}

71
	png_init_io (png_ptr, f);
72
	png_read_info (png_ptr, info_ptr);
73
	png_get_IHDR (png_ptr, info_ptr, &w, &h, &depth, &ctype, &inttype, NULL, NULL);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	/* Ok, we want to work with 24 bit images.
	 * However, PNG can vary depth per channel.
	 * So, we use the png_set_expand function to expand
	 * everything into a format libart expects.
	 * We also use png_set_strip_16 to reduce down to 8 bit/chan.
	 */
	if (ctype == PNG_COLOR_TYPE_PALETTE && depth <= 8)
		png_set_expand (png_ptr);

	if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8)
		png_set_expand (png_ptr);

	if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS)) {
		png_set_expand (png_ptr);
		g_warning ("FIXME: We are going to crash");
90
	}
91

92 93
	if (depth == 16)
		png_set_strip_16 (png_ptr);
94

95 96 97
	/* We also have png "packing" bits into bytes if < 8 */
	if (depth < 8)
		png_set_packing (png_ptr);
98

99 100 101
	/* Lastly, if the PNG is greyscale, convert to RGB */
	if (ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_GRAY_ALPHA)
		png_set_gray_to_rgb (png_ptr);
102

103 104 105 106 107 108 109 110 111 112 113 114 115
	/* ...and if we're interlaced... */
	passes = png_set_interlace_handling (png_ptr);

	/* Update our info structs */
	png_read_update_info (png_ptr, info_ptr);

	/* Allocate some memory and set up row array */
	/* This "inhales vigorously"... */
	if (ctype & PNG_COLOR_MASK_ALPHA)
		bpp = 4;
	else
		bpp = 3;

116 117
	pixels = malloc (w * h * bpp);
	rows = malloc (h * sizeof (png_bytep));
118 119

	if (!pixels || !rows) {
120 121 122 123 124 125
		if (pixels)
			free (pixels);

		if (rows)
			free (rows);

126 127 128
		png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
		return NULL;
	}
129

130 131
	/* Icky code, but it has to be done... */
	for (i = 0; i < h; i++) {
132
		if ((rows[i] = malloc (w * bpp)) == NULL) {
133
			int n;
134

135
			for (n = 0; n < i; n++)
136 137 138 139
				free (rows[i]);

			free (rows);
			free (pixels);
140 141 142
			png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
			return NULL;
		}
143 144
	}

145 146 147
	/* And we FINALLY get here... */
	png_read_image (png_ptr, rows);
	png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
148

149 150 151 152 153
	/* Now stuff the bytes into pixels & free rows[y] */

	temp = pixels;

	for (y = 0; y < h; y++) {
154
		rowdata = rows[y];
155 156 157 158
		for (x = 0; x < w; x++) {
			temp[0] = rowdata[(x * bpp)];
			temp[1] = rowdata[(x * bpp) + 1];
			temp[2] = rowdata[(x * bpp) + 2];
159

160 161
			if (bpp == 4)
				temp[3] = rowdata[(x * bpp) + 3];
162

163 164
			temp += bpp;
		}
165
		free (rows[y]);
166
	}
167
	free (rows);
168

169
	if (ctype & PNG_COLOR_MASK_ALPHA)
170 171 172
		return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, TRUE,
						 w, h, w * 4,
						 free_buffer, NULL);
173
	else
174 175 176
		return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, FALSE,
						 w, h, w * 3,
						 free_buffer, NULL);
177
}