Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.h

Issue 2257513002: Refactor ImageDecoder factories (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 enum AlphaOption { 87 enum AlphaOption {
88 AlphaPremultiplied, 88 AlphaPremultiplied,
89 AlphaNotPremultiplied 89 AlphaNotPremultiplied
90 }; 90 };
91 91
92 enum GammaAndColorProfileOption { 92 enum GammaAndColorProfileOption {
93 GammaAndColorProfileApplied, 93 GammaAndColorProfileApplied,
94 GammaAndColorProfileIgnored 94 GammaAndColorProfileIgnored
95 }; 95 };
96 96
97 enum class SniffResult {
98 JPEG,
99 PNG,
100 GIF,
101 WEBP,
102 ICO,
103 BMP,
104 InsufficientData,
105 Invalid
106 };
107
108 static SniffResult determineImageType(const char* data, size_t length);
109 static SniffResult determineImageType(const SharedBuffer&);
110 static SniffResult determineImageType(const SegmentReader&);
111
112 ImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOption s, size_t maxDecodedBytes)
113 : m_premultiplyAlpha(alphaOption == AlphaPremultiplied)
114 , m_ignoreGammaAndColorProfile(colorOptions == GammaAndColorProfileIgnor ed)
115 , m_maxDecodedBytes(maxDecodedBytes) { }
116
117 virtual ~ImageDecoder() { } 97 virtual ~ImageDecoder() { }
118 98
119 // Returns a caller-owned decoder of the appropriate type. Returns 0 if 99 // Returns a caller-owned decoder of the appropriate type. Returns nullptr if
120 // we can't sniff a supported type from the provided data (possibly 100 // we can't sniff a supported type from the provided data (possibly
121 // because there isn't enough data yet). 101 // because there isn't enough data yet).
122 // Sets m_maxDecodedBytes to Platform::maxImageDecodedBytes(). 102 // Sets m_maxDecodedBytes to Platform::maxImageDecodedBytes().
123 static std::unique_ptr<ImageDecoder> create(SniffResult, AlphaOption, GammaA ndColorProfileOption); 103 static std::unique_ptr<ImageDecoder> create(PassRefPtr<SegmentReader> data, bool dataComplete,
104 AlphaOption, GammaAndColorProfileOption);
105 static std::unique_ptr<ImageDecoder> create(PassRefPtr<SharedBuffer> data, b ool dataComplete,
106 AlphaOption alphaoption, GammaAndColorProfileOption colorOptions)
107 {
108 return create(SegmentReader::createFromSharedBuffer(data), dataComplete, alphaoption, colorOptions);
109 }
110
124 111
125 virtual String filenameExtension() const = 0; 112 virtual String filenameExtension() const = 0;
126 113
127 bool isAllDataReceived() const { return m_isAllDataReceived; } 114 bool isAllDataReceived() const { return m_isAllDataReceived; }
128 115
116 // Returns true if the buffer holds enough data to instantiate a decoder.
117 // This is useful for callers to determine whether a decoder instantiation
118 // failure is due to insufficient or bad data.
119 static bool hasSufficientDataToSniffImageType(const SharedBuffer&);
120
129 void setData(PassRefPtr<SegmentReader> data, bool allDataReceived) 121 void setData(PassRefPtr<SegmentReader> data, bool allDataReceived)
130 { 122 {
131 if (m_failed) 123 if (m_failed)
132 return; 124 return;
133 m_data = data; 125 m_data = data;
134 m_isAllDataReceived = allDataReceived; 126 m_isAllDataReceived = allDataReceived;
135 onSetData(m_data.get()); 127 onSetData(m_data.get());
136 } 128 }
137 129
138 void setData(PassRefPtr<SharedBuffer> data, bool allDataReceived) 130 void setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 findRequiredPreviousFrame(0, false)); 267 findRequiredPreviousFrame(0, false));
276 } 268 }
277 m_frameBufferCache[0].setMemoryAllocator(allocator); 269 m_frameBufferCache[0].setMemoryAllocator(allocator);
278 } 270 }
279 271
280 virtual bool canDecodeToYUV() { return false; } 272 virtual bool canDecodeToYUV() { return false; }
281 virtual bool decodeToYUV() { return false; } 273 virtual bool decodeToYUV() { return false; }
282 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) { } 274 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) { }
283 275
284 protected: 276 protected:
277 ImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOption s, size_t maxDecodedBytes)
278 : m_premultiplyAlpha(alphaOption == AlphaPremultiplied)
279 , m_ignoreGammaAndColorProfile(colorOptions == GammaAndColorProfileIgnor ed)
280 , m_maxDecodedBytes(maxDecodedBytes) { }
281
285 // Calculates the most recent frame whose image data may be needed in 282 // Calculates the most recent frame whose image data may be needed in
286 // order to decode frame |frameIndex|, based on frame disposal methods 283 // order to decode frame |frameIndex|, based on frame disposal methods
287 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether 284 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether
288 // the rectangle of frame at |frameIndex| is known to be opaque. 285 // the rectangle of frame at |frameIndex| is known to be opaque.
289 // If no previous frame's data is required, returns WTF::kNotFound. 286 // If no previous frame's data is required, returns WTF::kNotFound.
290 // 287 //
291 // This function requires that the previous frame's 288 // This function requires that the previous frame's
292 // |m_requiredPreviousFrameIndex| member has been set correctly. The 289 // |m_requiredPreviousFrameIndex| member has been set correctly. The
293 // easiest way to ensure this is for subclasses to call this method and 290 // easiest way to ensure this is for subclasses to call this method and
294 // store the result on the frame via setRequiredPreviousFrameIndex() 291 // store the result on the frame via setRequiredPreviousFrameIndex()
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 ImageOrientation m_orientation; 324 ImageOrientation m_orientation;
328 325
329 // The maximum amount of memory a decoded image should require. Ideally, 326 // The maximum amount of memory a decoded image should require. Ideally,
330 // image decoders should downsample large images to fit under this limit 327 // image decoders should downsample large images to fit under this limit
331 // (and then return the downsampled size from decodedSize()). Ignoring 328 // (and then return the downsampled size from decodedSize()). Ignoring
332 // this limit can cause excessive memory use or even crashes on low- 329 // this limit can cause excessive memory use or even crashes on low-
333 // memory devices. 330 // memory devices.
334 const size_t m_maxDecodedBytes; 331 const size_t m_maxDecodedBytes;
335 332
336 private: 333 private:
334 enum class SniffResult {
335 JPEG,
336 PNG,
337 GIF,
338 WEBP,
339 ICO,
340 BMP,
341 Invalid
342 };
343
344 static SniffResult determineImageType(const char* data, size_t length);
345
337 // Some code paths compute the size of the image as "width * height * 4" 346 // Some code paths compute the size of the image as "width * height * 4"
338 // and return it as a (signed) int. Avoid overflow. 347 // and return it as a (signed) int. Avoid overflow.
339 static bool sizeCalculationMayOverflow(unsigned width, unsigned height) 348 static bool sizeCalculationMayOverflow(unsigned width, unsigned height)
340 { 349 {
341 unsigned long long total_size = static_cast<unsigned long long>(width) 350 unsigned long long total_size = static_cast<unsigned long long>(width)
342 * static_cast<unsigned long long>(height); 351 * static_cast<unsigned long long>(height);
343 return total_size > ((1 << 29) - 1); 352 return total_size > ((1 << 29) - 1);
344 } 353 }
345 354
346 IntSize m_size; 355 IntSize m_size;
347 bool m_sizeAvailable = false; 356 bool m_sizeAvailable = false;
348 bool m_isAllDataReceived = false; 357 bool m_isAllDataReceived = false;
349 bool m_failed = false; 358 bool m_failed = false;
350 359
351 bool m_hasColorProfile = false; 360 bool m_hasColorProfile = false;
352 ImageFrame::ICCProfile m_colorProfile; 361 ImageFrame::ICCProfile m_colorProfile;
353 362
354 #if USE(QCMSLIB) 363 #if USE(QCMSLIB)
355 QCMSTransformUniquePtr m_sourceToOutputDeviceColorTransform; 364 QCMSTransformUniquePtr m_sourceToOutputDeviceColorTransform;
356 #endif 365 #endif
357 }; 366 };
358 367
359 } // namespace blink 368 } // namespace blink
360 369
361 #endif 370 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698