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