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

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: cleanup 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. Th is is useful for
117 // callers to determine whether a decoder instantiation failure is due to in sufficient or bad data.
Peter Kasting 2016/08/19 23:04:58 Nit: Seems like most of this file prefers to wrap
f(malita) 2016/08/22 01:51:16 Done.
118 static bool isSufficientData(const SharedBuffer&);
Peter Kasting 2016/08/19 23:04:58 Nit: I'm not a big fan of this function name. Som
f(malita) 2016/08/22 01:51:16 Changed to hasSufficientDataToSniffImageType().
119
129 void setData(PassRefPtr<SegmentReader> data, bool allDataReceived) 120 void setData(PassRefPtr<SegmentReader> data, bool allDataReceived)
130 { 121 {
131 if (m_failed) 122 if (m_failed)
132 return; 123 return;
133 m_data = data; 124 m_data = data;
134 m_isAllDataReceived = allDataReceived; 125 m_isAllDataReceived = allDataReceived;
135 onSetData(m_data.get()); 126 onSetData(m_data.get());
136 } 127 }
137 128
138 void setData(PassRefPtr<SharedBuffer> data, bool allDataReceived) 129 void setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 findRequiredPreviousFrame(0, false)); 266 findRequiredPreviousFrame(0, false));
276 } 267 }
277 m_frameBufferCache[0].setMemoryAllocator(allocator); 268 m_frameBufferCache[0].setMemoryAllocator(allocator);
278 } 269 }
279 270
280 virtual bool canDecodeToYUV() { return false; } 271 virtual bool canDecodeToYUV() { return false; }
281 virtual bool decodeToYUV() { return false; } 272 virtual bool decodeToYUV() { return false; }
282 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) { } 273 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) { }
283 274
284 protected: 275 protected:
276 ImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOption s, size_t maxDecodedBytes)
277 : m_premultiplyAlpha(alphaOption == AlphaPremultiplied)
278 , m_ignoreGammaAndColorProfile(colorOptions == GammaAndColorProfileIgnor ed)
279 , m_maxDecodedBytes(maxDecodedBytes) { }
280
285 // Calculates the most recent frame whose image data may be needed in 281 // Calculates the most recent frame whose image data may be needed in
286 // order to decode frame |frameIndex|, based on frame disposal methods 282 // order to decode frame |frameIndex|, based on frame disposal methods
287 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether 283 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether
288 // the rectangle of frame at |frameIndex| is known to be opaque. 284 // the rectangle of frame at |frameIndex| is known to be opaque.
289 // If no previous frame's data is required, returns WTF::kNotFound. 285 // If no previous frame's data is required, returns WTF::kNotFound.
290 // 286 //
291 // This function requires that the previous frame's 287 // This function requires that the previous frame's
292 // |m_requiredPreviousFrameIndex| member has been set correctly. The 288 // |m_requiredPreviousFrameIndex| member has been set correctly. The
293 // easiest way to ensure this is for subclasses to call this method and 289 // easiest way to ensure this is for subclasses to call this method and
294 // store the result on the frame via setRequiredPreviousFrameIndex() 290 // store the result on the frame via setRequiredPreviousFrameIndex()
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 ImageOrientation m_orientation; 323 ImageOrientation m_orientation;
328 324
329 // The maximum amount of memory a decoded image should require. Ideally, 325 // The maximum amount of memory a decoded image should require. Ideally,
330 // image decoders should downsample large images to fit under this limit 326 // image decoders should downsample large images to fit under this limit
331 // (and then return the downsampled size from decodedSize()). Ignoring 327 // (and then return the downsampled size from decodedSize()). Ignoring
332 // this limit can cause excessive memory use or even crashes on low- 328 // this limit can cause excessive memory use or even crashes on low-
333 // memory devices. 329 // memory devices.
334 const size_t m_maxDecodedBytes; 330 const size_t m_maxDecodedBytes;
335 331
336 private: 332 private:
333 enum class SniffResult {
334 JPEG,
335 PNG,
336 GIF,
337 WEBP,
338 ICO,
339 BMP,
340 Invalid
341 };
342
343 static SniffResult determineImageType(const char* data, size_t length);
344
337 // Some code paths compute the size of the image as "width * height * 4" 345 // Some code paths compute the size of the image as "width * height * 4"
338 // and return it as a (signed) int. Avoid overflow. 346 // and return it as a (signed) int. Avoid overflow.
339 static bool sizeCalculationMayOverflow(unsigned width, unsigned height) 347 static bool sizeCalculationMayOverflow(unsigned width, unsigned height)
340 { 348 {
341 unsigned long long total_size = static_cast<unsigned long long>(width) 349 unsigned long long total_size = static_cast<unsigned long long>(width)
342 * static_cast<unsigned long long>(height); 350 * static_cast<unsigned long long>(height);
343 return total_size > ((1 << 29) - 1); 351 return total_size > ((1 << 29) - 1);
344 } 352 }
345 353
346 IntSize m_size; 354 IntSize m_size;
347 bool m_sizeAvailable = false; 355 bool m_sizeAvailable = false;
348 bool m_isAllDataReceived = false; 356 bool m_isAllDataReceived = false;
349 bool m_failed = false; 357 bool m_failed = false;
350 358
351 bool m_hasColorProfile = false; 359 bool m_hasColorProfile = false;
352 ImageFrame::ICCProfile m_colorProfile; 360 ImageFrame::ICCProfile m_colorProfile;
353 361
354 #if USE(QCMSLIB) 362 #if USE(QCMSLIB)
355 QCMSTransformUniquePtr m_sourceToOutputDeviceColorTransform; 363 QCMSTransformUniquePtr m_sourceToOutputDeviceColorTransform;
356 #endif 364 #endif
357 }; 365 };
358 366
359 } // namespace blink 367 } // namespace blink
360 368
361 #endif 369 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698