| 1 |
/* |
| 2 |
|
| 3 |
writepng.c |
| 4 |
|
| 5 |
Copyright 1996-2006 Han The Thanh <[email protected]> |
| 6 |
Copyright 2006-2013 Taco Hoekwater <[email protected]> |
| 7 |
|
| 8 |
This file is part of LuaTeX. |
| 9 |
|
| 10 |
LuaTeX is free software; you can redistribute it and/or modify it under the terms |
| 11 |
of the GNU General Public License as published by the Free Software Foundation; |
| 12 |
either version 2 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 |
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License along with |
| 19 |
LuaTeX; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ptexlib.h" |
| 24 |
#include <assert.h> |
| 25 |
#include "image/image.h" |
| 26 |
#include "image/writepng.h" |
| 27 |
|
| 28 |
static void close_and_cleanup_png(image_dict * idict) |
| 29 |
{ |
| 30 |
if (img_file(idict) != NULL) { |
| 31 |
xfclose(img_file(idict), img_filepath(idict)); |
| 32 |
img_file(idict) = NULL; |
| 33 |
} |
| 34 |
if (img_png_ptr(idict) != NULL) { |
| 35 |
png_destroy_read_struct(&(img_png_png_ptr(idict)), &(img_png_info_ptr(idict)), NULL); |
| 36 |
xfree(img_png_ptr(idict)); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void flush_png_info(image_dict * idict) |
| 41 |
{ |
| 42 |
close_and_cleanup_png(idict); |
| 43 |
} |
| 44 |
|
| 45 |
/*tex A dummy function: */ |
| 46 |
|
| 47 |
static void warn(png_structp png_ptr, png_const_charp msg) |
| 48 |
{ |
| 49 |
(void)png_ptr; (void)msg; |
| 50 |
} |
| 51 |
|
| 52 |
void read_png_info(image_dict * idict) |
| 53 |
{ |
| 54 |
png_structp png_p; |
| 55 |
png_infop info_p; |
| 56 |
if (img_type(idict) != IMG_TYPE_PNG) { |
| 57 |
normal_error("readpng","conflicting image dictionary"); |
| 58 |
} |
| 59 |
if (img_file(idict) != NULL) { |
| 60 |
normal_error("readpng","image data already read"); |
| 61 |
} |
| 62 |
img_totalpages(idict) = 1; |
| 63 |
img_pagenum(idict) = 1; |
| 64 |
img_xres(idict) = img_yres(idict) = 0; |
| 65 |
img_file(idict) = xfopen(img_filepath(idict), FOPEN_RBIN_MODE); |
| 66 |
img_png_ptr(idict) = xtalloc(1, png_img_struct); |
| 67 |
if ((png_p = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, warn)) == NULL) { |
| 68 |
normal_error("readpng","png_create_read_struct() failed"); |
| 69 |
} |
| 70 |
img_png_png_ptr(idict) = png_p; |
| 71 |
if ((info_p = png_create_info_struct(png_p)) == NULL) { |
| 72 |
normal_error("readpng","png_create_info_struct() failed"); |
| 73 |
} |
| 74 |
img_png_info_ptr(idict) = info_p; |
| 75 |
if (setjmp(png_jmpbuf(png_p))) { |
| 76 |
normal_error("readpng","internal error"); |
| 77 |
} |
| 78 |
#if PNG_LIBPNG_VER >= 10603 |
| 79 |
/*tex ignore possibly incorrect CMF bytes */ |
| 80 |
png_set_option(png_p, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON); |
| 81 |
#endif |
| 82 |
png_init_io(png_p, img_file(idict)); |
| 83 |
png_read_info(png_p, info_p); |
| 84 |
/*tex resolution support */ |
| 85 |
img_xsize(idict) = (int) png_get_image_width(png_p, info_p); |
| 86 |
img_ysize(idict) = (int) png_get_image_height(png_p, info_p); |
| 87 |
if (png_get_valid(png_p, info_p, PNG_INFO_pHYs)) { |
| 88 |
img_xres(idict) = round(0.0254 * (double) png_get_x_pixels_per_meter(png_p, info_p)); |
| 89 |
img_yres(idict) = round(0.0254 * (double) png_get_y_pixels_per_meter(png_p, info_p)); |
| 90 |
} |
| 91 |
switch (png_get_color_type(png_p, info_p)) { |
| 92 |
case PNG_COLOR_TYPE_PALETTE: |
| 93 |
img_procset(idict) |= PROCSET_IMAGE_C | PROCSET_IMAGE_I; |
| 94 |
break; |
| 95 |
case PNG_COLOR_TYPE_GRAY: |
| 96 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 97 |
img_procset(idict) |= PROCSET_IMAGE_B; |
| 98 |
break; |
| 99 |
case PNG_COLOR_TYPE_RGB: |
| 100 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 101 |
img_procset(idict) |= PROCSET_IMAGE_C; |
| 102 |
break; |
| 103 |
default: |
| 104 |
formatted_error("readpng","unsupported type of color_type '%i'",(int) png_get_color_type(png_p, info_p)); |
| 105 |
} |
| 106 |
img_colordepth(idict) = png_get_bit_depth(png_p, info_p); |
| 107 |
/*tex So we can optionally keep open a file in |img|. */ |
| 108 |
if (! img_keepopen(idict)) { |
| 109 |
close_and_cleanup_png(idict); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#define write_gray_pixel_16(r) \ |
| 114 |
if (j % 4 == 0 || j % 4 == 1) \ |
| 115 |
pdf_quick_out(pdf, *r++); \ |
| 116 |
else \ |
| 117 |
smask[smask_ptr++] = *r++ \ |
| 118 |
|
| 119 |
#define write_gray_pixel_8(r) \ |
| 120 |
if (j % 2 == 0) \ |
| 121 |
pdf_quick_out(pdf, *r++); \ |
| 122 |
else \ |
| 123 |
smask[smask_ptr++] = *r++ |
| 124 |
|
| 125 |
#define write_rgb_pixel_16(r) \ |
| 126 |
if (!(j % 8 == 6 || j % 8 == 7)) \ |
| 127 |
pdf_quick_out(pdf, *r++); \ |
| 128 |
else \ |
| 129 |
smask[smask_ptr++] = *r++ |
| 130 |
|
| 131 |
#define write_rgb_pixel_8(r) \ |
| 132 |
if (j % 4 != 3) \ |
| 133 |
pdf_quick_out(pdf, *r++); \ |
| 134 |
else \ |
| 135 |
smask[smask_ptr++] = *r++ |
| 136 |
|
| 137 |
#define write_simple_pixel(r) pdf_quick_out(pdf,*r++) |
| 138 |
|
| 139 |
#define write_noninterlaced(outmac) \ |
| 140 |
for (i = 0; i < (int) png_get_image_height(png_p, info_p); i++) { \ |
| 141 |
png_read_row(png_p, row, NULL); \ |
| 142 |
r = row; \ |
| 143 |
k = (size_t) png_get_rowbytes(png_p, info_p); \ |
| 144 |
while (k > 0) { \ |
| 145 |
l = (k > pdf->buf->size) ? pdf->buf->size : k; \ |
| 146 |
pdf_room(pdf, l); \ |
| 147 |
for (j = 0; j < l; j++) { \ |
| 148 |
outmac; \ |
| 149 |
} \ |
| 150 |
k -= l; \ |
| 151 |
} \ |
| 152 |
} |
| 153 |
|
| 154 |
#define write_interlaced(outmac) \ |
| 155 |
for (i = 0; i < (int) png_get_image_height(png_p, info_p); i++) { \ |
| 156 |
row = rows[i]; \ |
| 157 |
k = (size_t) png_get_rowbytes(png_p, info_p); \ |
| 158 |
while (k > 0) { \ |
| 159 |
l = (k > pdf->buf->size) ? pdf->buf->size : k; \ |
| 160 |
pdf_room(pdf, l); \ |
| 161 |
for (j = 0; j < l; j++) { \ |
| 162 |
outmac; \ |
| 163 |
} \ |
| 164 |
k -= l; \ |
| 165 |
} \ |
| 166 |
xfree(rows[i]); \ |
| 167 |
} |
| 168 |
|
| 169 |
static void write_palette_streamobj(PDF pdf, int palette_objnum, png_colorp palette, int num_palette) |
| 170 |
{ |
| 171 |
int i; |
| 172 |
if (palette_objnum == 0) |
| 173 |
return; |
| 174 |
assert(palette != NULL); |
| 175 |
pdf_begin_obj(pdf, palette_objnum, OBJSTM_NEVER); |
| 176 |
pdf_begin_dict(pdf); |
| 177 |
pdf_dict_add_streaminfo(pdf); |
| 178 |
pdf_end_dict(pdf); |
| 179 |
pdf_begin_stream(pdf); |
| 180 |
for (i = 0; i < num_palette; i++) { |
| 181 |
pdf_room(pdf, 3); |
| 182 |
pdf_quick_out(pdf, palette[i].red); |
| 183 |
pdf_quick_out(pdf, palette[i].green); |
| 184 |
pdf_quick_out(pdf, palette[i].blue); |
| 185 |
} |
| 186 |
pdf_end_stream(pdf); |
| 187 |
pdf_end_obj(pdf); |
| 188 |
} |
| 189 |
|
| 190 |
static void write_smask_streamobj(PDF pdf, image_dict * idict, int smask_objnum, png_bytep smask, int smask_size) |
| 191 |
{ |
| 192 |
int i; |
| 193 |
png_structp png_p = img_png_png_ptr(idict); |
| 194 |
png_infop info_p = img_png_info_ptr(idict); |
| 195 |
png_byte bitdepth = png_get_bit_depth(png_p, info_p); |
| 196 |
pdf_begin_obj(pdf, smask_objnum, OBJSTM_NEVER); |
| 197 |
pdf_begin_dict(pdf); |
| 198 |
pdf_dict_add_name(pdf, "Type", "XObject"); |
| 199 |
pdf_dict_add_name(pdf, "Subtype", "Image"); |
| 200 |
pdf_dict_add_img_filename(pdf, idict); |
| 201 |
/* if (img_attr(idict) != NULL && strlen(img_attr(idict)) > 0) */ |
| 202 |
/* pdf_printf(pdf, "\n%s\n", img_attr(idict)); */ |
| 203 |
pdf_dict_add_int(pdf, "Width", (int) png_get_image_width(png_p, info_p)); |
| 204 |
pdf_dict_add_int(pdf, "Height", (int) png_get_image_height(png_p, info_p)); |
| 205 |
pdf_dict_add_int(pdf, "BitsPerComponent", (bitdepth == 16 ? 8 : bitdepth)); |
| 206 |
pdf_dict_add_name(pdf, "ColorSpace", "DeviceGray"); |
| 207 |
pdf_dict_add_streaminfo(pdf); |
| 208 |
pdf_end_dict(pdf); |
| 209 |
pdf_begin_stream(pdf); |
| 210 |
for (i = 0; i < smask_size; i++) { |
| 211 |
if (i % 8 == 0) |
| 212 |
pdf_room(pdf, 8); |
| 213 |
pdf_quick_out(pdf, smask[i]); |
| 214 |
if (bitdepth == 16) |
| 215 |
i++; |
| 216 |
} |
| 217 |
pdf_end_stream(pdf); |
| 218 |
pdf_end_obj(pdf); |
| 219 |
} |
| 220 |
|
| 221 |
static void write_png_gray(PDF pdf, image_dict * idict) |
| 222 |
{ |
| 223 |
int i; |
| 224 |
size_t j, k, l; |
| 225 |
png_structp png_p = img_png_png_ptr(idict); |
| 226 |
png_infop info_p = img_png_info_ptr(idict); |
| 227 |
png_bytep row, r, *rows; |
| 228 |
pdf_dict_add_streaminfo(pdf); |
| 229 |
pdf_end_dict(pdf); |
| 230 |
pdf_begin_stream(pdf); |
| 231 |
if (png_get_interlace_type(png_p, info_p) == PNG_INTERLACE_NONE) { |
| 232 |
row = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 233 |
write_noninterlaced(write_simple_pixel(r)); |
| 234 |
xfree(row); |
| 235 |
} else { |
| 236 |
if (png_get_image_height(png_p, info_p) * png_get_rowbytes(png_p, info_p) >= 10240000L) { |
| 237 |
formatted_warning("pngwrite","large interlaced bitmap might cause out of memory"); |
| 238 |
} |
| 239 |
rows = xtalloc(png_get_image_height(png_p, info_p), png_bytep); |
| 240 |
for (i = 0; i < (int) png_get_image_height(png_p, info_p); i++) { |
| 241 |
rows[i] = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 242 |
} |
| 243 |
png_read_image(png_p, rows); |
| 244 |
write_interlaced(write_simple_pixel(row)); |
| 245 |
xfree(rows); |
| 246 |
} |
| 247 |
pdf_end_stream(pdf); |
| 248 |
pdf_end_obj(pdf); |
| 249 |
} |
| 250 |
|
| 251 |
static void write_png_gray_alpha(PDF pdf, image_dict * idict) |
| 252 |
{ |
| 253 |
int i; |
| 254 |
size_t j, k, l; |
| 255 |
png_structp png_p = img_png_png_ptr(idict); |
| 256 |
png_infop info_p = img_png_info_ptr(idict); |
| 257 |
png_bytep row, r, *rows; |
| 258 |
int smask_objnum = 0; |
| 259 |
png_bytep smask; |
| 260 |
int smask_ptr = 0; |
| 261 |
int smask_size = 0; |
| 262 |
smask_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 263 |
pdf_dict_add_ref(pdf, "SMask", (int) smask_objnum); |
| 264 |
smask_size = (int) ((png_get_rowbytes(png_p, info_p) / 2) * png_get_image_height(png_p, info_p)); |
| 265 |
smask = xtalloc((unsigned) smask_size, png_byte); |
| 266 |
pdf_dict_add_streaminfo(pdf); |
| 267 |
pdf_end_dict(pdf); |
| 268 |
pdf_begin_stream(pdf); |
| 269 |
if (png_get_interlace_type(png_p, info_p) == PNG_INTERLACE_NONE) { |
| 270 |
row = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 271 |
if ((png_get_bit_depth(png_p, info_p) == 16) && (pdf->image_hicolor != 0)) { |
| 272 |
write_noninterlaced(write_gray_pixel_16(r)); |
| 273 |
} else { |
| 274 |
write_noninterlaced(write_gray_pixel_8(r)); |
| 275 |
} |
| 276 |
xfree(row); |
| 277 |
} else { |
| 278 |
if (png_get_image_height(png_p, info_p) * png_get_rowbytes(png_p, info_p) >= 10240000L) { |
| 279 |
formatted_warning("pngwrite","large interlaced bitmap might cause out of memory"); |
| 280 |
} |
| 281 |
rows = xtalloc(png_get_image_height(png_p, info_p), png_bytep); |
| 282 |
for (i = 0; i < (int) png_get_image_height(png_p, info_p); i++) { |
| 283 |
rows[i] = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 284 |
} |
| 285 |
png_read_image(png_p, rows); |
| 286 |
if ((png_get_bit_depth(png_p, info_p) == 16) && (pdf->image_hicolor != 0)) { |
| 287 |
write_interlaced(write_gray_pixel_16(row)); |
| 288 |
} else { |
| 289 |
write_interlaced(write_gray_pixel_8(row)); |
| 290 |
} |
| 291 |
xfree(rows); |
| 292 |
} |
| 293 |
pdf_end_stream(pdf); |
| 294 |
pdf_end_obj(pdf); |
| 295 |
write_smask_streamobj(pdf, idict, smask_objnum, smask, smask_size); |
| 296 |
xfree(smask); |
| 297 |
} |
| 298 |
|
| 299 |
static void write_png_rgb_alpha(PDF pdf, image_dict * idict) |
| 300 |
{ |
| 301 |
int i; |
| 302 |
size_t j, k, l; |
| 303 |
png_structp png_p = img_png_png_ptr(idict); |
| 304 |
png_infop info_p = img_png_info_ptr(idict); |
| 305 |
png_bytep row, r, *rows; |
| 306 |
int smask_objnum = 0; |
| 307 |
png_bytep smask; |
| 308 |
int smask_ptr = 0; |
| 309 |
int smask_size = 0; |
| 310 |
smask_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 311 |
pdf_dict_add_ref(pdf, "SMask", (int) smask_objnum); |
| 312 |
smask_size = (int) ((png_get_rowbytes(png_p, info_p) / 4) * png_get_image_height(png_p, info_p)); |
| 313 |
smask = xtalloc((unsigned) smask_size, png_byte); |
| 314 |
pdf_dict_add_streaminfo(pdf); |
| 315 |
pdf_end_dict(pdf); |
| 316 |
pdf_begin_stream(pdf); |
| 317 |
if (png_get_interlace_type(png_p, info_p) == PNG_INTERLACE_NONE) { |
| 318 |
row = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 319 |
if ((png_get_bit_depth(png_p, info_p) == 16) && (pdf->image_hicolor != 0)) { |
| 320 |
write_noninterlaced(write_rgb_pixel_16(r)); |
| 321 |
} else { |
| 322 |
write_noninterlaced(write_rgb_pixel_8(r)); |
| 323 |
} |
| 324 |
xfree(row); |
| 325 |
} else { |
| 326 |
if (png_get_image_height(png_p, info_p) * png_get_rowbytes(png_p, info_p) >= 10240000L) { |
| 327 |
formatted_warning("pngwrite","large interlaced bitmap might cause out of memory"); |
| 328 |
} |
| 329 |
rows = xtalloc(png_get_image_height(png_p, info_p), png_bytep); |
| 330 |
for (i = 0; i < (int) png_get_image_height(png_p, info_p); i++) { |
| 331 |
rows[i] = xtalloc(png_get_rowbytes(png_p, info_p), png_byte); |
| 332 |
} |
| 333 |
png_read_image(png_p, rows); |
| 334 |
if ((png_get_bit_depth(png_p, info_p) == 16) && (pdf->image_hicolor != 0)) { |
| 335 |
write_interlaced(write_rgb_pixel_16(row)); |
| 336 |
} else { |
| 337 |
write_interlaced(write_rgb_pixel_8(row)); |
| 338 |
} |
| 339 |
xfree(rows); |
| 340 |
} |
| 341 |
pdf_end_stream(pdf); |
| 342 |
pdf_end_obj(pdf); |
| 343 |
write_smask_streamobj(pdf, idict, smask_objnum, smask, smask_size); |
| 344 |
xfree(smask); |
| 345 |
} |
| 346 |
|
| 347 |
/*tex |
| 348 |
|
| 349 |
The |copy_png| code is cheerfully gleaned from Thomas Merz' PDFlib, file |
| 350 |
|p_png.c| ``SPNG - Simple PNG''. The goal is to use pdf's native FlateDecode |
| 351 |
support, if that is possible. Only a subset of the png files allows this, but for |
| 352 |
these it greatly improves inclusion speed. |
| 353 |
|
| 354 |
In the ``PNG Copy'' mode only the IDAT chunks are copied; all other chunks from |
| 355 |
the PNG file are discarded. If there are any other chunks in the PNG file, which |
| 356 |
might influence the visual appearance of the image, or if image processing like |
| 357 |
gamma change is requested, the ``PNG Copy'' function must be skipped; therefore |
| 358 |
the lengthy tests. |
| 359 |
|
| 360 |
*/ |
| 361 |
|
| 362 |
static int spng_getint(FILE * f) |
| 363 |
{ |
| 364 |
unsigned char buf[4]; |
| 365 |
if (fread(buf, 1, 4, f) != 4) { |
| 366 |
normal_error("writepng", "reading chunk type failed"); |
| 367 |
} |
| 368 |
return ((((((int) buf[0] << 8) + buf[1]) << 8) + buf[2]) << 8) + buf[3]; |
| 369 |
} |
| 370 |
|
| 371 |
#define SPNG_CHUNK_IDAT 0x49444154 |
| 372 |
#define SPNG_CHUNK_IEND 0x49454E44 |
| 373 |
|
| 374 |
static void copy_png(PDF pdf, image_dict * idict) |
| 375 |
{ |
| 376 |
int type, streamlength = 0, idat = 0; |
| 377 |
size_t len; |
| 378 |
boolean endflag = false; |
| 379 |
FILE *f; |
| 380 |
png_structp png_p; |
| 381 |
png_infop info_p; |
| 382 |
assert(idict != NULL); |
| 383 |
png_p = img_png_png_ptr(idict); |
| 384 |
info_p = img_png_info_ptr(idict); |
| 385 |
f = (FILE *) png_get_io_ptr(png_p); |
| 386 |
/*tex 1st pass to find overall stream /Length */ |
| 387 |
if (fseek(f, 8, SEEK_SET) != 0) |
| 388 |
normal_error("writepng", "fseek in file failed"); |
| 389 |
do { |
| 390 |
len = spng_getint(f); |
| 391 |
type = spng_getint(f); |
| 392 |
switch (type) { |
| 393 |
case SPNG_CHUNK_IEND: |
| 394 |
endflag = true; |
| 395 |
break; |
| 396 |
case SPNG_CHUNK_IDAT: |
| 397 |
streamlength += len; |
| 398 |
default: |
| 399 |
if (fseek(f, len + 4, SEEK_CUR) != 0) { |
| 400 |
normal_error("writepng", "fseek in file failed"); |
| 401 |
} |
| 402 |
} |
| 403 |
} while (endflag == false); |
| 404 |
pdf_dict_add_int(pdf, "Length", streamlength); |
| 405 |
pdf_dict_add_name(pdf, "Filter", "FlateDecode"); |
| 406 |
pdf_add_name(pdf, "DecodeParms"); |
| 407 |
pdf_begin_dict(pdf); |
| 408 |
pdf_dict_add_int(pdf, "Colors", png_get_color_type(png_p,info_p) == PNG_COLOR_TYPE_RGB ? 3 : 1); |
| 409 |
pdf_dict_add_int(pdf, "Columns", png_get_image_width(png_p, info_p)); |
| 410 |
pdf_dict_add_int(pdf, "BitsPerComponent", png_get_bit_depth(png_p, info_p)); |
| 411 |
pdf_dict_add_int(pdf, "Predictor", 10); |
| 412 |
pdf_end_dict(pdf); |
| 413 |
pdf_end_dict(pdf); |
| 414 |
pdf_begin_stream(pdf); |
| 415 |
/*tex 2nd pass to copy data */ |
| 416 |
endflag = false; |
| 417 |
if (fseek(f, 8, SEEK_SET) != 0) |
| 418 |
normal_error("writepng", "fseek in file failed"); |
| 419 |
do { |
| 420 |
len = spng_getint(f); |
| 421 |
type = spng_getint(f); |
| 422 |
switch (type) { |
| 423 |
case SPNG_CHUNK_IDAT: |
| 424 |
/*tex do copy */ |
| 425 |
if (idat == 2) { |
| 426 |
normal_error("writepng", "IDAT chunk sequence broken"); |
| 427 |
} |
| 428 |
idat = 1; |
| 429 |
if (read_file_to_buf(pdf, f, len) != len) { |
| 430 |
normal_error("writepng", "fread failed"); |
| 431 |
} else if (fseek(f, 4, SEEK_CUR) != 0) { |
| 432 |
normal_error("writepng", "fseek in file failed"); |
| 433 |
} |
| 434 |
break; |
| 435 |
case SPNG_CHUNK_IEND: |
| 436 |
/*tex done */ |
| 437 |
endflag = true; |
| 438 |
break; |
| 439 |
default: |
| 440 |
if (idat == 1) |
| 441 |
idat = 2; |
| 442 |
if (fseek(f, len + 4, SEEK_CUR) != 0) |
| 443 |
normal_error("writepng", "fseek in file failed"); |
| 444 |
} |
| 445 |
} while (endflag == false); |
| 446 |
pdf_end_stream(pdf); |
| 447 |
pdf_end_obj(pdf); |
| 448 |
} |
| 449 |
|
| 450 |
static void reopen_png(image_dict * idict) |
| 451 |
{ |
| 452 |
int width, height, xres, yres; |
| 453 |
/*tex A consistency check: */ |
| 454 |
width = img_xsize(idict); |
| 455 |
height = img_ysize(idict); |
| 456 |
xres = img_xres(idict); |
| 457 |
yres = img_yres(idict); |
| 458 |
/*tex We need to ake sure that the file kept open. */ |
| 459 |
img_keepopen(idict) = 1; |
| 460 |
read_png_info(idict); |
| 461 |
if (width != img_xsize(idict) || height != img_ysize(idict) || xres != img_xres(idict) || yres != img_yres(idict)) { |
| 462 |
normal_error("writepng", "image dimensions have changed"); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
void write_png(PDF pdf, image_dict * idict) |
| 467 |
{ |
| 468 |
#ifndef PNG_FP_1 |
| 469 |
/*tex for libpng < 1.5.0 */ |
| 470 |
# define PNG_FP_1 100000 |
| 471 |
#endif |
| 472 |
int num_palette, palette_objnum = 0; |
| 473 |
boolean png_copy = true; |
| 474 |
double gamma = 0.0; |
| 475 |
png_fixed_point int_file_gamma = 0; |
| 476 |
png_structp png_p; |
| 477 |
png_infop info_p; |
| 478 |
png_colorp palette; |
| 479 |
assert(idict != NULL); |
| 480 |
if (img_file(idict) == NULL) |
| 481 |
reopen_png(idict); |
| 482 |
assert(img_png_ptr(idict) != NULL); |
| 483 |
png_p = img_png_png_ptr(idict); |
| 484 |
info_p = img_png_info_ptr(idict); |
| 485 |
/*tex simple transparency support */ |
| 486 |
if (png_get_valid(png_p, info_p, PNG_INFO_tRNS)) { |
| 487 |
png_set_tRNS_to_alpha(png_p); |
| 488 |
png_copy = false; |
| 489 |
} |
| 490 |
/*tex alpha channel support */ |
| 491 |
if ((pdf->major_version == 1 && pdf->minor_version < 4) |
| 492 |
&& png_get_color_type(png_p, info_p) | PNG_COLOR_MASK_ALPHA) { |
| 493 |
png_set_strip_alpha(png_p); |
| 494 |
png_copy = false; |
| 495 |
} |
| 496 |
/*tex 16 bit depth support */ |
| 497 |
if (pdf->major_version == 1 && pdf->minor_version < 5) |
| 498 |
pdf->image_hicolor = 0; |
| 499 |
if ((png_get_bit_depth(png_p, info_p) == 16) && (pdf->image_hicolor == 0)) { |
| 500 |
png_set_strip_16(png_p); |
| 501 |
png_copy = false; |
| 502 |
} |
| 503 |
/*tex gamma support */ |
| 504 |
if (png_get_valid(png_p, info_p, PNG_INFO_gAMA)) { |
| 505 |
png_get_gAMA(png_p, info_p, &gamma); |
| 506 |
png_get_gAMA_fixed(png_p, info_p, &int_file_gamma); |
| 507 |
} |
| 508 |
if (pdf->image_apply_gamma) { |
| 509 |
if (png_get_valid(png_p, info_p, PNG_INFO_gAMA)) |
| 510 |
png_set_gamma(png_p, (pdf->gamma / 1000.0), gamma); |
| 511 |
else |
| 512 |
png_set_gamma(png_p, (pdf->gamma / 1000.0), (1000.0 / pdf->image_gamma)); |
| 513 |
png_copy = false; |
| 514 |
} |
| 515 |
/*tex reset structure */ |
| 516 |
(void) png_set_interlace_handling(png_p); |
| 517 |
png_read_update_info(png_p, info_p); |
| 518 |
pdf_begin_obj(pdf, img_objnum(idict), OBJSTM_NEVER); |
| 519 |
pdf_begin_dict(pdf); |
| 520 |
pdf_dict_add_name(pdf, "Type", "XObject"); |
| 521 |
pdf_dict_add_name(pdf, "Subtype", "Image"); |
| 522 |
pdf_dict_add_img_filename(pdf, idict); |
| 523 |
if (img_attr(idict) != NULL && strlen(img_attr(idict)) > 0) { |
| 524 |
pdf_printf(pdf, "\n%s\n", img_attr(idict)); |
| 525 |
} |
| 526 |
pdf_dict_add_int(pdf, "Width", (int) png_get_image_width(png_p, info_p)); |
| 527 |
pdf_dict_add_int(pdf, "Height", (int) png_get_image_height(png_p, info_p)); |
| 528 |
pdf_dict_add_int(pdf, "BitsPerComponent", (int) png_get_bit_depth(png_p, info_p)); |
| 529 |
if (img_colorspace(idict) != 0) { |
| 530 |
pdf_dict_add_ref(pdf, "ColorSpace", (int) img_colorspace(idict)); |
| 531 |
} else { |
| 532 |
switch (png_get_color_type(png_p, info_p)) { |
| 533 |
case PNG_COLOR_TYPE_PALETTE: |
| 534 |
png_get_PLTE(png_p, info_p, &palette, &num_palette); |
| 535 |
palette_objnum = pdf_create_obj(pdf, obj_type_others, 0); |
| 536 |
pdf_add_name(pdf, "ColorSpace"); |
| 537 |
pdf_begin_array(pdf); |
| 538 |
pdf_add_name(pdf, "Indexed"); |
| 539 |
pdf_add_name(pdf, "DeviceRGB"); |
| 540 |
/*tex hival */ |
| 541 |
pdf_add_int(pdf, (int) (num_palette - 1)); |
| 542 |
/*tex lookup */ |
| 543 |
pdf_add_ref(pdf, (int) palette_objnum); |
| 544 |
pdf_end_array(pdf); |
| 545 |
break; |
| 546 |
case PNG_COLOR_TYPE_GRAY: |
| 547 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 548 |
pdf_dict_add_name(pdf, "ColorSpace", "DeviceGray"); |
| 549 |
break; |
| 550 |
case PNG_COLOR_TYPE_RGB: |
| 551 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 552 |
pdf_dict_add_name(pdf, "ColorSpace", "DeviceRGB"); |
| 553 |
break; |
| 554 |
default: |
| 555 |
formatted_error("writepng", "unsupported color_type '%i'", png_get_color_type(png_p, info_p)); |
| 556 |
} |
| 557 |
} |
| 558 |
if (png_copy |
| 559 |
&& (pdf->major_version > 1 || pdf->minor_version > 1) |
| 560 |
&& png_get_interlace_type(png_p, info_p) == PNG_INTERLACE_NONE |
| 561 |
&& (png_get_color_type(png_p, info_p) == PNG_COLOR_TYPE_GRAY |
| 562 |
|| png_get_color_type(png_p, info_p) == PNG_COLOR_TYPE_RGB) |
| 563 |
&& !pdf->image_apply_gamma |
| 564 |
&& (!png_get_valid(png_p, info_p, PNG_INFO_gAMA) || int_file_gamma == PNG_FP_1) |
| 565 |
&& !png_get_valid(png_p, info_p, PNG_INFO_cHRM) |
| 566 |
&& !png_get_valid(png_p, info_p, PNG_INFO_iCCP) |
| 567 |
&& !png_get_valid(png_p, info_p, PNG_INFO_sBIT) |
| 568 |
&& !png_get_valid(png_p, info_p, PNG_INFO_sRGB) |
| 569 |
&& !png_get_valid(png_p, info_p, PNG_INFO_bKGD) |
| 570 |
&& !png_get_valid(png_p, info_p, PNG_INFO_hIST) |
| 571 |
&& !png_get_valid(png_p, info_p, PNG_INFO_tRNS) |
| 572 |
&& !png_get_valid(png_p, info_p, PNG_INFO_sPLT)) { |
| 573 |
copy_png(pdf, idict); |
| 574 |
} else { |
| 575 |
if (img_errorlevel(idict) > 1) { |
| 576 |
if (!png_copy) |
| 577 |
normal_warning("pngcopy","failed"); |
| 578 |
if (!(pdf->major_version == 1 && pdf->minor_version > 1)) |
| 579 |
formatted_warning("pngcopy","skipped because minorversion is '%d'", pdf->minor_version); |
| 580 |
if (!(png_get_interlace_type(png_p, info_p) == PNG_INTERLACE_NONE)) |
| 581 |
normal_warning("pngcopy","skipped because of interlacing"); |
| 582 |
if (!((png_get_color_type(png_p, info_p) == PNG_COLOR_TYPE_GRAY) |
| 583 |
|| (png_get_color_type(png_p, info_p) == PNG_COLOR_TYPE_RGB))) |
| 584 |
normal_warning("pngcopy","skipped because of colortype"); |
| 585 |
if (pdf->image_apply_gamma) |
| 586 |
normal_warning("pngcopy","skipped because of gamma (1)"); |
| 587 |
if (!(!png_get_valid(png_p, info_p, PNG_INFO_gAMA) || int_file_gamma == PNG_FP_1)) |
| 588 |
normal_warning("pngcopy","skipped because of gamma (2)"); |
| 589 |
if (png_get_valid(png_p, info_p, PNG_INFO_cHRM)) |
| 590 |
normal_warning("pngcopy","skipped because of cHRM"); |
| 591 |
if (png_get_valid(png_p, info_p, PNG_INFO_iCCP)) |
| 592 |
normal_warning("pngcopy","skipped because of iCCP"); |
| 593 |
if (png_get_valid(png_p, info_p, PNG_INFO_sBIT)) |
| 594 |
normal_warning("pngcopy","skipped because of sBIT"); |
| 595 |
if (png_get_valid(png_p, info_p, PNG_INFO_sRGB)) |
| 596 |
normal_warning("pngcopy","skipped because of sRGB"); |
| 597 |
if (png_get_valid(png_p, info_p, PNG_INFO_bKGD)) |
| 598 |
normal_warning("pngcopy","skipped because of bKGD"); |
| 599 |
if (png_get_valid(png_p, info_p, PNG_INFO_hIST)) |
| 600 |
normal_warning("pngcopy","skipped because of hIST"); |
| 601 |
if (png_get_valid(png_p, info_p, PNG_INFO_tRNS)) |
| 602 |
normal_warning("pngcopy","skipped because of tRNS"); |
| 603 |
if (png_get_valid(png_p, info_p, PNG_INFO_sPLT)) |
| 604 |
normal_warning("pngcopy","skipped because of sPLT"); |
| 605 |
} |
| 606 |
switch (png_get_color_type(png_p, info_p)) { |
| 607 |
case PNG_COLOR_TYPE_PALETTE: |
| 608 |
case PNG_COLOR_TYPE_GRAY: |
| 609 |
case PNG_COLOR_TYPE_RGB: |
| 610 |
write_png_gray(pdf, idict); |
| 611 |
break; |
| 612 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 613 |
if (pdf->major_version > 1 || pdf->minor_version >= 4) { |
| 614 |
write_png_gray_alpha(pdf, idict); |
| 615 |
} else |
| 616 |
write_png_gray(pdf, idict); |
| 617 |
break; |
| 618 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 619 |
if (pdf->major_version > 1 || pdf->minor_version >= 4) { |
| 620 |
write_png_rgb_alpha(pdf, idict); |
| 621 |
} else |
| 622 |
write_png_gray(pdf, idict); |
| 623 |
break; |
| 624 |
default: |
| 625 |
assert(0); |
| 626 |
} |
| 627 |
} |
| 628 |
write_palette_streamobj(pdf, palette_objnum, palette, num_palette); |
| 629 |
/*tex always */ |
| 630 |
close_and_cleanup_png(idict); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
/*tex |
| 635 |
|
| 636 |
Called after the xobject generated by |write_png| has been finished; used to |
| 637 |
write out additional objects. |
| 638 |
|
| 639 |
*/ |
| 640 |
|
| 641 |
void write_additional_png_objects(PDF pdf) |
| 642 |
{ |
| 643 |
(void) pdf; |
| 644 |
return; |
| 645 |
} |