Bumping manifests a=b2g-bump
[gecko.git] / image / decoders / nsPNGDecoder.h
blobddcee6230d387c3c6d57ac6c3c04bd22b754c9c1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsPNGDecoder_h
8 #define nsPNGDecoder_h
10 #include "Decoder.h"
12 #include "gfxTypes.h"
14 #include "nsCOMPtr.h"
16 #include "png.h"
18 #include "qcms.h"
20 namespace mozilla {
21 namespace image {
22 class RasterImage;
24 class nsPNGDecoder : public Decoder
26 public:
27 explicit nsPNGDecoder(RasterImage* aImage);
28 virtual ~nsPNGDecoder();
30 virtual void InitInternal() MOZ_OVERRIDE;
31 virtual void WriteInternal(const char* aBuffer, uint32_t aCount) MOZ_OVERRIDE;
32 virtual Telemetry::ID SpeedHistogram() MOZ_OVERRIDE;
34 void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
35 int32_t width, int32_t height,
36 gfx::SurfaceFormat format);
37 void EndImageFrame();
39 // Check if PNG is valid ICO (32bpp RGBA)
40 // http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
41 bool IsValidICO() const
43 // If there are errors in the call to png_get_IHDR, the error_callback in
44 // nsPNGDecoder.cpp is called. In this error callback we do a longjmp, so
45 // we need to save the jump buffer here. Oterwise we'll end up without a
46 // proper callstack.
47 if (setjmp(png_jmpbuf(mPNG))) {
48 // We got here from a longjmp call indirectly from png_get_IHDR
49 return false;
52 png_uint_32
53 png_width, // Unused
54 png_height; // Unused
56 int png_bit_depth,
57 png_color_type;
59 if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth,
60 &png_color_type, nullptr, nullptr, nullptr)) {
62 return ((png_color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
63 png_color_type == PNG_COLOR_TYPE_RGB) &&
64 png_bit_depth == 8);
65 } else {
66 return false;
70 public:
71 png_structp mPNG;
72 png_infop mInfo;
73 nsIntRect mFrameRect;
74 uint8_t* mCMSLine;
75 uint8_t* interlacebuf;
76 qcms_profile* mInProfile;
77 qcms_transform* mTransform;
79 gfx::SurfaceFormat format;
81 // For size decodes
82 uint8_t mSizeBytes[8]; // Space for width and height, both 4 bytes
83 uint32_t mHeaderBytesRead;
85 // whether CMS or premultiplied alpha are forced off
86 uint32_t mCMSMode;
88 uint8_t mChannels;
89 bool mFrameHasNoAlpha;
90 bool mFrameIsHidden;
91 bool mDisablePremultipliedAlpha;
93 struct AnimFrameInfo
95 AnimFrameInfo();
96 #ifdef PNG_APNG_SUPPORTED
97 AnimFrameInfo(png_structp aPNG, png_infop aInfo);
98 #endif
100 DisposalMethod mDispose;
101 BlendMethod mBlend;
102 int32_t mTimeout;
105 AnimFrameInfo mAnimInfo;
107 // The number of frames we've finished.
108 uint32_t mNumFrames;
110 // libpng callbacks
111 // We put these in the class so that they can access protected members.
112 static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr);
113 static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row,
114 png_uint_32 row_num, int pass);
115 #ifdef PNG_APNG_SUPPORTED
116 static void PNGAPI frame_info_callback(png_structp png_ptr,
117 png_uint_32 frame_num);
118 #endif
119 static void PNGAPI end_callback(png_structp png_ptr, png_infop info_ptr);
120 static void PNGAPI error_callback(png_structp png_ptr,
121 png_const_charp error_msg);
122 static void PNGAPI warning_callback(png_structp png_ptr,
123 png_const_charp warning_msg);
125 // This is defined in the PNG spec as an invariant. We use it to
126 // do manual validation without libpng.
127 static const uint8_t pngSignatureBytes[];
130 } // namespace image
131 } // namespace mozilla
133 #endif // nsPNGDecoder_h