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 | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 14 | #include "SkSwizzler.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 15 | #include "SkStream.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 16 | #include "SkTemplates.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 17 | |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 18 | class JpegDecoderMgr; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * |
| 22 | * This class implements the decoding for jpeg images |
| 23 | * |
| 24 | */ |
| 25 | class SkJpegCodec : public SkCodec { |
| 26 | public: |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 27 | static bool IsJpeg(const void*, size_t); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * Assumes IsJpeg was called and returned true |
| 31 | * Creates a jpeg decoder |
| 32 | * Takes ownership of the stream |
| 33 | */ |
| 34 | static SkCodec* NewFromStream(SkStream*); |
| 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&, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 47 | SkPMColor*, int*, 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 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 53 | SkEncodedFormat onGetEncodedFormat() const override { |
| 54 | return kJPEG_SkEncodedFormat; |
| 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 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 61 | sk_sp<SkData> getICCData() const override { return fICCData; } |
| 62 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 63 | private: |
| 64 | |
| 65 | /* |
| 66 | * Read enough of the stream to initialize the SkJpegCodec. |
| 67 | * Returns a bool representing success or failure. |
| 68 | * |
| 69 | * @param codecOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | * If this returns true, and codecOut was not nullptr, |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 71 | * codecOut will be set to a new SkJpegCodec. |
| 72 | * |
| 73 | * @param decoderMgrOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | * If this returns true, and codecOut was nullptr, |
| 75 | * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 76 | * JpegDecoderMgr pointer. |
| 77 | * |
| 78 | * @param stream |
| 79 | * Deleted on failure. |
| 80 | * codecOut will take ownership of it in the case where we created a codec. |
| 81 | * Ownership is unchanged when we set decoderMgrOut. |
| 82 | * |
| 83 | */ |
| 84 | static bool ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 85 | JpegDecoderMgr** decoderMgrOut); |
| 86 | |
| 87 | /* |
| 88 | * Creates an instance of the decoder |
| 89 | * Called only by NewFromStream |
| 90 | * |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 91 | * @param info contains properties of the encoded data |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 92 | * @param stream the encoded image data |
| 93 | * @param decoderMgr holds decompress struct, src manager, and error manager |
| 94 | * takes ownership |
| 95 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 96 | SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream, |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 97 | JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin, |
| 98 | sk_sp<SkData> iccData); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 99 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 100 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 101 | * Checks if the conversion between the input image and the requested output |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 102 | * image has been implemented |
| 103 | * Sets the output color space |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 104 | */ |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 105 | bool setOutputColorSpace(const SkImageInfo& dst); |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 106 | |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 107 | // scanline decoding |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 108 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 109 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 110 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 111 | SkPMColor ctable[], int* ctableCount) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 112 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 113 | bool onSkipScanlines(int count) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 114 | |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 115 | SkAutoTDelete<JpegDecoderMgr> fDecoderMgr; |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 116 | // We will save the state of the decompress struct after reading the header. |
| 117 | // This allows us to safely call onGetScaledDimensions() at any time. |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 118 | const int fReadyState; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 119 | |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 120 | // scanline decoding |
| 121 | SkAutoTMalloc<uint8_t> fStorage; // Only used if sampling is needed |
| 122 | uint8_t* fSrcRow; // Only used if sampling is needed |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 123 | // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo |
| 124 | // cannot take the exact the subset that we need, we will use the swizzler |
| 125 | // to further subset the output from libjpeg-turbo. |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 126 | SkIRect fSwizzlerSubset; |
| 127 | SkAutoTDelete<SkSwizzler> fSwizzler; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 128 | |
msarett | 39979d8 | 2016-07-28 17:11:18 -0700 | [diff] [blame] | 129 | sk_sp<SkData> fICCData; |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 130 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 131 | typedef SkCodec INHERITED; |
| 132 | }; |
| 133 | |
| 134 | #endif |