PNGImage.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. #define min(a,b) (a <= b ? a : b)
  28. IMPLEMENT_CONOBJECT(PNGImage);
  29. #define PNGSIGSIZE 8
  30. PNGImage::PNGImage() : mPNGImageType(PNGTYPE_UNKNOWN)
  31. {
  32. mWidth = 0;
  33. mHeight = 0;
  34. mColorType = 0;
  35. mBitDepth = 0;
  36. mPng = 0;
  37. mInfo = 0;
  38. mRowPointers = 0;
  39. mRead = false;
  40. mWrite = false;
  41. }
  42. PNGImage::~PNGImage()
  43. {
  44. }
  45. bool PNGImage::Read(const char* filePath)
  46. {
  47. if(mRead == true || mWrite == true)
  48. CleanMemoryUsage();
  49. /* open file and test for it being a png */
  50. FILE *fp = fopen(filePath, "rb");
  51. if (fp == NULL)
  52. {
  53. Con::printf("PNGImage::Read File %s could not be opened for reading.", filePath);
  54. return false;
  55. }
  56. dStrcpy(mReadFilePath, filePath);
  57. png_byte pngsig[PNGSIGSIZE];
  58. fread((char*)pngsig, 1, PNGSIGSIZE, fp);
  59. if (png_sig_cmp(pngsig, 0, PNGSIGSIZE) != 0)
  60. {
  61. Con::printf("PNGImage::Read File %s is not recognized as a PNG file.", filePath);
  62. return false;
  63. }
  64. /* initialize stuff */
  65. mPng = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  66. if (mPng == NULL)
  67. {
  68. Con::printf("PNGImage::Read png_create_read_struct failed.");
  69. return false;
  70. }
  71. mInfo = png_create_info_struct(mPng);
  72. if (mInfo == NULL)
  73. {
  74. png_destroy_read_struct(&mPng, NULL, NULL);
  75. Con::printf("PNGImage::Read png_create_info_struct failed.");
  76. return false;
  77. }
  78. if (setjmp(png_jmpbuf(mPng)))
  79. {
  80. png_destroy_read_struct(&mPng, NULL, NULL);
  81. Con::printf("PNGImage::Read Error during init_io.");
  82. return false;
  83. }
  84. png_init_io(mPng, fp);
  85. png_set_sig_bytes(mPng, 8);
  86. png_read_info(mPng, mInfo);
  87. // Obtain image information
  88. mWidth = png_get_image_width(mPng, mInfo);
  89. mHeight = png_get_image_height(mPng, mInfo);
  90. mColorType = png_get_color_type(mPng, mInfo);
  91. mBitDepth = png_get_bit_depth(mPng, mInfo);
  92. mRowPointers = (png_bytep*) dMalloc(sizeof(png_bytep) * mHeight);
  93. unsigned int y = 0;
  94. for (y = 0; y < mHeight; y++)
  95. mRowPointers[y] = (png_byte*) dMalloc(png_get_rowbytes(mPng,mInfo));
  96. // Will read the actual png data into mRowPointers
  97. png_read_image(mPng, mRowPointers);
  98. fclose(fp);
  99. mRead = true;
  100. if(png_get_color_type(mPng, mInfo) == PNG_COLOR_TYPE_RGBA)
  101. mPNGImageType = PNGTYPE_RGBA;
  102. else if(png_get_color_type(mPng, mInfo) == PNG_COLOR_TYPE_RGB)
  103. mPNGImageType = PNGTYPE_RGB;
  104. else
  105. mPNGImageType = PNGTYPE_UNKNOWN;
  106. return true;
  107. }
  108. bool PNGImage::Create(U32 width, U32 height, PNGImageType imageType)
  109. {
  110. mRead = true;
  111. mHeight = height;
  112. mWidth = width;
  113. mRowPointers = (png_bytep*) dMalloc(sizeof(png_bytep) * mHeight);
  114. unsigned int y = 0;
  115. for (y = 0; y < height; y++)
  116. mRowPointers[y] = (png_byte*) dMalloc(sizeof(png_bytep) * mWidth);
  117. if(imageType == PNGTYPE_RGBA)
  118. {
  119. mColorType = PNG_COLOR_TYPE_RGBA;
  120. mPNGImageType = PNGTYPE_RGBA;
  121. }
  122. else if(imageType == PNGTYPE_RGB)
  123. {
  124. mColorType = PNG_COLOR_TYPE_RGB;
  125. mPNGImageType = PNGTYPE_RGB;
  126. }
  127. else
  128. {
  129. mPNGImageType = PNGTYPE_UNKNOWN;
  130. }
  131. mBitDepth = 8;
  132. ClearImageData();
  133. return true;
  134. }
  135. bool PNGImage::ClearImageData()
  136. {
  137. if(mRowPointers == NULL)
  138. return false;
  139. if(mPNGImageType == PNGTYPE_RGBA)
  140. {
  141. for(U32 h = 0; h < mHeight; ++h)
  142. {
  143. png_byte* currentRow = mRowPointers[h];
  144. for(U32 w = 0; w < mWidth; ++w)
  145. {
  146. png_byte* currentPngByte = &(currentRow[w*4]);
  147. currentPngByte[0] = 255;
  148. currentPngByte[1] = 255;
  149. currentPngByte[2] = 255;
  150. currentPngByte[3] = 255;
  151. }
  152. }
  153. }
  154. else if(mPNGImageType == PNGTYPE_RGB)
  155. {
  156. for(U32 h = 0; h < mHeight; ++h)
  157. {
  158. png_byte* currentRow = mRowPointers[h];
  159. for(U32 w = 0; w < mWidth; ++w)
  160. {
  161. png_byte* currentPngByte = &(currentRow[w*3]);
  162. currentPngByte[0] = 255;
  163. currentPngByte[1] = 255;
  164. currentPngByte[2] = 255;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. bool PNGImage::MergeOn(U32 x, U32 y, const PNGImage* inc)
  171. {
  172. if(x + inc->GetWidth() > mWidth || y + inc->GetHeight() > mHeight)
  173. return false;
  174. if(mPNGImageType == PNGTYPE_RGBA && inc->GetPNGImageType() == PNGTYPE_RGBA)
  175. {
  176. for(U32 h = 0; h < inc->GetHeight(); ++h)
  177. {
  178. png_byte* incRow = inc->GetRowPointers()[h];
  179. png_byte* currentRow = mRowPointers[y + h];
  180. for(U32 w = 0; w < inc->GetWidth(); ++w)
  181. {
  182. png_byte* incPngByte = &(incRow[w*4]);
  183. png_byte* currentPngByte = &(currentRow[(x+w)*4]);
  184. //float incAlpha = (incPngByte[3]/255.0f);
  185. //float currentAlpha = (currentPngByte[3]/255.0f);
  186. //float red = ((float)incPngByte[0]/255.0f * incAlpha) + ((float)currentPngByte[0]/255.0f * (currentAlpha - incAlpha));
  187. //float green = ((float)incPngByte[1]/255.0f * incAlpha) + ((float)currentPngByte[1]/255.0f * (currentAlpha - incAlpha));
  188. //float blue = ((float)incPngByte[2]/255.0f * incAlpha) + ((float)currentPngByte[2]/255.0f * (currentAlpha - incAlpha));
  189. ////float alpha = ((float)incPngByte[3]/255.0f * incAlpha) + ((float)currentPngByte[3]/255.0f * (currentAlpha - incAlpha));
  190. //currentPngByte[0] = (png_byte)min(red * 255.0f, 255.0f);
  191. //currentPngByte[1] = (png_byte)min(green * 255.0f, 255.0f);
  192. //currentPngByte[2] = (png_byte)min(blue * 255.0f, 255.0f);
  193. ////currentPngByte[3] = (png_byte)min(blue * 255.0f, 255.0f);
  194. currentPngByte[0] = incPngByte[0];
  195. currentPngByte[1] = incPngByte[1];
  196. currentPngByte[2] = incPngByte[2];
  197. }
  198. }
  199. }
  200. else if(mPNGImageType == PNGTYPE_RGB && inc->GetPNGImageType() == PNGTYPE_RGBA)
  201. {
  202. for(U32 h = 0; h < inc->GetHeight(); ++h)
  203. {
  204. png_byte* incRow = inc->GetRowPointers()[h];
  205. png_byte* currentRow = mRowPointers[y + h];
  206. for(U32 w = 0; w < inc->GetWidth(); ++w)
  207. {
  208. png_byte* incPngByte = &(incRow[w*4]);
  209. png_byte* currentPngByte = &(currentRow[(x+w)*3]);
  210. float incAlpha = (incPngByte[3]/255.0f);
  211. float red = ((float)incPngByte[0]/255.0f * incAlpha) + ((float)currentPngByte[0]/255.0f * (1.0f - incAlpha));
  212. float green = ((float)incPngByte[1]/255.0f * incAlpha) + ((float)currentPngByte[1]/255.0f * (1.0f - incAlpha));
  213. float blue = ((float)incPngByte[2]/255.0f * incAlpha) + ((float)currentPngByte[2]/255.0f * (1.0f - incAlpha));
  214. currentPngByte[0] = (png_byte)min(red * 255.0f, 255.0f);
  215. currentPngByte[1] = (png_byte)min(green * 255.0f, 255.0f);
  216. currentPngByte[2] = (png_byte)min(blue * 255.0f, 255.0f);
  217. }
  218. }
  219. }
  220. else if(mPNGImageType == PNGTYPE_RGBA && inc->GetPNGImageType() == PNGTYPE_RGB)
  221. {
  222. for(U32 h = 0; h < inc->GetHeight(); ++h)
  223. {
  224. png_byte* incRow = inc->GetRowPointers()[h];
  225. png_byte* currentRow = mRowPointers[y + h];
  226. for(U32 w = 0; w < inc->GetWidth(); ++w)
  227. {
  228. png_byte* incPngByte = &(incRow[w*3]);
  229. png_byte* currentPngByte = &(currentRow[(x+w)*4]);
  230. currentPngByte[0] = incPngByte[0];
  231. currentPngByte[1] = incPngByte[1];
  232. currentPngByte[2] = incPngByte[2];
  233. }
  234. }
  235. }
  236. else if(mPNGImageType == PNGTYPE_RGB && inc->GetPNGImageType() == PNGTYPE_RGB)
  237. {
  238. for(U32 h = 0; h < inc->GetHeight(); ++h)
  239. {
  240. png_byte* incRow = inc->GetRowPointers()[h];
  241. png_byte* currentRow = mRowPointers[y + h];
  242. for(U32 w = 0; w < inc->GetWidth(); ++w)
  243. {
  244. png_byte* incPngByte = &(incRow[w*3]);
  245. png_byte* currentPngByte = &(currentRow[(x+w)*3]);
  246. currentPngByte[0] = incPngByte[0];
  247. currentPngByte[1] = incPngByte[1];
  248. currentPngByte[2] = incPngByte[2];
  249. }
  250. }
  251. }
  252. return true;
  253. }
  254. bool PNGImage::Write(const char* filePath)
  255. {
  256. if(mRead == false)
  257. return false;
  258. if(mPng != NULL && mInfo != NULL)
  259. {
  260. png_destroy_read_struct(&mPng, &mInfo, NULL);
  261. mPng = NULL;
  262. mInfo = NULL;
  263. }
  264. else
  265. {
  266. if(mPng != NULL)
  267. {
  268. png_destroy_read_struct(&mPng, NULL, NULL);
  269. mPng = NULL;
  270. }
  271. else if(mInfo != NULL)
  272. {
  273. png_read_info(mPng, mInfo);
  274. mInfo = NULL;
  275. }
  276. }
  277. /* create file */
  278. FILE *fp = fopen(filePath, "wb");
  279. if (!fp)
  280. {
  281. Con::printf("PNGImage::Write File %s could not be opened for writing.", filePath);
  282. return false;
  283. }
  284. // png_destroy_read_struct(&mPng, NULL, NULL);
  285. // png_destroy_info_struct(mPng, NULL);
  286. /* initialize stuff */
  287. mPng = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  288. if (!mPng)
  289. {
  290. Con::printf("PNGImage::Write png_create_write_struct failed.");
  291. return false;
  292. }
  293. mInfo = png_create_info_struct(mPng);
  294. if (mInfo == NULL)
  295. {
  296. Con::printf("PNGImage::Write png_create_info_struct failed.");
  297. return false;
  298. }
  299. if (setjmp(png_jmpbuf(mPng)))
  300. {
  301. Con::printf("PNGImage::Write Error during init_io.");
  302. return false;
  303. }
  304. png_init_io(mPng, fp);
  305. /* write header */
  306. if (setjmp(png_jmpbuf(mPng)))
  307. {
  308. Con::printf("PNGImage::Write Error during writing header.");
  309. return false;
  310. }
  311. png_set_IHDR(mPng, mInfo, mWidth, mHeight,
  312. mBitDepth, mColorType, PNG_INTERLACE_NONE,
  313. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  314. png_write_info(mPng, mInfo);
  315. /* write bytes */
  316. if (setjmp(png_jmpbuf(mPng)))
  317. {
  318. Con::printf("PNGImage::Write Error during writing bytes.");
  319. return false;
  320. }
  321. png_write_image(mPng, mRowPointers);
  322. /* end write */
  323. if (setjmp(png_jmpbuf(mPng)))
  324. {
  325. Con::printf("PNGImage::Write Error during end of write.");
  326. return false;
  327. }
  328. png_write_end(mPng, NULL);
  329. fclose(fp);
  330. mRead = false;
  331. mWrite = true;
  332. return true;
  333. }
  334. bool PNGImage::CleanMemoryUsage()
  335. {
  336. if(mRead == true && mWrite == true)
  337. return false;
  338. if(mRead == true)
  339. {
  340. if(mPng != NULL && mInfo != NULL)
  341. {
  342. png_destroy_read_struct(&mPng, &mInfo, NULL);
  343. mPng = NULL;
  344. mInfo = NULL;
  345. }
  346. else
  347. {
  348. if(mPng != NULL)
  349. {
  350. png_destroy_read_struct(&mPng, NULL, NULL);
  351. mPng = NULL;
  352. }
  353. else if(mInfo != NULL)
  354. {
  355. png_read_info(mPng, mInfo);
  356. mInfo = NULL;
  357. }
  358. }
  359. for (unsigned int y = 0; y < mHeight; y++)
  360. dFree(mRowPointers[y]);
  361. dFree(mRowPointers);
  362. mRowPointers= NULL;
  363. }
  364. else if(mWrite == true)
  365. {
  366. if(mPng != NULL && mInfo != NULL)
  367. {
  368. png_destroy_write_struct(&mPng, &mInfo);
  369. mPng = NULL;
  370. mInfo = NULL;
  371. }
  372. else
  373. {
  374. if(mPng != NULL)
  375. {
  376. png_destroy_write_struct(&mPng, NULL);
  377. mPng = NULL;
  378. }
  379. if(mInfo != NULL)
  380. {
  381. png_read_info(mPng, mInfo);
  382. mInfo = NULL;
  383. }
  384. }
  385. for (unsigned int y = 0; y < mHeight; y++)
  386. dFree(mRowPointers[y]);
  387. dFree(mRowPointers);
  388. mRowPointers = NULL;
  389. }
  390. return true;
  391. }
  392. ConsoleFunction(CaptureScreenArea, bool, 7, 7, "(posX, posY, width, height, fileName, fileType) Capture a specific area of the screen")
  393. {
  394. GLint positionX = dAtoi(argv[1]);
  395. GLint positionY = dAtoi(argv[2]);
  396. U32 width = dAtoi(argv[3]);
  397. U32 height = dAtoi(argv[4]);
  398. FileStream fStream;
  399. if(!fStream.open(argv[5], FileStream::Write))
  400. {
  401. Con::printf("Failed to open file '%s'.", argv[5]);
  402. return false;
  403. }
  404. // Read gl pixels here
  405. glReadBuffer(GL_FRONT);
  406. Point2I extent;
  407. extent.x = width;
  408. extent.y = height;
  409. U8 * pixels = new U8[extent.x * extent.y * 4];
  410. glReadPixels(positionX, positionY, extent.x, extent.y, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  411. GBitmap * bitmap = new GBitmap;
  412. bitmap->allocateBitmap(U32(extent.x), U32(extent.y));
  413. // flip the rows
  414. for(U32 y = 0; y < (U32)extent.y; y++)
  415. dMemcpy(bitmap->getAddress(0, extent.y - y - 1), pixels + y * extent.x * 3, U32(extent.x * 3));
  416. if ( dStrcmp( argv[6], "JPEG" ) == 0 )
  417. bitmap->writeJPEG(fStream);
  418. else if( dStrcmp( argv[6], "PNG" ) == 0)
  419. bitmap->writePNG(fStream);
  420. else
  421. bitmap->writePNG(fStream);
  422. fStream.close();
  423. delete [] pixels;
  424. delete bitmap;
  425. return true;
  426. }
  427. ConsoleMethod(PNGImage, CreateBaseImage, bool, 5, 5, "(width, height, imageType) Create the base image to merge onto ")
  428. {
  429. U32 width = dAtoi(argv[2]);
  430. U32 height = dAtoi(argv[3]);
  431. return object->Create(width, height, (PNGImageType)dAtoi(argv[4]));
  432. }
  433. ConsoleMethod(PNGImage, MergeOn, bool, 5, 5, "(x, y, imageFile) Add an image to the spritesheet")
  434. {
  435. U32 width = dAtoi(argv[2]);
  436. U32 height = dAtoi(argv[3]);
  437. // File name is argv[4]
  438. FileStream fStream;
  439. if(!fStream.open(argv[4], FileStream::Read))
  440. {
  441. Con::printf("Failed to open file '%s'.", argv[4]);
  442. return false;
  443. }
  444. PNGImage* newImage = new PNGImage();
  445. bool didReadImage = newImage->Read(argv[4]);
  446. if(!didReadImage)
  447. {
  448. newImage->CleanMemoryUsage();
  449. delete newImage;
  450. return false;
  451. }
  452. fStream.close();
  453. bool didMergeOn = object->MergeOn(width, height, newImage);
  454. newImage->CleanMemoryUsage();
  455. delete newImage;
  456. return didMergeOn;
  457. }
  458. ConsoleMethod(PNGImage, SaveImage, bool, 3, 3, "(fileName) Save the new spritesheet to a file")
  459. {
  460. FileStream fStream;
  461. if(!fStream.open(argv[2], FileStream::Write))
  462. {
  463. Con::printf("Failed to open file '%s'.", argv[2]);
  464. return false;
  465. }
  466. fStream.close();
  467. object->Write(argv[2]);
  468. object->CleanMemoryUsage();
  469. return true;
  470. }