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: |
| 28 | |
| 29 | /* |
| 30 | * Checks the start of the stream to see if the image is a jpeg |
| 31 | * Does not take ownership of the stream |
| 32 | */ |
| 33 | static bool IsJpeg(SkStream*); |
| 34 | |
| 35 | /* |
| 36 | * Assumes IsJpeg was called and returned true |
| 37 | * Creates a jpeg decoder |
| 38 | * Takes ownership of the stream |
| 39 | */ |
| 40 | static SkCodec* NewFromStream(SkStream*); |
| 41 | |
| 42 | protected: |
| 43 | |
| 44 | /* |
| 45 | * Recommend a set of destination dimensions given a requested scale |
| 46 | */ |
| 47 | SkISize onGetScaledDimensions(float desiredScale) const override; |
| 48 | |
| 49 | /* |
| 50 | * Initiates the jpeg decode |
| 51 | */ |
| 52 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame^] | 53 | SkPMColor*, int*, int*) override; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 54 | |
| 55 | SkEncodedFormat onGetEncodedFormat() const override { |
| 56 | return kJPEG_SkEncodedFormat; |
| 57 | } |
| 58 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 59 | bool onRewind() override; |
| 60 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 61 | bool onDimensionsSupported(const SkISize&) override; |
| 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 | * |
| 91 | * @param srcInfo contains the source width and height |
| 92 | * @param stream the encoded image data |
| 93 | * @param decoderMgr holds decompress struct, src manager, and error manager |
| 94 | * takes ownership |
| 95 | */ |
| 96 | SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr); |
| 97 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 98 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 99 | * Checks if the conversion between the input image and the requested output |
| 100 | * image has been implemented |
| 101 | * Sets the output color space |
| 102 | */ |
| 103 | bool setOutputColorSpace(const SkImageInfo& dst); |
| 104 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 105 | // scanline decoding |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame^] | 106 | SkSampler* getSampler(bool createIfNecessary) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 107 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
| 108 | SkPMColor ctable[], int* ctableCount) override; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame^] | 109 | int onGetScanlines(void* dst, int count, size_t rowBytes) override; |
| 110 | bool onSkipScanlines(int count) override; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 111 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 112 | SkAutoTDelete<JpegDecoderMgr> fDecoderMgr; |
msarett | fbccb59 | 2015-09-01 06:43:41 -0700 | [diff] [blame] | 113 | // We will save the state of the decompress struct after reading the header. |
| 114 | // This allows us to safely call onGetScaledDimensions() at any time. |
| 115 | const int fReadyState; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 116 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 117 | // scanline decoding |
| 118 | SkAutoMalloc fStorage; // Only used if sampling is needed |
| 119 | uint8_t* fSrcRow; // Only used if sampling is needed |
| 120 | SkAutoTDelete<SkSwizzler> fSwizzler; |
| 121 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 122 | typedef SkCodec INHERITED; |
| 123 | }; |
| 124 | |
| 125 | #endif |