blob: 51a741a525aa0936442aa7ac50c179f6e07db5fb [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"
14#include "SkJpegUtility.h"
15#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
59private:
60
61 /*
62 * Read enough of the stream to initialize the SkJpegCodec.
63 * Returns a bool representing success or failure.
64 *
65 * @param codecOut
66 * If this returns true, and codecOut was not NULL,
67 * codecOut will be set to a new SkJpegCodec.
68 *
69 * @param decoderMgrOut
70 * If this returns true, and codecOut was NULL,
71 * decoderMgrOut must be non-NULL and decoderMgrOut will be set to a new
72 * JpegDecoderMgr pointer.
73 *
74 * @param stream
75 * Deleted on failure.
76 * codecOut will take ownership of it in the case where we created a codec.
77 * Ownership is unchanged when we set decoderMgrOut.
78 *
79 */
80 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
81 JpegDecoderMgr** decoderMgrOut);
82
83 /*
84 * Creates an instance of the decoder
85 * Called only by NewFromStream
86 *
87 * @param srcInfo contains the source width and height
88 * @param stream the encoded image data
89 * @param decoderMgr holds decompress struct, src manager, and error manager
90 * takes ownership
91 */
92 SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr);
93
94 SkAutoTDelete<JpegDecoderMgr> fDecoderMgr;
95
96 typedef SkCodec INHERITED;
97};
98
99#endif