1 // ==========================================================
2 // PNG Loader and Writer
3 //
4 // Design and implementation by
5 // - Floris van den Berg (flvdberg@wxs.nl)
6 // - Herve Drolon (drolon@infonie.fr)
7 // - Detlev Vendt (detlev.vendt@brillit.de)
8 // - Aaron Shumate (trek@startreker.com)
9 // - Tanner Helland (tannerhelland@users.sf.net)
10 //
11 // This file is part of FreeImage 3
12 //
13 // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
14 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
15 // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
16 // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
17 // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
18 // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
19 // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
20 // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
21 // THIS DISCLAIMER.
22 //
23 // Use at your own risk!
24 // ==========================================================
25 
26 #ifdef _MSC_VER
27 #pragma warning (disable : 4786) // identifier was truncated to 'number' characters
28 #endif
29 
30 #include "FreeImage.h"
31 #include "Utilities.h"
32 
33 #include "../Metadata/FreeImageTag.h"
34 
35 // ----------------------------------------------------------
36 
37 #define PNG_BYTES_TO_CHECK 8
38 
39 #undef PNG_Z_DEFAULT_COMPRESSION	// already used in ../LibPNG/pnglibconf.h
40 
41 // ----------------------------------------------------------
42 
43 #include "../ZLib/zlib.h"
44 #include "../LibPNG/png.h"
45 
46 // ----------------------------------------------------------
47 
48 typedef struct {
49     FreeImageIO *s_io;
50     fi_handle    s_handle;
51 } fi_ioStructure, *pfi_ioStructure;
52 
53 // ==========================================================
54 // Plugin Interface
55 // ==========================================================
56 
57 static int s_format_id;
58 
59 // ==========================================================
60 // libpng interface
61 // ==========================================================
62 
63 static void
_ReadProc(png_structp png_ptr,unsigned char * data,png_size_t size)64 _ReadProc(png_structp png_ptr, unsigned char *data, png_size_t size) {
65     pfi_ioStructure pfio = (pfi_ioStructure)png_get_io_ptr(png_ptr);
66 	unsigned n = pfio->s_io->read_proc(data, (unsigned int)size, 1, pfio->s_handle);
67 	if(size && (n == 0)) {
68 		throw "Read error: invalid or corrupted PNG file";
69 	}
70 }
71 
72 static void
_WriteProc(png_structp png_ptr,unsigned char * data,png_size_t size)73 _WriteProc(png_structp png_ptr, unsigned char *data, png_size_t size) {
74     pfi_ioStructure pfio = (pfi_ioStructure)png_get_io_ptr(png_ptr);
75     pfio->s_io->write_proc(data, (unsigned int)size, 1, pfio->s_handle);
76 }
77 
78 static void
_FlushProc(png_structp png_ptr)79 _FlushProc(png_structp png_ptr) {
80 	(png_structp)png_ptr;
81 	// empty flush implementation
82 }
83 
84 static void
error_handler(png_structp png_ptr,const char * error)85 error_handler(png_structp png_ptr, const char *error) {
86 	FreeImage_OutputMessageProc(s_format_id, error);
87 	png_longjmp(png_ptr, 1);
88 }
89 
90 // in FreeImage warnings disabled
91 
92 static void
warning_handler(png_structp png_ptr,const char * warning)93 warning_handler(png_structp png_ptr, const char *warning) {
94 	(png_structp)png_ptr;
95 	(char*)warning;
96 }
97 
98 // ==========================================================
99 // Metadata routines
100 // ==========================================================
101 
102 static BOOL
ReadMetadata(png_structp png_ptr,png_infop info_ptr,FIBITMAP * dib)103 ReadMetadata(png_structp png_ptr, png_infop info_ptr, FIBITMAP *dib) {
104 	// XMP keyword
105 	const char *g_png_xmp_keyword = "XML:com.adobe.xmp";
106 
107 	FITAG *tag = NULL;
108 	png_textp text_ptr = NULL;
109 	png_timep mod_time = NULL;
110 	int num_text = 0;
111 
112 	// iTXt/tEXt/zTXt chuncks
113 	if(png_get_text(png_ptr, info_ptr, &text_ptr, &num_text) > 0) {
114 		for(int i = 0; i < num_text; i++) {
115 			// create a tag
116 			tag = FreeImage_CreateTag();
117 			if(!tag) return FALSE;
118 
119 			DWORD tag_length = (DWORD) MAX(text_ptr[i].text_length, text_ptr[i].itxt_length);
120 
121 			FreeImage_SetTagLength(tag, tag_length);
122 			FreeImage_SetTagCount(tag, tag_length);
123 			FreeImage_SetTagType(tag, FIDT_ASCII);
124 			FreeImage_SetTagValue(tag, text_ptr[i].text);
125 
126 			if(strcmp(text_ptr[i].key, g_png_xmp_keyword) == 0) {
127 				// store the tag as XMP
128 				FreeImage_SetTagKey(tag, g_TagLib_XMPFieldName);
129 				FreeImage_SetMetadata(FIMD_XMP, dib, FreeImage_GetTagKey(tag), tag);
130 			} else {
131 				// store the tag as a comment
132 				FreeImage_SetTagKey(tag, text_ptr[i].key);
133 				FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
134 			}
135 
136 			// destroy the tag
137 			FreeImage_DeleteTag(tag);
138 		}
139 	}
140 
141 	// timestamp chunk
142 	if(png_get_tIME(png_ptr, info_ptr, &mod_time)) {
143 		char timestamp[32];
144 		// create a tag
145 		tag = FreeImage_CreateTag();
146 		if(!tag) return FALSE;
147 
148 		// convert as 'yyyy:MM:dd hh:mm:ss'
149 		sprintf(timestamp, "%4d:%02d:%02d %2d:%02d:%02d", mod_time->year, mod_time->month, mod_time->day, mod_time->hour, mod_time->minute, mod_time->second);
150 
151 		DWORD tag_length = (DWORD)strlen(timestamp) + 1;
152 		FreeImage_SetTagLength(tag, tag_length);
153 		FreeImage_SetTagCount(tag, tag_length);
154 		FreeImage_SetTagType(tag, FIDT_ASCII);
155 		FreeImage_SetTagID(tag, TAG_DATETIME);
156 		FreeImage_SetTagValue(tag, timestamp);
157 
158 		// store the tag as Exif-TIFF
159 		FreeImage_SetTagKey(tag, "DateTime");
160 		FreeImage_SetMetadata(FIMD_EXIF_MAIN, dib, FreeImage_GetTagKey(tag), tag);
161 
162 		// destroy the tag
163 		FreeImage_DeleteTag(tag);
164 	}
165 
166 	return TRUE;
167 }
168 
169 static BOOL
WriteMetadata(png_structp png_ptr,png_infop info_ptr,FIBITMAP * dib)170 WriteMetadata(png_structp png_ptr, png_infop info_ptr, FIBITMAP *dib) {
171 	// XMP keyword
172 	const char *g_png_xmp_keyword = "XML:com.adobe.xmp";
173 
174 	FITAG *tag = NULL;
175 	FIMETADATA *mdhandle = NULL;
176 	BOOL bResult = TRUE;
177 
178 	png_text text_metadata;
179 	png_time mod_time;
180 
181 	// set the 'Comments' metadata as iTXt chuncks
182 
183 	mdhandle = FreeImage_FindFirstMetadata(FIMD_COMMENTS, dib, &tag);
184 
185 	if(mdhandle) {
186 		do {
187 			memset(&text_metadata, 0, sizeof(png_text));
188 			text_metadata.compression = 1;							// iTXt, none
189 			text_metadata.key = (char*)FreeImage_GetTagKey(tag);	// keyword, 1-79 character description of "text"
190 			text_metadata.text = (char*)FreeImage_GetTagValue(tag);	// comment, may be an empty string (ie "")
191 			text_metadata.text_length = FreeImage_GetTagLength(tag);// length of the text string
192 			text_metadata.itxt_length = FreeImage_GetTagLength(tag);// length of the itxt string
193 			text_metadata.lang = 0;		 // language code, 0-79 characters or a NULL pointer
194 			text_metadata.lang_key = 0;	 // keyword translated UTF-8 string, 0 or more chars or a NULL pointer
195 
196 			// set the tag
197 			png_set_text(png_ptr, info_ptr, &text_metadata, 1);
198 
199 		} while(FreeImage_FindNextMetadata(mdhandle, &tag));
200 
201 		FreeImage_FindCloseMetadata(mdhandle);
202 		bResult &= TRUE;
203 	}
204 
205 	// set the 'XMP' metadata as iTXt chuncks
206 	tag = NULL;
207 	FreeImage_GetMetadata(FIMD_XMP, dib, g_TagLib_XMPFieldName, &tag);
208 	if(tag && FreeImage_GetTagLength(tag)) {
209 		memset(&text_metadata, 0, sizeof(png_text));
210 		text_metadata.compression = 1;							// iTXt, none
211 		text_metadata.key = (char*)g_png_xmp_keyword;			// keyword, 1-79 character description of "text"
212 		text_metadata.text = (char*)FreeImage_GetTagValue(tag);	// comment, may be an empty string (ie "")
213 		text_metadata.text_length = FreeImage_GetTagLength(tag);// length of the text string
214 		text_metadata.itxt_length = FreeImage_GetTagLength(tag);// length of the itxt string
215 		text_metadata.lang = 0;		 // language code, 0-79 characters or a NULL pointer
216 		text_metadata.lang_key = 0;	 // keyword translated UTF-8 string, 0 or more chars or a NULL pointer
217 
218 		// set the tag
219 		png_set_text(png_ptr, info_ptr, &text_metadata, 1);
220 		bResult &= TRUE;
221 	}
222 
223 	// set the Exif-TIFF 'DateTime' metadata as a tIME chunk
224 	tag = NULL;
225 	FreeImage_GetMetadata(FIMD_EXIF_MAIN, dib, "DateTime", &tag);
226 	if(tag && FreeImage_GetTagLength(tag)) {
227 		int year, month, day, hour, minute, second;
228 		const char *value = (char*)FreeImage_GetTagValue(tag);
229 		if(sscanf(value, "%4d:%02d:%02d %2d:%02d:%02d", &year, &month, &day, &hour, &minute, &second) == 6) {
230 			mod_time.year	= (png_uint_16)year;
231 			mod_time.month	= (png_byte)month;
232 			mod_time.day	= (png_byte)day;
233 			mod_time.hour	= (png_byte)hour;
234 			mod_time.minute	= (png_byte)minute;
235 			mod_time.second	= (png_byte)second;
236 			png_set_tIME (png_ptr, info_ptr, &mod_time);
237 		}
238 	}
239 
240 	return bResult;
241 }
242 
243 // ==========================================================
244 // Plugin Implementation
245 // ==========================================================
246 
247 static const char * DLL_CALLCONV
Format()248 Format() {
249 	return "PNG";
250 }
251 
252 static const char * DLL_CALLCONV
Description()253 Description() {
254 	return "Portable Network Graphics";
255 }
256 
257 static const char * DLL_CALLCONV
Extension()258 Extension() {
259 	return "png";
260 }
261 
262 static const char * DLL_CALLCONV
RegExpr()263 RegExpr() {
264 	return "^.PNG\r";
265 }
266 
267 static const char * DLL_CALLCONV
MimeType()268 MimeType() {
269 	return "image/png";
270 }
271 
272 static BOOL DLL_CALLCONV
Validate(FreeImageIO * io,fi_handle handle)273 Validate(FreeImageIO *io, fi_handle handle) {
274 	BYTE png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
275 	BYTE signature[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
276 
277 	io->read_proc(&signature, 1, 8, handle);
278 
279 	return (memcmp(png_signature, signature, 8) == 0);
280 }
281 
282 static BOOL DLL_CALLCONV
SupportsExportDepth(int depth)283 SupportsExportDepth(int depth) {
284 	return (
285 			(depth == 1) ||
286 			(depth == 4) ||
287 			(depth == 8) ||
288 			(depth == 24) ||
289 			(depth == 32)
290 		);
291 }
292 
293 static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type)294 SupportsExportType(FREE_IMAGE_TYPE type) {
295 	return (
296 		(type == FIT_BITMAP) ||
297 		(type == FIT_UINT16) ||
298 		(type == FIT_RGB16) ||
299 		(type == FIT_RGBA16)
300 	);
301 }
302 
303 static BOOL DLL_CALLCONV
SupportsICCProfiles()304 SupportsICCProfiles() {
305 	return TRUE;
306 }
307 
308 static BOOL DLL_CALLCONV
SupportsNoPixels()309 SupportsNoPixels() {
310 	return TRUE;
311 }
312 
313 // --------------------------------------------------------------------------
314 
315 /**
316 Configure the decoder so that decoded pixels are compatible with a FREE_IMAGE_TYPE format.
317 Set conversion instructions as needed.
318 @param png_ptr PNG handle
319 @param info_ptr PNG info handle
320 @param flags Decoder flags
321 @param output_image_type Returned FreeImage converted image type
322 @return Returns TRUE if successful, returns FALSE otherwise
323 @see png_read_update_info
324 */
325 static BOOL
ConfigureDecoder(png_structp png_ptr,png_infop info_ptr,int flags,FREE_IMAGE_TYPE * output_image_type)326 ConfigureDecoder(png_structp png_ptr, png_infop info_ptr, int flags, FREE_IMAGE_TYPE *output_image_type) {
327 	// get original image info
328 	const int color_type = png_get_color_type(png_ptr, info_ptr);
329 	const int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
330 	const int pixel_depth = bit_depth * png_get_channels(png_ptr, info_ptr);
331 
332 	FREE_IMAGE_TYPE image_type = FIT_BITMAP;	// assume standard image type
333 
334 	// check for transparency table or single transparent color
335 	BOOL bIsTransparent = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) == PNG_INFO_tRNS ? TRUE : FALSE;
336 
337 	// check allowed combinations of colour type and bit depth
338 	// then get converted FreeImage type
339 
340 	switch(color_type) {
341 		case PNG_COLOR_TYPE_GRAY:		// color type '0', bitdepth = 1, 2, 4, 8, 16
342 			switch(bit_depth) {
343 				case 1:
344 				case 2:
345 				case 4:
346 				case 8:
347 					// expand grayscale images to the full 8-bit from 2-bit/pixel
348 					if (pixel_depth == 2) {
349 						png_set_expand_gray_1_2_4_to_8(png_ptr);
350 					}
351 
352 					// if a tRNS chunk is provided, we must also expand the grayscale data to 8-bits,
353 					// this allows us to make use of the transparency table with existing FreeImage methods
354 					if (bIsTransparent && (pixel_depth < 8)) {
355 						png_set_expand_gray_1_2_4_to_8(png_ptr);
356 					}
357 					break;
358 
359 				case 16:
360 					image_type = (pixel_depth == 16) ? FIT_UINT16 : FIT_UNKNOWN;
361 
362 					// 16-bit grayscale images can contain a transparent value (shade)
363 					// if found, expand the transparent value to a full alpha channel
364 					if (bIsTransparent && (image_type != FIT_UNKNOWN)) {
365 						// expand tRNS to a full alpha channel
366 						png_set_tRNS_to_alpha(png_ptr);
367 
368 						// expand new 16-bit gray + 16-bit alpha to full 64-bit RGBA
369 						png_set_gray_to_rgb(png_ptr);
370 
371 						image_type = FIT_RGBA16;
372 					}
373 					break;
374 
375 				default:
376 					image_type = FIT_UNKNOWN;
377 					break;
378 			}
379 			break;
380 
381 		case PNG_COLOR_TYPE_RGB:		// color type '2', bitdepth = 8, 16
382 			switch(bit_depth) {
383 				case 8:
384 					image_type = (pixel_depth == 24) ? FIT_BITMAP : FIT_UNKNOWN;
385 					break;
386 				case 16:
387 					image_type = (pixel_depth == 48) ? FIT_RGB16 : FIT_UNKNOWN;
388 					break;
389 				default:
390 					image_type = FIT_UNKNOWN;
391 					break;
392 			}
393 			// sometimes, 24- or 48-bit images may contain transparency information
394 			// check for this use case and convert to an alpha-compatible format
395 			if (bIsTransparent && (image_type != FIT_UNKNOWN)) {
396 				// if the image is 24-bit RGB, mark it as 32-bit; if it is 48-bit, mark it as 64-bit
397 				image_type = (pixel_depth == 24) ? FIT_BITMAP : (pixel_depth == 48) ? FIT_RGBA16 : FIT_UNKNOWN;
398 				// expand tRNS chunk to alpha channel
399 				png_set_tRNS_to_alpha(png_ptr);
400 			}
401 			break;
402 
403 		case PNG_COLOR_TYPE_PALETTE:	// color type '3', bitdepth = 1, 2, 4, 8
404 			switch(bit_depth) {
405 				case 1:
406 				case 2:
407 				case 4:
408 				case 8:
409 					// expand palette images to the full 8 bits from 2 bits/pixel
410 					if (pixel_depth == 2) {
411 						png_set_packing(png_ptr);
412 					}
413 
414 					// if a tRNS chunk is provided, we must also expand the palletized data to 8-bits,
415 					// this allows us to make use of the transparency table with existing FreeImage methods
416 					if (bIsTransparent && (pixel_depth < 8)) {
417 						png_set_packing(png_ptr);
418 					}
419 					break;
420 
421 				default:
422 					image_type = FIT_UNKNOWN;
423 					break;
424 			}
425 			break;
426 
427 		case PNG_COLOR_TYPE_GRAY_ALPHA:	// color type '4', bitdepth = 8, 16
428 			switch(bit_depth) {
429 				case 8:
430 					// 8-bit grayscale + 8-bit alpha => convert to 32-bit RGBA
431 					image_type = (pixel_depth == 16) ? FIT_BITMAP : FIT_UNKNOWN;
432 					break;
433 				case 16:
434 					// 16-bit grayscale + 16-bit alpha => convert to 64-bit RGBA
435 					image_type = (pixel_depth == 32) ? FIT_RGBA16 : FIT_UNKNOWN;
436 					break;
437 				default:
438 					image_type = FIT_UNKNOWN;
439 					break;
440 			}
441 			// expand 8-bit greyscale + 8-bit alpha to 32-bit
442 			// expand 16-bit greyscale + 16-bit alpha to 64-bit
443 			png_set_gray_to_rgb(png_ptr);
444 			break;
445 
446 		case PNG_COLOR_TYPE_RGB_ALPHA:	// color type '6', bitdepth = 8, 16
447 			switch(bit_depth) {
448 				case 8:
449 					break;
450 				case 16:
451 					image_type = (pixel_depth == 64) ? FIT_RGBA16 : FIT_UNKNOWN;
452 					break;
453 				default:
454 					image_type = FIT_UNKNOWN;
455 					break;
456 			}
457 			break;
458 	}
459 
460 	// check for unknown or invalid formats
461 	if(image_type == FIT_UNKNOWN) {
462 		*output_image_type = image_type;
463 		return FALSE;
464 	}
465 
466 #ifndef FREEIMAGE_BIGENDIAN
467 	if((image_type == FIT_UINT16) || (image_type == FIT_RGB16) || (image_type == FIT_RGBA16)) {
468 		// turn on 16-bit byte swapping
469 		png_set_swap(png_ptr);
470 	}
471 #endif
472 
473 #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
474 	if((image_type == FIT_BITMAP) && ((color_type == PNG_COLOR_TYPE_RGB) || (color_type == PNG_COLOR_TYPE_RGB_ALPHA))) {
475 		// flip the RGB pixels to BGR (or RGBA to BGRA)
476 		png_set_bgr(png_ptr);
477 	}
478 #endif
479 
480 	// gamma correction
481 	// unlike the example in the libpng documentation, we have *no* idea where
482 	// this file may have come from--so if it doesn't have a file gamma, don't
483 	// do any correction ("do no harm")
484 
485 	if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
486 		double gamma = 0;
487 		double screen_gamma = 2.2;
488 
489 		if (png_get_gAMA(png_ptr, info_ptr, &gamma) && ( flags & PNG_IGNOREGAMMA ) != PNG_IGNOREGAMMA) {
490 			png_set_gamma(png_ptr, screen_gamma, gamma);
491 		}
492 	}
493 
494 	// all transformations have been registered; now update info_ptr data
495 	png_read_update_info(png_ptr, info_ptr);
496 
497 	// return the output image type
498 	*output_image_type = image_type;
499 
500 	return TRUE;
501 }
502 
503 static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO * io,fi_handle handle,int page,int flags,void * data)504 Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
505 	png_structp png_ptr = NULL;
506 	png_infop info_ptr = NULL;
507 	png_uint_32 width, height;
508 	int color_type;
509 	int bit_depth;
510 	int pixel_depth = 0;	// pixel_depth = bit_depth * channels
511 
512 	FIBITMAP *dib = NULL;
513 	png_bytepp row_pointers = NULL;
514 
515     fi_ioStructure fio;
516     fio.s_handle = handle;
517 	fio.s_io = io;
518 
519 	if (handle) {
520 		BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
521 
522 		try {
523 			// check to see if the file is in fact a PNG file
524 
525 			BYTE png_check[PNG_BYTES_TO_CHECK];
526 
527 			io->read_proc(png_check, PNG_BYTES_TO_CHECK, 1, handle);
528 
529 			if (png_sig_cmp(png_check, (png_size_t)0, PNG_BYTES_TO_CHECK) != 0) {
530 				return NULL;	// Bad signature
531 			}
532 
533 			// create the chunk manage structure
534 
535 			png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, error_handler, warning_handler);
536 
537 			if (!png_ptr) {
538 				return NULL;
539 			}
540 
541 			// create the info structure
542 
543 		    info_ptr = png_create_info_struct(png_ptr);
544 
545 			if (!info_ptr) {
546 				png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
547 				return NULL;
548 			}
549 
550 			// init the IO
551 
552 			png_set_read_fn(png_ptr, &fio, _ReadProc);
553 
554 			// PNG errors will be redirected here
555 
556 			if (setjmp(png_jmpbuf(png_ptr))) {
557 				// assume error_handler was called before by the PNG library
558 				throw((const char*)NULL);
559 			}
560 
561 			// because we have already read the signature...
562 
563 			png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK);
564 
565 			// read the IHDR chunk
566 
567 			png_read_info(png_ptr, info_ptr);
568 			png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
569 
570 			// configure the decoder
571 
572 			FREE_IMAGE_TYPE image_type = FIT_BITMAP;
573 
574 			if(!ConfigureDecoder(png_ptr, info_ptr, flags, &image_type)) {
575 				throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
576 			}
577 
578 			// update image info
579 
580 			color_type = png_get_color_type(png_ptr, info_ptr);
581 			bit_depth = png_get_bit_depth(png_ptr, info_ptr);
582 			pixel_depth = bit_depth * png_get_channels(png_ptr, info_ptr);
583 
584 			// create a dib and write the bitmap header
585 			// set up the dib palette, if needed
586 
587 			switch (color_type) {
588 				case PNG_COLOR_TYPE_RGB:
589 				case PNG_COLOR_TYPE_RGB_ALPHA:
590 					dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
591 					break;
592 
593 				case PNG_COLOR_TYPE_PALETTE:
594 					dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
595 					if(dib) {
596 						png_colorp png_palette = NULL;
597 						int palette_entries = 0;
598 
599 						png_get_PLTE(png_ptr,info_ptr, &png_palette, &palette_entries);
600 
601 						palette_entries = MIN((unsigned)palette_entries, FreeImage_GetColorsUsed(dib));
602 
603 						// store the palette
604 
605 						RGBQUAD *palette = FreeImage_GetPalette(dib);
606 						for(int i = 0; i < palette_entries; i++) {
607 							palette[i].rgbRed   = png_palette[i].red;
608 							palette[i].rgbGreen = png_palette[i].green;
609 							palette[i].rgbBlue  = png_palette[i].blue;
610 						}
611 					}
612 					break;
613 
614 				case PNG_COLOR_TYPE_GRAY:
615 					dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
616 
617 					if(dib && (pixel_depth <= 8)) {
618 						RGBQUAD *palette = FreeImage_GetPalette(dib);
619 						const int palette_entries = 1 << pixel_depth;
620 
621 						for(int i = 0; i < palette_entries; i++) {
622 							palette[i].rgbRed   =
623 							palette[i].rgbGreen =
624 							palette[i].rgbBlue  = (BYTE)((i * 255) / (palette_entries - 1));
625 						}
626 					}
627 					break;
628 
629 				default:
630 					throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
631 			}
632 
633 			if(!dib) {
634 				throw FI_MSG_ERROR_DIB_MEMORY;
635 			}
636 
637 			// store the transparency table
638 
639 			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
640 				// array of alpha (transparency) entries for palette
641 				png_bytep trans_alpha = NULL;
642 				// number of transparent entries
643 				int num_trans = 0;
644 				// graylevel or color sample values of the single transparent color for non-paletted images
645 				png_color_16p trans_color = NULL;
646 
647 				png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color);
648 
649 				if((color_type == PNG_COLOR_TYPE_GRAY) && trans_color) {
650 					// single transparent color
651 					if (trans_color->gray < 256) {
652 						BYTE table[256];
653 						memset(table, 0xFF, 256);
654 						table[trans_color->gray] = 0;
655 						FreeImage_SetTransparencyTable(dib, table, 256);
656 					}
657 					// check for a full transparency table, too
658 					else if ((trans_alpha) && (pixel_depth <= 8)) {
659 						FreeImage_SetTransparencyTable(dib, (BYTE *)trans_alpha, num_trans);
660 					}
661 
662 				} else if((color_type == PNG_COLOR_TYPE_PALETTE) && trans_alpha) {
663 					// transparency table
664 					FreeImage_SetTransparencyTable(dib, (BYTE *)trans_alpha, num_trans);
665 				}
666 			}
667 
668 			// store the background color (only supported for FIT_BITMAP types)
669 
670 			if ((image_type == FIT_BITMAP) && png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD)) {
671 				// Get the background color to draw transparent and alpha images over.
672 				// Note that even if the PNG file supplies a background, you are not required to
673 				// use it - you should use the (solid) application background if it has one.
674 
675 				png_color_16p image_background = NULL;
676 				RGBQUAD rgbBkColor;
677 
678 				if (png_get_bKGD(png_ptr, info_ptr, &image_background)) {
679 					rgbBkColor.rgbRed      = (BYTE)image_background->red;
680 					rgbBkColor.rgbGreen    = (BYTE)image_background->green;
681 					rgbBkColor.rgbBlue     = (BYTE)image_background->blue;
682 					rgbBkColor.rgbReserved = 0;
683 
684 					FreeImage_SetBackgroundColor(dib, &rgbBkColor);
685 				}
686 			}
687 
688 			// get physical resolution
689 
690 			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
691 				png_uint_32 res_x, res_y;
692 
693 				// we'll overload this var and use 0 to mean no phys data,
694 				// since if it's not in meters we can't use it anyway
695 
696 				int res_unit_type = PNG_RESOLUTION_UNKNOWN;
697 
698 				png_get_pHYs(png_ptr,info_ptr, &res_x, &res_y, &res_unit_type);
699 
700 				if (res_unit_type == PNG_RESOLUTION_METER) {
701 					FreeImage_SetDotsPerMeterX(dib, res_x);
702 					FreeImage_SetDotsPerMeterY(dib, res_y);
703 				}
704 			}
705 
706 			// get possible ICC profile
707 
708 			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
709 				png_charp profile_name = NULL;
710 				png_bytep profile_data = NULL;
711 				png_uint_32 profile_length = 0;
712 				int  compression_type;
713 
714 				png_get_iCCP(png_ptr, info_ptr, &profile_name, &compression_type, &profile_data, &profile_length);
715 
716 				// copy ICC profile data (must be done after FreeImage_AllocateHeader)
717 
718 				FreeImage_CreateICCProfile(dib, profile_data, profile_length);
719 			}
720 
721 			// --- header only mode => clean-up and return
722 
723 			if (header_only) {
724 				// get possible metadata (it can be located both before and after the image data)
725 				ReadMetadata(png_ptr, info_ptr, dib);
726 				if (png_ptr) {
727 					// clean up after the read, and free any memory allocated - REQUIRED
728 					png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
729 				}
730 				return dib;
731 			}
732 
733 			// set the individual row_pointers to point at the correct offsets
734 
735 			row_pointers = (png_bytepp)malloc(height * sizeof(png_bytep));
736 
737 			if (!row_pointers) {
738 				png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
739 				FreeImage_Unload(dib);
740 				return NULL;
741 			}
742 
743 			// read in the bitmap bits via the pointer table
744 			// allow loading of PNG with minor errors (such as images with several IDAT chunks)
745 
746 			for (png_uint_32 k = 0; k < height; k++) {
747 				row_pointers[height - 1 - k] = FreeImage_GetScanLine(dib, k);
748 			}
749 
750 			png_set_benign_errors(png_ptr, 1);
751 			png_read_image(png_ptr, row_pointers);
752 
753 			// check if the bitmap contains transparency, if so enable it in the header
754 
755 			if (FreeImage_GetBPP(dib) == 32) {
756 				if (FreeImage_GetColorType(dib) == FIC_RGBALPHA) {
757 					FreeImage_SetTransparent(dib, TRUE);
758 				} else {
759 					FreeImage_SetTransparent(dib, FALSE);
760 				}
761 			}
762 
763 			// cleanup
764 
765 			if (row_pointers) {
766 				free(row_pointers);
767 				row_pointers = NULL;
768 			}
769 
770 			// read the rest of the file, getting any additional chunks in info_ptr
771 
772 			png_read_end(png_ptr, info_ptr);
773 
774 			// get possible metadata (it can be located both before and after the image data)
775 
776 			ReadMetadata(png_ptr, info_ptr, dib);
777 
778 			if (png_ptr) {
779 				// clean up after the read, and free any memory allocated - REQUIRED
780 				png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
781 			}
782 
783 			return dib;
784 
785 		} catch (const char *text) {
786 			if (png_ptr) {
787 				png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
788 			}
789 			if (row_pointers) {
790 				free(row_pointers);
791 			}
792 			if (dib) {
793 				FreeImage_Unload(dib);
794 			}
795 			if (NULL != text) {
796 				FreeImage_OutputMessageProc(s_format_id, text);
797 			}
798 
799 			return NULL;
800 		}
801 	}
802 
803 	return NULL;
804 }
805 
806 // --------------------------------------------------------------------------
807 
808 static BOOL DLL_CALLCONV
Save(FreeImageIO * io,FIBITMAP * dib,fi_handle handle,int page,int flags,void * data)809 Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
810 	png_structp png_ptr;
811 	png_infop info_ptr;
812 	png_colorp palette = NULL;
813 	png_uint_32 width, height;
814 	BOOL has_alpha_channel = FALSE;
815 
816 	RGBQUAD *pal;					// pointer to dib palette
817 	int bit_depth, pixel_depth;		// pixel_depth = bit_depth * channels
818 	int palette_entries;
819 	int	interlace_type;
820 
821 	fi_ioStructure fio;
822     fio.s_handle = handle;
823 	fio.s_io = io;
824 
825 	if ((dib) && (handle)) {
826 		try {
827 			// create the chunk manage structure
828 
829 			png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, error_handler, warning_handler);
830 
831 			if (!png_ptr)  {
832 				return FALSE;
833 			}
834 
835 			// allocate/initialize the image information data.
836 
837 			info_ptr = png_create_info_struct(png_ptr);
838 
839 			if (!info_ptr)  {
840 				png_destroy_write_struct(&png_ptr,  (png_infopp)NULL);
841 				return FALSE;
842 			}
843 
844 			// Set error handling.  REQUIRED if you aren't supplying your own
845 			// error handling functions in the png_create_write_struct() call.
846 
847 			if (setjmp(png_jmpbuf(png_ptr)))  {
848 				// if we get here, we had a problem reading the file
849 
850 				png_destroy_write_struct(&png_ptr, &info_ptr);
851 
852 				return FALSE;
853 			}
854 
855 			// init the IO
856 
857 			png_set_write_fn(png_ptr, &fio, _WriteProc, _FlushProc);
858 
859 			// set physical resolution
860 
861 			png_uint_32 res_x = (png_uint_32)FreeImage_GetDotsPerMeterX(dib);
862 			png_uint_32 res_y = (png_uint_32)FreeImage_GetDotsPerMeterY(dib);
863 
864 			if ((res_x > 0) && (res_y > 0))  {
865 				png_set_pHYs(png_ptr, info_ptr, res_x, res_y, PNG_RESOLUTION_METER);
866 			}
867 
868 			// Set the image information here.  Width and height are up to 2^31,
869 			// bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
870 			// the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
871 			// PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
872 			// or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
873 			// PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
874 			// currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
875 
876 			width = FreeImage_GetWidth(dib);
877 			height = FreeImage_GetHeight(dib);
878 			pixel_depth = FreeImage_GetBPP(dib);
879 
880 			BOOL bInterlaced = FALSE;
881 			if( (flags & PNG_INTERLACED) == PNG_INTERLACED) {
882 				interlace_type = PNG_INTERLACE_ADAM7;
883 				bInterlaced = TRUE;
884 			} else {
885 				interlace_type = PNG_INTERLACE_NONE;
886 			}
887 
888 			// set the ZLIB compression level or default to PNG default compression level (ZLIB level = 6)
889 			int zlib_level = flags & 0x0F;
890 			if((zlib_level >= 1) && (zlib_level <= 9)) {
891 				png_set_compression_level(png_ptr, zlib_level);
892 			} else if((flags & PNG_Z_NO_COMPRESSION) == PNG_Z_NO_COMPRESSION) {
893 				png_set_compression_level(png_ptr, Z_NO_COMPRESSION);
894 			}
895 
896 			// filtered strategy works better for high color images
897 			if(pixel_depth >= 16){
898 				png_set_compression_strategy(png_ptr, Z_FILTERED);
899 				png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
900 			} else {
901 				png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
902 			}
903 
904 			FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
905 			if(image_type == FIT_BITMAP) {
906 				// standard image type
907 				bit_depth = (pixel_depth > 8) ? 8 : pixel_depth;
908 			} else {
909 				// 16-bit greyscale or 16-bit RGB(A)
910 				bit_depth = 16;
911 			}
912 
913 			// check for transparent images
914 			BOOL bIsTransparent =
915 				(image_type == FIT_BITMAP) && FreeImage_IsTransparent(dib) && (FreeImage_GetTransparencyCount(dib) > 0) ? TRUE : FALSE;
916 
917 			switch (FreeImage_GetColorType(dib)) {
918 				case FIC_MINISWHITE:
919 					if(!bIsTransparent) {
920 						// Invert monochrome files to have 0 as black and 1 as white (no break here)
921 						png_set_invert_mono(png_ptr);
922 					}
923 					// (fall through)
924 
925 				case FIC_MINISBLACK:
926 					if(!bIsTransparent) {
927 						png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
928 							PNG_COLOR_TYPE_GRAY, interlace_type,
929 							PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
930 						break;
931 					}
932 					// If a monochrome image is transparent, save it with a palette
933 					// (fall through)
934 
935 				case FIC_PALETTE:
936 				{
937 					png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
938 						PNG_COLOR_TYPE_PALETTE, interlace_type,
939 						PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
940 
941 					// set the palette
942 
943 					palette_entries = 1 << bit_depth;
944 					palette = (png_colorp)png_malloc(png_ptr, palette_entries * sizeof (png_color));
945 					pal = FreeImage_GetPalette(dib);
946 
947 					for (int i = 0; i < palette_entries; i++) {
948 						palette[i].red   = pal[i].rgbRed;
949 						palette[i].green = pal[i].rgbGreen;
950 						palette[i].blue  = pal[i].rgbBlue;
951 					}
952 
953 					png_set_PLTE(png_ptr, info_ptr, palette, palette_entries);
954 
955 					// You must not free palette here, because png_set_PLTE only makes a link to
956 					// the palette that you malloced.  Wait until you are about to destroy
957 					// the png structure.
958 
959 					break;
960 				}
961 
962 				case FIC_RGBALPHA :
963 					has_alpha_channel = TRUE;
964 
965 					png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
966 						PNG_COLOR_TYPE_RGBA, interlace_type,
967 						PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
968 
969 #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
970 					// flip BGR pixels to RGB
971 					if(image_type == FIT_BITMAP) {
972 						png_set_bgr(png_ptr);
973 					}
974 #endif
975 					break;
976 
977 				case FIC_RGB:
978 					png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
979 						PNG_COLOR_TYPE_RGB, interlace_type,
980 						PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
981 
982 #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
983 					// flip BGR pixels to RGB
984 					if(image_type == FIT_BITMAP) {
985 						png_set_bgr(png_ptr);
986 					}
987 #endif
988 					break;
989 
990 				case FIC_CMYK:
991 					break;
992 			}
993 
994 			// write possible ICC profile
995 
996 			FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
997 			if (iccProfile->size && iccProfile->data) {
998 				// skip ICC profile check
999 				png_set_option(png_ptr, PNG_SKIP_sRGB_CHECK_PROFILE, 1);
1000 				png_set_iCCP(png_ptr, info_ptr, "Embedded Profile", 0, (png_const_bytep)iccProfile->data, iccProfile->size);
1001 			}
1002 
1003 			// write metadata
1004 
1005 			WriteMetadata(png_ptr, info_ptr, dib);
1006 
1007 			// Optional gamma chunk is strongly suggested if you have any guess
1008 			// as to the correct gamma of the image.
1009 			// png_set_gAMA(png_ptr, info_ptr, gamma);
1010 
1011 			// set the transparency table
1012 
1013 			if (bIsTransparent) {
1014 				png_set_tRNS(png_ptr, info_ptr, FreeImage_GetTransparencyTable(dib), FreeImage_GetTransparencyCount(dib), NULL);
1015 			}
1016 
1017 			// set the background color
1018 
1019 			if(FreeImage_HasBackgroundColor(dib)) {
1020 				png_color_16 image_background;
1021 				RGBQUAD rgbBkColor;
1022 
1023 				FreeImage_GetBackgroundColor(dib, &rgbBkColor);
1024 				memset(&image_background, 0, sizeof(png_color_16));
1025 				image_background.blue  = rgbBkColor.rgbBlue;
1026 				image_background.green = rgbBkColor.rgbGreen;
1027 				image_background.red   = rgbBkColor.rgbRed;
1028 				image_background.index = rgbBkColor.rgbReserved;
1029 
1030 				png_set_bKGD(png_ptr, info_ptr, &image_background);
1031 			}
1032 
1033 			// Write the file header information.
1034 
1035 			png_write_info(png_ptr, info_ptr);
1036 
1037 			// write out the image data
1038 
1039 #ifndef FREEIMAGE_BIGENDIAN
1040 			if (bit_depth == 16) {
1041 				// turn on 16 bit byte swapping
1042 				png_set_swap(png_ptr);
1043 			}
1044 #endif
1045 
1046 			int number_passes = 1;
1047 			if (bInterlaced) {
1048 				number_passes = png_set_interlace_handling(png_ptr);
1049 			}
1050 
1051 			if ((pixel_depth == 32) && (!has_alpha_channel)) {
1052 				BYTE *buffer = (BYTE *)malloc(width * 3);
1053 
1054 				// transparent conversion to 24-bit
1055 				// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
1056 				for (int pass = 0; pass < number_passes; pass++) {
1057 					for (png_uint_32 k = 0; k < height; k++) {
1058 						FreeImage_ConvertLine32To24(buffer, FreeImage_GetScanLine(dib, height - k - 1), width);
1059 						png_write_row(png_ptr, buffer);
1060 					}
1061 				}
1062 				free(buffer);
1063 			} else {
1064 				// the number of passes is either 1 for non-interlaced images, or 7 for interlaced images
1065 				for (int pass = 0; pass < number_passes; pass++) {
1066 					for (png_uint_32 k = 0; k < height; k++) {
1067 						png_write_row(png_ptr, FreeImage_GetScanLine(dib, height - k - 1));
1068 					}
1069 				}
1070 			}
1071 
1072 			// It is REQUIRED to call this to finish writing the rest of the file
1073 			// Bug with png_flush
1074 
1075 			png_write_end(png_ptr, info_ptr);
1076 
1077 			// clean up after the write, and free any memory allocated
1078 			if (palette) {
1079 				png_free(png_ptr, palette);
1080 			}
1081 
1082 			png_destroy_write_struct(&png_ptr, &info_ptr);
1083 
1084 			return TRUE;
1085 
1086 		} catch (const char *text) {
1087 			if(png_ptr) {
1088 				png_destroy_write_struct(&png_ptr, &info_ptr);
1089 			}
1090 			FreeImage_OutputMessageProc(s_format_id, text);
1091 		}
1092 	}
1093 
1094 	return FALSE;
1095 }
1096 
1097 // ==========================================================
1098 //   Init
1099 // ==========================================================
1100 
1101 void DLL_CALLCONV
InitPNG(Plugin * plugin,int format_id)1102 InitPNG(Plugin *plugin, int format_id) {
1103 	s_format_id = format_id;
1104 
1105 	plugin->format_proc = Format;
1106 	plugin->description_proc = Description;
1107 	plugin->extension_proc = Extension;
1108 	plugin->regexpr_proc = RegExpr;
1109 	plugin->open_proc = NULL;
1110 	plugin->close_proc = NULL;
1111 	plugin->pagecount_proc = NULL;
1112 	plugin->pagecapability_proc = NULL;
1113 	plugin->load_proc = Load;
1114 	plugin->save_proc = Save;
1115 	plugin->validate_proc = Validate;
1116 	plugin->mime_proc = MimeType;
1117 	plugin->supports_export_bpp_proc = SupportsExportDepth;
1118 	plugin->supports_export_type_proc = SupportsExportType;
1119 	plugin->supports_icc_profiles_proc = SupportsICCProfiles;
1120 	plugin->supports_no_pixels_proc = SupportsNoPixels;
1121 }
1122