PNGImage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "PNGImage.h"
  23. #include "math/mPoint.h"
  24. #include "graphics/gBitmap.h"
  25. #include "platform/platformGL.h"
  26. #include "platform/platform.h"
  27. #include "PNGImage_ScriptBinding.h"
  28. #define min(a,b) (a <= b ? a : b)
  29. IMPLEMENT_CONOBJECT(PNGImage);
  30. #define PNGSIGSIZE 8
  31. PNGImage::PNGImage() : mPNGImageType(PNGTYPE_UNKNOWN)
  32. {
  33. mWidth = 0;
  34. mHeight = 0;
  35. mColorType = 0;
  36. mBitDepth = 0;
  37. mPng = 0;
  38. mInfo = 0;
  39. mRowPointers = 0;
  40. mRead = false;
  41. mWrite = false;
  42. }
  43. PNGImage::~PNGImage()
  44. {
  45. }
  46. bool PNGImage::Read(const char* filePath)
  47. {
  48. if(mRead == true || mWrite == true)
  49. CleanMemoryUsage();
  50. /* open file and test for it being a png */
  51. FILE *fp = fopen(filePath, "rb");
  52. if (fp == NULL)
  53. {
  54. Con::printf("PNGImage::Read File %s could not be opened for reading.", filePath);
  55. return false;
  56. }
  57. dStrcpy(mReadFilePath, filePath);
  58. png_byte pngsig[PNGSIGSIZE];
  59. fread((char*)pngsig, 1, PNGSIGSIZE, fp);
  60. if (png_sig_cmp(pngsig, 0, PNGSIGSIZE) != 0)
  61. {
  62. Con::printf("PNGImage::Read File %s is not recognized as a PNG file.", filePath);
  63. return false;
  64. }
  65. /* initialize stuff */
  66. mPng = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  67. if (mPng == NULL)
  68. {
  69. Con::printf("PNGImage::Read png_create_read_struct failed.");
  70. return false;
  71. }
  72. mInfo = png_create_info_struct(mPng);
  73. if (mInfo == NULL)
  74. {
  75. png_destroy_read_struct(&mPng, NULL, NULL);
  76. Con::printf("PNGImage::Read png_create_info_struct failed.");
  77. return false;
  78. }
  79. if (setjmp(png_jmpbuf(mPng)))
  80. {
  81. png_destroy_read_struct(&mPng, NULL, NULL);
  82. Con::printf("PNGImage::Read Error during init_io.");
  83. return false;
  84. }
  85. png_init_io(mPng, fp);
  86. png_set_sig_bytes(mPng, 8);
  87. png_read_info(mPng, mInfo);
  88. // Obtain image information
  89. mWidth = png_get_image_width(mPng, mInfo);
  90. mHeight = png_get_image_height(mPng, mInfo);
  91. mColorType = png_get_color_type(mPng, mInfo);
  92. mBitDepth = png_get_bit_depth(mPng, mInfo);
  93. mRowPointers = (png_bytep*) dMalloc(sizeof(png_bytep) * mHeight);
  94. unsigned int y = 0;
  95. for (y = 0; y < mHeight; y++)
  96. mRowPointers[y] = (png_byte*) dMalloc(png_get_rowbytes(mPng,mInfo));
  97. // Will read the actual png data into mRowPointers
  98. png_read_image(mPng, mRowPointers);
  99. fclose(fp);
  100. mRead = true;
  101. if(png_get_color_type(mPng, mInfo) == PNG_COLOR_TYPE_RGBA)
  102. mPNGImageType = PNGTYPE_RGBA;
  103. else if(png_get_color_type(mPng, mInfo) == PNG_COLOR_TYPE_RGB)
  104. mPNGImageType = PNGTYPE_RGB;
  105. else
  106. mPNGImageType = PNGTYPE_UNKNOWN;
  107. return true;
  108. }
  109. bool PNGImage::Create(U32 width, U32 height, PNGImageType imageType)
  110. {
  111. mRead = true;
  112. mHeight = height;
  113. mWidth = width;
  114. mRowPointers = (png_bytep*) dMalloc(sizeof(png_bytep) * mHeight);
  115. unsigned int y = 0;
  116. for (y = 0; y < height; y++)
  117. mRowPointers[y] = (png_byte*) dMalloc(sizeof(png_bytep) * mWidth);
  118. if(imageType == PNGTYPE_RGBA)
  119. {
  120. mColorType = PNG_COLOR_TYPE_RGBA;
  121. mPNGImageType = PNGTYPE_RGBA;
  122. }
  123. else if(imageType == PNGTYPE_RGB)
  124. {
  125. mColorType = PNG_COLOR_TYPE_RGB;
  126. mPNGImageType = PNGTYPE_RGB;
  127. }
  128. else
  129. {
  130. mPNGImageType = PNGTYPE_UNKNOWN;
  131. }
  132. mBitDepth = 8;
  133. ClearImageData();
  134. return true;
  135. }
  136. bool PNGImage::ClearImageData()
  137. {
  138. if(mRowPointers == NULL)
  139. return false;
  140. if(mPNGImageType == PNGTYPE_RGBA)
  141. {
  142. for(U32 h = 0; h < mHeight; ++h)
  143. {
  144. png_byte* currentRow = mRowPointers[h];
  145. for(U32 w = 0; w < mWidth; ++w)
  146. {
  147. png_byte* currentPngByte = &(currentRow[w*4]);
  148. currentPngByte[0] = 255;
  149. currentPngByte[1] = 255;
  150. currentPngByte[2] = 255;
  151. currentPngByte[3] = 255;
  152. }
  153. }
  154. }
  155. else if(mPNGImageType == PNGTYPE_RGB)
  156. {
  157. for(U32 h = 0; h < mHeight; ++h)
  158. {
  159. png_byte* currentRow = mRowPointers[h];
  160. for(U32 w = 0; w < mWidth; ++w)
  161. {
  162. png_byte* currentPngByte = &(currentRow[w*3]);
  163. currentPngByte[0] = 255;
  164. currentPngByte[1] = 255;
  165. currentPngByte[2] = 255;
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. bool PNGImage::MergeOn(U32 x, U32 y, const PNGImage* inc)
  172. {
  173. if(x + inc->GetWidth() > mWidth || y + inc->GetHeight() > mHeight)
  174. return false;
  175. if(mPNGImageType == PNGTYPE_RGBA && inc->GetPNGImageType() == PNGTYPE_RGBA)
  176. {
  177. for(U32 h = 0; h < inc->GetHeight(); ++h)
  178. {
  179. png_byte* incRow = inc->GetRowPointers()[h];
  180. png_byte* currentRow = mRowPointers[y + h];
  181. for(U32 w = 0; w < inc->GetWidth(); ++w)
  182. {
  183. png_byte* incPngByte = &(incRow[w*4]);
  184. png_byte* currentPngByte = &(currentRow[(x+w)*4]);
  185. //float incAlpha = (incPngByte[3]/255.0f);
  186. //float currentAlpha = (currentPngByte[3]/255.0f);
  187. //float red = ((float)incPngByte[0]/255.0f * incAlpha) + ((float)currentPngByte[0]/255.0f * (currentAlpha - incAlpha));
  188. //float green = ((float)incPngByte[1]/255.0f * incAlpha) + ((float)currentPngByte[1]/255.0f * (currentAlpha - incAlpha));
  189. //float blue = ((float)incPngByte[2]/255.0f * incAlpha) + ((float)currentPngByte[2]/255.0f * (currentAlpha - incAlpha));
  190. ////float alpha = ((float)incPngByte[3]/255.0f * incAlpha) + ((float)currentPngByte[3]/255.0f * (currentAlpha - incAlpha));
  191. //currentPngByte[0] = (png_byte)min(red * 255.0f, 255.0f);
  192. //currentPngByte[1] = (png_byte)min(green * 255.0f, 255.0f);
  193. //currentPngByte[2] = (png_byte)min(blue * 255.0f, 255.0f);
  194. ////currentPngByte[3] = (png_byte)min(blue * 255.0f, 255.0f);
  195. currentPngByte[0] = incPngByte[0];
  196. currentPngByte[1] = incPngByte[1];
  197. currentPngByte[2] = incPngByte[2];
  198. }
  199. }
  200. }
  201. else if(mPNGImageType == PNGTYPE_RGB && inc->GetPNGImageType() == PNGTYPE_RGBA)
  202. {
  203. for(U32 h = 0; h < inc->GetHeight(); ++h)
  204. {
  205. png_byte* incRow = inc->GetRowPointers()[h];
  206. png_byte* currentRow = mRowPointers[y + h];
  207. for(U32 w = 0; w < inc->GetWidth(); ++w)
  208. {
  209. png_byte* incPngByte = &(incRow[w*4]);
  210. png_byte* currentPngByte = &(currentRow[(x+w)*3]);
  211. float incAlpha = (incPngByte[3]/255.0f);
  212. float red = ((float)incPngByte[0]/255.0f * incAlpha) + ((float)currentPngByte[0]/255.0f * (1.0f - incAlpha));
  213. float green = ((float)incPngByte[1]/255.0f * incAlpha) + ((float)currentPngByte[1]/255.0f * (1.0f - incAlpha));
  214. float blue = ((float)incPngByte[2]/255.0f * incAlpha) + ((float)currentPngByte[2]/255.0f * (1.0f - incAlpha));
  215. currentPngByte[0] = (png_byte)min(red * 255.0f, 255.0f);
  216. currentPngByte[1] = (png_byte)min(green * 255.0f, 255.0f);
  217. currentPngByte[2] = (png_byte)min(blue * 255.0f, 255.0f);
  218. }
  219. }
  220. }
  221. else if(mPNGImageType == PNGTYPE_RGBA && inc->GetPNGImageType() == PNGTYPE_RGB)
  222. {
  223. for(U32 h = 0; h < inc->GetHeight(); ++h)
  224. {
  225. png_byte* incRow = inc->GetRowPointers()[h];
  226. png_byte* currentRow = mRowPointers[y + h];
  227. for(U32 w = 0; w < inc->GetWidth(); ++w)
  228. {
  229. png_byte* incPngByte = &(incRow[w*3]);
  230. png_byte* currentPngByte = &(currentRow[(x+w)*4]);
  231. currentPngByte[0] = incPngByte[0];
  232. currentPngByte[1] = incPngByte[1];
  233. currentPngByte[2] = incPngByte[2];
  234. }
  235. }
  236. }
  237. else if(mPNGImageType == PNGTYPE_RGB && inc->GetPNGImageType() == PNGTYPE_RGB)
  238. {
  239. for(U32 h = 0; h < inc->GetHeight(); ++h)
  240. {
  241. png_byte* incRow = inc->GetRowPointers()[h];
  242. png_byte* currentRow = mRowPointers[y + h];
  243. for(U32 w = 0; w < inc->GetWidth(); ++w)
  244. {
  245. png_byte* incPngByte = &(incRow[w*3]);
  246. png_byte* currentPngByte = &(currentRow[(x+w)*3]);
  247. currentPngByte[0] = incPngByte[0];
  248. currentPngByte[1] = incPngByte[1];
  249. currentPngByte[2] = incPngByte[2];
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. bool PNGImage::Write(const char* filePath)
  256. {
  257. if(mRead == false)
  258. return false;
  259. if(mPng != NULL && mInfo != NULL)
  260. {
  261. png_destroy_read_struct(&mPng, &mInfo, NULL);
  262. mPng = NULL;
  263. mInfo = NULL;
  264. }
  265. else
  266. {
  267. if(mPng != NULL)
  268. {
  269. png_destroy_read_struct(&mPng, NULL, NULL);
  270. mPng = NULL;
  271. }
  272. else if(mInfo != NULL)
  273. {
  274. png_read_info(mPng, mInfo);
  275. mInfo = NULL;
  276. }
  277. }
  278. /* create file */
  279. FILE *fp = fopen(filePath, "wb");
  280. if (!fp)
  281. {
  282. Con::printf("PNGImage::Write File %s could not be opened for writing.", filePath);
  283. return false;
  284. }
  285. // png_destroy_read_struct(&mPng, NULL, NULL);
  286. // png_destroy_info_struct(mPng, NULL);
  287. /* initialize stuff */
  288. mPng = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  289. if (!mPng)
  290. {
  291. Con::printf("PNGImage::Write png_create_write_struct failed.");
  292. return false;
  293. }
  294. mInfo = png_create_info_struct(mPng);
  295. if (mInfo == NULL)
  296. {
  297. Con::printf("PNGImage::Write png_create_info_struct failed.");
  298. return false;
  299. }
  300. if (setjmp(png_jmpbuf(mPng)))
  301. {
  302. Con::printf("PNGImage::Write Error during init_io.");
  303. return false;
  304. }
  305. png_init_io(mPng, fp);
  306. /* write header */
  307. if (setjmp(png_jmpbuf(mPng)))
  308. {
  309. Con::printf("PNGImage::Write Error during writing header.");
  310. return false;
  311. }
  312. png_set_IHDR(mPng, mInfo, mWidth, mHeight,
  313. mBitDepth, mColorType, PNG_INTERLACE_NONE,
  314. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  315. png_write_info(mPng, mInfo);
  316. /* write bytes */
  317. if (setjmp(png_jmpbuf(mPng)))
  318. {
  319. Con::printf("PNGImage::Write Error during writing bytes.");
  320. return false;
  321. }
  322. png_write_image(mPng, mRowPointers);
  323. /* end write */
  324. if (setjmp(png_jmpbuf(mPng)))
  325. {
  326. Con::printf("PNGImage::Write Error during end of write.");
  327. return false;
  328. }
  329. png_write_end(mPng, NULL);
  330. fclose(fp);
  331. mRead = false;
  332. mWrite = true;
  333. return true;
  334. }
  335. bool PNGImage::CleanMemoryUsage()
  336. {
  337. if(mRead == true && mWrite == true)
  338. return false;
  339. if(mRead == true)
  340. {
  341. if(mPng != NULL && mInfo != NULL)
  342. {
  343. png_destroy_read_struct(&mPng, &mInfo, NULL);
  344. mPng = NULL;
  345. mInfo = NULL;
  346. }
  347. else
  348. {
  349. if(mPng != NULL)
  350. {
  351. png_destroy_read_struct(&mPng, NULL, NULL);
  352. mPng = NULL;
  353. }
  354. else if(mInfo != NULL)
  355. {
  356. png_read_info(mPng, mInfo);
  357. mInfo = NULL;
  358. }
  359. }
  360. for (unsigned int y = 0; y < mHeight; y++)
  361. dFree(mRowPointers[y]);
  362. dFree(mRowPointers);
  363. mRowPointers= NULL;
  364. }
  365. else if(mWrite == true)
  366. {
  367. if(mPng != NULL && mInfo != NULL)
  368. {
  369. png_destroy_write_struct(&mPng, &mInfo);
  370. mPng = NULL;
  371. mInfo = NULL;
  372. }
  373. else
  374. {
  375. if(mPng != NULL)
  376. {
  377. png_destroy_write_struct(&mPng, NULL);
  378. mPng = NULL;
  379. }
  380. if(mInfo != NULL)
  381. {
  382. png_read_info(mPng, mInfo);
  383. mInfo = NULL;
  384. }
  385. }
  386. for (unsigned int y = 0; y < mHeight; y++)
  387. dFree(mRowPointers[y]);
  388. dFree(mRowPointers);
  389. mRowPointers = NULL;
  390. }
  391. return true;
  392. }