OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 15 matching lines...) Expand all Loading... | |
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include "config.h" | 29 #include "config.h" |
30 #include "core/platform/image-decoders/webp/WEBPImageDecoder.h" | 30 #include "core/platform/image-decoders/webp/WEBPImageDecoder.h" |
31 | 31 |
32 #include "core/platform/PlatformInstrumentation.h" | 32 #include "core/platform/PlatformInstrumentation.h" |
33 | 33 |
34 #ifdef QCMS_WEBP_COLOR_CORRECTION | 34 #ifdef QCMS_WEBP_COLOR_CORRECTION |
35 #include "qcms.h" | 35 #include "qcms.h" |
36 #include "webp/demux.h" | 36 #endif |
37 #else | 37 |
38 #undef ICCP_FLAG | 38 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
39 #include "RuntimeEnabledFeatures.h" | |
40 #include "webp/format_constants.h" | |
41 #endif | |
42 | |
43 #if (WEBP_DECODER_ABI_VERSION < 0x0163) | |
44 // Backward emulation for versions earlier than 0.1.99. | |
45 #define MODE_rgbA MODE_RGBA | |
46 #define MODE_bgrA MODE_BGRA | |
47 #define ALPHA_FLAG 0 | |
48 #define ICCP_FLAG 0 | |
Noel Gordon
2013/05/01 19:11:41
Remove. ICCP_FLAG use is guarded in decode() now.
urvang (Google)
2013/05/01 22:25:16
Done.
| |
49 #elif (WEBP_DECODER_ABI_VERSION <= 0x0200) | |
50 // Backward emulation for versions earlier than 0.3.0. | |
51 #define ALPHA_FLAG 0x000010 | |
39 #define ICCP_FLAG 0 | 52 #define ICCP_FLAG 0 |
Noel Gordon
2013/05/01 19:11:41
Ditto.
urvang (Google)
2013/05/01 22:25:16
Done.
| |
40 #endif | 53 #endif |
41 | 54 |
42 // Backward emulation for earlier versions than 0.1.99. | |
43 #if (WEBP_DECODER_ABI_VERSION < 0x0163) | |
44 #define MODE_rgbA MODE_RGBA | |
45 #define MODE_bgrA MODE_BGRA | |
46 #endif | |
47 | |
48 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) | 55 #if CPU(BIG_ENDIAN) || CPU(MIDDLE_ENDIAN) |
49 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } | 56 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } |
50 #elif SK_B32_SHIFT | 57 #elif SK_B32_SHIFT |
51 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } | 58 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_rgbA : M ODE_RGBA; } |
52 #else // LITTLE_ENDIAN, output BGRA pixels. | 59 #else // LITTLE_ENDIAN, output BGRA pixels. |
53 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_bgrA : M ODE_BGRA; } | 60 inline WEBP_CSP_MODE outputMode(bool hasAlpha) { return hasAlpha ? MODE_bgrA : M ODE_BGRA; } |
54 #endif | 61 #endif |
55 | 62 |
56 namespace WebCore { | 63 namespace WebCore { |
57 | 64 |
58 WEBPImageDecoder::WEBPImageDecoder(ImageSource::AlphaOption alphaOption, | 65 WEBPImageDecoder::WEBPImageDecoder(ImageSource::AlphaOption alphaOption, |
59 ImageSource::GammaAndColorProfileOption gamma AndColorProfileOption) | 66 ImageSource::GammaAndColorProfileOption gamma AndColorProfileOption) |
60 : ImageDecoder(alphaOption, gammaAndColorProfileOption) | 67 : ImageDecoder(alphaOption, gammaAndColorProfileOption) |
61 , m_decoder(0) | 68 , m_decoder(0) |
62 , m_hasAlpha(false) | |
63 , m_formatFlags(0) | 69 , m_formatFlags(0) |
64 #ifdef QCMS_WEBP_COLOR_CORRECTION | 70 #ifdef QCMS_WEBP_COLOR_CORRECTION |
65 , m_haveReadProfile(false) | 71 , m_haveReadProfile(false) |
66 , m_transform(0) | 72 , m_transform(0) |
73 #endif | |
74 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
67 , m_decodedHeight(0) | 75 , m_decodedHeight(0) |
76 , m_haveAlreadyParsedThisData(false) | |
77 , m_demux(0) | |
78 , m_demuxState(WEBP_DEMUX_PARSING_HEADER) | |
79 , m_haveReadAnimParams(false) | |
80 , m_repetitionCount(cAnimationLoopOnce) | |
68 #endif | 81 #endif |
69 { | 82 { |
70 WebPInitDecBuffer(&m_decoderBuffer); | |
71 } | 83 } |
72 | 84 |
73 WEBPImageDecoder::~WEBPImageDecoder() | 85 WEBPImageDecoder::~WEBPImageDecoder() |
74 { | 86 { |
75 clear(); | 87 clear(); |
76 } | 88 } |
77 | 89 |
78 void WEBPImageDecoder::clear() | 90 void WEBPImageDecoder::clear() |
79 { | 91 { |
80 #ifdef QCMS_WEBP_COLOR_CORRECTION | 92 #ifdef QCMS_WEBP_COLOR_CORRECTION |
81 if (m_transform) | 93 if (m_transform) |
82 qcms_transform_release(m_transform); | 94 qcms_transform_release(m_transform); |
83 m_transform = 0; | 95 m_transform = 0; |
84 #endif | 96 #endif |
85 WebPFreeDecBuffer(&m_decoderBuffer); | 97 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
86 if (m_decoder) | 98 WebPDemuxDelete(m_demux); |
87 WebPIDelete(m_decoder); | 99 m_demux = 0; |
100 #endif | |
101 clearDecoder(); | |
102 } | |
103 | |
104 void WEBPImageDecoder::clearDecoder() | |
105 { | |
106 WebPIDelete(m_decoder); | |
88 m_decoder = 0; | 107 m_decoder = 0; |
108 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
109 m_decodedHeight = 0; | |
110 #endif | |
89 } | 111 } |
90 | 112 |
91 bool WEBPImageDecoder::isSizeAvailable() | 113 bool WEBPImageDecoder::isSizeAvailable() |
92 { | 114 { |
93 if (!ImageDecoder::isSizeAvailable()) | 115 if (!ImageDecoder::isSizeAvailable()) { |
94 decode(true); | 116 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
95 | 117 if (!updateDemuxer()) |
118 return 0; | |
119 #else | |
120 decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), true, 0); | |
121 #endif | |
122 } | |
96 return ImageDecoder::isSizeAvailable(); | 123 return ImageDecoder::isSizeAvailable(); |
97 } | 124 } |
98 | 125 |
99 ImageFrame* WEBPImageDecoder::frameBufferAtIndex(size_t index) | 126 size_t WEBPImageDecoder::frameCount() |
100 { | 127 { |
101 if (index) | 128 #ifdef WEBP_ICC_ANIMATION_SUPPORT |
129 if (!updateDemuxer()) | |
102 return 0; | 130 return 0; |
103 | 131 #else |
104 if (m_frameBufferCache.isEmpty()) { | 132 if (m_frameBufferCache.isEmpty()) { |
105 m_frameBufferCache.resize(1); | 133 m_frameBufferCache.resize(1); |
106 m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha); | 134 m_frameBufferCache[0].setPremultiplyAlpha(m_premultiplyAlpha); |
107 } | 135 } |
108 | 136 #endif |
109 ImageFrame& frame = m_frameBufferCache[0]; | 137 return m_frameBufferCache.size(); |
110 if (frame.status() != ImageFrame::FrameComplete) { | 138 } |
139 | |
140 ImageFrame* WEBPImageDecoder::frameBufferAtIndex(size_t index) | |
141 { | |
142 if (index >= frameCount()) | |
143 return 0; | |
144 | |
145 ImageFrame& frame = m_frameBufferCache[index]; | |
146 if (frame.status() == ImageFrame::FrameComplete) | |
147 return &frame; | |
148 | |
149 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
150 if (RuntimeEnabledFeatures::animatedWebPEnabled()) { | |
151 if (index && (m_frameBufferCache[index - 1].status() != ImageFrame::Fram eComplete)) | |
152 return 0; // We haven't fully decoded the previous frame yet. | |
153 ASSERT(m_demux); | |
154 WebPIterator fIter; | |
Noel Gordon
2013/05/01 19:11:41
fIter again (abbrev). Perhaps call it buffer.
urvang (Google)
2013/05/01 22:25:16
Calling it 'webpFrame' now.
| |
155 if (!WebPDemuxGetFrame(m_demux, index + 1, &fIter)) | |
156 return 0; | |
157 if ((m_formatFlags & ANIMATION_FLAG) && !initFrameBuffer(fIter, index)) | |
158 return 0; | |
111 PlatformInstrumentation::willDecodeImage("WEBP"); | 159 PlatformInstrumentation::willDecodeImage("WEBP"); |
112 decode(false); | 160 decode(fIter.fragment.bytes, fIter.fragment.size, false, index); |
113 PlatformInstrumentation::didDecodeImage(); | 161 PlatformInstrumentation::didDecodeImage(); |
114 } | 162 WebPDemuxReleaseIterator(&fIter); |
163 return &frame; | |
164 } | |
165 #endif | |
166 | |
167 ASSERT(!index); | |
168 PlatformInstrumentation::willDecodeImage("WEBP"); | |
169 decode(reinterpret_cast<const uint8_t*>(m_data->data()), m_data->size(), fal se, index); | |
170 PlatformInstrumentation::didDecodeImage(); | |
115 return &frame; | 171 return &frame; |
116 } | 172 } |
117 | 173 |
174 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
175 | |
176 void WEBPImageDecoder::setData(SharedBuffer* data, bool allDataReceived) | |
177 { | |
178 if (failed()) | |
179 return; | |
180 | |
181 ImageDecoder::setData(data, allDataReceived); | |
182 | |
183 // Mark that we have new data. | |
184 if (m_demuxState != WEBP_DEMUX_DONE) | |
185 m_haveAlreadyParsedThisData = false; | |
186 } | |
187 | |
188 bool WEBPImageDecoder::updateDemuxer() | |
189 { | |
190 if (m_haveAlreadyParsedThisData) | |
191 return true; | |
192 | |
193 static const size_t minSizeForDemux = RIFF_HEADER_SIZE + CHUNK_HEADER_SIZE; | |
194 if (m_data->size() < minSizeForDemux) | |
195 return false; // Wait for headers so that WebPDemuxPartial doesn't retur n null. | |
196 | |
197 WebPDemuxDelete(m_demux); | |
198 WebPData inputData = { reinterpret_cast<const uint8_t*>(m_data->data()), m_d ata->size() }; | |
199 m_demux = WebPDemuxPartial(&inputData, &m_demuxState); | |
200 if (!m_demux) | |
201 return setFailed(); // Must be a failure as we have at least 'minSizeFor Demux' bytes. | |
202 m_haveAlreadyParsedThisData = true; | |
Noel Gordon
2013/05/01 19:11:41
Should this line be moved somewhere before the ret
urvang (Google)
2013/05/01 22:25:16
Done.
| |
203 | |
204 if (m_demuxState <= WEBP_DEMUX_PARSING_HEADER) | |
205 return true; // Not enough data for parsing canvas width/height yet. | |
Noel Gordon
2013/05/01 19:11:41
Return false; for consistency.
urvang (Google)
2013/05/01 22:25:16
Done.
| |
206 | |
207 if (!ImageDecoder::isSizeAvailable()) { | |
208 if (!setSize(WebPDemuxGetI(m_demux, WEBP_FF_CANVAS_WIDTH), WebPDemuxGetI (m_demux, WEBP_FF_CANVAS_HEIGHT))) | |
209 return setFailed(); | |
210 m_formatFlags = WebPDemuxGetI(m_demux, WEBP_FF_FORMAT_FLAGS); | |
211 } | |
212 ASSERT(ImageDecoder::isSizeAvailable()); | |
213 const bool hasAnimation = (m_formatFlags & ANIMATION_FLAG); | |
Noel Gordon
2013/05/01 19:11:41
213-215. Could we move these lines up so we exit e
urvang (Google)
2013/05/01 22:25:16
Changed the code a bit to exit before setSize() ca
| |
214 if (!RuntimeEnabledFeatures::animatedWebPEnabled() && hasAnimation) | |
215 return setFailed(); | |
216 const size_t newFrameCount = WebPDemuxGetI(m_demux, WEBP_FF_FRAME_COUNT); | |
217 if (hasAnimation && !m_haveReadAnimParams && (newFrameCount >= 1)) { | |
Noel Gordon
2013/05/01 19:11:41
/curious newFrameCount >= 1. Do single-frame imag
urvang (Google)
2013/05/01 22:25:16
Ideally, no. But theoretically, single-frame anima
| |
218 // As we have parsed at least one frame (even if partially), | |
219 // we must already have parsed the animation properties. | |
220 // This is because ANIM chunk always precedes ANMF chunks. | |
221 const uint32_t loopCount = WebPDemuxGetI(m_demux, WEBP_FF_LOOP_COUNT); | |
222 // Note: The following casts an 'unsigned int' to 'int'. But that is fin e, because loop count is always <= 16 bits. | |
Noel Gordon
2013/05/01 19:11:41
How about turning the comment into code.
int loop
urvang (Google)
2013/05/01 22:25:16
That seems a bit ugly. Let me put an assert instea
Noel Gordon
2013/05/02 18:00:27
Maybe loopCount is just unnecessary noise.
m_repe
| |
223 m_repetitionCount = (!loopCount) ? cAnimationLoopInfinite : loopCount; | |
Noel Gordon
2013/05/01 19:11:41
... = !loopCount ? cAnimationLoopInfinite ...
urvang (Google)
2013/05/01 22:25:16
Done.
| |
224 m_haveReadAnimParams = true; | |
225 } | |
226 if (newFrameCount > m_frameBufferCache.size()) { | |
227 m_frameBufferCache.resize(newFrameCount); | |
228 for (size_t i = 0; i < newFrameCount; ++i) | |
229 m_frameBufferCache[i].setPremultiplyAlpha(m_premultiplyAlpha); | |
230 } | |
231 return true; | |
232 } | |
233 | |
234 // FIXME: This method is very similar to the one in GIFImageDecoder.cpp and shou ld be refactored. | |
235 bool WEBPImageDecoder::initFrameBuffer(const WebPIterator& fIter, size_t frameIn dex) | |
Noel Gordon
2013/05/01 19:11:41
fIter. Call it frame.
urvang (Google)
2013/05/01 22:25:16
Done.
| |
236 { | |
237 ImageFrame& buffer = m_frameBufferCache[frameIndex]; | |
238 if (buffer.status() != ImageFrame::FrameEmpty) // Already initialized. | |
239 return true; | |
240 | |
241 // Initialize the frame rect in our buffer. | |
242 IntRect frameRect(fIter.x_offset, fIter.y_offset, fIter.width, fIter.height) ; | |
243 | |
244 // Make sure the frameRect doesn't extend outside the buffer. | |
245 if (frameRect.maxX() > size().width()) | |
246 frameRect.setWidth(size().width() - fIter.x_offset); | |
247 if (frameRect.maxY() > size().height()) | |
248 frameRect.setHeight(size().height() - fIter.y_offset); | |
249 | |
250 const int left = upperBoundScaledX(frameRect.x()); | |
251 const int right = lowerBoundScaledX(frameRect.maxX(), left); | |
252 const int top = upperBoundScaledY(frameRect.y()); | |
253 const int bottom = lowerBoundScaledY(frameRect.maxY(), top); | |
254 buffer.setOriginalFrameRect(IntRect(left, top, right - left, bottom - top)); | |
255 | |
256 buffer.setDisposalMethod(fIter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? ImageFrame::DisposeOverwriteBgcolor : ImageFrame::DisposeKeep); | |
Noel Gordon
2013/05/01 19:11:41
So note that the only disposals methods for a webp
urvang (Google)
2013/05/01 22:25:16
see comment in 279
| |
257 buffer.setDuration(fIter.duration); | |
258 buffer.setHasAlpha(m_formatFlags & ALPHA_FLAG); | |
259 | |
260 if (!frameIndex) { | |
261 // This is the first frame, so we're not relying on any previous data. | |
Noel Gordon
2013/05/01 19:11:41
... and that this if clause deals with !frameIndex
urvang (Google)
2013/05/01 22:25:16
see comment at 279
| |
262 if (!buffer.setSize(scaledSize().width(), scaledSize().height())) | |
263 return setFailed(); | |
264 } else { | |
265 // The starting state for this frame depends on the previous frame's | |
266 // disposal method. | |
267 const ImageFrame& prevBuffer = m_frameBufferCache[frameIndex - 1]; | |
268 ASSERT(prevBuffer.status() == ImageFrame::FrameComplete); | |
269 const IntRect& prevRect = prevBuffer.originalFrameRect(); | |
270 const ImageFrame::FrameDisposalMethod prevMethod = prevBuffer.disposalMe thod(); | |
271 if ((prevMethod == ImageFrame::DisposeKeep) || (prevMethod == ImageFrame ::DisposeNotSpecified)) { | |
Noel Gordon
2013/05/01 19:11:41
Is prevMethod == ImageFrame::DisposeNotSpecified a
urvang (Google)
2013/05/01 22:25:16
It's not. I kept this code snippet as similar to G
| |
272 // Preserve the last frame as the starting state for this frame. | |
273 if (!buffer.copyBitmapData(prevBuffer)) | |
274 return setFailed(); | |
275 } else { // prevMethod == ImageFrame::DisposeOverwriteBgcolor | |
Noel Gordon
2013/05/01 19:11:41
This comment could break, an ASSERT would not. Tu
urvang (Google)
2013/05/01 22:25:16
Done.
| |
276 // We want to clear the previous frame to transparent, without | |
277 // affecting pixels in the image outside of the frame. | |
278 // So, we copy the whole previous buffer, then clear just its frame. | |
279 if (!frameIndex || prevRect.contains(IntRect(IntPoint(), scaledSize( )))) { | |
Noel Gordon
2013/05/01 19:11:41
Given the comment @261, what value does !frameInde
urvang (Google)
2013/05/01 22:25:16
I see what you are getting at.
But again, the ide
Noel Gordon
2013/05/02 18:00:27
Similar is fine: my view was a FIXME would also he
| |
280 // Clearing the first frame, or a frame the size of the whole | |
281 // image, results in a completely empty image. | |
282 if (!buffer.setSize(scaledSize().width(), scaledSize().height()) ) | |
283 return setFailed(); | |
284 } else { | |
285 // Copy the whole previous buffer, then clear just its frame. | |
286 if (!buffer.copyBitmapData(prevBuffer)) | |
287 return setFailed(); | |
288 for (int y = prevRect.y(); y < prevRect.maxY(); ++y) { | |
289 for (int x = prevRect.x(); x < prevRect.maxX(); ++x) | |
290 buffer.setRGBA(x, y, 0, 0, 0, 0); | |
291 } | |
292 } | |
293 } | |
294 } | |
295 | |
296 // Update frame status to be partially complete. | |
297 buffer.setStatus(ImageFrame::FramePartial); | |
298 return true; | |
299 } | |
300 | |
301 void WEBPImageDecoder::clearFrameBufferCache(size_t clearBeforeFrame) | |
302 { | |
303 // We always preserve at least one frame. | |
Noel Gordon
2013/05/01 19:11:41
The question is why? The GIF decoder bails if m_f
urvang (Google)
2013/05/01 22:25:16
Note that GIF decoder never clears the 'end' (last
Noel Gordon
2013/05/02 18:00:27
Right, merged. Thanks for the explanation.
| |
304 if (m_frameBufferCache.size() <= 1) | |
305 return; | |
306 | |
307 // Find the last frame we need to preserve in the cache to facilitate | |
308 // the construction of next frames (needed by initFrame() and | |
309 // applyPostProcessing()) . This frame is either: | |
310 // * The last decoded frame in cache, OR | |
311 // * The first frame (if cache doesn't contain any decoded frames). | |
312 const int lastFrame = std::min(clearBeforeFrame, m_frameBufferCache.size() - 1); | |
313 Vector<ImageFrame>::iterator i(m_frameBufferCache.begin() + lastFrame); | |
314 while ((i != m_frameBufferCache.begin()) && (i->status() != ImageFrame::Fram eComplete)) | |
315 --i; | |
316 | |
317 // Now |i| holds the last frame we need to preserve; clear prior frames. | |
318 for (Vector<ImageFrame>::iterator j(m_frameBufferCache.begin()); j != i; ++j ) { | |
319 ASSERT(j->status() != ImageFrame::FramePartial); | |
320 if (j->status() != ImageFrame::FrameEmpty) | |
321 j->clearPixelData(); | |
322 } | |
323 } | |
324 | |
325 #endif // WEBP_ICC_ANIMATION_SUPPORT | |
326 | |
118 #ifdef QCMS_WEBP_COLOR_CORRECTION | 327 #ifdef QCMS_WEBP_COLOR_CORRECTION |
119 | 328 |
120 void WEBPImageDecoder::createColorTransform(const char* data, size_t size) | 329 void WEBPImageDecoder::createColorTransform(const char* data, size_t size) |
121 { | 330 { |
122 if (m_transform) | 331 if (m_transform) |
123 qcms_transform_release(m_transform); | 332 qcms_transform_release(m_transform); |
124 m_transform = 0; | 333 m_transform = 0; |
125 | 334 |
126 qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile(); | 335 qcms_profile* deviceProfile = ImageDecoder::qcmsOutputDeviceProfile(); |
127 if (!deviceProfile) | 336 if (!deviceProfile) |
128 return; | 337 return; |
129 qcms_profile* inputProfile = qcms_profile_from_memory(data, size); | 338 qcms_profile* inputProfile = qcms_profile_from_memory(data, size); |
130 if (!inputProfile) | 339 if (!inputProfile) |
131 return; | 340 return; |
132 | 341 |
133 // We currently only support color profiles for RGB profiled images. | 342 // We currently only support color profiles for RGB profiled images. |
134 ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile)); | 343 ASSERT(icSigRgbData == qcms_profile_get_color_space(inputProfile)); |
135 // The input image pixels are RGBA format. | 344 // The input image pixels are RGBA format. |
136 qcms_data_type format = QCMS_DATA_RGBA_8; | 345 qcms_data_type format = QCMS_DATA_RGBA_8; |
137 // FIXME: Don't force perceptual intent if the image profile contains an int ent. | 346 // FIXME: Don't force perceptual intent if the image profile contains an int ent. |
138 m_transform = qcms_transform_create(inputProfile, format, deviceProfile, QCM S_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL); | 347 m_transform = qcms_transform_create(inputProfile, format, deviceProfile, QCM S_DATA_RGBA_8, QCMS_INTENT_PERCEPTUAL); |
139 | 348 |
140 qcms_profile_release(inputProfile); | 349 qcms_profile_release(inputProfile); |
141 } | 350 } |
142 | 351 |
143 void WEBPImageDecoder::readColorProfile(const uint8_t* data, size_t size) | 352 void WEBPImageDecoder::readColorProfile() |
144 { | 353 { |
145 WebPChunkIterator chunkIterator; | 354 WebPChunkIterator chunkIterator; |
146 WebPData inputData = { data, size }; | 355 if (!WebPDemuxGetChunk(m_demux, "ICCP", 1, &chunkIterator)) { |
147 WebPDemuxState state; | |
148 | |
149 WebPDemuxer* demuxer = WebPDemuxPartial(&inputData, &state); | |
150 if (!WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunkIterator)) { | |
151 WebPDemuxReleaseChunkIterator(&chunkIterator); | 356 WebPDemuxReleaseChunkIterator(&chunkIterator); |
152 WebPDemuxDelete(demuxer); | |
153 return; | 357 return; |
154 } | 358 } |
155 | 359 |
156 const char* profileData = reinterpret_cast<const char*>(chunkIterator.chunk. bytes); | 360 const char* profileData = reinterpret_cast<const char*>(chunkIterator.chunk. bytes); |
157 size_t profileSize = chunkIterator.chunk.size; | 361 size_t profileSize = chunkIterator.chunk.size; |
158 | 362 |
159 // Only accept RGB color profiles from input class devices. | 363 // Only accept RGB color profiles from input class devices. |
160 bool ignoreProfile = false; | 364 bool ignoreProfile = false; |
161 if (profileSize < ImageDecoder::iccColorProfileHeaderLength) | 365 if (profileSize < ImageDecoder::iccColorProfileHeaderLength) |
162 ignoreProfile = true; | 366 ignoreProfile = true; |
163 else if (!ImageDecoder::rgbColorProfile(profileData, profileSize)) | 367 else if (!ImageDecoder::rgbColorProfile(profileData, profileSize)) |
164 ignoreProfile = true; | 368 ignoreProfile = true; |
165 else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileSize)) | 369 else if (!ImageDecoder::inputDeviceColorProfile(profileData, profileSize)) |
166 ignoreProfile = true; | 370 ignoreProfile = true; |
167 | 371 |
168 if (!ignoreProfile) | 372 if (!ignoreProfile) |
169 createColorTransform(profileData, profileSize); | 373 createColorTransform(profileData, profileSize); |
170 | 374 |
171 WebPDemuxReleaseChunkIterator(&chunkIterator); | 375 WebPDemuxReleaseChunkIterator(&chunkIterator); |
172 WebPDemuxDelete(demuxer); | |
173 } | 376 } |
174 | 377 |
175 void WEBPImageDecoder::applyColorProfile(const uint8_t* data, size_t size, Image Frame& buffer) | 378 #endif // QCMS_WEBP_COLOR_CORRECTION |
379 | |
380 #ifdef WEBP_ICC_ANIMATION_SUPPORT | |
381 void WEBPImageDecoder::applyPostProcessing(size_t frameIndex) | |
176 { | 382 { |
383 ImageFrame& buffer = m_frameBufferCache[frameIndex]; | |
177 int width; | 384 int width; |
178 int decodedHeight; | 385 int decodedHeight; |
179 if (!WebPIDecGetRGB(m_decoder, &decodedHeight, &width, 0, 0)) | 386 if (!WebPIDecGetRGB(m_decoder, &decodedHeight, &width, 0, 0)) |
180 return; // See also https://bugs.webkit.org/show_bug.cgi?id=74062 | 387 return; // See also https://bugs.webkit.org/show_bug.cgi?id=74062 |
181 if (decodedHeight <= 0) | 388 if (decodedHeight <= 0) |
182 return; | 389 return; |
183 | |
184 if (!m_haveReadProfile) { | |
185 readColorProfile(data, size); | |
186 m_haveReadProfile = true; | |
187 } | |
188 | |
189 ASSERT(width == scaledSize().width()); | 390 ASSERT(width == scaledSize().width()); |
Noel Gordon
2013/05/01 19:11:41
These ASSERTs are gonna hurt. The width here is t
urvang (Google)
2013/05/01 22:25:16
OK, to clear things up:
- The frame width/height *
Noel Gordon
2013/05/02 18:00:27
So the frame width can be smaller than the canvas
| |
190 ASSERT(decodedHeight <= scaledSize().height()); | 391 ASSERT(decodedHeight <= scaledSize().height()); |
392 const int left = buffer.originalFrameRect().x(); | |
393 const int top = buffer.originalFrameRect().y(); | |
191 | 394 |
192 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | 395 #ifdef QCMS_WEBP_COLOR_CORRECTION |
193 uint8_t* row = reinterpret_cast<uint8_t*>(buffer.getAddr(0, y)); | 396 // Color Profile. |
Noel Gordon
2013/05/01 19:11:41
Redundant comment given the include guard right?
urvang (Google)
2013/05/01 22:25:16
Maybe, yes. But, the include guard can be removed
Noel Gordon
2013/05/02 18:00:27
Blink style would encourage coherency as follows.
| |
194 if (qcms_transform* transform = colorTransform()) | 397 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) { |
195 qcms_transform_data_type(transform, row, row, width, QCMS_OUTPUT_RGB X); | 398 if (!m_haveReadProfile) { |
196 uint8_t* pixel = row; | 399 readColorProfile(); |
197 for (int x = 0; x < width; ++x, pixel += 4) | 400 m_haveReadProfile = true; |
198 buffer.setRGBA(x, y, pixel[0], pixel[1], pixel[2], pixel[3]); | 401 } |
402 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | |
403 const int canvasY = top + y; | |
404 uint8_t* row = reinterpret_cast<uint8_t*>(buffer.getAddr(left, canva sY)); | |
405 if (qcms_transform* transform = colorTransform()) | |
406 qcms_transform_data_type(transform, row, row, width, QCMS_OUTPUT _RGBX); | |
407 uint8_t* pixel = row; | |
408 for (int x = 0; x < width; ++x, pixel += 4) { | |
409 const int canvasX = left + x; | |
410 buffer.setRGBA(canvasX, canvasY, pixel[0], pixel[1], pixel[2], p ixel[3]); | |
411 } | |
412 } | |
413 } | |
414 #endif // QCMS_WEBP_COLOR_CORRECTION | |
415 | |
416 // Frame disposal: | |
417 // During the decoding of current frame, we may have set some pixels to be t ransparent (i.e. alpha < 255). | |
418 // However, the value of each of these pixels should have been determined by blending it against the value | |
419 // of that pixel in the previous frame. So, we correct these pixels based on disposal method of the previous | |
420 // frame and the previous frame buffer. | |
421 if ((m_formatFlags & ANIMATION_FLAG) && frameIndex) { | |
422 ImageFrame& prevBuffer = m_frameBufferCache[frameIndex - 1]; | |
423 ImageFrame::FrameDisposalMethod prevMethod = prevBuffer.disposalMethod() ; | |
424 ASSERT(prevBuffer.status() == ImageFrame::FrameComplete); | |
425 if (prevMethod == ImageFrame::DisposeKeep) { // Restore transparent pixe ls to pixels in previous canvas. | |
426 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | |
427 const int canvasY = top + y; | |
428 for (int x = 0; x < width; ++x) { | |
429 const int canvasX = left + x; | |
430 ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canv asY); | |
431 // FIXME: Use alpha-blending when alpha is between 0 and 255 . | |
432 // Alpha-blending is being implemented in: https://bugs.webk it.org/show_bug.cgi?id=17022 | |
433 if (!((pixel >> 24) & 0xff)) { // Need to restore. | |
Noel Gordon
2013/05/01 19:11:41
Unless you want to break Android, write this as
urvang (Google)
2013/05/01 22:25:16
Done.
| |
434 const ImageFrame::PixelData prevPixel = *prevBuffer.getA ddr(canvasX, canvasY); | |
Noel Gordon
2013/05/01 19:11:41
s/const//
urvang (Google)
2013/05/01 22:25:16
I thought a const would be desirable?
Anyway, remo
| |
435 pixel = prevPixel; | |
436 } | |
437 } | |
438 } | |
439 } else if (prevMethod == ImageFrame::DisposeOverwriteBgcolor) { | |
440 const IntRect& prevRect = prevBuffer.originalFrameRect(); | |
441 // We need to restore transparent pixels to as they were just after initFrame() call. That is: | |
442 // * Transparent if it belongs to prevRect <-- This is a no-op. | |
443 // * Pixel in the previous canvas otherwise <-- Need to restore. | |
444 for (int y = m_decodedHeight; y < decodedHeight; ++y) { | |
445 const int canvasY = top + y; | |
446 for (int x = 0; x < width; ++x) { | |
447 const int canvasX = left + x; | |
448 ImageFrame::PixelData& pixel = *buffer.getAddr(canvasX, canv asY); | |
449 const ImageFrame::PixelData prevPixel = *prevBuffer.getAddr( canvasX, canvasY); | |
Noel Gordon
2013/05/01 19:11:41
Should prevPixel be defined inside the if clause w
urvang (Google)
2013/05/01 22:25:16
Done.
| |
450 // FIXME: Use alpha-blending when alpha is between 0 and 255 . | |
451 if (!((pixel >> 24) & 0xff) && !prevRect.contains(IntPoint(c anvasX, canvasY))) // Need to restore. | |
Noel Gordon
2013/05/01 19:11:41
Again, don't break the droids.
unsigned alpha =
urvang (Google)
2013/05/01 22:25:16
Done.
| |
452 pixel = prevPixel; | |
453 } | |
454 } | |
455 } | |
199 } | 456 } |
200 | 457 |
201 m_decodedHeight = decodedHeight; | 458 m_decodedHeight = decodedHeight; |
202 } | 459 } |
460 #endif // WEBP_ICC_ANIMATION_SUPPORT | |
203 | 461 |
204 #endif // QCMS_WEBP_COLOR_CORRECTION | 462 bool WEBPImageDecoder::decode(const uint8_t* dataBytes, size_t dataSize, bool on lySize, size_t frameIndex) |
205 | |
206 bool WEBPImageDecoder::decode(bool onlySize) | |
207 { | 463 { |
208 if (failed()) | 464 if (failed()) |
209 return false; | 465 return false; |
210 | 466 |
211 const uint8_t* dataBytes = reinterpret_cast<const uint8_t*>(m_data->data()); | |
212 const size_t dataSize = m_data->size(); | |
213 | |
214 if (!ImageDecoder::isSizeAvailable()) { | 467 if (!ImageDecoder::isSizeAvailable()) { |
215 static const size_t imageHeaderSize = 30; | 468 static const size_t imageHeaderSize = 30; |
216 if (dataSize < imageHeaderSize) | 469 if (dataSize < imageHeaderSize) |
217 return false; | 470 return false; |
218 int width, height; | 471 int width, height; |
219 #ifdef QCMS_WEBP_COLOR_CORRECTION | 472 #if (WEBP_DECODER_ABI_VERSION >= 0x0163) |
220 WebPData inputData = { dataBytes, dataSize }; | |
221 WebPDemuxState state; | |
222 WebPDemuxer* demuxer = WebPDemuxPartial(&inputData, &state); | |
223 if (!demuxer) | |
224 return setFailed(); | |
225 | |
226 width = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_WIDTH); | |
227 height = WebPDemuxGetI(demuxer, WEBP_FF_CANVAS_HEIGHT); | |
228 m_formatFlags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); | |
229 m_hasAlpha = !!(m_formatFlags & ALPHA_FLAG); | |
230 | |
231 WebPDemuxDelete(demuxer); | |
232 if (state <= WEBP_DEMUX_PARSING_HEADER) | |
233 return false; | |
234 #elif (WEBP_DECODER_ABI_VERSION >= 0x0163) | |
235 WebPBitstreamFeatures features; | 473 WebPBitstreamFeatures features; |
236 if (WebPGetFeatures(dataBytes, dataSize, &features) != VP8_STATUS_OK) | 474 if (WebPGetFeatures(dataBytes, dataSize, &features) != VP8_STATUS_OK) |
237 return setFailed(); | 475 return setFailed(); |
238 width = features.width; | 476 width = features.width; |
239 height = features.height; | 477 height = features.height; |
240 m_hasAlpha = features.has_alpha; | 478 m_formatFlags = features.has_alpha ? ALPHA_FLAG : 0; |
241 #else | 479 #else |
242 // Earlier version won't be able to display WebP files with alpha. | 480 // Earlier version won't be able to display WebP files with alpha. |
243 if (!WebPGetInfo(dataBytes, dataSize, &width, &height)) | 481 if (!WebPGetInfo(dataBytes, dataSize, &width, &height)) |
244 return setFailed(); | 482 return setFailed(); |
245 m_hasAlpha = false; | |
246 #endif | 483 #endif |
247 if (!setSize(width, height)) | 484 if (!setSize(width, height)) |
248 return setFailed(); | 485 return setFailed(); |
249 } | 486 } |
250 | 487 |
251 ASSERT(ImageDecoder::isSizeAvailable()); | 488 ASSERT(ImageDecoder::isSizeAvailable()); |
252 if (onlySize) | 489 if (onlySize) |
253 return true; | 490 return true; |
254 | 491 |
255 ASSERT(!m_frameBufferCache.isEmpty()); | 492 ASSERT(m_frameBufferCache.size() > frameIndex); |
256 ImageFrame& buffer = m_frameBufferCache[0]; | 493 ImageFrame& buffer = m_frameBufferCache[frameIndex]; |
257 ASSERT(buffer.status() != ImageFrame::FrameComplete); | 494 ASSERT(buffer.status() != ImageFrame::FrameComplete); |
258 | 495 |
259 if (buffer.status() == ImageFrame::FrameEmpty) { | 496 if (buffer.status() == ImageFrame::FrameEmpty) { |
260 if (!buffer.setSize(size().width(), size().height())) | 497 if (!buffer.setSize(scaledSize().width(), scaledSize().height())) |
261 return setFailed(); | 498 return setFailed(); |
262 buffer.setStatus(ImageFrame::FramePartial); | 499 buffer.setStatus(ImageFrame::FramePartial); |
263 buffer.setHasAlpha(m_hasAlpha); | 500 buffer.setHasAlpha(m_formatFlags & ALPHA_FLAG); |
264 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); | 501 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); |
265 } | 502 } |
266 | 503 |
504 const IntRect& frameRect = buffer.originalFrameRect(); | |
267 if (!m_decoder) { | 505 if (!m_decoder) { |
268 WEBP_CSP_MODE mode = outputMode(m_hasAlpha); | 506 WEBP_CSP_MODE mode = outputMode(m_formatFlags & ALPHA_FLAG); |
269 if (!m_premultiplyAlpha) | 507 if (!m_premultiplyAlpha) |
270 mode = outputMode(false); | 508 mode = outputMode(false); |
509 #ifdef QCMS_WEBP_COLOR_CORRECTION | |
271 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 510 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) |
272 mode = MODE_RGBA; // Decode to RGBA for input to libqcms. | 511 mode = MODE_RGBA; // Decode to RGBA for input to libqcms. |
512 #endif | |
513 WebPInitDecBuffer(&m_decoderBuffer); | |
273 m_decoderBuffer.colorspace = mode; | 514 m_decoderBuffer.colorspace = mode; |
274 m_decoderBuffer.u.RGBA.stride = size().width() * sizeof(ImageFrame::Pixe lData); | 515 m_decoderBuffer.u.RGBA.stride = size().width() * sizeof(ImageFrame::Pixe lData); |
275 m_decoderBuffer.u.RGBA.size = m_decoderBuffer.u.RGBA.stride * size().hei ght(); | 516 m_decoderBuffer.u.RGBA.size = m_decoderBuffer.u.RGBA.stride * frameRect. height(); |
276 m_decoderBuffer.is_external_memory = 1; | 517 m_decoderBuffer.is_external_memory = 1; |
277 m_decoder = WebPINewDecoder(&m_decoderBuffer); | 518 m_decoder = WebPINewDecoder(&m_decoderBuffer); |
278 if (!m_decoder) | 519 if (!m_decoder) |
279 return setFailed(); | 520 return setFailed(); |
280 } | 521 } |
281 | 522 |
282 m_decoderBuffer.u.RGBA.rgba = reinterpret_cast<uint8_t*>(buffer.getAddr(0, 0 )); | 523 m_decoderBuffer.u.RGBA.rgba = reinterpret_cast<uint8_t*>(buffer.getAddr(fram eRect.x(), frameRect.y())); |
283 | 524 |
284 switch (WebPIUpdate(m_decoder, dataBytes, dataSize)) { | 525 switch (WebPIUpdate(m_decoder, dataBytes, dataSize)) { |
285 case VP8_STATUS_OK: | 526 case VP8_STATUS_OK: |
286 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 527 applyPostProcessing(frameIndex); |
287 applyColorProfile(dataBytes, dataSize, buffer); | |
288 buffer.setStatus(ImageFrame::FrameComplete); | 528 buffer.setStatus(ImageFrame::FrameComplete); |
289 clear(); | 529 clearDecoder(); |
290 return true; | 530 return true; |
291 case VP8_STATUS_SUSPENDED: | 531 case VP8_STATUS_SUSPENDED: |
292 if ((m_formatFlags & ICCP_FLAG) && !ignoresGammaAndColorProfile()) | 532 applyPostProcessing(frameIndex); |
293 applyColorProfile(dataBytes, dataSize, buffer); | |
294 return false; | 533 return false; |
295 default: | 534 default: |
296 clear(); | 535 clear(); |
297 return setFailed(); | 536 return setFailed(); |
298 } | 537 } |
299 } | 538 } |
300 | 539 |
301 } // namespace WebCore | 540 } // namespace WebCore |
OLD | NEW |