llimagepng.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file llimagepng.cpp
  3. * @brief LLImageFormatted glue to encode / decode PNG files.
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llpngwrapper.h"
  34. #include "llimagepng.h"
  35. LLImagePNG::LLImagePNG()
  36. : LLImageFormatted(IMG_CODEC_PNG),
  37. mTmpWriteBuffer(NULL)
  38. {
  39. }
  40. LLImagePNG::~LLImagePNG()
  41. {
  42. if (mTmpWriteBuffer)
  43. {
  44. delete[] mTmpWriteBuffer;
  45. }
  46. }
  47. // Parses PNG image information and set the appropriate width, height and
  48. // components (channels) information.
  49. //virtual
  50. bool LLImagePNG::updateData()
  51. {
  52. resetLastError();
  53. // Check to make sure that this instance has been initialized with data
  54. if (!getData() || getDataSize() == 0)
  55. {
  56. setLastError("Uninitialized instance of LLImagePNG");
  57. return false;
  58. }
  59. // Decode the PNG data and extract sizing information
  60. LLPngWrapper pngWrapper;
  61. if (!pngWrapper.isValidPng(getData()))
  62. {
  63. setLastError("LLImagePNG data does not have a valid PNG header!");
  64. return false;
  65. }
  66. LLPngWrapper::ImageInfo infop;
  67. if (!pngWrapper.readPng(getData(), getDataSize(), NULL, &infop))
  68. {
  69. setLastError(pngWrapper.getErrorMessage());
  70. return false;
  71. }
  72. setSize(infop.mWidth, infop.mHeight, infop.mComponents);
  73. return true;
  74. }
  75. // Decodes an in-memory PNG image into the raw RGB or RGBA format used within
  76. // SecondLife.
  77. //virtual
  78. bool LLImagePNG::decode(LLImageRaw* raw_image)
  79. {
  80. resetLastError();
  81. if (!raw_image)
  82. {
  83. llwarns << "Attempted to decode a NULL raw image buffer address"
  84. << llendl;
  85. llassert(false);
  86. return false;
  87. }
  88. // Check to make sure that this instance has been initialized with data
  89. if (!getData() || getDataSize() == 0)
  90. {
  91. setLastError("LLImagePNG trying to decode an image with no data !");
  92. return false;
  93. }
  94. // Decode the PNG data into the raw image
  95. LLPngWrapper pngWrapper;
  96. if (!pngWrapper.isValidPng(getData()))
  97. {
  98. setLastError("LLImagePNG data does not have a valid PNG header !");
  99. return false;
  100. }
  101. if (!pngWrapper.readPng(getData(), getDataSize(), raw_image))
  102. {
  103. setLastError(pngWrapper.getErrorMessage());
  104. return false;
  105. }
  106. return true;
  107. }
  108. // Encodes the in memory RGB image into PNG format.
  109. //virtual
  110. bool LLImagePNG::encode(const LLImageRaw* raw_image)
  111. {
  112. llassert_always(raw_image);
  113. resetLastError();
  114. // Image logical size
  115. setSize(raw_image->getWidth(), raw_image->getHeight(),
  116. raw_image->getComponents());
  117. // Temporary buffer to hold the encoded image. Note: the final image
  118. // size should be much smaller due to compression.
  119. if (mTmpWriteBuffer)
  120. {
  121. delete[] mTmpWriteBuffer;
  122. }
  123. U32 bufferSize = getWidth() * getHeight() * getComponents() + 1024;
  124. U8* mTmpWriteBuffer = new (std::nothrow) U8[bufferSize];
  125. if (!mTmpWriteBuffer)
  126. {
  127. LLMemory::allocationFailed(bufferSize);
  128. setLastError("Unable to encode a PNG image: out of memory.");
  129. return false;
  130. }
  131. // Delegate actual encoding work to wrapper
  132. LLPngWrapper pngWrapper;
  133. if (!pngWrapper.writePng(raw_image, mTmpWriteBuffer))
  134. {
  135. setLastError(pngWrapper.getErrorMessage());
  136. return false;
  137. }
  138. // Resize internal buffer and copy from temp
  139. bool res = false;
  140. U32 encodedSize = pngWrapper.getFinalSize();
  141. if (allocateData(encodedSize))
  142. {
  143. memcpy(getData(), mTmpWriteBuffer, encodedSize);
  144. res = true;
  145. }
  146. delete[] mTmpWriteBuffer;
  147. return res;
  148. }