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