PngFile.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*-------------------------------------
  2. * PNGFILE.C -- Image File Functions
  3. *-------------------------------------
  4. *
  5. * Copyright 2000,2017 Willem van Schaik.
  6. *
  7. * This code is released under the libpng license.
  8. * For conditions of distribution and use, see the disclaimer
  9. * and license in png.h
  10. */
  11. #include <windows.h>
  12. #include <commdlg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <zlib.h>
  16. #include "png.h"
  17. #include "pngfile.h"
  18. #include "cexcept.h"
  19. define_exception_type(const char *);
  20. extern struct exception_context the_exception_context[1];
  21. struct exception_context the_exception_context[1];
  22. png_const_charp msg;
  23. static OPENFILENAME ofn;
  24. static png_structp png_ptr = NULL;
  25. static png_infop info_ptr = NULL;
  26. /* cexcept interface */
  27. static void
  28. png_cexcept_error(png_structp png_ptr, png_const_charp msg)
  29. {
  30. if(png_ptr)
  31. ;
  32. #ifdef PNG_CONSOLE_IO_SUPPORTED
  33. fprintf(stderr, "libpng error: %s\n", msg);
  34. #endif
  35. {
  36. Throw msg;
  37. }
  38. }
  39. /* Windows open-file functions */
  40. void PngFileInitialize (HWND hwnd)
  41. {
  42. static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
  43. TEXT ("All Files (*.*)\0*.*\0\0");
  44. ofn.lStructSize = sizeof (OPENFILENAME);
  45. ofn.hwndOwner = hwnd;
  46. ofn.hInstance = NULL;
  47. ofn.lpstrFilter = szFilter;
  48. ofn.lpstrCustomFilter = NULL;
  49. ofn.nMaxCustFilter = 0;
  50. ofn.nFilterIndex = 0;
  51. ofn.lpstrFile = NULL; /* Set in Open and Close functions */
  52. ofn.nMaxFile = MAX_PATH;
  53. ofn.lpstrFileTitle = NULL; /* Set in Open and Close functions */
  54. ofn.nMaxFileTitle = MAX_PATH;
  55. ofn.lpstrInitialDir = NULL;
  56. ofn.lpstrTitle = NULL;
  57. ofn.Flags = 0; /* Set in Open and Close functions */
  58. ofn.nFileOffset = 0;
  59. ofn.nFileExtension = 0;
  60. ofn.lpstrDefExt = TEXT ("png");
  61. ofn.lCustData = 0;
  62. ofn.lpfnHook = NULL;
  63. ofn.lpTemplateName = NULL;
  64. }
  65. BOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  66. {
  67. ofn.hwndOwner = hwnd;
  68. ofn.lpstrFile = pstrFileName;
  69. ofn.lpstrFileTitle = pstrTitleName;
  70. ofn.Flags = OFN_HIDEREADONLY;
  71. return GetOpenFileName (&ofn);
  72. }
  73. BOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  74. {
  75. ofn.hwndOwner = hwnd;
  76. ofn.lpstrFile = pstrFileName;
  77. ofn.lpstrFileTitle = pstrTitleName;
  78. ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  79. return GetSaveFileName (&ofn);
  80. }
  81. /* PNG image handler functions */
  82. BOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
  83. int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
  84. {
  85. static FILE *pfFile;
  86. png_byte pbSig[8];
  87. int iBitDepth;
  88. int iColorType;
  89. double dGamma;
  90. png_color_16 *pBackground;
  91. png_uint_32 ulChannels;
  92. png_uint_32 ulRowBytes;
  93. png_byte *pbImageData = *ppbImageData;
  94. static png_byte **ppbRowPointers = NULL;
  95. int i;
  96. /* open the PNG input file */
  97. if (!pstrFileName)
  98. {
  99. *ppbImageData = pbImageData = NULL;
  100. return FALSE;
  101. }
  102. if (!(pfFile = fopen(pstrFileName, "rb")))
  103. {
  104. *ppbImageData = pbImageData = NULL;
  105. return FALSE;
  106. }
  107. /* first check the eight byte PNG signature */
  108. fread(pbSig, 1, 8, pfFile);
  109. if (png_sig_cmp(pbSig, 0, 8))
  110. {
  111. *ppbImageData = pbImageData = NULL;
  112. return FALSE;
  113. }
  114. /* create the two png(-info) structures */
  115. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
  116. (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
  117. if (!png_ptr)
  118. {
  119. *ppbImageData = pbImageData = NULL;
  120. return FALSE;
  121. }
  122. info_ptr = png_create_info_struct(png_ptr);
  123. if (!info_ptr)
  124. {
  125. png_destroy_read_struct(&png_ptr, NULL, NULL);
  126. *ppbImageData = pbImageData = NULL;
  127. return FALSE;
  128. }
  129. Try
  130. {
  131. /* initialize the png structure */
  132. #ifdef PNG_STDIO_SUPPORTED
  133. png_init_io(png_ptr, pfFile);
  134. #else
  135. png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
  136. #endif
  137. png_set_sig_bytes(png_ptr, 8);
  138. /* read all PNG info up to image data */
  139. png_read_info(png_ptr, info_ptr);
  140. /* get width, height, bit-depth and color-type */
  141. png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
  142. &iColorType, NULL, NULL, NULL);
  143. /* expand images of all color-type and bit-depth to 3x8-bit RGB */
  144. /* let the library process alpha, transparency, background, etc. */
  145. #ifdef PNG_READ_16_TO_8_SUPPORTED
  146. if (iBitDepth == 16)
  147. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  148. png_set_scale_16(png_ptr);
  149. # else
  150. png_set_strip_16(png_ptr);
  151. # endif
  152. #endif
  153. if (iColorType == PNG_COLOR_TYPE_PALETTE)
  154. png_set_expand(png_ptr);
  155. if (iBitDepth < 8)
  156. png_set_expand(png_ptr);
  157. if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  158. png_set_expand(png_ptr);
  159. if (iColorType == PNG_COLOR_TYPE_GRAY ||
  160. iColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
  161. png_set_gray_to_rgb(png_ptr);
  162. /* set the background color to draw transparent and alpha images over */
  163. if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
  164. {
  165. png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
  166. pBkgColor->red = (byte) pBackground->red;
  167. pBkgColor->green = (byte) pBackground->green;
  168. pBkgColor->blue = (byte) pBackground->blue;
  169. }
  170. else
  171. {
  172. pBkgColor = NULL;
  173. }
  174. /* if required set gamma conversion */
  175. if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
  176. png_set_gamma(png_ptr, (double) 2.2, dGamma);
  177. /* after the transformations are registered, update info_ptr data */
  178. png_read_update_info(png_ptr, info_ptr);
  179. /* get again width, height and the new bit-depth and color-type */
  180. png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
  181. &iColorType, NULL, NULL, NULL);
  182. /* row_bytes is the width x number of channels */
  183. ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
  184. ulChannels = png_get_channels(png_ptr, info_ptr);
  185. *piChannels = ulChannels;
  186. /* now we can allocate memory to store the image */
  187. if (pbImageData)
  188. {
  189. free (pbImageData);
  190. pbImageData = NULL;
  191. }
  192. if ((*piHeight) > ((size_t)(-1))/ulRowBytes) {
  193. {
  194. png_error(png_ptr, "Visual PNG: image is too big");
  195. }
  196. if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
  197. * sizeof(png_byte))) == NULL)
  198. {
  199. png_error(png_ptr, "Visual PNG: out of memory");
  200. }
  201. *ppbImageData = pbImageData;
  202. /* and allocate memory for an array of row-pointers */
  203. if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
  204. * sizeof(png_bytep))) == NULL)
  205. {
  206. png_error(png_ptr, "Visual PNG: out of memory");
  207. }
  208. /* set the individual row-pointers to point at the correct offsets */
  209. for (i = 0; i < (*piHeight); i++)
  210. ppbRowPointers[i] = pbImageData + i * ulRowBytes;
  211. /* now we can go ahead and just read the whole image */
  212. png_read_image(png_ptr, ppbRowPointers);
  213. /* read the additional chunks in the PNG file (not really needed) */
  214. png_read_end(png_ptr, NULL);
  215. /* and we're done */
  216. free (ppbRowPointers);
  217. ppbRowPointers = NULL;
  218. /* yepp, done */
  219. }
  220. Catch (msg)
  221. {
  222. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  223. *ppbImageData = pbImageData = NULL;
  224. if(ppbRowPointers)
  225. free (ppbRowPointers);
  226. fclose(pfFile);
  227. return FALSE;
  228. }
  229. fclose (pfFile);
  230. return TRUE;
  231. }
  232. BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
  233. int iWidth, int iHeight, png_color bkgColor)
  234. {
  235. const int ciBitDepth = 8;
  236. const int ciChannels = 3;
  237. static FILE *pfFile;
  238. png_uint_32 ulRowBytes;
  239. static png_byte **ppbRowPointers = NULL;
  240. int i;
  241. /* open the PNG output file */
  242. if (!pstrFileName)
  243. return FALSE;
  244. if (!(pfFile = fopen(pstrFileName, "wb")))
  245. return FALSE;
  246. /* prepare the standard PNG structures */
  247. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
  248. (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
  249. if (!png_ptr)
  250. {
  251. fclose(pfFile);
  252. return FALSE;
  253. }
  254. info_ptr = png_create_info_struct(png_ptr);
  255. if (!info_ptr) {
  256. fclose(pfFile);
  257. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  258. return FALSE;
  259. }
  260. Try
  261. {
  262. /* initialize the png structure */
  263. #ifdef PNG_STDIO_SUPPORTED
  264. png_init_io(png_ptr, pfFile);
  265. #else
  266. png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
  267. #endif
  268. /* we're going to write a very simple 3x8-bit RGB image */
  269. png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
  270. PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
  271. PNG_FILTER_TYPE_BASE);
  272. /* write the file header information */
  273. png_write_info(png_ptr, info_ptr);
  274. /* swap the BGR pixels in the DiData structure to RGB */
  275. png_set_bgr(png_ptr);
  276. /* row_bytes is the width x number of channels */
  277. ulRowBytes = iWidth * ciChannels;
  278. /* we can allocate memory for an array of row-pointers */
  279. if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
  280. Throw "Visualpng: Out of memory";
  281. /* set the individual row-pointers to point at the correct offsets */
  282. for (i = 0; i < iHeight; i++)
  283. ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
  284. /* write out the entire image data in one call */
  285. png_write_image (png_ptr, ppbRowPointers);
  286. /* write the additional chunks to the PNG file (not really needed) */
  287. png_write_end(png_ptr, info_ptr);
  288. /* and we're done */
  289. free (ppbRowPointers);
  290. ppbRowPointers = NULL;
  291. /* clean up after the write, and free any memory allocated */
  292. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  293. /* yepp, done */
  294. }
  295. Catch (msg)
  296. {
  297. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  298. if(ppbRowPointers)
  299. free (ppbRowPointers);
  300. fclose(pfFile);
  301. return FALSE;
  302. }
  303. fclose (pfFile);
  304. return TRUE;
  305. }
  306. #ifndef PNG_STDIO_SUPPORTED
  307. static void
  308. png_read_data(png_structp png_ptr, png_bytep data, size_t length)
  309. {
  310. size_t check;
  311. /* fread() returns 0 on error, so it is OK to store this in a size_t
  312. * instead of an int, which is what fread() actually returns.
  313. */
  314. check = fread(data, 1, length, (FILE *)png_ptr->io_ptr);
  315. if (check != length)
  316. {
  317. png_error(png_ptr, "Read Error");
  318. }
  319. }
  320. static void
  321. png_write_data(png_structp png_ptr, png_bytep data, size_t length)
  322. {
  323. png_uint_32 check;
  324. check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
  325. if (check != length)
  326. {
  327. png_error(png_ptr, "Write Error");
  328. }
  329. }
  330. static void
  331. png_flush(png_structp png_ptr)
  332. {
  333. FILE *io_ptr;
  334. io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
  335. if (io_ptr != NULL)
  336. fflush(io_ptr);
  337. }
  338. #endif
  339. /*-----------------
  340. * end of source
  341. *-----------------
  342. */