msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkJpegCodec_DEFINED |
| 9 | #define SkJpegCodec_DEFINED |
| 10 | |
| 11 | #include "SkCodec.h" |
| 12 | #include "SkImageInfo.h" |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 13 | #include "SkSwizzler.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 15 | #include "SkTemplates.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 16 | |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 17 | class JpegDecoderMgr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * |
| 21 | * This class implements the decoding for jpeg images |
| 22 | * |
| 23 | */ |
| 24 | class SkJpegCodec : public SkCodec { |
| 25 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 26 | static bool IsJpeg(const void*, size_t); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Assumes IsJpeg was called and returned true |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 30 | * Takes ownership of the stream |
| 31 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 32 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 33 | |
| 34 | protected: |
| 35 | |
| 36 | /* |
| 37 | * Recommend a set of destination dimensions given a requested scale |
| 38 | */ |
| 39 | SkISize onGetScaledDimensions(float desiredScale) const override; |
| 40 | |
| 41 | /* |
| 42 | * Initiates the jpeg decode |
| 43 | */ |
| 44 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 45 | int*) override; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 46 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 47 | bool onQueryYUV8(SkYUVASizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override; |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 48 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 49 | Result onGetYUV8Planes(const SkYUVASizeInfo& sizeInfo, |
| 50 | void* planes[SkYUVASizeInfo::kMaxCount]) override; |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 51 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 52 | SkEncodedImageFormat onGetEncodedFormat() const override { |
| 53 | return SkEncodedImageFormat::kJPEG; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 54 | } |
| 55 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 56 | bool onRewind() override; |
| 57 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 58 | bool onDimensionsSupported(const SkISize&) override; |
| 59 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 60 | bool conversionSupported(const SkImageInfo&, bool, bool) override; |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 61 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 62 | private: |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 63 | /* |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 64 | * Allows SkRawCodec to communicate the color profile from the exif data. |
Matt Sarett | c5eabe7 | 2017-02-24 14:51:08 -0500 | [diff] [blame] | 65 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 66 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*, |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 67 | std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile); |
Matt Sarett | c5eabe7 | 2017-02-24 14:51:08 -0500 | [diff] [blame] | 68 | |
| 69 | /* |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 70 | * Read enough of the stream to initialize the SkJpegCodec. |
| 71 | * Returns a bool representing success or failure. |
| 72 | * |
| 73 | * @param codecOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | * If this returns true, and codecOut was not nullptr, |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 75 | * codecOut will be set to a new SkJpegCodec. |
| 76 | * |
| 77 | * @param decoderMgrOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | * If this returns true, and codecOut was nullptr, |
| 79 | * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 80 | * JpegDecoderMgr pointer. |
| 81 | * |
| 82 | * @param stream |
| 83 | * Deleted on failure. |
| 84 | * codecOut will take ownership of it in the case where we created a codec. |
| 85 | * Ownership is unchanged when we set decoderMgrOut. |
| 86 | * |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 87 | * @param defaultColorProfile |
| 88 | * If the jpeg does not have an embedded color profile, the image data should |
| 89 | * be tagged with this color profile. |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 90 | */ |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 91 | static Result ReadHeader(SkStream* stream, SkCodec** codecOut, |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 92 | JpegDecoderMgr** decoderMgrOut, |
| 93 | std::unique_ptr<SkEncodedInfo::ICCProfile> defaultColorProfile); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Creates an instance of the decoder |
| 97 | * Called only by NewFromStream |
| 98 | * |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 99 | * @param info contains properties of the encoded data |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 100 | * @param stream the encoded image data |
| 101 | * @param decoderMgr holds decompress struct, src manager, and error manager |
| 102 | * takes ownership |
| 103 | */ |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 104 | SkJpegCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, |
| 105 | JpegDecoderMgr* decoderMgr, SkEncodedOrigin origin); |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 106 | |
Matt Sarett | 7f15b68 | 2017-02-24 17:22:09 -0500 | [diff] [blame] | 107 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options, |
| 108 | bool needsCMYKToRGB); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 109 | void allocateStorage(const SkImageInfo& dstInfo); |
Matt Sarett | c8c901f | 2017-01-24 16:16:33 -0500 | [diff] [blame] | 110 | int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, const Options&); |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Scanline decoding. |
| 114 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 115 | SkSampler* getSampler(bool createIfNecessary) override; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 116 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 117 | const Options& options) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 118 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 119 | bool onSkipScanlines(int count) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 120 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 121 | std::unique_ptr<JpegDecoderMgr> fDecoderMgr; |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 122 | |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 123 | // We will save the state of the decompress struct after reading the header. |
| 124 | // This allows us to safely call onGetScaledDimensions() at any time. |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 125 | const int fReadyState; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 126 | |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 127 | |
| 128 | SkAutoTMalloc<uint8_t> fStorage; |
| 129 | uint8_t* fSwizzleSrcRow; |
| 130 | uint32_t* fColorXformSrcRow; |
| 131 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 132 | // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo |
| 133 | // cannot take the exact the subset that we need, we will use the swizzler |
| 134 | // to further subset the output from libjpeg-turbo. |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 135 | SkIRect fSwizzlerSubset; |
| 136 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 137 | std::unique_ptr<SkSwizzler> fSwizzler; |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 138 | |
Matt Sarett | c5eabe7 | 2017-02-24 14:51:08 -0500 | [diff] [blame] | 139 | friend class SkRawCodec; |
| 140 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 141 | typedef SkCodec INHERITED; |
| 142 | }; |
| 143 | |
| 144 | #endif |