PngFile.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*-------------------------------------
  2. * PNGFILE.C -- Image File Functions
  3. *-------------------------------------
  4. *
  5. * Copyright 2000, 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 ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
  193. * sizeof(png_byte))) == NULL)
  194. {
  195. png_error(png_ptr, "Visual PNG: out of memory");
  196. }
  197. *ppbImageData = pbImageData;
  198. /* and allocate memory for an array of row-pointers */
  199. if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
  200. * sizeof(png_bytep))) == NULL)
  201. {
  202. png_error(png_ptr, "Visual PNG: out of memory");
  203. }
  204. /* set the individual row-pointers to point at the correct offsets */
  205. for (i = 0; i < (*piHeight); i++)
  206. ppbRowPointers[i] = pbImageData + i * ulRowBytes;
  207. /* now we can go ahead and just read the whole image */
  208. png_read_image(png_ptr, ppbRowPointers);
  209. /* read the additional chunks in the PNG file (not really needed) */
  210. png_read_end(png_ptr, NULL);
  211. /* and we're done */
  212. free (ppbRowPointers);
  213. ppbRowPointers = NULL;
  214. /* yepp, done */
  215. }
  216. Catch (msg)
  217. {
  218. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  219. *ppbImageData = pbImageData = NULL;
  220. if(ppbRowPointers)
  221. free (ppbRowPointers);
  222. fclose(pfFile);
  223. return FALSE;
  224. }
  225. fclose (pfFile);
  226. return TRUE;
  227. }
  228. BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
  229. int iWidth, int iHeight, png_color bkgColor)
  230. {
  231. const int ciBitDepth = 8;
  232. const int ciChannels = 3;
  233. static FILE *pfFile;
  234. png_uint_32 ulRowBytes;
  235. static png_byte **ppbRowPointers = NULL;
  236. int i;
  237. /* open the PNG output file */
  238. if (!pstrFileName)
  239. return FALSE;
  240. if (!(pfFile = fopen(pstrFileName, "wb")))
  241. return FALSE;
  242. /* prepare the standard PNG structures */
  243. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
  244. (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
  245. if (!png_ptr)
  246. {
  247. fclose(pfFile);
  248. return FALSE;
  249. }
  250. info_ptr = png_create_info_struct(png_ptr);
  251. if (!info_ptr) {
  252. fclose(pfFile);
  253. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  254. return FALSE;
  255. }
  256. Try
  257. {
  258. /* initialize the png structure */
  259. #ifdef PNG_STDIO_SUPPORTED
  260. png_init_io(png_ptr, pfFile);
  261. #else
  262. png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
  263. #endif
  264. /* we're going to write a very simple 3x8-bit RGB image */
  265. png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
  266. PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
  267. PNG_FILTER_TYPE_BASE);
  268. /* write the file header information */
  269. png_write_info(png_ptr, info_ptr);
  270. /* swap the BGR pixels in the DiData structure to RGB */
  271. png_set_bgr(png_ptr);
  272. /* row_bytes is the width x number of channels */
  273. ulRowBytes = iWidth * ciChannels;
  274. /* we can allocate memory for an array of row-pointers */
  275. if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
  276. Throw "Visualpng: Out of memory";
  277. /* set the individual row-pointers to point at the correct offsets */
  278. for (i = 0; i < iHeight; i++)
  279. ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
  280. /* write out the entire image data in one call */
  281. png_write_image (png_ptr, ppbRowPointers);
  282. /* write the additional chunks to the PNG file (not really needed) */
  283. png_write_end(png_ptr, info_ptr);
  284. /* and we're done */
  285. free (ppbRowPointers);
  286. ppbRowPointers = NULL;
  287. /* clean up after the write, and free any memory allocated */
  288. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  289. /* yepp, done */
  290. }
  291. Catch (msg)
  292. {
  293. png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
  294. if(ppbRowPointers)
  295. free (ppbRowPointers);
  296. fclose(pfFile);
  297. return FALSE;
  298. }
  299. fclose (pfFile);
  300. return TRUE;
  301. }
  302. #ifndef PNG_STDIO_SUPPORTED
  303. static void
  304. png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  305. {
  306. png_size_t check;
  307. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  308. * instead of an int, which is what fread() actually returns.
  309. */
  310. check = (png_size_t)fread(data, (png_size_t)1, length,
  311. (FILE *)png_ptr->io_ptr);
  312. if (check != length)
  313. {
  314. png_error(png_ptr, "Read Error");
  315. }
  316. }
  317. static void
  318. png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  319. {
  320. png_uint_32 check;
  321. check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
  322. if (check != length)
  323. {
  324. png_error(png_ptr, "Write Error");
  325. }
  326. }
  327. static void
  328. png_flush(png_structp png_ptr)
  329. {
  330. FILE *io_ptr;
  331. io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
  332. if (io_ptr != NULL)
  333. fflush(io_ptr);
  334. }
  335. #endif
  336. /*-----------------
  337. * end of source
  338. *-----------------
  339. */