blob: d2b23a121691cdeb9279860e024658b66858ea92 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkImageDecoder_DEFINED
18#define SkImageDecoder_DEFINED
19
20#include "SkBitmap.h"
21#include "SkRefCnt.h"
22
23class SkStream;
24
25/** \class SkImageDecoder
26
27 Base class for decoding compressed images into a SkBitmap
28*/
29class SkImageDecoder {
30public:
31 virtual ~SkImageDecoder();
weita@google.com25e98342009-05-11 16:06:22 +000032
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 enum Format {
34 kUnknown_Format,
35 kBMP_Format,
36 kGIF_Format,
37 kICO_Format,
38 kJPEG_Format,
39 kPNG_Format,
40 kWBMP_Format,
weita@google.com25e98342009-05-11 16:06:22 +000041
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 kLastKnownFormat = kWBMP_Format
43 };
weita@google.com25e98342009-05-11 16:06:22 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 /** Return the compressed data's format (see Format enum)
46 */
47 virtual Format getFormat() const;
48
49 /** Returns true if the decoder should try to dither the resulting image.
50 The default setting is true.
51 */
52 bool getDitherImage() const { return fDitherImage; }
weita@google.com25e98342009-05-11 16:06:22 +000053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 /** Set to true if the the decoder should try to dither the resulting image.
55 The default setting is true.
56 */
57 void setDitherImage(bool dither) { fDitherImage = dither; }
58
59 /** \class Peeker
weita@google.com25e98342009-05-11 16:06:22 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 Base class for optional callbacks to retrieve meta/chunk data out of
62 an image as it is being decoded.
63 */
64 class Peeker : public SkRefCnt {
65 public:
66 /** Return true to continue decoding, or false to indicate an error, which
67 will cause the decoder to not return the image.
68 */
69 virtual bool peek(const char tag[], const void* data, size_t length) = 0;
70 };
71
72 Peeker* getPeeker() const { return fPeeker; }
73 Peeker* setPeeker(Peeker*);
weita@google.com25e98342009-05-11 16:06:22 +000074
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 /** \class Peeker
weita@google.com25e98342009-05-11 16:06:22 +000076
reed@android.com8a1c16f2008-12-17 15:59:43 +000077 Base class for optional callbacks to retrieve meta/chunk data out of
78 an image as it is being decoded.
79 */
80 class Chooser : public SkRefCnt {
81 public:
82 virtual void begin(int count) {}
83 virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
84 /** Return the index of the subimage you want, or -1 to choose none of them.
85 */
86 virtual int choose() = 0;
87 };
88
89 Chooser* getChooser() const { return fChooser; }
90 Chooser* setChooser(Chooser*);
91
92 SkBitmap::Allocator* getAllocator() const { return fAllocator; }
93 SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
94
95 // sample-size, if set to > 1, tells the decoder to return a smaller than
96 // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample
97 // size is set to 3, then the returned bitmap will be 1/3 as wide and high,
98 // and will contain 1/9 as many pixels as the original.
99 // Note: this is a hint, and the codec may choose to ignore this, or only
100 // approximate the sample size.
101 int getSampleSize() const { return fSampleSize; }
102 void setSampleSize(int size);
weita@google.com25e98342009-05-11 16:06:22 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 /** Reset the sampleSize to its default of 1
105 */
106 void resetSampleSize() { this->setSampleSize(1); }
107
108 /** Decoding is synchronous, but for long decodes, a different thread can
109 call this method safely. This sets a state that the decoders will
110 periodically check, and if they see it changed to cancel, they will
111 cancel. This will result in decode() returning false. However, there is
112 no guarantee that the decoder will see the state change in time, so
113 it is possible that cancelDecode() will be called, but will be ignored
114 and decode() will return true (assuming no other problems were
115 encountered).
weita@google.com25e98342009-05-11 16:06:22 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 This state is automatically reset at the beginning of decode().
118 */
119 void cancelDecode() {
120 // now the subclass must query shouldCancelDecode() to be informed
121 // of the request
122 fShouldCancelDecode = true;
123 }
124
125 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then
126 only the bitmap's width/height/config need be set. If kDecodePixels_Mode
127 is passed, then the bitmap must have pixels or a pixelRef.
128 */
129 enum Mode {
130 kDecodeBounds_Mode, //!< only return width/height/config in bitmap
131 kDecodePixels_Mode //!< return entire bitmap (including pixels)
132 };
weita@google.com25e98342009-05-11 16:06:22 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 /** Given a stream, decode it into the specified bitmap.
135 If the decoder can decompress the image, it calls bitmap.setConfig(),
136 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(),
137 which will allocated a pixelRef. To access the pixel memory, the codec
138 needs to call lockPixels/unlockPixels on the
139 bitmap. It can then set the pixels with the decompressed image.
weita@google.com25e98342009-05-11 16:06:22 +0000140 * If the image cannot be decompressed, return false. After the
141 * decoding, the function converts the decoded config in bitmap
142 * to pref if possible. Whether a conversion is feasible is
143 * tested by Bitmap::canCopyTo(pref).
144
145 note: document use of Allocator, Peeker and Chooser
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 */
147 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode);
148
149 /** Given a stream, this will try to find an appropriate decoder object.
150 If none is found, the method returns NULL.
151 */
152 static SkImageDecoder* Factory(SkStream*);
reed@android.coma14ea0e2009-03-17 17:59:53 +0000153
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 /** Decode the image stored in the specified file, and store the result
155 in bitmap. Return true for success or false on failure.
156
157 If pref is kNo_Config, then the decoder is free to choose the most natural
158 config given the image data. If pref something other than kNo_Config,
159 the decoder will attempt to decode the image into that format, unless
160 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
161 config does not support that), in which case the decoder will choose a
162 closest match configuration.
163 */
164 static bool DecodeFile(const char file[], SkBitmap* bitmap,
165 SkBitmap::Config prefConfig, Mode);
166 static bool DecodeFile(const char file[], SkBitmap* bitmap)
167 {
168 return DecodeFile(file, bitmap, SkBitmap::kNo_Config, kDecodePixels_Mode);
169 }
170 /** Decode the image stored in the specified memory buffer, and store the
171 result in bitmap. Return true for success or false on failure.
172
173 If pref is kNo_Config, then the decoder is free to choose the most natural
174 config given the image data. If pref something other than kNo_Config,
175 the decoder will attempt to decode the image into that format, unless
176 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
177 config does not support that), in which case the decoder will choose a
178 closest match configuration.
179 */
180 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
181 SkBitmap::Config prefConfig, Mode);
182 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap)
183 {
184 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config,
185 kDecodePixels_Mode);
186 }
187 /** Decode the image stored in the specified SkStream, and store the result
188 in bitmap. Return true for success or false on failure.
189
190 If pref is kNo_Config, then the decoder is free to choose the most
191 natural config given the image data. If pref something other than
192 kNo_Config, the decoder will attempt to decode the image into that
193 format, unless there is a conflict (e.g. the image has per-pixel alpha
194 and the bitmap's config does not support that), in which case the
195 decoder will choose a closest match configuration.
196 */
197 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap,
198 SkBitmap::Config prefConfig, Mode);
199 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap)
200 {
201 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
202 kDecodePixels_Mode);
203 }
weita@google.com25e98342009-05-11 16:06:22 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 /** Return the default config for the running device.
206 Currently this used as a suggestion to image decoders that need to guess
207 what config they should decode into.
208 Default is kNo_Config, but this can be changed with SetDeviceConfig()
209 */
210 static SkBitmap::Config GetDeviceConfig();
211 /** Set the default config for the running device.
212 Currently this used as a suggestion to image decoders that need to guess
213 what config they should decode into.
214 Default is kNo_Config.
215 This can be queried with GetDeviceConfig()
216 */
217 static void SetDeviceConfig(SkBitmap::Config);
218
219 /** @cond UNIT_TEST */
220 SkDEBUGCODE(static void UnitTest();)
221 /** @endcond */
222
223protected:
224 // must be overridden in subclasses. This guy is called by decode(...)
225 virtual bool onDecode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref,
226 Mode) = 0;
227
228 /** Can be queried from within onDecode, to see if the user (possibly in
229 a different thread) has requested the decode to cancel. If this returns
230 true, your onDecode() should stop and return false.
231 Each subclass needs to decide how often it can query this, to balance
232 responsiveness with performance.
weita@google.com25e98342009-05-11 16:06:22 +0000233
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 Calling this outside of onDecode() may return undefined values.
235 */
236
237public:
238 bool shouldCancelDecode() const { return fShouldCancelDecode; }
239
weita@google.com25e98342009-05-11 16:06:22 +0000240protected:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000241 SkImageDecoder();
242
243 // helper function for decoders to handle the (common) case where there is only
244 // once choice available in the image file.
245 bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const;
246
247 /* Helper for subclasses. Call this to allocate the pixel memory given the bitmap's
248 width/height/rowbytes/config. Returns true on success. This method handles checking
249 for an optional Allocator.
250 */
251 bool allocPixelRef(SkBitmap*, SkColorTable*) const;
252
253private:
254 Peeker* fPeeker;
255 Chooser* fChooser;
256 SkBitmap::Allocator* fAllocator;
257 int fSampleSize;
258 bool fDitherImage;
259 mutable bool fShouldCancelDecode;
260
261 // illegal
262 SkImageDecoder(const SkImageDecoder&);
263 SkImageDecoder& operator=(const SkImageDecoder&);
264};
265
reed@android.coma14ea0e2009-03-17 17:59:53 +0000266/** Calling newDecoder with a stream returns a new matching imagedecoder
267 instance, or NULL if none can be found. The caller must manage its ownership
268 of the stream as usual, calling unref() when it is done, as the returned
269 decoder may have called ref() (and if so, the decoder is responsible for
270 balancing its ownership when it is destroyed).
271 */
272class SkImageDecoderFactory : public SkRefCnt {
273public:
274 virtual SkImageDecoder* newDecoder(SkStream*) = 0;
275};
276
277class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
278public:
279 // calls SkImageDecoder::Factory(stream)
280 virtual SkImageDecoder* newDecoder(SkStream* stream) {
281 return SkImageDecoder::Factory(stream);
282 }
283};
284
285
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286#endif