blob: 5745dbd5ba255032321bba0454d033684b15a21f [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"
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000016#include "SkRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRefCnt.h"
mtklein@google.combd6343b2013-09-04 17:20:18 +000018#include "SkTRegistry.h"
scroggo@google.com7def5e12013-05-31 14:00:10 +000019#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
21class SkStream;
scroggo@google.comb5571b32013-09-25 21:34:24 +000022class SkStreamRewindable;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
24/** \class SkImageDecoder
25
26 Base class for decoding compressed images into a SkBitmap
27*/
scroggo@google.com7def5e12013-05-31 14:00:10 +000028class SkImageDecoder : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029public:
30 virtual ~SkImageDecoder();
weita@google.com25e98342009-05-11 16:06:22 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 enum Format {
33 kUnknown_Format,
34 kBMP_Format,
35 kGIF_Format,
36 kICO_Format,
37 kJPEG_Format,
38 kPNG_Format,
39 kWBMP_Format,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000040 kWEBP_Format,
weita@google.com25e98342009-05-11 16:06:22 +000041
scroggo@google.com39edf4c2013-04-25 17:33:51 +000042 kLastKnownFormat = kWEBP_Format,
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 };
weita@google.com25e98342009-05-11 16:06:22 +000044
scroggo@google.com4c6adf92013-04-17 21:07:55 +000045 /** Return the format of image this decoder can decode. If this decoder can decode multiple
46 formats, kUnknown_Format will be returned.
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 */
48 virtual Format getFormat() const;
49
scroggo@google.comb5571b32013-09-25 21:34:24 +000050 /** Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined.
51 Rewinds the stream before returning.
scroggo@google.com39edf4c2013-04-25 17:33:51 +000052 */
scroggo@google.comb5571b32013-09-25 21:34:24 +000053 static Format GetStreamFormat(SkStreamRewindable*);
scroggo@google.com39edf4c2013-04-25 17:33:51 +000054
scroggo@google.comf98118e2013-05-15 14:53:49 +000055 /** Return a readable string of the Format provided.
56 */
57 static const char* GetFormatName(Format);
58
scroggo@google.com4c6adf92013-04-17 21:07:55 +000059 /** Return a readable string of the value returned by getFormat().
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000060 */
61 const char* getFormatName() const;
62
scroggo@google.com8d239242013-10-01 17:27:15 +000063 /** Whether the decoder should skip writing zeroes to output if possible.
64 */
65 bool getSkipWritingZeroes() const { return fSkipWritingZeroes; }
66
67 /** Set to true if the decoder should skip writing any zeroes when
68 creating the output image.
69 This is a hint that may not be respected by the decoder.
70 It should only be used if it is known that the memory to write
71 to has already been set to 0; otherwise the resulting image will
72 have garbage.
73 This is ideal for images that contain a lot of completely transparent
74 pixels, but may be a performance hit for an image that has only a
75 few transparent pixels.
76 The default is false.
77 */
78 void setSkipWritingZeroes(bool skip) { fSkipWritingZeroes = skip; }
79
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 /** Returns true if the decoder should try to dither the resulting image.
81 The default setting is true.
82 */
83 bool getDitherImage() const { return fDitherImage; }
weita@google.com25e98342009-05-11 16:06:22 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 /** Set to true if the the decoder should try to dither the resulting image.
86 The default setting is true.
87 */
88 void setDitherImage(bool dither) { fDitherImage = dither; }
89
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000090 /** Returns true if the decoder should try to decode the
91 resulting image to a higher quality even at the expense of
92 the decoding speed.
93 */
94 bool getPreferQualityOverSpeed() const { return fPreferQualityOverSpeed; }
95
96 /** Set to true if the the decoder should try to decode the
97 resulting image to a higher quality even at the expense of
98 the decoding speed.
99 */
100 void setPreferQualityOverSpeed(bool qualityOverSpeed) {
101 fPreferQualityOverSpeed = qualityOverSpeed;
102 }
103
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000104 /** Set to true to require the decoder to return a bitmap with unpremultiplied
105 colors. The default is false, meaning the resulting bitmap will have its
106 colors premultiplied.
107 NOTE: Passing true to this function may result in a bitmap which cannot
108 be properly used by Skia.
109 */
110 void setRequireUnpremultipliedColors(bool request) {
111 fRequireUnpremultipliedColors = request;
112 }
113
114 /** Returns true if the decoder will only return bitmaps with unpremultiplied
115 colors.
116 */
117 bool getRequireUnpremultipliedColors() const { return fRequireUnpremultipliedColors; }
118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 /** \class Peeker
weita@google.com25e98342009-05-11 16:06:22 +0000120
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 Base class for optional callbacks to retrieve meta/chunk data out of
122 an image as it is being decoded.
123 */
124 class Peeker : public SkRefCnt {
125 public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000126 SK_DECLARE_INST_COUNT(Peeker)
127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 /** Return true to continue decoding, or false to indicate an error, which
129 will cause the decoder to not return the image.
130 */
131 virtual bool peek(const char tag[], const void* data, size_t length) = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000132 private:
133 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 };
135
136 Peeker* getPeeker() const { return fPeeker; }
137 Peeker* setPeeker(Peeker*);
weita@google.com25e98342009-05-11 16:06:22 +0000138
scroggo@google.combc69ce92013-07-09 15:45:14 +0000139 /** \class Chooser
weita@google.com25e98342009-05-11 16:06:22 +0000140
scroggo@google.combc69ce92013-07-09 15:45:14 +0000141 Base class for optional callbacks to choose an image from a format that
142 contains multiple images.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 */
144 class Chooser : public SkRefCnt {
145 public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000146 SK_DECLARE_INST_COUNT(Chooser)
147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 virtual void begin(int count) {}
149 virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
150 /** Return the index of the subimage you want, or -1 to choose none of them.
151 */
152 virtual int choose() = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000153
154 private:
155 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 };
157
158 Chooser* getChooser() const { return fChooser; }
159 Chooser* setChooser(Chooser*);
160
scroggo@google.comf698c822013-07-18 19:34:49 +0000161 /**
162 @Deprecated. Use the struct version instead.
163
164 This optional table describes the caller's preferred config based on
reed@android.com3f1f06a2010-03-03 21:04:12 +0000165 information about the src data. For this table, the src attributes are
166 described in terms of depth (index (8), 16, 32/24) and if there is
167 per-pixel alpha. These inputs combine to create an index into the
168 pref[] table, which contains the caller's preferred config for that
169 input, or kNo_Config if there is no preference.
170
scroggo@google.comf698c822013-07-18 19:34:49 +0000171 To specify no preference, call setPrefConfigTable(NULL), which is
reed@android.com3f1f06a2010-03-03 21:04:12 +0000172 the default.
173
174 Note, it is still at the discretion of the codec as to what output
175 config is actually returned, as it may not be able to support the
176 caller's preference.
177
178 Here is how the index into the table is computed from the src:
179 depth [8, 16, 32/24] -> 0, 2, 4
180 alpha [no, yes] -> 0, 1
181 The two index values are OR'd together.
182 src: 8-index, no-alpha -> 0
183 src: 8-index, yes-alpha -> 1
184 src: 16bit, no-alpha -> 2 // e.g. 565
185 src: 16bit, yes-alpha -> 3 // e.g. 1555
186 src: 32/24, no-alpha -> 4
187 src: 32/24, yes-alpha -> 5
188 */
rmistry@google.comd6bab022013-12-02 13:50:38 +0000189 void setPrefConfigTable(const SkBitmap::Config pref[5]);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000190
scroggo@google.comf698c822013-07-18 19:34:49 +0000191 /**
192 * Optional table describing the caller's preferred config based on
193 * information about the src data. Each field should be set to the
194 * preferred config for a src described in the name of the field. The
195 * src attributes are described in terms of depth (8-index,
196 * 8bit-grayscale, or 8-bits/component) and whether there is per-pixel
197 * alpha (does not apply to grayscale). If the caller has no preference
198 * for a particular src type, its slot should be set to kNo_Config.
199 *
200 * NOTE ABOUT PREFERRED CONFIGS:
201 * If a config is preferred, either using a pref table or as a parameter
202 * to some flavor of decode, it is still at the discretion of the codec
203 * as to what output config is actually returned, as it may not be able
204 * to support the caller's preference.
205 *
206 * If a bitmap is decoded into SkBitmap::A8_Config, the resulting bitmap
207 * will either be a conversion of the grayscale in the case of a
208 * grayscale source or the alpha channel in the case of a source with
209 * an alpha channel.
210 */
211 struct PrefConfigTable {
212 SkBitmap::Config fPrefFor_8Index_NoAlpha_src;
213 SkBitmap::Config fPrefFor_8Index_YesAlpha_src;
214 SkBitmap::Config fPrefFor_8Gray_src;
215 SkBitmap::Config fPrefFor_8bpc_NoAlpha_src;
216 SkBitmap::Config fPrefFor_8bpc_YesAlpha_src;
217 };
218
219 /**
220 * Set an optional table for specifying the caller's preferred config
221 * based on information about the src data.
222 *
223 * The default is no preference, which will assume the config set by
224 * decode is preferred.
225 */
226 void setPrefConfigTable(const PrefConfigTable&);
227
228 /**
229 * Do not use a PrefConfigTable to determine the output config. This
230 * is the default, so there is no need to call unless a PrefConfigTable
231 * was previously set.
232 */
233 void resetPrefConfigTable() { fUsePrefTable = false; }
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 SkBitmap::Allocator* getAllocator() const { return fAllocator; }
236 SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
237
238 // sample-size, if set to > 1, tells the decoder to return a smaller than
239 // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample
240 // size is set to 3, then the returned bitmap will be 1/3 as wide and high,
241 // and will contain 1/9 as many pixels as the original.
242 // Note: this is a hint, and the codec may choose to ignore this, or only
243 // approximate the sample size.
244 int getSampleSize() const { return fSampleSize; }
245 void setSampleSize(int size);
weita@google.com25e98342009-05-11 16:06:22 +0000246
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 /** Reset the sampleSize to its default of 1
248 */
249 void resetSampleSize() { this->setSampleSize(1); }
250
251 /** Decoding is synchronous, but for long decodes, a different thread can
252 call this method safely. This sets a state that the decoders will
253 periodically check, and if they see it changed to cancel, they will
254 cancel. This will result in decode() returning false. However, there is
255 no guarantee that the decoder will see the state change in time, so
256 it is possible that cancelDecode() will be called, but will be ignored
257 and decode() will return true (assuming no other problems were
258 encountered).
weita@google.com25e98342009-05-11 16:06:22 +0000259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 This state is automatically reset at the beginning of decode().
261 */
262 void cancelDecode() {
263 // now the subclass must query shouldCancelDecode() to be informed
264 // of the request
265 fShouldCancelDecode = true;
266 }
267
268 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then
269 only the bitmap's width/height/config need be set. If kDecodePixels_Mode
270 is passed, then the bitmap must have pixels or a pixelRef.
271 */
272 enum Mode {
273 kDecodeBounds_Mode, //!< only return width/height/config in bitmap
274 kDecodePixels_Mode //!< return entire bitmap (including pixels)
275 };
weita@google.com25e98342009-05-11 16:06:22 +0000276
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 /** Given a stream, decode it into the specified bitmap.
278 If the decoder can decompress the image, it calls bitmap.setConfig(),
279 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(),
280 which will allocated a pixelRef. To access the pixel memory, the codec
281 needs to call lockPixels/unlockPixels on the
282 bitmap. It can then set the pixels with the decompressed image.
weita@google.com25e98342009-05-11 16:06:22 +0000283 * If the image cannot be decompressed, return false. After the
284 * decoding, the function converts the decoded config in bitmap
285 * to pref if possible. Whether a conversion is feasible is
286 * tested by Bitmap::canCopyTo(pref).
287
scroggo@google.combc69ce92013-07-09 15:45:14 +0000288 If an SkBitmap::Allocator is installed via setAllocator, it will be
289 used to allocate the pixel memory. A clever allocator can be used
290 to allocate the memory from a cache, volatile memory, or even from
291 an existing bitmap's memory.
292
293 If a Peeker is installed via setPeeker, it may be used to peek into
294 meta data during the decode.
295
296 If a Chooser is installed via setChooser, it may be used to select
297 which image to return from a format that contains multiple images.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298 */
scroggo@google.combc69ce92013-07-09 15:45:14 +0000299 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode);
300 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) {
301 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode);
reed@android.com3f1f06a2010-03-03 21:04:12 +0000302 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000304 /**
305 * Given a stream, build an index for doing tile-based decode.
306 * The built index will be saved in the decoder, and the image size will
307 * be returned in width and height.
308 *
309 * Return true for success or false on failure.
310 */
scroggo@google.comb5571b32013-09-25 21:34:24 +0000311 bool buildTileIndex(SkStreamRewindable*, int *width, int *height);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000312
313 /**
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000314 * Decode a rectangle subset in the image.
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000315 * The method can only be called after buildTileIndex().
316 *
317 * Return true for success.
318 * Return false if the index is never built or failing in decoding.
319 */
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000320 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref);
321
322 /**
323 * @Deprecated
324 * Use decodeSubset instead.
325 */
326 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pref) {
327 return this->decodeSubset(bitmap, rect, pref);
328 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 /** Given a stream, this will try to find an appropriate decoder object.
331 If none is found, the method returns NULL.
332 */
scroggo@google.comb5571b32013-09-25 21:34:24 +0000333 static SkImageDecoder* Factory(SkStreamRewindable*);
reed@android.coma14ea0e2009-03-17 17:59:53 +0000334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 /** Decode the image stored in the specified file, and store the result
336 in bitmap. Return true for success or false on failure.
337
scroggo@google.comf698c822013-07-18 19:34:49 +0000338 @param prefConfig If the PrefConfigTable is not set, prefer this config.
339 See NOTE ABOUT PREFERRED CONFIGS.
reed@android.com31d1c642009-06-15 18:45:19 +0000340
reed@android.comb3ade9d2009-06-15 13:04:45 +0000341 @param format On success, if format is non-null, it is set to the format
342 of the decoded file. On failure it is ignored.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 */
344 static bool DecodeFile(const char file[], SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000345 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000346 Format* format = NULL);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000347 static bool DecodeFile(const char file[], SkBitmap* bitmap) {
348 return DecodeFile(file, bitmap, SkBitmap::kNo_Config,
349 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 }
351 /** Decode the image stored in the specified memory buffer, and store the
352 result in bitmap. Return true for success or false on failure.
353
scroggo@google.comf698c822013-07-18 19:34:49 +0000354 @param prefConfig If the PrefConfigTable is not set, prefer this config.
355 See NOTE ABOUT PREFERRED CONFIGS.
reed@android.com31d1c642009-06-15 18:45:19 +0000356
357 @param format On success, if format is non-null, it is set to the format
reed@android.comb3ade9d2009-06-15 13:04:45 +0000358 of the decoded buffer. On failure it is ignored.
359 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000361 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000362 Format* format = NULL);
reed@android.comb3ade9d2009-06-15 13:04:45 +0000363 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000365 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 }
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000367
368 /**
369 * Decode memory.
370 * @param info Output parameter. Returns info about the encoded image.
371 * @param target Contains the address of pixel memory to decode into
372 * (which must be large enough to hold the width in info) and
373 * the row bytes to use. If NULL, returns info and does not
374 * decode pixels.
375 * @return bool Whether the function succeeded.
376 *
377 * Sample usage:
378 * <code>
379 * // Determine the image's info: width/height/config
reed@google.com2bd8b812013-11-01 13:46:54 +0000380 * SkImageInfo info;
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000381 * bool success = DecodeMemoryToTarget(src, size, &info, NULL);
382 * if (!success) return;
383 * // Allocate space for the result:
384 * SkBitmapFactory::Target target;
385 * target.fAddr = malloc/other allocation
386 * target.fRowBytes = ...
387 * // Now decode the actual pixels into target. &info is optional,
388 * // and could be NULL
389 * success = DecodeMemoryToTarget(src, size, &info, &target);
390 * </code>
391 */
reed@google.com2bd8b812013-11-01 13:46:54 +0000392 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImageInfo* info,
scroggo@google.comf8d7d272013-02-22 21:38:35 +0000393 const SkBitmapFactory::Target* target);
394
scroggo@google.comb5571b32013-09-25 21:34:24 +0000395 /** Decode the image stored in the specified SkStreamRewindable, and store the result
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 in bitmap. Return true for success or false on failure.
397
scroggo@google.comf698c822013-07-18 19:34:49 +0000398 @param prefConfig If the PrefConfigTable is not set, prefer this config.
399 See NOTE ABOUT PREFERRED CONFIGS.
reed@android.com31d1c642009-06-15 18:45:19 +0000400
reed@android.comb3ade9d2009-06-15 13:04:45 +0000401 @param format On success, if format is non-null, it is set to the format
402 of the decoded stream. On failure it is ignored.
403 */
scroggo@google.comb5571b32013-09-25 21:34:24 +0000404 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000405 SkBitmap::Config prefConfig, Mode,
reed@android.com31d1c642009-06-15 18:45:19 +0000406 Format* format = NULL);
scroggo@google.comb5571b32013-09-25 21:34:24 +0000407 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config,
reed@android.comb3ade9d2009-06-15 13:04:45 +0000409 kDecodePixels_Mode, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 }
weita@google.com25e98342009-05-11 16:06:22 +0000411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 /** Return the default config for the running device.
413 Currently this used as a suggestion to image decoders that need to guess
414 what config they should decode into.
415 Default is kNo_Config, but this can be changed with SetDeviceConfig()
416 */
417 static SkBitmap::Config GetDeviceConfig();
418 /** Set the default config for the running device.
419 Currently this used as a suggestion to image decoders that need to guess
420 what config they should decode into.
421 Default is kNo_Config.
422 This can be queried with GetDeviceConfig()
423 */
424 static void SetDeviceConfig(SkBitmap::Config);
425
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426protected:
427 // must be overridden in subclasses. This guy is called by decode(...)
reed@android.com3f1f06a2010-03-03 21:04:12 +0000428 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000430 // If the decoder wants to support tiled based decoding,
431 // this method must be overridden. This guy is called by buildTileIndex(...)
scroggo@google.comb5571b32013-09-25 21:34:24 +0000432 virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000433 return false;
434 }
435
436 // If the decoder wants to support tiled based decoding,
437 // this method must be overridden. This guy is called by decodeRegion(...)
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000438 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000439 return false;
440 }
441
442 /*
443 * Crop a rectangle from the src Bitmap to the dest Bitmap. src and dst are
444 * both sampled by sampleSize from an original Bitmap.
445 *
446 * @param dst the destination bitmap.
447 * @param src the source bitmap that is sampled by sampleSize from the
448 * original bitmap.
449 * @param sampleSize the sample size that src is sampled from the original bitmap.
450 * @param (dstX, dstY) the upper-left point of the dest bitmap in terms of
451 * the coordinate in the original bitmap.
452 * @param (width, height) the width and height of the unsampled dst.
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000453 * @param (srcX, srcY) the upper-left point of the src bitmap in terms of
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000454 * the coordinate in the original bitmap.
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000455 * @return bool Whether or not it succeeded.
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000456 */
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000457 bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000458 int dstX, int dstY, int width, int height,
459 int srcX, int srcY);
460
scroggo@google.com468142b2013-07-09 15:48:24 +0000461 /**
462 * Copy all fields on this decoder to the other decoder. Used by subclasses
463 * to decode a subimage using a different decoder, but with the same settings.
464 */
465 void copyFieldsToOther(SkImageDecoder* other);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000466
scroggo@google.com468142b2013-07-09 15:48:24 +0000467 /**
468 * Return the default preference being used by the current or latest call to
469 * decode.
470 */
471 SkBitmap::Config getDefaultPref() { return fDefaultPref; }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000472
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 /** Can be queried from within onDecode, to see if the user (possibly in
474 a different thread) has requested the decode to cancel. If this returns
475 true, your onDecode() should stop and return false.
476 Each subclass needs to decide how often it can query this, to balance
477 responsiveness with performance.
weita@google.com25e98342009-05-11 16:06:22 +0000478
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479 Calling this outside of onDecode() may return undefined values.
480 */
481
482public:
483 bool shouldCancelDecode() const { return fShouldCancelDecode; }
484
weita@google.com25e98342009-05-11 16:06:22 +0000485protected:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 SkImageDecoder();
487
488 // helper function for decoders to handle the (common) case where there is only
489 // once choice available in the image file.
490 bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) const;
491
492 /* Helper for subclasses. Call this to allocate the pixel memory given the bitmap's
493 width/height/rowbytes/config. Returns true on success. This method handles checking
494 for an optional Allocator.
495 */
496 bool allocPixelRef(SkBitmap*, SkColorTable*) const;
497
scroggo@google.comf698c822013-07-18 19:34:49 +0000498 /**
499 * The raw data of the src image.
500 */
reed@android.com3f1f06a2010-03-03 21:04:12 +0000501 enum SrcDepth {
scroggo@google.comf698c822013-07-18 19:34:49 +0000502 // Color-indexed.
reed@android.com3f1f06a2010-03-03 21:04:12 +0000503 kIndex_SrcDepth,
scroggo@google.comf698c822013-07-18 19:34:49 +0000504 // Grayscale in 8 bits.
505 k8BitGray_SrcDepth,
506 // 8 bits per component. Used for 24 bit if there is no alpha.
507 k32Bit_SrcDepth,
reed@android.com3f1f06a2010-03-03 21:04:12 +0000508 };
509 /** The subclass, inside onDecode(), calls this to determine the config of
510 the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the
511 src image. This routine returns the caller's preference given
512 srcDepth and hasAlpha, or kNo_Config if there is no preference.
513
514 Note: this also takes into account GetDeviceConfig(), so the subclass
515 need not call that.
516 */
517 SkBitmap::Config getPrefConfig(SrcDepth, bool hasAlpha) const;
518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519private:
520 Peeker* fPeeker;
521 Chooser* fChooser;
522 SkBitmap::Allocator* fAllocator;
523 int fSampleSize;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000524 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false
scroggo@google.comf698c822013-07-18 19:34:49 +0000525 PrefConfigTable fPrefTable; // use if fUsePrefTable is true
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 bool fDitherImage;
reed@android.com3f1f06a2010-03-03 21:04:12 +0000527 bool fUsePrefTable;
scroggo@google.com8d239242013-10-01 17:27:15 +0000528 bool fSkipWritingZeroes;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 mutable bool fShouldCancelDecode;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000530 bool fPreferQualityOverSpeed;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000531 bool fRequireUnpremultipliedColors;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532};
533
reed@android.coma14ea0e2009-03-17 17:59:53 +0000534/** Calling newDecoder with a stream returns a new matching imagedecoder
535 instance, or NULL if none can be found. The caller must manage its ownership
536 of the stream as usual, calling unref() when it is done, as the returned
537 decoder may have called ref() (and if so, the decoder is responsible for
538 balancing its ownership when it is destroyed).
539 */
540class SkImageDecoderFactory : public SkRefCnt {
541public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000542 SK_DECLARE_INST_COUNT(SkImageDecoderFactory)
543
scroggo@google.comb5571b32013-09-25 21:34:24 +0000544 virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0;
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +0000545
546private:
547 typedef SkRefCnt INHERITED;
reed@android.coma14ea0e2009-03-17 17:59:53 +0000548};
549
550class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
551public:
552 // calls SkImageDecoder::Factory(stream)
scroggo@google.comb5571b32013-09-25 21:34:24 +0000553 virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) {
reed@android.coma14ea0e2009-03-17 17:59:53 +0000554 return SkImageDecoder::Factory(stream);
555 }
556};
557
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000558// This macro declares a global (i.e., non-class owned) creation entry point
559// for each decoder (e.g., CreateJPEGImageDecoder)
560#define DECLARE_DECODER_CREATOR(codec) \
561 SkImageDecoder *Create ## codec ();
562
563// This macro defines the global creation entry point for each decoder. Each
564// decoder implementation that registers with the decoder factory must call it.
565#define DEFINE_DECODER_CREATOR(codec) \
566 SkImageDecoder *Create ## codec () { \
567 return SkNEW( Sk ## codec ); \
568 }
569
570// All the decoders known by Skia. Note that, depending on the compiler settings,
571// not all of these will be available
572DECLARE_DECODER_CREATOR(BMPImageDecoder);
573DECLARE_DECODER_CREATOR(GIFImageDecoder);
574DECLARE_DECODER_CREATOR(ICOImageDecoder);
575DECLARE_DECODER_CREATOR(JPEGImageDecoder);
576DECLARE_DECODER_CREATOR(PNGImageDecoder);
577DECLARE_DECODER_CREATOR(WBMPImageDecoder);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000578DECLARE_DECODER_CREATOR(WEBPImageDecoder);
reed@android.coma14ea0e2009-03-17 17:59:53 +0000579
mtklein@google.combd6343b2013-09-04 17:20:18 +0000580
581// Typedefs to make registering decoder and formatter callbacks easier.
582// These have to be defined outside SkImageDecoder. :(
scroggo@google.comb5571b32013-09-25 21:34:24 +0000583typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecoder_DecodeReg;
584typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecoder_FormatReg;
mtklein@google.combd6343b2013-09-04 17:20:18 +0000585
reed@android.com8a1c16f2008-12-17 15:59:43 +0000586#endif