blob: 6d1bb7241d5dd3aeb44e584d02840de23e7eb678 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkImageDecoder_DEFINED
11#define SkImageDecoder_DEFINED
12
13#include "SkBitmap.h"
scroggo@google.comf8d7d272013-02-22 21:38:35 +000014#include "SkBitmapFactory.h"
15#include "SkImage.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRefCnt.h"
17
18class SkStream;
19
20/** \class SkImageDecoder
21
22 Base class for decoding compressed images into a SkBitmap
23*/
24class SkImageDecoder {
25public:
26 virtual ~SkImageDecoder();
weita@google.com25e98342009-05-11 16:06:22 +000027
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 enum Format {
29 kUnknown_Format,
30 kBMP_Format,
31 kGIF_Format,
32 kICO_Format,
33 kJPEG_Format,
34 kPNG_Format,
35 kWBMP_Format,
weita@google.com25e98342009-05-11 16:06:22 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 kLastKnownFormat = kWBMP_Format
38 };
weita@google.com25e98342009-05-11 16:06:22 +000039
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 /** Return the compressed data's format (see Format enum)
41 */
42 virtual Format getFormat() const;
43
44 /** Returns true if the decoder should try to dither the resulting image.
45 The default setting is true.
46 */
47 bool getDitherImage() const { return fDitherImage; }
weita@google.com25e98342009-05-11 16:06:22 +000048
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 /** Set to true if the the decoder should try to dither the resulting image.
50 The default setting is true.
51 */
52 void setDitherImage(bool dither) { fDitherImage = dither; }
53
54 /** \class Peeker
weita@google.com25e98342009-05-11 16:06:22 +000055
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 Base class for optional callbacks to retrieve meta/chunk data out of
57 an image as it is being decoded.
58 */
59 class Peeker : public SkRefCnt {
60 public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000061 SK_DECLARE_INST_COUNT(Peeker)
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 /** Return true to continue decoding, or false to indicate an error, which
64 will cause the decoder to not return the image.
65 */
66 virtual bool peek(const char tag[], const void* data, size_t length) = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000067 private:
68 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 };
70
71 Peeker* getPeeker() const { return fPeeker; }
72 Peeker* setPeeker(Peeker*);
weita@google.com25e98342009-05-11 16:06:22 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 /** \class Peeker
weita@google.com25e98342009-05-11 16:06:22 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 Base class for optional callbacks to retrieve meta/chunk data out of
77 an image as it is being decoded.
78 */
79 class Chooser : public SkRefCnt {
80 public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000081 SK_DECLARE_INST_COUNT(Chooser)
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 virtual void begin(int count) {}
84 virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
85 /** Return the index of the subimage you want, or -1 to choose none of them.
86 */
87 virtual int choose() = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000088
89 private:
90 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 };
92
93 Chooser* getChooser() const { return fChooser; }
94 Chooser* setChooser(Chooser*);
95
reed@android.com3f1f06a2010-03-03 21:04:12 +000096 /** This optional table describes the caller's preferred config based on
97 information about the src data. For this table, the src attributes are
98 described in terms of depth (index (8), 16, 32/24) and if there is
99 per-pixel alpha. These inputs combine to create an index into the
100 pref[] table, which contains the caller's preferred config for that
101 input, or kNo_Config if there is no preference.
102
103 To specify no preferrence, call setPrefConfigTable(NULL), which is
104 the default.
105
106 Note, it is still at the discretion of the codec as to what output
107 config is actually returned, as it may not be able to support the
108 caller's preference.
109
110 Here is how the index into the table is computed from the src:
111 depth [8, 16, 32/24] -> 0, 2, 4
112 alpha [no, yes] -> 0, 1
113 The two index values are OR'd together.
114 src: 8-index, no-alpha -> 0
115 src: 8-index, yes-alpha -> 1
116 src: 16bit, no-alpha -> 2 // e.g. 565
117 src: 16bit, yes-alpha -> 3 // e.g. 1555
118 src: 32/24, no-alpha -> 4
119 src: 32/24, yes-alpha -> 5
120 */
121 void setPrefConfigTable(const SkBitmap::Config pref[6]);
122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkBitmap::Allocator* getAllocator() const { return fAllocator; }
124 SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
125
126 // sample-size, if set to > 1, tells the decoder to return a smaller than
127 // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample
128 // size is set to 3, then the returned bitmap will be 1/3 as wide and high,
129 // and will contain 1/9 as many pixels as the original.
130 // Note: this is a hint, and the codec may choose to ignore this, or only
131 // approximate the sample size.
132 int getSampleSize() const { return fSampleSize; }
133 void setSampleSize(int size);
weita@google.com25e98342009-05-11 16:06:22 +0000134
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 /** Reset the sampleSize to its default of 1
136 */
137 void resetSampleSize() { this->setSampleSize(1); }
138
139 /** Decoding is synchronous, but for long decodes, a different thread can
140 call this method safely. This sets a state that the decoders will
141 periodically check, and if they see it changed to cancel, they will
142 cancel. This will result in decode() returning false. However, there is
143 no guarantee that the decoder will see the state change in time, so
144 it is possible that cancelDecode() will be called, but will be ignored
145 and decode() will return true (assuming no other problems were
146 encountered).
weita@google.com25e98342009-05-11 16:06:22 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 This state is automatically reset at the beginning of decode().
149 */
150 void cancelDecode() {
151 // now the subclass must query shouldCancelDecode() to be informed
152 // of the request
153 fShouldCancelDecode = true;
154 }
155
156 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then
157 only the bitmap's width/height/config need be set. If kDecodePixels_Mode
158 is passed, then the bitmap must have pixels or a pixelRef.
159 */
160 enum Mode {
161 kDecodeBounds_Mode, //!< only return width/height/config in bitmap
162 kDecodePixels_Mode //!< return entire bitmap (including pixels)
163 };
weita@google.com25e98342009-05-11 16:06:22 +0000164
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 /** Given a stream, decode it into the specified bitmap.
166 If the decoder can decompress the image, it calls bitmap.setConfig(),
167 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(),
168 which will allocated a pixelRef. To access the pixel memory, the codec
169 needs to call lockPixels/unlockPixels on the
170 bitmap. It can then set the pixels with the decompressed image.
weita@google.com25e98342009-05-11 16:06:22 +0000171 * If the image cannot be decompressed, return false. After the
172 * decoding, the function converts the decoded config in bitmap
173 * to pref if possible. Whether a conversion is feasible is
174 * tested by Bitmap::canCopyTo(pref).
175
176 note: document use of Allocator, Peeker and Chooser
reed@android.com8a1c16f2008-12-17 15:59:43 +0000177 */
178 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000179 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) {
180 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode);
181 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182
183 /** Given a stream, this will try to find an appropriate decoder object.
184 If none is found, the method returns NULL.
185 */
186 static SkImageDecoder* Factory(SkStream*);
reed@android.coma14ea0e2009-03-17 17:59:53 +0000187
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 /** Decode the image stored in the specified file, and store the result
189 in bitmap. Return true for success or false on failure.
190
191 If pref is kNo_Config, then the decoder is free to choose the most natural
192 config given the image data. If pref something other than kNo_Config,
193 the decoder will attempt to decode the image into that format, unless
194 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
195 config does not support that), in which case the decoder will choose a
196 closest match configuration.
reed@android.com31d1c642009-06-15 18:45:19 +0000197
reed@android.comb3ade9d2009-06-15 13:04:45 +0000198 @param format On success, if format is non-null, it is set to the format
199 of the decoded file. On failure it is ignored.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 */
201 static bool DecodeFile(const char file[], SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000202 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000203 Format* format = NULL);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000204 static bool DecodeFile(const char file[], SkBitmap* bitmap) {
205 return DecodeFile(file, bitmap, SkBitmap::kNo_Config,
206 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000207 }
208 /** Decode the image stored in the specified memory buffer, and store the
209 result in bitmap. Return true for success or false on failure.
210
211 If pref is kNo_Config, then the decoder is free to choose the most natural
212 config given the image data. If pref something other than kNo_Config,
213 the decoder will attempt to decode the image into that format, unless
214 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's
215 config does not support that), in which case the decoder will choose a
216 closest match configuration.
reed@android.com31d1c642009-06-15 18:45:19 +0000217
218 @param format On success, if format is non-null, it is set to the format
reed@android.comb3ade9d2009-06-15 13:04:45 +0000219 of the decoded buffer. On failure it is ignored.
220 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000222 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000223 Format* format = NULL);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000224 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000226 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 }
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000228
229 /**
230 * Decode memory.
231 * @param info Output parameter. Returns info about the encoded image.
232 * @param target Contains the address of pixel memory to decode into
233 * (which must be large enough to hold the width in info) and
234 * the row bytes to use. If NULL, returns info and does not
235 * decode pixels.
236 * @return bool Whether the function succeeded.
237 *
238 * Sample usage:
239 * <code>
240 * // Determine the image's info: width/height/config
241 * SkImage::Info info;
242 * bool success = DecodeMemoryToTarget(src, size, &info, NULL);
243 * if (!success) return;
244 * // Allocate space for the result:
245 * SkBitmapFactory::Target target;
246 * target.fAddr = malloc/other allocation
247 * target.fRowBytes = ...
248 * // Now decode the actual pixels into target. &info is optional,
249 * // and could be NULL
250 * success = DecodeMemoryToTarget(src, size, &info, &target);
251 * </code>
252 */
253 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::Info* info,
254 const SkBitmapFactory::Target* target);
255
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 /** Decode the image stored in the specified SkStream, and store the result
257 in bitmap. Return true for success or false on failure.
258
259 If pref is kNo_Config, then the decoder is free to choose the most
260 natural config given the image data. If pref something other than
261 kNo_Config, the decoder will attempt to decode the image into that
262 format, unless there is a conflict (e.g. the image has per-pixel alpha
263 and the bitmap's config does not support that), in which case the
264 decoder will choose a closest match configuration.
reed@android.com31d1c642009-06-15 18:45:19 +0000265
reed@android.comb3ade9d2009-06-15 13:04:45 +0000266 @param format On success, if format is non-null, it is set to the format
267 of the decoded stream. On failure it is ignored.
268 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000270 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000271 Format* format = NULL);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000272 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000274 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 }
weita@google.com25e98342009-05-11 16:06:22 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 /** Return the default config for the running device.
278 Currently this used as a suggestion to image decoders that need to guess
279 what config they should decode into.
280 Default is kNo_Config, but this can be changed with SetDeviceConfig()
281 */
282 static SkBitmap::Config GetDeviceConfig();
283 /** Set the default config for the running device.
284 Currently this used as a suggestion to image decoders that need to guess
285 what config they should decode into.
286 Default is kNo_Config.
287 This can be queried with GetDeviceConfig()
288 */
289 static void SetDeviceConfig(SkBitmap::Config);
290
291 /** @cond UNIT_TEST */
292 SkDEBUGCODE(static void UnitTest();)
293 /** @endcond */
294
295protected:
296 // must be overridden in subclasses. This guy is called by decode(...)
reed@android.com3f1f06a2010-03-03 21:04:12 +0000297 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298
299 /** Can be queried from within onDecode, to see if the user (possibly in
300 a different thread) has requested the decode to cancel. If this returns
301 true, your onDecode() should stop and return false.
302 Each subclass needs to decide how often it can query this, to balance
303 responsiveness with performance.
weita@google.com25e98342009-05-11 16:06:22 +0000304
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305 Calling this outside of onDecode() may return undefined values.
306 */
307
308public:
309 bool shouldCancelDecode() const { return fShouldCancelDecode; }
310
weita@google.com25e98342009-05-11 16:06:22 +0000311protected:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 SkImageDecoder();
313
314 // helper function for decoders to handle the (common) case where there is only
315 // once choice available in the image file.
316 bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const;
317
318 /* Helper for subclasses. Call this to allocate the pixel memory given the bitmap's
319 width/height/rowbytes/config. Returns true on success. This method handles checking
320 for an optional Allocator.
321 */
322 bool allocPixelRef(SkBitmap*, SkColorTable*) const;
323
reed@android.com3f1f06a2010-03-03 21:04:12 +0000324 enum SrcDepth {
325 kIndex_SrcDepth,
326 k16Bit_SrcDepth,
327 k32Bit_SrcDepth
328 };
329 /** The subclass, inside onDecode(), calls this to determine the config of
330 the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the
331 src image. This routine returns the caller's preference given
332 srcDepth and hasAlpha, or kNo_Config if there is no preference.
333
334 Note: this also takes into account GetDeviceConfig(), so the subclass
335 need not call that.
336 */
337 SkBitmap::Config getPrefConfig(SrcDepth, bool hasAlpha) const;
338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339private:
340 Peeker* fPeeker;
341 Chooser* fChooser;
342 SkBitmap::Allocator* fAllocator;
343 int fSampleSize;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000344 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false
345 SkBitmap::Config fPrefTable[6]; // use if fUsePrefTable is true
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 bool fDitherImage;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000347 bool fUsePrefTable;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348 mutable bool fShouldCancelDecode;
349
350 // illegal
351 SkImageDecoder(const SkImageDecoder&);
352 SkImageDecoder& operator=(const SkImageDecoder&);
353};
354
reed@android.coma14ea0e2009-03-17 17:59:53 +0000355/** Calling newDecoder with a stream returns a new matching imagedecoder
356 instance, or NULL if none can be found. The caller must manage its ownership
357 of the stream as usual, calling unref() when it is done, as the returned
358 decoder may have called ref() (and if so, the decoder is responsible for
359 balancing its ownership when it is destroyed).
360 */
361class SkImageDecoderFactory : public SkRefCnt {
362public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000363 SK_DECLARE_INST_COUNT(SkImageDecoderFactory)
364
reed@android.coma14ea0e2009-03-17 17:59:53 +0000365 virtual SkImageDecoder* newDecoder(SkStream*) = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000366
367private:
368 typedef SkRefCnt INHERITED;
reed@android.coma14ea0e2009-03-17 17:59:53 +0000369};
370
371class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
372public:
373 // calls SkImageDecoder::Factory(stream)
374 virtual SkImageDecoder* newDecoder(SkStream* stream) {
375 return SkImageDecoder::Factory(stream);
376 }
377};
378
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000379// This macro declares a global (i.e., non-class owned) creation entry point
380// for each decoder (e.g., CreateJPEGImageDecoder)
381#define DECLARE_DECODER_CREATOR(codec) \
382 SkImageDecoder *Create ## codec ();
383
384// This macro defines the global creation entry point for each decoder. Each
385// decoder implementation that registers with the decoder factory must call it.
386#define DEFINE_DECODER_CREATOR(codec) \
387 SkImageDecoder *Create ## codec () { \
388 return SkNEW( Sk ## codec ); \
389 }
390
391// All the decoders known by Skia. Note that, depending on the compiler settings,
392// not all of these will be available
393DECLARE_DECODER_CREATOR(BMPImageDecoder);
394DECLARE_DECODER_CREATOR(GIFImageDecoder);
395DECLARE_DECODER_CREATOR(ICOImageDecoder);
396DECLARE_DECODER_CREATOR(JPEGImageDecoder);
397DECLARE_DECODER_CREATOR(PNGImageDecoder);
398DECLARE_DECODER_CREATOR(WBMPImageDecoder);
reed@android.coma14ea0e2009-03-17 17:59:53 +0000399
reed@android.com8a1c16f2008-12-17 15:59:43 +0000400#endif