1999-01-05 04:31:03 +00:00
|
|
|
/*
|
1999-06-29 02:36:03 +00:00
|
|
|
* io-png.c: GdkPixBuf I/O for PNG files.
|
1999-06-30 15:28:43 +00:00
|
|
|
* Copyright (C) 1999 Mark Crichton
|
|
|
|
* Author: Mark Crichton <crichton@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
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
|
1999-01-05 04:31:03 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
1999-06-29 02:36:03 +00:00
|
|
|
#include <glib.h>
|
1999-01-05 04:31:03 +00:00
|
|
|
#include "gdk-pixbuf.h"
|
|
|
|
#include "gdk-pixbuf-io.h"
|
|
|
|
#include <png.h>
|
|
|
|
|
|
|
|
/* Shared library entry point */
|
1999-06-29 02:36:03 +00:00
|
|
|
GdkPixBuf *image_load(FILE * f)
|
1999-01-05 04:31:03 +00:00
|
|
|
{
|
1999-06-30 15:28:43 +00:00
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr, end_info;
|
|
|
|
gint i, depth, ctype, inttype, passes, bpp; /* bpp = BYTES/pixel */
|
|
|
|
png_uint_32 w, h, x, y;
|
|
|
|
png_bytepp rows;
|
|
|
|
art_u8 *pixels, *temp, *rowdata;
|
|
|
|
GdkPixBuf *pixbuf;
|
1999-09-22 22:30:51 +00:00
|
|
|
ArtPixBuf *art_pixbuf;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
g_return_val_if_fail (f != NULL, NULL);
|
1999-06-30 15:28:43 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (!png_ptr)
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
info_ptr = png_create_info_struct (png_ptr);
|
1999-06-30 15:28:43 +00:00
|
|
|
if (!info_ptr) {
|
1999-09-22 22:30:51 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, NULL, NULL);
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
}
|
1999-09-22 22:30:51 +00:00
|
|
|
|
|
|
|
end_info = png_create_info_struct (png_ptr);
|
1999-06-30 15:28:43 +00:00
|
|
|
if (!end_info) {
|
1999-09-22 22:30:51 +00:00
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
}
|
1999-09-22 22:30:51 +00:00
|
|
|
|
|
|
|
if (setjmp (png_ptr->jmpbuf)) {
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
|
|
|
return NULL;
|
1999-06-30 15:28:43 +00:00
|
|
|
}
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
png_init_io (png_ptr, f);
|
|
|
|
png_read_info (png_ptr, info_ptr);
|
|
|
|
png_get_IHDR (png_ptr, info_ptr, &w, &h, &depth, &ctype, &inttype,
|
|
|
|
NULL, NULL);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +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)
|
1999-09-22 22:30:51 +00:00
|
|
|
png_set_expand (png_ptr);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
if (ctype == PNG_COLOR_TYPE_GRAY && depth < 8)
|
1999-09-22 22:30:51 +00:00
|
|
|
png_set_expand (png_ptr);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
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-01-05 21:27:07 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
if (depth == 16)
|
1999-09-22 22:30:51 +00:00
|
|
|
png_set_strip_16 (png_ptr);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* We also have png "packing" bits into bytes if < 8 */
|
|
|
|
if (depth < 8)
|
1999-09-22 22:30:51 +00:00
|
|
|
png_set_packing (png_ptr);
|
1999-01-05 21:27:07 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* Lastly, if the PNG is greyscale, convert to RGB */
|
|
|
|
if (ctype == PNG_COLOR_TYPE_GRAY || ctype == PNG_COLOR_TYPE_GRAY_ALPHA)
|
1999-09-22 22:30:51 +00:00
|
|
|
png_set_gray_to_rgb (png_ptr);
|
1999-01-05 04:31:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* ...and if we're interlaced... */
|
1999-09-22 22:30:51 +00:00
|
|
|
passes = png_set_interlace_handling (png_ptr);
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* Update our info structs */
|
1999-09-22 22:30:51 +00:00
|
|
|
png_read_update_info (png_ptr, info_ptr);
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* Allocate some memory and set up row array */
|
1999-09-22 22:30:51 +00:00
|
|
|
/* This "inhales vigorously"... */
|
1999-06-30 15:28:43 +00:00
|
|
|
if (ctype & PNG_COLOR_MASK_ALPHA)
|
|
|
|
bpp = 4;
|
|
|
|
else
|
|
|
|
bpp = 3;
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
pixels = art_alloc (w * h * bpp);
|
|
|
|
rows = g_malloc (h * sizeof(png_bytep));
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
if (!pixels || !rows) {
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
1999-06-30 15:28:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* Icky code, but it has to be done... */
|
|
|
|
for (i = 0; i < h; i++) {
|
1999-09-22 22:30:51 +00:00
|
|
|
if ((rows[i] = g_malloc (w * sizeof (art_u8) * bpp)) == NULL) {
|
1999-06-30 15:28:43 +00:00
|
|
|
int n;
|
|
|
|
for (n = 0; n < i; n++)
|
1999-09-22 22:30:51 +00:00
|
|
|
g_free (rows[i]);
|
|
|
|
g_free (rows);
|
|
|
|
art_free (pixels);
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
1999-06-30 15:28:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* And we FINALLY get here... */
|
1999-09-22 22:30:51 +00:00
|
|
|
png_read_image (png_ptr, rows);
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
|
1999-06-29 02:54:16 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
/* Now stuff the bytes into pixels & free rows[y] */
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
temp = pixels;
|
1999-01-05 21:27:07 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
(png_bytep) rowdata = rows[y];
|
|
|
|
for (x = 0; x < w; x++) {
|
|
|
|
temp[0] = rowdata[(x * bpp)];
|
|
|
|
temp[1] = rowdata[(x * bpp) + 1];
|
|
|
|
temp[2] = rowdata[(x * bpp) + 2];
|
|
|
|
if (bpp == 4)
|
|
|
|
temp[3] = rowdata[(x * bpp) + 3];
|
|
|
|
temp += bpp;
|
1999-06-29 02:36:03 +00:00
|
|
|
}
|
1999-09-22 22:30:51 +00:00
|
|
|
g_free (rows[y]);
|
1999-06-30 15:28:43 +00:00
|
|
|
}
|
1999-09-22 22:30:51 +00:00
|
|
|
g_free (rows);
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
if (ctype & PNG_COLOR_MASK_ALPHA)
|
1999-09-22 22:30:51 +00:00
|
|
|
art_pixbuf = art_pixbuf_new_rgba (pixels, w, h, (w * 4));
|
1999-06-30 15:28:43 +00:00
|
|
|
else
|
1999-09-22 22:30:51 +00:00
|
|
|
art_pixbuf = art_pixbuf_new_rgb (pixels, w, h, (w * 3));
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
|
1999-07-19 04:21:09 +00:00
|
|
|
|
1999-09-22 22:30:51 +00:00
|
|
|
if (!pixbuf)
|
|
|
|
art_free (pixels);
|
1999-06-29 02:36:03 +00:00
|
|
|
|
1999-06-30 15:28:43 +00:00
|
|
|
return pixbuf;
|
1999-01-05 04:31:03 +00:00
|
|
|
}
|
1999-07-29 21:17:40 +00:00
|
|
|
|
|
|
|
int image_save(GdkPixBuf *pixbuf, FILE *file)
|
|
|
|
{
|
1999-07-30 15:34:18 +00:00
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
|
|
|
art_u8 *data;
|
|
|
|
gint y, h, w;
|
1999-08-09 06:09:24 +00:00
|
|
|
png_bytepp row_ptr;
|
1999-07-30 15:34:18 +00:00
|
|
|
png_color_8 sig_bit;
|
|
|
|
gint type;
|
1999-08-09 06:09:24 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(file != NULL, FALSE);
|
|
|
|
g_return_val_if_fail(pixbuf != NULL, FALSE);
|
1999-07-30 15:34:18 +00:00
|
|
|
|
|
|
|
h = pixbuf->art_pixbuf->height;
|
|
|
|
w = pixbuf->art_pixbuf->width;
|
|
|
|
|
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (png_ptr == NULL) {
|
|
|
|
fclose(file);
|
1999-08-09 06:09:24 +00:00
|
|
|
return FALSE;
|
1999-07-30 15:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
if (info_ptr == NULL) {
|
|
|
|
fclose(file);
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
|
1999-08-09 06:09:24 +00:00
|
|
|
return FALSE;
|
1999-07-30 15:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(png_ptr->jmpbuf)) {
|
|
|
|
fclose(file);
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
|
1999-08-09 06:09:24 +00:00
|
|
|
return FALSE;
|
1999-07-30 15:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
png_init_io(png_ptr, file);
|
|
|
|
if (pixbuf->art_pixbuf->has_alpha) {
|
|
|
|
sig_bit.alpha = 8;
|
|
|
|
type = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
|
|
} else {
|
|
|
|
sig_bit.alpha = 0;
|
|
|
|
type = PNG_COLOR_TYPE_RGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_bit.red = sig_bit.green = sig_bit.blue = 8;
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, w, h, 8, type, PNG_INTERLACE_NONE,
|
|
|
|
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
|
|
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
png_set_shift(png_ptr, &sig_bit);
|
|
|
|
png_set_packing(png_ptr);
|
|
|
|
|
|
|
|
data = pixbuf->art_pixbuf->pixels;
|
1999-08-09 06:09:24 +00:00
|
|
|
row_ptr = g_new(png_byte *, h);
|
1999-07-30 15:34:18 +00:00
|
|
|
|
1999-08-09 06:09:24 +00:00
|
|
|
for (y = 0; y < h; y++)
|
|
|
|
row_ptr[y] = data + y * pixbuf->art_pixbuf->rowstride;
|
|
|
|
#if 0
|
|
|
|
{
|
1999-07-30 15:34:18 +00:00
|
|
|
if (pixbuf->art_pixbuf->has_alpha)
|
|
|
|
row_ptr[y] = data + (w * y * 4);
|
|
|
|
else
|
|
|
|
row_ptr[y] = data + (w * y * 3);
|
|
|
|
}
|
1999-08-09 06:09:24 +00:00
|
|
|
#endif
|
1999-07-30 15:34:18 +00:00
|
|
|
|
|
|
|
png_write_image(png_ptr, row_ptr);
|
1999-08-09 06:09:24 +00:00
|
|
|
g_free (row_ptr);
|
|
|
|
|
1999-07-30 15:34:18 +00:00
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|