Files
wmaker/wrlib/png.c
T

242 lines
5.8 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/* png.c - load PNG image from file
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Raster graphics library
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
1998-09-29 22:36:29 +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.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* 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.
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
/* AIX requires this to be the first thing in the file. */
#ifdef __GNUC__
# define alloca __builtin_alloca
#else
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef _AIX
# pragma alloca
# else
# ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
# endif
# endif
# endif
#endif
#ifdef USE_PNG
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <png.h>
#include "wraster.h"
RImage*
RLoadPNG(RContext *context, char *file, int index)
{
char *tmp;
RImage *image=NULL;
FILE *f;
png_structp png;
png_infop pinfo, einfo;
png_color_16p bkcolor;
int alpha;
int x, y, i;
double gamma, sgamma;
png_uint_32 width, height;
int depth, junk, color_type;
png_bytep *png_rows;
unsigned char *ptr;
2004-10-12 21:28:27 +00:00
f = fopen(file, "rb");
1998-09-29 22:36:29 +00:00
if (!f) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_OPEN;
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
(png_error_ptr)NULL, (png_error_ptr)NULL);
1998-09-29 22:36:29 +00:00
if (!png) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_NOMEMORY;
fclose(f);
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
pinfo = png_create_info_struct(png);
if (!pinfo) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_NOMEMORY;
fclose(f);
png_destroy_read_struct(&png, NULL, NULL);
return NULL;
1998-09-29 22:36:29 +00:00
}
einfo = png_create_info_struct(png);
if (!einfo) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_NOMEMORY;
fclose(f);
png_destroy_read_struct(&png, &pinfo, NULL);
return NULL;
1998-09-29 22:36:29 +00:00
}
1998-10-21 14:43:47 +00:00
RErrorCode = RERR_INTERNAL;
1998-09-29 22:36:29 +00:00
if (setjmp(png->jmpbuf)) {
2004-10-12 21:28:27 +00:00
fclose(f);
png_destroy_read_struct(&png, &pinfo, &einfo);
if (image)
RReleaseImage(image);
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
png_init_io(png, f);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
png_read_info(png, pinfo);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type,
2004-10-12 21:28:27 +00:00
&junk, &junk, &junk);
1998-09-29 22:36:29 +00:00
2002-10-25 03:43:57 +00:00
/* sanity check */
if (width < 1 || height < 1) {
2004-10-12 21:28:27 +00:00
fclose(f);
png_destroy_read_struct(&png, &pinfo, &einfo);
RErrorCode = RERR_BADIMAGEFILE;
return NULL;
2002-10-25 03:43:57 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* check for an alpha channel */
if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
2004-10-12 21:28:27 +00:00
alpha = True;
1998-09-29 22:36:29 +00:00
else
2004-10-12 21:28:27 +00:00
alpha = (color_type & PNG_COLOR_MASK_ALPHA);
1998-09-29 22:36:29 +00:00
/* allocate RImage */
image = RCreateImage(width, height, alpha);
if (!image) {
2004-10-12 21:28:27 +00:00
fclose(f);
png_destroy_read_struct(&png, &pinfo, &einfo);
return NULL;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* normalize to 8bpp with alpha channel */
2000-03-09 22:22:41 +00:00
if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
1998-09-29 22:36:29 +00:00
png_set_expand(png);
2000-03-09 22:22:41 +00:00
if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
1998-09-29 22:36:29 +00:00
png_set_expand(png);
if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
png_set_expand(png);
if (depth == 16)
png_set_strip_16(png);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2004-10-12 21:28:27 +00:00
png_set_gray_to_rgb(png);
1998-09-29 22:36:29 +00:00
/* set gamma correction */
if ((context->attribs->flags & RC_GammaCorrection)
2004-10-12 21:28:27 +00:00
&& context->depth != 8) {
sgamma = (context->attribs->rgamma + context->attribs->ggamma +
context->attribs->bgamma) / 3;
1998-09-29 22:36:29 +00:00
} else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
2004-10-12 21:28:27 +00:00
sgamma = atof(tmp);
if (sgamma==0)
sgamma = 1;
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
/* blah */
sgamma = 2.2;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (png_get_gAMA(png, pinfo, &gamma))
2004-10-12 21:28:27 +00:00
png_set_gamma(png, sgamma, gamma);
1998-09-29 22:36:29 +00:00
else
2004-10-12 21:28:27 +00:00
png_set_gamma(png, sgamma, 0.45);
1998-09-29 22:36:29 +00:00
/* do the transforms */
png_read_update_info(png, pinfo);
/* set background color */
if (png_get_bKGD(png, pinfo, &bkcolor)) {
2004-10-12 21:28:27 +00:00
image->background.red = bkcolor->red >> 8;
image->background.green = bkcolor->green >> 8;
image->background.blue = bkcolor->blue >> 8;
1998-09-29 22:36:29 +00:00
}
png_rows = alloca(sizeof(char*)*height);
if (!png_rows) {
2004-10-12 21:28:27 +00:00
RErrorCode = RERR_NOMEMORY;
fclose(f);
RReleaseImage(image);
png_destroy_read_struct(&png, &pinfo, &einfo);
1998-09-29 22:36:29 +00:00
#ifdef C_ALLOCA
2004-10-12 21:28:27 +00:00
alloca(0);
1998-09-29 22:36:29 +00:00
#endif
2004-10-12 21:28:27 +00:00
return NULL;
1998-09-29 22:36:29 +00:00
}
for (y=0; y<height; y++) {
2004-10-12 21:28:27 +00:00
png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
if (!png_rows[y]) {
RErrorCode = RERR_NOMEMORY;
fclose(f);
RReleaseImage(image);
png_destroy_read_struct(&png, &pinfo, &einfo);
1998-09-29 22:36:29 +00:00
#ifdef C_ALLOCA
2004-10-12 21:28:27 +00:00
alloca(0);
1998-09-29 22:36:29 +00:00
#endif
2004-10-12 21:28:27 +00:00
return NULL;
}
1998-09-29 22:36:29 +00:00
}
/* read data */
png_read_image(png, png_rows);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
png_read_end(png, einfo);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
png_destroy_read_struct(&png, &pinfo, &einfo);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
fclose(f);
ptr = image->data;
1998-09-29 22:36:29 +00:00
/* convert to RImage */
if (alpha) {
2004-10-12 21:28:27 +00:00
for (y=0; y<height; y++) {
for (x=0, i=width*4; x<i; x++, ptr++) {
*ptr = *(png_rows[y]+x);
}
}
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
for (y=0; y<height; y++) {
for (x=0, i=width*3; x<i; x++, ptr++) {
*ptr = *(png_rows[y]+x);
}
}
1998-09-29 22:36:29 +00:00
}
#ifdef C_ALLOCA
alloca(0);
#endif
return image;
}
#endif /* USE_PNG */
2004-10-12 21:28:27 +00:00