blob: 38e41e757cbe82334cb845d313cc54bd2ede1c68 [file] [log] [blame]
msarette16b04a2015-04-15 07:32:19 -07001/*
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"
mtklein525e90a2015-06-18 09:58:57 -070014#include "SkJpegUtility_codec.h"
msarette16b04a2015-04-15 07:32:19 -070015#include "SkStream.h"
16
17extern "C" {
18 #include "jpeglib.h"
19}
20
scroggo1c005e42015-08-04 09:24:45 -070021class SkScanlineDecoder;
22
msarette16b04a2015-04-15 07:32:19 -070023/*
24 *
25 * This class implements the decoding for jpeg images
26 *
27 */
28class SkJpegCodec : public SkCodec {
29public:
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
scroggo1c005e42015-08-04 09:24:45 -070044 /*
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
msarette16b04a2015-04-15 07:32:19 -070051protected:
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
scroggob427db12015-08-12 07:24:13 -070068 bool onRewind() override;
69
msarette16b04a2015-04-15 07:32:19 -070070private:
71
72 /*
73 * Read enough of the stream to initialize the SkJpegCodec.
74 * Returns a bool representing success or failure.
75 *
76 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -070077 * If this returns true, and codecOut was not nullptr,
msarette16b04a2015-04-15 07:32:19 -070078 * codecOut will be set to a new SkJpegCodec.
79 *
80 * @param decoderMgrOut
halcanary96fcdcc2015-08-27 07:41:13 -070081 * If this returns true, and codecOut was nullptr,
82 * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new
msarette16b04a2015-04-15 07:32:19 -070083 * 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
msarett97fdea62015-04-29 08:17:15 -0700105 /*
msarett1c8a5872015-07-07 08:50:01 -0700106 * 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 /*
emmaleer8f4ba762015-08-14 07:44:46 -0700113 * Checks if we can natively scale to the requested dimensions and natively scales the
114 * dimensions if possible
msarett97fdea62015-04-29 08:17:15 -0700115 */
emmaleer8f4ba762015-08-14 07:44:46 -0700116 bool nativelyScaleToDimensions(uint32_t width, uint32_t height);
msarett97fdea62015-04-29 08:17:15 -0700117
msarette16b04a2015-04-15 07:32:19 -0700118 SkAutoTDelete<JpegDecoderMgr> fDecoderMgr;
msarettfbccb592015-09-01 06:43:41 -0700119 // We will save the state of the decompress struct after reading the header.
120 // This allows us to safely call onGetScaledDimensions() at any time.
121 const int fReadyState;
emmaleer8f4ba762015-08-14 07:44:46 -0700122
msarett97fdea62015-04-29 08:17:15 -0700123 friend class SkJpegScanlineDecoder;
msarette16b04a2015-04-15 07:32:19 -0700124
125 typedef SkCodec INHERITED;
126};
127
128#endif