/image/decoders/nsPNGDecoder.h

http://github.com/zpao/v8monkey · C Header · 147 lines · 73 code · 24 blank · 50 comment · 4 complexity · 2cd5683da5714f44632d86034c9324fd MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is mozilla.org code.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 2001
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Stuart Parmenter <pavlov@netscape.com>
  25. * Bobby Holley <bobbyholley@gmail.com>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either the GNU General Public License Version 2 or later (the "GPL"), or
  29. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. #ifndef nsPNGDecoder_h__
  41. #define nsPNGDecoder_h__
  42. #include "Decoder.h"
  43. #include "imgIDecoderObserver.h"
  44. #include "gfxASurface.h"
  45. #include "nsCOMPtr.h"
  46. #include "png.h"
  47. #include "qcms.h"
  48. namespace mozilla {
  49. namespace image {
  50. class RasterImage;
  51. class nsPNGDecoder : public Decoder
  52. {
  53. public:
  54. nsPNGDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver);
  55. virtual ~nsPNGDecoder();
  56. virtual void InitInternal();
  57. virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
  58. virtual Telemetry::ID SpeedHistogram();
  59. void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
  60. PRInt32 width, PRInt32 height,
  61. gfxASurface::gfxImageFormat format);
  62. void SetAnimFrameInfo();
  63. void EndImageFrame();
  64. // Check if PNG is valid ICO (32bpp RGBA)
  65. // http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
  66. bool IsValidICO() const
  67. {
  68. png_uint_32
  69. png_width, // Unused
  70. png_height; // Unused
  71. int png_bit_depth,
  72. png_color_type;
  73. if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth,
  74. &png_color_type, NULL, NULL, NULL)) {
  75. return (png_color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  76. png_bit_depth == 8);
  77. } else {
  78. return false;
  79. }
  80. }
  81. public:
  82. png_structp mPNG;
  83. png_infop mInfo;
  84. nsIntRect mFrameRect;
  85. PRUint8 *mCMSLine;
  86. PRUint8 *interlacebuf;
  87. PRUint8 *mImageData;
  88. qcms_profile *mInProfile;
  89. qcms_transform *mTransform;
  90. gfxASurface::gfxImageFormat format;
  91. // For size decodes
  92. PRUint8 *mHeaderBuf;
  93. PRUint32 mHeaderBytesRead;
  94. PRUint8 mChannels;
  95. bool mFrameHasNoAlpha;
  96. bool mFrameIsHidden;
  97. // whether CMS or premultiplied alpha are forced off
  98. PRUint32 mCMSMode;
  99. bool mDisablePremultipliedAlpha;
  100. /*
  101. * libpng callbacks
  102. *
  103. * We put these in the class so that they can access protected members.
  104. */
  105. static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr);
  106. static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row,
  107. png_uint_32 row_num, int pass);
  108. #ifdef PNG_APNG_SUPPORTED
  109. static void PNGAPI frame_info_callback(png_structp png_ptr,
  110. png_uint_32 frame_num);
  111. #endif
  112. static void PNGAPI end_callback(png_structp png_ptr, png_infop info_ptr);
  113. static void PNGAPI error_callback(png_structp png_ptr,
  114. png_const_charp error_msg);
  115. static void PNGAPI warning_callback(png_structp png_ptr,
  116. png_const_charp warning_msg);
  117. // This is defined in the PNG spec as an invariant. We use it to
  118. // do manual validation without libpng.
  119. static const PRUint8 pngSignatureBytes[];
  120. };
  121. } // namespace image
  122. } // namespace mozilla
  123. #endif // nsPNGDecoder_h__