1999-10-20 21:20:49 +00:00
|
|
|
|
/* GdkPixbuf library - JPEG image loader
|
|
|
|
|
*
|
1999-06-30 15:28:43 +00:00
|
|
|
|
* Copyright (C) 1999 Mark Crichton
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* Copyright (C) 1999 The Free Software Foundation
|
|
|
|
|
*
|
|
|
|
|
* Authors: Mark Crichton <crichton@gimp.org>
|
|
|
|
|
* Federico Mena-Quintero <federico@gimp.org>
|
1999-01-05 04:31:03 +00:00
|
|
|
|
*
|
1999-06-30 15:28:43 +00:00
|
|
|
|
* 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
|
1999-10-20 21:20:49 +00:00
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
1999-01-05 04:31:03 +00:00
|
|
|
|
*/
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-01-05 04:31:03 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <png.h>
|
1999-10-20 21:20:49 +00:00
|
|
|
|
#include "gdk-pixbuf.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Destroy notification function for the libart pixbuf */
|
|
|
|
|
static void
|
|
|
|
|
free_buffer (gpointer user_data, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
free (data);
|
|
|
|
|
}
|
1999-01-05 04:31:03 +00:00
|
|
|
|
|
|
|
|
|
/* Shared library entry point */
|
1999-10-18 19:29:45 +00:00
|
|
|
|
GdkPixbuf *
|
|
|
|
|
image_load (FILE *f)
|
1999-01-05 04:31:03 +00:00
|
|
|
|
{
|
1999-10-18 19:29:45 +00:00
|
|
|
|
png_structp png_ptr;
|
|
|
|
|
png_infop info_ptr, end_info;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
gint i, depth, ctype, inttype, passes, bpp;
|
1999-10-22 15:18:03 +00:00
|
|
|
|
png_uint_32 w, h;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
png_bytepp rows;
|
1999-10-22 15:18:03 +00:00
|
|
|
|
guchar *pixels;
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
png_init_io (png_ptr, f);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
png_read_info (png_ptr, info_ptr);
|
1999-10-20 21:20:49 +00:00
|
|
|
|
png_get_IHDR (png_ptr, info_ptr, &w, &h, &depth, &ctype, &inttype, NULL, NULL);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
|
|
|
|
|
/* 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");
|
1999-06-30 15:28:43 +00:00
|
|
|
|
}
|
1999-06-29 02:36:03 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (depth == 16)
|
|
|
|
|
png_set_strip_16 (png_ptr);
|
1999-06-29 02:54:16 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
/* We also have png "packing" bits into bytes if < 8 */
|
|
|
|
|
if (depth < 8)
|
|
|
|
|
png_set_packing (png_ptr);
|
1999-06-29 02:36:03 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
/* 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);
|
1999-01-05 21:27:07 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
/* ...and if we're interlaced... */
|
|
|
|
|
passes = png_set_interlace_handling (png_ptr);
|
|
|
|
|
|
|
|
|
|
png_read_update_info (png_ptr, info_ptr);
|
|
|
|
|
|
|
|
|
|
if (ctype & PNG_COLOR_MASK_ALPHA)
|
|
|
|
|
bpp = 4;
|
|
|
|
|
else
|
|
|
|
|
bpp = 3;
|
|
|
|
|
|
1999-10-20 21:20:49 +00:00
|
|
|
|
pixels = malloc (w * h * bpp);
|
1999-10-22 15:18:03 +00:00
|
|
|
|
if (!pixels) {
|
1999-10-18 19:29:45 +00:00
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-22 15:18:03 +00:00
|
|
|
|
rows = g_new (png_bytep, h);
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-22 15:18:03 +00:00
|
|
|
|
for (i = 0; i < h; i++)
|
|
|
|
|
rows[i] = pixels + i * w * bpp;
|
1999-10-20 21:20:49 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
png_read_image (png_ptr, rows);
|
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
1999-10-22 15:18:03 +00:00
|
|
|
|
g_free (rows);
|
1999-07-19 04:21:09 +00:00
|
|
|
|
|
1999-10-18 19:29:45 +00:00
|
|
|
|
if (ctype & PNG_COLOR_MASK_ALPHA)
|
1999-10-20 21:20:49 +00:00
|
|
|
|
return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, TRUE,
|
|
|
|
|
w, h, w * 4,
|
|
|
|
|
free_buffer, NULL);
|
1999-10-18 19:29:45 +00:00
|
|
|
|
else
|
1999-10-20 21:20:49 +00:00
|
|
|
|
return gdk_pixbuf_new_from_data (pixels, ART_PIX_RGB, FALSE,
|
|
|
|
|
w, h, w * 3,
|
|
|
|
|
free_buffer, NULL);
|
1999-07-30 15:34:18 +00:00
|
|
|
|
}
|