1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <png.h>
#include "loader.h"
#ifdef USE_X11
# include "viewer.h"
#endif
static const char *ct[] = {
"gray", "X1", "rgb", "palette",
"graya", "X5", "rgba", "X7",
};
struct png_state {
FILE *infile;
png_structp png;
png_infop info;
png_bytep row;
png_uint_32 w,h;
int color_type;
};
static void*
png_init(FILE *fp, char *filename, struct ida_image_info *i)
{
struct png_state *h;
int bit_depth, interlace_type;
int pass, number_passes, y;
png_uint_32 resx, resy;
int unit;
h = malloc(sizeof(*h));
memset(h,0,sizeof(*h));
h->infile = fp;
h->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (NULL == h->png)
goto oops;
h->info = png_create_info_struct(h->png);
if (NULL == h->info)
goto oops;
png_init_io(h->png, h->infile);
png_read_info(h->png, h->info);
png_get_IHDR(h->png, h->info, &h->w, &h->h,
&bit_depth,&h->color_type,&interlace_type, NULL,NULL);
png_get_pHYs(h->png, h->info, &resx, &resy, &unit);
i->width = h->w;
i->height = h->h;
if (PNG_RESOLUTION_METER == unit)
i->dpi = res_m_to_inch(resx);
if (debug)
fprintf(stderr,"png: color_type=%s #1\n",ct[h->color_type]);
png_set_packing(h->png);
if (bit_depth == 16)
png_set_strip_16(h->png);
if (h->color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(h->png);
if (h->color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_gray_1_2_4_to_8(h->png);
number_passes = png_set_interlace_handling(h->png);
png_read_update_info(h->png, h->info);
h->color_type = png_get_color_type(h->png, h->info);
if (debug)
fprintf(stderr,"png: color_type=%s #2\n",ct[h->color_type]);
h->row = malloc(i->width * 4);
for (pass = 0; pass < number_passes-1; pass++) {
for (y = 0; y < i->height; y++) {
png_read_rows(h->png, &h->row, NULL, 1);
}
}
return h;
oops:
if (h->row)
free(h->row);
if (h->png)
png_destroy_read_struct(&h->png, NULL, NULL);
fclose(h->infile);
free(h);
return NULL;
}
static void
png_read(unsigned char *dst, int line, void *data)
{
struct png_state *h = data;
switch (h->color_type) {
case PNG_COLOR_TYPE_GRAY:
png_read_rows(h->png, &h->row, NULL, 1);
load_gray(dst,h->row,h->w);
break;
case PNG_COLOR_TYPE_RGB:
png_read_rows(h->png, &dst, NULL, 1);
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
png_read_rows(h->png, &h->row, NULL, 1);
load_rgba(dst,h->row,h->w);
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
png_read_rows(h->png, &h->row, NULL, 1);
load_graya(dst,h->row,h->w);
break;
default:
/* shouldn't happen */
fprintf(stderr,"Oops: %s:%d\n",__FILE__,__LINE__);
exit(1);
}
}
static void
png_done(void *data)
{
struct png_state *h = data;
free(h->row);
png_destroy_read_struct(&h->png, &h->info, NULL);
fclose(h->infile);
free(h);
}
struct ida_loader png_loader = {
magic: "\x89PNG",
moff: 0,
mlen: 4,
name: "libpng",
init: png_init,
read: png_read,
done: png_done,
};
#ifdef USE_X11
/* ---------------------------------------------------------------------- */
/* save */
static int
png_write(FILE *fp, struct ida_image *img)
{
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep row;
int y;
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also check that
* the library version is compatible with the one used at compile time,
* in case we are using dynamically linked libraries. REQUIRED.
*/
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if (png_ptr == NULL)
goto oops;
/* Allocate/initialize the image information data. REQUIRED */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
goto oops;
if (setjmp(png_jmpbuf(png_ptr)))
goto oops;
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, img->i.width, img->i.height, 8,
PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if (img->i.dpi) {
png_set_pHYs(png_ptr, info_ptr,
res_inch_to_m(img->i.dpi),
res_inch_to_m(img->i.dpi),
PNG_RESOLUTION_METER);
}
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
for (y = 0; y < img->i.height; y++) {
row = img->data + y * 3 * img->i.width;
png_write_rows(png_ptr, &row, 1);
}
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
oops:
fprintf(stderr,"can't save image: libpng error\n");
if (png_ptr)
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return -1;
}
struct ida_writer png_writer = {
label: "PNG",
ext: { "png", NULL},
write: png_write,
};
#endif
|