PageRenderTime 111ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llimage/llimagepng.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 136 lines | 68 code | 23 blank | 45 comment | 9 complexity | 9f38e6c41182eadf7b62e6ce533c5930 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * @file llimagepng.cpp
  3. * @brief LLImageFormatted glue to encode / decode PNG files.
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "stdtypes.h"
  28. #include "llerror.h"
  29. #include "llimage.h"
  30. #include "llpngwrapper.h"
  31. #include "llimagepng.h"
  32. // ---------------------------------------------------------------------------
  33. // LLImagePNG
  34. // ---------------------------------------------------------------------------
  35. LLImagePNG::LLImagePNG()
  36. : LLImageFormatted(IMG_CODEC_PNG)
  37. {
  38. }
  39. LLImagePNG::~LLImagePNG()
  40. {
  41. }
  42. // Virtual
  43. // Parse PNG image information and set the appropriate
  44. // width, height and component (channel) information.
  45. BOOL LLImagePNG::updateData()
  46. {
  47. resetLastError();
  48. // Check to make sure that this instance has been initialized with data
  49. if (!getData() || (0 == getDataSize()))
  50. {
  51. setLastError("Uninitialized instance of LLImagePNG");
  52. return FALSE;
  53. }
  54. // Decode the PNG data and extract sizing information
  55. LLPngWrapper pngWrapper;
  56. LLPngWrapper::ImageInfo infop;
  57. if (! pngWrapper.readPng(getData(), NULL, &infop))
  58. {
  59. setLastError(pngWrapper.getErrorMessage());
  60. return FALSE;
  61. }
  62. setSize(infop.mWidth, infop.mHeight, infop.mComponents);
  63. return TRUE;
  64. }
  65. // Virtual
  66. // Decode an in-memory PNG image into the raw RGB or RGBA format
  67. // used within SecondLife.
  68. BOOL LLImagePNG::decode(LLImageRaw* raw_image, F32 decode_time)
  69. {
  70. llassert_always(raw_image);
  71. resetLastError();
  72. // Check to make sure that this instance has been initialized with data
  73. if (!getData() || (0 == getDataSize()))
  74. {
  75. setLastError("LLImagePNG trying to decode an image with no data!");
  76. return FALSE;
  77. }
  78. // Decode the PNG data into the raw image
  79. LLPngWrapper pngWrapper;
  80. if (! pngWrapper.readPng(getData(), raw_image))
  81. {
  82. setLastError(pngWrapper.getErrorMessage());
  83. return FALSE;
  84. }
  85. return TRUE;
  86. }
  87. // Virtual
  88. // Encode the in memory RGB image into PNG format.
  89. BOOL LLImagePNG::encode(const LLImageRaw* raw_image, F32 encode_time)
  90. {
  91. llassert_always(raw_image);
  92. resetLastError();
  93. // Image logical size
  94. setSize(raw_image->getWidth(), raw_image->getHeight(), raw_image->getComponents());
  95. // Temporary buffer to hold the encoded image. Note: the final image
  96. // size should be much smaller due to compression.
  97. U32 bufferSize = getWidth() * getHeight() * getComponents() + 1024;
  98. U8* tmpWriteBuffer = new U8[ bufferSize ];
  99. // Delegate actual encoding work to wrapper
  100. LLPngWrapper pngWrapper;
  101. if (! pngWrapper.writePng(raw_image, tmpWriteBuffer))
  102. {
  103. setLastError(pngWrapper.getErrorMessage());
  104. delete[] tmpWriteBuffer;
  105. return FALSE;
  106. }
  107. // Resize internal buffer and copy from temp
  108. U32 encodedSize = pngWrapper.getFinalSize();
  109. allocateData(encodedSize);
  110. memcpy(getData(), tmpWriteBuffer, encodedSize);
  111. delete[] tmpWriteBuffer;
  112. return TRUE;
  113. }