Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
12 KiB

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