blob: 9e08c869cf31327f4eef3778fc7ad1a493989cff [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"
msarett50ce1f22016-07-29 06:23:33 -070013#include "SkColorSpaceXform.h"
msarette16b04a2015-04-15 07:32:19 -070014#include "SkImageInfo.h"
msarett39b2d5a2016-02-17 08:26:31 -080015#include "SkSwizzler.h"
msarette16b04a2015-04-15 07:32:19 -070016#include "SkStream.h"
scroggo565901d2015-12-10 10:44:13 -080017#include "SkTemplates.h"
msarette16b04a2015-04-15 07:32:19 -070018
msarett39b2d5a2016-02-17 08:26:31 -080019class JpegDecoderMgr;
msarette16b04a2015-04-15 07:32:19 -070020
21/*
22 *
23 * This class implements the decoding for jpeg images
24 *
25 */
26class SkJpegCodec : public SkCodec {
27public:
scroggodb30be22015-12-08 18:54:13 -080028 static bool IsJpeg(const void*, size_t);
msarette16b04a2015-04-15 07:32:19 -070029
30 /*
31 * Assumes IsJpeg was called and returned true
32 * Creates a jpeg decoder
33 * Takes ownership of the stream
34 */
35 static SkCodec* NewFromStream(SkStream*);
36
37protected:
38
39 /*
40 * Recommend a set of destination dimensions given a requested scale
41 */
42 SkISize onGetScaledDimensions(float desiredScale) const override;
43
44 /*
45 * Initiates the jpeg decode
46 */
47 Result onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, const Options&,
msarette6dd0042015-10-09 11:07:34 -070048 SkPMColor*, int*, int*) override;
msarette16b04a2015-04-15 07:32:19 -070049
msarett4984c3c2016-03-10 05:44:43 -080050 bool onQueryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const override;
msarettb714fb02016-01-22 14:46:42 -080051
msarett4984c3c2016-03-10 05:44:43 -080052 Result onGetYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) override;
msarettb714fb02016-01-22 14:46:42 -080053
msarette16b04a2015-04-15 07:32:19 -070054 SkEncodedFormat onGetEncodedFormat() const override {
55 return kJPEG_SkEncodedFormat;
56 }
57
scroggob427db12015-08-12 07:24:13 -070058 bool onRewind() override;
59
scroggoe7fc14b2015-10-02 13:14:46 -070060 bool onDimensionsSupported(const SkISize&) 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
halcanary96fcdcc2015-08-27 07:41:13 -070069 * If this returns true, and codecOut was not nullptr,
msarette16b04a2015-04-15 07:32:19 -070070 * codecOut will be set to a new SkJpegCodec.
71 *
72 * @param decoderMgrOut
halcanary96fcdcc2015-08-27 07:41:13 -070073 * If this returns true, and codecOut was nullptr,
74 * decoderMgrOut must be non-nullptr and decoderMgrOut will be set to a new
msarette16b04a2015-04-15 07:32:19 -070075 * 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 *
msarettc30c4182016-04-20 11:53:35 -070090 * @param info contains properties of the encoded data
msarette16b04a2015-04-15 07:32:19 -070091 * @param stream the encoded image data
92 * @param decoderMgr holds decompress struct, src manager, and error manager
93 * takes ownership
94 */
msarettc30c4182016-04-20 11:53:35 -070095 SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
Matt Saretta9e9bfc2016-11-01 12:19:50 -040096 JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin);
msarette16b04a2015-04-15 07:32:19 -070097
msarett97fdea62015-04-29 08:17:15 -070098 /*
msarett1c8a5872015-07-07 08:50:01 -070099 * Checks if the conversion between the input image and the requested output
msarett50ce1f22016-07-29 06:23:33 -0700100 * image has been implemented.
101 *
102 * Sets the output color space.
msarett1c8a5872015-07-07 08:50:01 -0700103 */
msarett2ecc35f2016-09-08 11:55:16 -0700104 bool setOutputColorSpace(const SkImageInfo& dst);
msarett1c8a5872015-07-07 08:50:01 -0700105
msarettfdb47572015-10-13 12:50:14 -0700106 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
msarett50ce1f22016-07-29 06:23:33 -0700107 void allocateStorage(const SkImageInfo& dstInfo);
108 int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count);
109
110 /*
111 * Scanline decoding.
112 */
msarette6dd0042015-10-09 11:07:34 -0700113 SkSampler* getSampler(bool createIfNecessary) override;
scroggo46c57472015-09-30 08:57:13 -0700114 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
msarettfdb47572015-10-13 12:50:14 -0700115 SkPMColor ctable[], int* ctableCount) override;
msarette6dd0042015-10-09 11:07:34 -0700116 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
117 bool onSkipScanlines(int count) override;
scroggo46c57472015-09-30 08:57:13 -0700118
Ben Wagner145dbcd2016-11-03 14:40:50 -0400119 std::unique_ptr<JpegDecoderMgr> fDecoderMgr;
msarett50ce1f22016-07-29 06:23:33 -0700120
msarettfbccb592015-09-01 06:43:41 -0700121 // We will save the state of the decompress struct after reading the header.
122 // This allows us to safely call onGetScaledDimensions() at any time.
msarett50ce1f22016-07-29 06:23:33 -0700123 const int fReadyState;
msarette16b04a2015-04-15 07:32:19 -0700124
msarett50ce1f22016-07-29 06:23:33 -0700125
126 SkAutoTMalloc<uint8_t> fStorage;
127 uint8_t* fSwizzleSrcRow;
128 uint32_t* fColorXformSrcRow;
129
msarett91c22b22016-02-22 12:27:46 -0800130 // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo
131 // cannot take the exact the subset that we need, we will use the swizzler
132 // to further subset the output from libjpeg-turbo.
msarett50ce1f22016-07-29 06:23:33 -0700133 SkIRect fSwizzlerSubset;
134
Ben Wagner145dbcd2016-11-03 14:40:50 -0400135 std::unique_ptr<SkSwizzler> fSwizzler;
msarett9876ac52016-06-01 14:47:18 -0700136
msarette16b04a2015-04-15 07:32:19 -0700137 typedef SkCodec INHERITED;
138};
139
140#endif