blob: 7aa275ce4efd6028b69eb48d10de69e8a03a6f9b [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"
msarettad8bcfe2016-03-07 07:09:03 -080012#include "SkColorSpace.h"
msarette16b04a2015-04-15 07:32:19 -070013#include "SkImageInfo.h"
msarett39b2d5a2016-02-17 08:26:31 -080014#include "SkSwizzler.h"
msarette16b04a2015-04-15 07:32:19 -070015#include "SkStream.h"
scroggo565901d2015-12-10 10:44:13 -080016#include "SkTemplates.h"
msarette16b04a2015-04-15 07:32:19 -070017
msarett39b2d5a2016-02-17 08:26:31 -080018class JpegDecoderMgr;
msarette16b04a2015-04-15 07:32:19 -070019
20/*
21 *
22 * This class implements the decoding for jpeg images
23 *
24 */
25class SkJpegCodec : public SkCodec {
26public:
scroggodb30be22015-12-08 18:54:13 -080027 static bool IsJpeg(const void*, size_t);
msarette16b04a2015-04-15 07:32:19 -070028
29 /*
30 * Assumes IsJpeg was called and returned true
31 * Creates a jpeg decoder
32 * Takes ownership of the stream
33 */
34 static SkCodec* NewFromStream(SkStream*);
35
36protected:
37
38 /*
39 * Recommend a set of destination dimensions given a requested scale
40 */
41 SkISize onGetScaledDimensions(float desiredScale) const override;
42
43 /*
44 * Initiates the jpeg decode
45 */
46 Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
msarette6dd0042015-10-09 11:07:34 -070047 SkPMColor*, int*, int*) override;
msarette16b04a2015-04-15 07:32:19 -070048
msarett4984c3c2016-03-10 05:44:43 -080049 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
msarettb714fb02016-01-22 14:46:42 -080050
msarett4984c3c2016-03-10 05:44:43 -080051 Result onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override;
msarettb714fb02016-01-22 14:46:42 -080052
msarette16b04a2015-04-15 07:32:19 -070053 SkEncodedFormat onGetEncodedFormat() const override {
54 return kJPEG_SkEncodedFormat;
55 }
56
scroggob427db12015-08-12 07:24:13 -070057 bool onRewind() override;
58
scroggoe7fc14b2015-10-02 13:14:46 -070059 bool onDimensionsSupported(const SkISize&) override;
60
msarett9876ac52016-06-01 14:47:18 -070061 sk_sp<SkData> getICCData() const override { return fICCData; }
62
msarette16b04a2015-04-15 07:32:19 -070063private:
64
65 /*
66 * Read enough of the stream to initialize the SkJpegCodec.
67 * Returns a bool representing success or failure.
68 *
69 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -070070 * If this returns true, and codecOut was not nullptr,
msarette16b04a2015-04-15 07:32:19 -070071 * codecOut will be set to a new SkJpegCodec.
72 *
73 * @param decoderMgrOut
halcanary96fcdcc2015-08-27 07:41:13 -070074 * If this returns true, and codecOut was nullptr,
75 * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new
msarette16b04a2015-04-15 07:32:19 -070076 * 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 *
msarettc30c4182016-04-20 11:53:35 -070091 * @param info contains properties of the encoded data
msarette16b04a2015-04-15 07:32:19 -070092 * @param stream the encoded image data
93 * @param decoderMgr holds decompress struct, src manager, and error manager
94 * takes ownership
95 */
msarettc30c4182016-04-20 11:53:35 -070096 SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
msarett9876ac52016-06-01 14:47:18 -070097 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin,
98 sk_sp<SkData> iccData);
msarette16b04a2015-04-15 07:32:19 -070099
msarett97fdea62015-04-29 08:17:15 -0700100 /*
msarett1c8a5872015-07-07 08:50:01 -0700101 * Checks if the conversion between the input image and the requested output
msarett39979d82016-07-28 17:11:18 -0700102 * image has been implemented
103 * Sets the output color space
msarett1c8a5872015-07-07 08:50:01 -0700104 */
msarett39979d82016-07-28 17:11:18 -0700105 bool setOutputColorSpace(const SkImageInfo& dst);
msarett1c8a5872015-07-07 08:50:01 -0700106
msarett39979d82016-07-28 17:11:18 -0700107 // scanline decoding
msarettfdb47572015-10-13 12:50:14 -0700108 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
msarette6dd0042015-10-09 11:07:34 -0700109 SkSampler* getSampler(bool createIfNecessary) override;
scroggo46c57472015-09-30 08:57:13 -0700110 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
msarettfdb47572015-10-13 12:50:14 -0700111 SkPMColor ctable[], int* ctableCount) override;
msarette6dd0042015-10-09 11:07:34 -0700112 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
113 bool onSkipScanlines(int count) override;
scroggo46c57472015-09-30 08:57:13 -0700114
msarett39979d82016-07-28 17:11:18 -0700115 SkAutoTDelete<JpegDecoderMgr> fDecoderMgr;
msarettfbccb592015-09-01 06:43:41 -0700116 // We will save the state of the decompress struct after reading the header.
117 // This allows us to safely call onGetScaledDimensions() at any time.
msarett39979d82016-07-28 17:11:18 -0700118 const int fReadyState;
msarette16b04a2015-04-15 07:32:19 -0700119
msarett39979d82016-07-28 17:11:18 -0700120 // scanline decoding
121 SkAutoTMalloc<uint8_t> fStorage; // Only used if sampling is needed
122 uint8_t* fSrcRow; // Only used if sampling is needed
msarett91c22b22016-02-22 12:27:46 -0800123 // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo
124 // cannot take the exact the subset that we need, we will use the swizzler
125 // to further subset the output from libjpeg-turbo.
msarett39979d82016-07-28 17:11:18 -0700126 SkIRect fSwizzlerSubset;
127 SkAutoTDelete<SkSwizzler> fSwizzler;
scroggo46c57472015-09-30 08:57:13 -0700128
msarett39979d82016-07-28 17:11:18 -0700129 sk_sp<SkData> fICCData;
msarett9876ac52016-06-01 14:47:18 -0700130
msarette16b04a2015-04-15 07:32:19 -0700131 typedef SkCodec INHERITED;
132};
133
134#endif