PngFile.c 12 KB

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