| OLD | NEW |
| 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 13 matching lines...) Expand all Loading... |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef ImageDecoder_h | 27 #ifndef ImageDecoder_h |
| 28 #define ImageDecoder_h | 28 #define ImageDecoder_h |
| 29 | 29 |
| 30 #include "SkColorPriv.h" | 30 #include "SkColorPriv.h" |
| 31 #include "SkColorSpaceXform.h" | 31 #include "SkColorSpaceXform.h" |
| 32 #include "platform/PlatformExport.h" | 32 #include "platform/PlatformExport.h" |
| 33 #include "platform/SharedBuffer.h" | 33 #include "platform/SharedBuffer.h" |
| 34 #include "platform/graphics/ColorBehavior.h" |
| 34 #include "platform/graphics/ImageOrientation.h" | 35 #include "platform/graphics/ImageOrientation.h" |
| 35 #include "platform/image-decoders/ImageAnimation.h" | 36 #include "platform/image-decoders/ImageAnimation.h" |
| 36 #include "platform/image-decoders/ImageFrame.h" | 37 #include "platform/image-decoders/ImageFrame.h" |
| 37 #include "platform/image-decoders/SegmentReader.h" | 38 #include "platform/image-decoders/SegmentReader.h" |
| 38 #include "public/platform/Platform.h" | 39 #include "public/platform/Platform.h" |
| 39 #include "wtf/Assertions.h" | 40 #include "wtf/Assertions.h" |
| 40 #include "wtf/RefPtr.h" | 41 #include "wtf/RefPtr.h" |
| 41 #include "wtf/Threading.h" | 42 #include "wtf/Threading.h" |
| 42 #include "wtf/Vector.h" | 43 #include "wtf/Vector.h" |
| 43 #include "wtf/text/WTFString.h" | 44 #include "wtf/text/WTFString.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 class PLATFORM_EXPORT ImageDecoder { | 80 class PLATFORM_EXPORT ImageDecoder { |
| 80 WTF_MAKE_NONCOPYABLE(ImageDecoder); | 81 WTF_MAKE_NONCOPYABLE(ImageDecoder); |
| 81 USING_FAST_MALLOC(ImageDecoder); | 82 USING_FAST_MALLOC(ImageDecoder); |
| 82 | 83 |
| 83 public: | 84 public: |
| 84 static const size_t noDecodedImageByteLimit = | 85 static const size_t noDecodedImageByteLimit = |
| 85 Platform::noDecodedImageByteLimit; | 86 Platform::noDecodedImageByteLimit; |
| 86 | 87 |
| 87 enum AlphaOption { AlphaPremultiplied, AlphaNotPremultiplied }; | 88 enum AlphaOption { AlphaPremultiplied, AlphaNotPremultiplied }; |
| 88 | 89 |
| 89 enum ColorSpaceOption { | |
| 90 // The embedded color profile is ignored entirely. The hasEmbeddedColorSpace | |
| 91 // method will always return false. No transformations are applied to the | |
| 92 // pixel data. SkImages created will have no assocated SkColorSpace. | |
| 93 ColorSpaceIgnored, | |
| 94 // The image will be transformed to the specified target color space. The | |
| 95 // hasEmbeddedColorSpace method will return the truth. SkImages created by | |
| 96 // this decoder will have no associated SkColorSpace. | |
| 97 // TODO(ccameron): This transform is applied only if the image has an | |
| 98 // embedded color profile, but should be applied always. | |
| 99 ColorSpaceTransformed, | |
| 100 // The image will not be transformed (to the extent possible). SkImages | |
| 101 // created by this decoder will have the SkColorSpace of the embedded | |
| 102 // color profile (or sRGB if there was no embedded color profile). | |
| 103 ColorSpaceTagged, | |
| 104 }; | |
| 105 | |
| 106 virtual ~ImageDecoder() {} | 90 virtual ~ImageDecoder() {} |
| 107 | 91 |
| 108 // Returns a caller-owned decoder of the appropriate type. Returns nullptr if | 92 // Returns a caller-owned decoder of the appropriate type. Returns nullptr if |
| 109 // we can't sniff a supported type from the provided data (possibly | 93 // we can't sniff a supported type from the provided data (possibly |
| 110 // because there isn't enough data yet). | 94 // because there isn't enough data yet). |
| 111 // Sets m_maxDecodedBytes to Platform::maxImageDecodedBytes(). | 95 // Sets m_maxDecodedBytes to Platform::maxImageDecodedBytes(). |
| 112 static std::unique_ptr<ImageDecoder> create(PassRefPtr<SegmentReader> data, | 96 static std::unique_ptr<ImageDecoder> create(PassRefPtr<SegmentReader> data, |
| 113 bool dataComplete, | 97 bool dataComplete, |
| 114 AlphaOption, | 98 AlphaOption, |
| 115 ColorSpaceOption, | 99 const ColorBehavior&); |
| 116 sk_sp<SkColorSpace>); | |
| 117 static std::unique_ptr<ImageDecoder> create( | 100 static std::unique_ptr<ImageDecoder> create( |
| 118 PassRefPtr<SharedBuffer> data, | 101 PassRefPtr<SharedBuffer> data, |
| 119 bool dataComplete, | 102 bool dataComplete, |
| 120 AlphaOption alphaoption, | 103 AlphaOption alphaoption, |
| 121 ColorSpaceOption colorOptions, | 104 const ColorBehavior& colorBehavior) { |
| 122 sk_sp<SkColorSpace> targetColorSpace) { | |
| 123 return create(SegmentReader::createFromSharedBuffer(std::move(data)), | 105 return create(SegmentReader::createFromSharedBuffer(std::move(data)), |
| 124 dataComplete, alphaoption, colorOptions, targetColorSpace); | 106 dataComplete, alphaoption, colorBehavior); |
| 125 } | 107 } |
| 126 | 108 |
| 127 virtual String filenameExtension() const = 0; | 109 virtual String filenameExtension() const = 0; |
| 128 | 110 |
| 129 bool isAllDataReceived() const { return m_isAllDataReceived; } | 111 bool isAllDataReceived() const { return m_isAllDataReceived; } |
| 130 | 112 |
| 131 // Returns true if the buffer holds enough data to instantiate a decoder. | 113 // Returns true if the buffer holds enough data to instantiate a decoder. |
| 132 // This is useful for callers to determine whether a decoder instantiation | 114 // This is useful for callers to determine whether a decoder instantiation |
| 133 // failure is due to insufficient or bad data. | 115 // failure is due to insufficient or bad data. |
| 134 static bool hasSufficientDataToSniffImageType(const SharedBuffer&); | 116 static bool hasSufficientDataToSniffImageType(const SharedBuffer&); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // animated images. | 199 // animated images. |
| 218 virtual float frameDurationAtIndex(size_t) const { return 0; } | 200 virtual float frameDurationAtIndex(size_t) const { return 0; } |
| 219 | 201 |
| 220 // Number of bytes in the decoded frame. Returns 0 if the decoder doesn't | 202 // Number of bytes in the decoded frame. Returns 0 if the decoder doesn't |
| 221 // have this frame cached (either because it hasn't been decoded, or because | 203 // have this frame cached (either because it hasn't been decoded, or because |
| 222 // it has been cleared). | 204 // it has been cleared). |
| 223 virtual size_t frameBytesAtIndex(size_t) const; | 205 virtual size_t frameBytesAtIndex(size_t) const; |
| 224 | 206 |
| 225 ImageOrientation orientation() const { return m_orientation; } | 207 ImageOrientation orientation() const { return m_orientation; } |
| 226 | 208 |
| 227 bool ignoresColorSpace() const { | 209 bool ignoresColorSpace() const { return m_colorBehavior.isIgnore(); } |
| 228 return m_colorSpaceOption == ColorSpaceIgnored; | 210 const ColorBehavior& colorBehavior() const { return m_colorBehavior; } |
| 229 } | |
| 230 ColorSpaceOption colorSpaceOption() const { return m_colorSpaceOption; } | |
| 231 sk_sp<SkColorSpace> targetColorSpace() const { return m_targetColorSpace; } | |
| 232 | |
| 233 // Set the target color profile into which all images with embedded color | |
| 234 // profiles should be converted. Note that only the first call to this | |
| 235 // function in this process has any effect. | |
| 236 static void setGlobalTargetColorProfile(const WebVector<char>&); | |
| 237 static sk_sp<SkColorSpace> globalTargetColorSpace(); | |
| 238 | |
| 239 // A target color space to be used by tests. | |
| 240 static sk_sp<SkColorSpace> targetColorSpaceForTesting(); | |
| 241 | 211 |
| 242 // This returns the color space that will be included in the SkImageInfo of | 212 // This returns the color space that will be included in the SkImageInfo of |
| 243 // SkImages created from this decoder. This will be nullptr unless the | 213 // SkImages created from this decoder. This will be nullptr unless the |
| 244 // decoder was created with the option ColorSpaceTagged. | 214 // decoder was created with the option ColorSpaceTagged. |
| 245 sk_sp<SkColorSpace> colorSpaceForSkImages() const; | 215 sk_sp<SkColorSpace> colorSpaceForSkImages() const; |
| 246 | 216 |
| 247 // This returns whether or not the image included a not-ignored embedded | 217 // This returns whether or not the image included a not-ignored embedded |
| 248 // color space. This is independent of whether or not that space's transform | 218 // color space. This is independent of whether or not that space's transform |
| 249 // has been baked into the pixel values. | 219 // has been baked into the pixel values. |
| 250 bool hasEmbeddedColorSpace() const { return m_embeddedColorSpace.get(); } | 220 bool hasEmbeddedColorSpace() const { return m_embeddedColorSpace.get(); } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 267 } |
| 298 m_frameBufferCache[0].setMemoryAllocator(allocator); | 268 m_frameBufferCache[0].setMemoryAllocator(allocator); |
| 299 } | 269 } |
| 300 | 270 |
| 301 virtual bool canDecodeToYUV() { return false; } | 271 virtual bool canDecodeToYUV() { return false; } |
| 302 virtual bool decodeToYUV() { return false; } | 272 virtual bool decodeToYUV() { return false; } |
| 303 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) {} | 273 virtual void setImagePlanes(std::unique_ptr<ImagePlanes>) {} |
| 304 | 274 |
| 305 protected: | 275 protected: |
| 306 ImageDecoder(AlphaOption alphaOption, | 276 ImageDecoder(AlphaOption alphaOption, |
| 307 ColorSpaceOption colorOptions, | 277 const ColorBehavior& colorBehavior, |
| 308 sk_sp<SkColorSpace> targetColorSpace, | |
| 309 size_t maxDecodedBytes) | 278 size_t maxDecodedBytes) |
| 310 : m_premultiplyAlpha(alphaOption == AlphaPremultiplied), | 279 : m_premultiplyAlpha(alphaOption == AlphaPremultiplied), |
| 311 m_colorSpaceOption(colorOptions), | 280 m_colorBehavior(colorBehavior), |
| 312 m_targetColorSpace(std::move(targetColorSpace)), | |
| 313 m_maxDecodedBytes(maxDecodedBytes), | 281 m_maxDecodedBytes(maxDecodedBytes), |
| 314 m_purgeAggressively(false) {} | 282 m_purgeAggressively(false) {} |
| 315 | 283 |
| 316 // Calculates the most recent frame whose image data may be needed in | 284 // Calculates the most recent frame whose image data may be needed in |
| 317 // order to decode frame |frameIndex|, based on frame disposal methods | 285 // order to decode frame |frameIndex|, based on frame disposal methods |
| 318 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether | 286 // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether |
| 319 // the rectangle of frame at |frameIndex| is known to be opaque. | 287 // the rectangle of frame at |frameIndex| is known to be opaque. |
| 320 // If no previous frame's data is required, returns WTF::kNotFound. | 288 // If no previous frame's data is required, returns WTF::kNotFound. |
| 321 // | 289 // |
| 322 // This function requires that the previous frame's | 290 // This function requires that the previous frame's |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 // the whole frame is opaque. | 346 // the whole frame is opaque. |
| 379 // | 347 // |
| 380 // This method should be called by the GIF and PNG decoder when the pixels in | 348 // This method should be called by the GIF and PNG decoder when the pixels in |
| 381 // the frame rect do *not* contain any transparent pixels. Before calling | 349 // the frame rect do *not* contain any transparent pixels. Before calling |
| 382 // this method, the caller must verify that the frame exists. | 350 // this method, the caller must verify that the frame exists. |
| 383 void correctAlphaWhenFrameBufferSawNoAlpha(size_t); | 351 void correctAlphaWhenFrameBufferSawNoAlpha(size_t); |
| 384 | 352 |
| 385 RefPtr<SegmentReader> m_data; // The encoded data. | 353 RefPtr<SegmentReader> m_data; // The encoded data. |
| 386 Vector<ImageFrame, 1> m_frameBufferCache; | 354 Vector<ImageFrame, 1> m_frameBufferCache; |
| 387 const bool m_premultiplyAlpha; | 355 const bool m_premultiplyAlpha; |
| 388 const ColorSpaceOption m_colorSpaceOption; | 356 const ColorBehavior m_colorBehavior; |
| 389 const sk_sp<SkColorSpace> m_targetColorSpace; | |
| 390 ImageOrientation m_orientation; | 357 ImageOrientation m_orientation; |
| 391 | 358 |
| 392 // The maximum amount of memory a decoded image should require. Ideally, | 359 // The maximum amount of memory a decoded image should require. Ideally, |
| 393 // image decoders should downsample large images to fit under this limit | 360 // image decoders should downsample large images to fit under this limit |
| 394 // (and then return the downsampled size from decodedSize()). Ignoring | 361 // (and then return the downsampled size from decodedSize()). Ignoring |
| 395 // this limit can cause excessive memory use or even crashes on low- | 362 // this limit can cause excessive memory use or even crashes on low- |
| 396 // memory devices. | 363 // memory devices. |
| 397 const size_t m_maxDecodedBytes; | 364 const size_t m_maxDecodedBytes; |
| 398 | 365 |
| 399 // While decoding, we may learn that there are so many animation frames that | 366 // While decoding, we may learn that there are so many animation frames that |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 bool m_hasHistogrammedColorSpace = false; | 419 bool m_hasHistogrammedColorSpace = false; |
| 453 | 420 |
| 454 sk_sp<SkColorSpace> m_embeddedColorSpace = nullptr; | 421 sk_sp<SkColorSpace> m_embeddedColorSpace = nullptr; |
| 455 bool m_sourceToTargetColorTransformNeedsUpdate = false; | 422 bool m_sourceToTargetColorTransformNeedsUpdate = false; |
| 456 std::unique_ptr<SkColorSpaceXform> m_sourceToTargetColorTransform; | 423 std::unique_ptr<SkColorSpaceXform> m_sourceToTargetColorTransform; |
| 457 }; | 424 }; |
| 458 | 425 |
| 459 } // namespace blink | 426 } // namespace blink |
| 460 | 427 |
| 461 #endif | 428 #endif |
| OLD | NEW |