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 |
| 30 | * Creates a jpeg decoder |
| 31 | * Takes ownership of the stream |
| 32 | */ |
| 33 | static SkCodec* NewFromStream(SkStream*); |
| 34 | |
| 35 | protected: |
| 36 | |
| 37 | /* |
| 38 | * Recommend a set of destination dimensions given a requested scale |
| 39 | */ |
| 40 | SkISize onGetScaledDimensions(float desiredScale) const override; |
| 41 | |
| 42 | /* |
| 43 | * Initiates the jpeg decode |
| 44 | */ |
| 45 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 46 | SkPMColor*, int*, int*) override; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 47 | |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 48 | bool onQueryYUV8(YUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override; |
| 49 | |
| 50 | Result onGetYUV8Planes(const YUVSizeInfo& sizeInfo, void* pixels[3]) override; |
| 51 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 52 | SkEncodedFormat onGetEncodedFormat() const override { |
| 53 | return kJPEG_SkEncodedFormat; |
| 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 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 60 | private: |
| 61 | |
| 62 | /* |
| 63 | * Read enough of the stream to initialize the SkJpegCodec. |
| 64 | * Returns a bool representing success or failure. |
| 65 | * |
| 66 | * @param codecOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | * If this returns true, and codecOut was not nullptr, |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 68 | * codecOut will be set to a new SkJpegCodec. |
| 69 | * |
| 70 | * @param decoderMgrOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 71 | * If this returns true, and codecOut was nullptr, |
| 72 | * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 73 | * JpegDecoderMgr pointer. |
| 74 | * |
| 75 | * @param stream |
| 76 | * Deleted on failure. |
| 77 | * codecOut will take ownership of it in the case where we created a codec. |
| 78 | * Ownership is unchanged when we set decoderMgrOut. |
| 79 | * |
| 80 | */ |
| 81 | static bool ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 82 | JpegDecoderMgr** decoderMgrOut); |
| 83 | |
| 84 | /* |
| 85 | * Creates an instance of the decoder |
| 86 | * Called only by NewFromStream |
| 87 | * |
| 88 | * @param srcInfo contains the source width and height |
| 89 | * @param stream the encoded image data |
| 90 | * @param decoderMgr holds decompress struct, src manager, and error manager |
| 91 | * takes ownership |
| 92 | */ |
| 93 | SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr); |
| 94 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 95 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 96 | * Checks if the conversion between the input image and the requested output |
| 97 | * image has been implemented |
| 98 | * Sets the output color space |
| 99 | */ |
| 100 | bool setOutputColorSpace(const SkImageInfo& dst); |
| 101 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 102 | // scanline decoding |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 103 | void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 104 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 105 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 106 | SkPMColor ctable[], int* ctableCount) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 107 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 108 | bool onSkipScanlines(int count) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 109 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 110 | SkAutoTDelete<JpegDecoderMgr> fDecoderMgr; |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 111 | // We will save the state of the decompress struct after reading the header. |
| 112 | // This allows us to safely call onGetScaledDimensions() at any time. |
| 113 | const int fReadyState; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 114 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 115 | // scanline decoding |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 116 | SkAutoTMalloc<uint8_t> fStorage; // Only used if sampling is needed |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 117 | uint8_t* fSrcRow; // Only used if sampling is needed |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame^] | 118 | // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo |
| 119 | // cannot take the exact the subset that we need, we will use the swizzler |
| 120 | // to further subset the output from libjpeg-turbo. |
| 121 | SkIRect fSwizzlerSubset; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 122 | SkAutoTDelete<SkSwizzler> fSwizzler; |
| 123 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 124 | typedef SkCodec INHERITED; |
| 125 | }; |
| 126 | |
| 127 | #endif |