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 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 21 | class SkScanlineDecoder; |
| 22 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 23 | /* |
| 24 | * |
| 25 | * This class implements the decoding for jpeg images |
| 26 | * |
| 27 | */ |
| 28 | class SkJpegCodec : public SkCodec { |
| 29 | public: |
| 30 | |
| 31 | /* |
| 32 | * Checks the start of the stream to see if the image is a jpeg |
| 33 | * Does not take ownership of the stream |
| 34 | */ |
| 35 | static bool IsJpeg(SkStream*); |
| 36 | |
| 37 | /* |
| 38 | * Assumes IsJpeg was called and returned true |
| 39 | * Creates a jpeg decoder |
| 40 | * Takes ownership of the stream |
| 41 | */ |
| 42 | static SkCodec* NewFromStream(SkStream*); |
| 43 | |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Assumes IsJpeg was called and returned true |
| 46 | * Creates a jpeg scanline decoder |
| 47 | * Takes ownership of the stream |
| 48 | */ |
| 49 | static SkScanlineDecoder* NewSDFromStream(SkStream*); |
| 50 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 51 | protected: |
| 52 | |
| 53 | /* |
| 54 | * Recommend a set of destination dimensions given a requested scale |
| 55 | */ |
| 56 | SkISize onGetScaledDimensions(float desiredScale) const override; |
| 57 | |
| 58 | /* |
| 59 | * Initiates the jpeg decode |
| 60 | */ |
| 61 | Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&, |
| 62 | SkPMColor*, int*) override; |
| 63 | |
| 64 | SkEncodedFormat onGetEncodedFormat() const override { |
| 65 | return kJPEG_SkEncodedFormat; |
| 66 | } |
| 67 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 68 | bool onRewind() override; |
| 69 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 70 | private: |
| 71 | |
| 72 | /* |
| 73 | * Read enough of the stream to initialize the SkJpegCodec. |
| 74 | * Returns a bool representing success or failure. |
| 75 | * |
| 76 | * @param codecOut |
| 77 | * If this returns true, and codecOut was not NULL, |
| 78 | * codecOut will be set to a new SkJpegCodec. |
| 79 | * |
| 80 | * @param decoderMgrOut |
| 81 | * If this returns true, and codecOut was NULL, |
| 82 | * decoderMgrOut must be non-NULL and decoderMgrOut will be set to a new |
| 83 | * JpegDecoderMgr pointer. |
| 84 | * |
| 85 | * @param stream |
| 86 | * Deleted on failure. |
| 87 | * codecOut will take ownership of it in the case where we created a codec. |
| 88 | * Ownership is unchanged when we set decoderMgrOut. |
| 89 | * |
| 90 | */ |
| 91 | static bool ReadHeader(SkStream* stream, SkCodec** codecOut, |
| 92 | JpegDecoderMgr** decoderMgrOut); |
| 93 | |
| 94 | /* |
| 95 | * Creates an instance of the decoder |
| 96 | * Called only by NewFromStream |
| 97 | * |
| 98 | * @param srcInfo contains the source width and height |
| 99 | * @param stream the encoded image data |
| 100 | * @param decoderMgr holds decompress struct, src manager, and error manager |
| 101 | * takes ownership |
| 102 | */ |
| 103 | SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr); |
| 104 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 105 | /* |
msarett | 1c8a587 | 2015-07-07 08:50:01 -0700 | [diff] [blame] | 106 | * Checks if the conversion between the input image and the requested output |
| 107 | * image has been implemented |
| 108 | * Sets the output color space |
| 109 | */ |
| 110 | bool setOutputColorSpace(const SkImageInfo& dst); |
| 111 | |
| 112 | /* |
emmaleer | e5aaa07 | 2015-08-13 11:44:05 -0700 | [diff] [blame^] | 113 | * Checks if we can scale to the requested dimensions and scales the dimensions |
| 114 | * if possible |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 115 | */ |
emmaleer | e5aaa07 | 2015-08-13 11:44:05 -0700 | [diff] [blame^] | 116 | bool scaleToDimensions(uint32_t width, uint32_t height); |
| 117 | |
| 118 | /* |
| 119 | * Create the swizzler based on the encoded format |
| 120 | */ |
| 121 | void initializeSwizzler(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
| 122 | const Options& options); |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 123 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 124 | SkAutoTDelete<JpegDecoderMgr> fDecoderMgr; |
emmaleer | e5aaa07 | 2015-08-13 11:44:05 -0700 | [diff] [blame^] | 125 | |
msarett | 97fdea6 | 2015-04-29 08:17:15 -0700 | [diff] [blame] | 126 | friend class SkJpegScanlineDecoder; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 127 | |
| 128 | typedef SkCodec INHERITED; |
| 129 | }; |
| 130 | |
| 131 | #endif |