blob: 122c6e445325326e3e83547bf0534378be8fcdab [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
21/*
22 *
23 * This class implements the decoding for jpeg images
24 *
25 */
26class SkJpegCodec : public SkCodec {
27public:
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
42protected:
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&,
53 SkPMColor*, int*) override;
54
55 SkEncodedFormat onGetEncodedFormat() const override {
56 return kJPEG_SkEncodedFormat;
57 }
58
msarett97fdea62015-04-29 08:17:15 -070059 SkScanlineDecoder* onGetScanlineDecoder(const SkImageInfo& dstInfo, const Options& options,
60 SkPMColor ctable[], int* ctableCount) override;
61
msarette16b04a2015-04-15 07:32:19 -070062private:
63
64 /*
65 * Read enough of the stream to initialize the SkJpegCodec.
66 * Returns a bool representing success or failure.
67 *
68 * @param codecOut
69 * If this returns true, and codecOut was not NULL,
70 * codecOut will be set to a new SkJpegCodec.
71 *
72 * @param decoderMgrOut
73 * If this returns true, and codecOut was NULL,
74 * decoderMgrOut must be non-NULL and decoderMgrOut will be set to a new
75 * JpegDecoderMgr pointer.
76 *
77 * @param stream
78 * Deleted on failure.
79 * codecOut will take ownership of it in the case where we created a codec.
80 * Ownership is unchanged when we set decoderMgrOut.
81 *
82 */
83 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
84 JpegDecoderMgr** decoderMgrOut);
85
86 /*
87 * Creates an instance of the decoder
88 * Called only by NewFromStream
89 *
90 * @param srcInfo contains the source width and height
91 * @param stream the encoded image data
92 * @param decoderMgr holds decompress struct, src manager, and error manager
93 * takes ownership
94 */
95 SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr);
96
msarett97fdea62015-04-29 08:17:15 -070097 /*
msarettc0e80c12015-07-01 06:50:35 -070098 * Explicit destructor is used to ensure that the scanline decoder is deleted
99 * before the decode manager.
100 */
101 ~SkJpegCodec() override;
102
103 /*
msarett97fdea62015-04-29 08:17:15 -0700104 * Handles rewinding the input stream if it is necessary
105 */
106 bool handleRewind();
107
108 /*
msarett1c8a5872015-07-07 08:50:01 -0700109 * Checks if the conversion between the input image and the requested output
110 * image has been implemented
111 * Sets the output color space
112 */
113 bool setOutputColorSpace(const SkImageInfo& dst);
114
115 /*
msarett97fdea62015-04-29 08:17:15 -0700116 * Checks if we can scale to the requested dimensions and scales the dimensions
117 * if possible
118 */
119 bool scaleToDimensions(uint32_t width, uint32_t height);
120
121 /*
122 * Create the swizzler based on the encoded format
123 */
124 void initializeSwizzler(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes,
125 const Options& options);
126
msarette16b04a2015-04-15 07:32:19 -0700127 SkAutoTDelete<JpegDecoderMgr> fDecoderMgr;
msarett97fdea62015-04-29 08:17:15 -0700128
129 friend class SkJpegScanlineDecoder;
msarette16b04a2015-04-15 07:32:19 -0700130
131 typedef SkCodec INHERITED;
132};
133
134#endif