|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
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/. */ |
|
6 |
|
7 #ifndef nsPNGDecoder_h__ |
|
8 #define nsPNGDecoder_h__ |
|
9 |
|
10 #include "Decoder.h" |
|
11 |
|
12 #include "gfxTypes.h" |
|
13 |
|
14 #include "nsCOMPtr.h" |
|
15 |
|
16 #include "png.h" |
|
17 |
|
18 #include "qcms.h" |
|
19 |
|
20 namespace mozilla { |
|
21 namespace image { |
|
22 class RasterImage; |
|
23 |
|
24 class nsPNGDecoder : public Decoder |
|
25 { |
|
26 public: |
|
27 nsPNGDecoder(RasterImage &aImage); |
|
28 virtual ~nsPNGDecoder(); |
|
29 |
|
30 virtual void InitInternal(); |
|
31 virtual void WriteInternal(const char* aBuffer, uint32_t aCount, DecodeStrategy aStrategy); |
|
32 virtual Telemetry::ID SpeedHistogram(); |
|
33 |
|
34 void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset, |
|
35 int32_t width, int32_t height, |
|
36 gfxImageFormat format); |
|
37 void EndImageFrame(); |
|
38 |
|
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 |
|
42 { |
|
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; |
|
50 } |
|
51 |
|
52 png_uint_32 |
|
53 png_width, // Unused |
|
54 png_height; // Unused |
|
55 |
|
56 int png_bit_depth, |
|
57 png_color_type; |
|
58 |
|
59 if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth, |
|
60 &png_color_type, nullptr, nullptr, nullptr)) { |
|
61 |
|
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; |
|
67 } |
|
68 } |
|
69 |
|
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; |
|
78 |
|
79 gfxImageFormat format; |
|
80 |
|
81 // For size decodes |
|
82 uint8_t mSizeBytes[8]; // Space for width and height, both 4 bytes |
|
83 uint32_t mHeaderBytesRead; |
|
84 |
|
85 // whether CMS or premultiplied alpha are forced off |
|
86 uint32_t mCMSMode; |
|
87 |
|
88 uint8_t mChannels; |
|
89 bool mFrameHasNoAlpha; |
|
90 bool mFrameIsHidden; |
|
91 bool mDisablePremultipliedAlpha; |
|
92 |
|
93 struct AnimFrameInfo |
|
94 { |
|
95 AnimFrameInfo(); |
|
96 #ifdef PNG_APNG_SUPPORTED |
|
97 AnimFrameInfo(png_structp aPNG, png_infop aInfo); |
|
98 #endif |
|
99 |
|
100 FrameBlender::FrameDisposalMethod mDispose; |
|
101 FrameBlender::FrameBlendMethod mBlend; |
|
102 int32_t mTimeout; |
|
103 }; |
|
104 |
|
105 AnimFrameInfo mAnimInfo; |
|
106 |
|
107 // The number of frames we've finished. |
|
108 uint32_t mNumFrames; |
|
109 |
|
110 /* |
|
111 * libpng callbacks |
|
112 * |
|
113 * We put these in the class so that they can access protected members. |
|
114 */ |
|
115 static void PNGAPI info_callback(png_structp png_ptr, png_infop info_ptr); |
|
116 static void PNGAPI row_callback(png_structp png_ptr, png_bytep new_row, |
|
117 png_uint_32 row_num, int pass); |
|
118 #ifdef PNG_APNG_SUPPORTED |
|
119 static void PNGAPI frame_info_callback(png_structp png_ptr, |
|
120 png_uint_32 frame_num); |
|
121 #endif |
|
122 static void PNGAPI end_callback(png_structp png_ptr, png_infop info_ptr); |
|
123 static void PNGAPI error_callback(png_structp png_ptr, |
|
124 png_const_charp error_msg); |
|
125 static void PNGAPI warning_callback(png_structp png_ptr, |
|
126 png_const_charp warning_msg); |
|
127 |
|
128 // This is defined in the PNG spec as an invariant. We use it to |
|
129 // do manual validation without libpng. |
|
130 static const uint8_t pngSignatureBytes[]; |
|
131 }; |
|
132 |
|
133 } // namespace image |
|
134 } // namespace mozilla |
|
135 |
|
136 #endif // nsPNGDecoder_h__ |