epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkImageDecoder_DEFINED |
| 11 | #define SkImageDecoder_DEFINED |
| 12 | |
| 13 | #include "SkBitmap.h" |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 14 | #include "SkBitmapFactory.h" |
| 15 | #include "SkImage.h" |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 16 | #include "SkRect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkRefCnt.h" |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 18 | #include "SkTRegistry.h" |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
| 21 | class SkStream; |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 22 | class SkStreamRewindable; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | /** \class SkImageDecoder |
| 25 | |
| 26 | Base class for decoding compressed images into a SkBitmap |
| 27 | */ |
scroggo@google.com | 7def5e1 | 2013-05-31 14:00:10 +0000 | [diff] [blame] | 28 | class SkImageDecoder : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | public: |
| 30 | virtual ~SkImageDecoder(); |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | 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.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 40 | kWEBP_Format, |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 41 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 42 | kLastKnownFormat = kWEBP_Format, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | }; |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 44 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 45 | /** Return the format of image this decoder can decode. If this decoder can decode multiple |
| 46 | formats, kUnknown_Format will be returned. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | */ |
| 48 | virtual Format getFormat() const; |
| 49 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 50 | /** Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined. |
| 51 | Rewinds the stream before returning. |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 52 | */ |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 53 | static Format GetStreamFormat(SkStreamRewindable*); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 54 | |
scroggo@google.com | f98118e | 2013-05-15 14:53:49 +0000 | [diff] [blame] | 55 | /** Return a readable string of the Format provided. |
| 56 | */ |
| 57 | static const char* GetFormatName(Format); |
| 58 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 59 | /** Return a readable string of the value returned by getFormat(). |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 60 | */ |
| 61 | const char* getFormatName() const; |
| 62 | |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 63 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 84 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | /** 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.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 90 | /** 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.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 104 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | /** \class Peeker |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 120 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 126 | SK_DECLARE_INST_COUNT(Peeker) |
| 127 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | /** 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 132 | private: |
| 133 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | Peeker* getPeeker() const { return fPeeker; } |
| 137 | Peeker* setPeeker(Peeker*); |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 138 | |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 139 | /** \class Chooser |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 140 | |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 141 | Base class for optional callbacks to choose an image from a format that |
| 142 | contains multiple images. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | */ |
| 144 | class Chooser : public SkRefCnt { |
| 145 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 146 | SK_DECLARE_INST_COUNT(Chooser) |
| 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 153 | |
| 154 | private: |
| 155 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | Chooser* getChooser() const { return fChooser; } |
| 159 | Chooser* setChooser(Chooser*); |
| 160 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 161 | /** |
| 162 | @Deprecated. Use the struct version instead. |
| 163 | |
| 164 | This optional table describes the caller's preferred config based on |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 165 | 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.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 171 | To specify no preference, call setPrefConfigTable(NULL), which is |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 172 | 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.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame^] | 189 | void setPrefConfigTable(const SkBitmap::Config pref[5]); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 190 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 191 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 246 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 259 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 276 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 283 | * 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.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 288 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | */ |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 299 | 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.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 302 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 304 | /** |
| 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.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 311 | bool buildTileIndex(SkStreamRewindable*, int *width, int *height); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 312 | |
| 313 | /** |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 314 | * Decode a rectangle subset in the image. |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 315 | * 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.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 320 | 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.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 329 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | /** Given a stream, this will try to find an appropriate decoder object. |
| 331 | If none is found, the method returns NULL. |
| 332 | */ |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 333 | static SkImageDecoder* Factory(SkStreamRewindable*); |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 334 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | /** 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.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 338 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
| 339 | See NOTE ABOUT PREFERRED CONFIGS. |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 340 | |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 341 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | */ |
| 344 | static bool DecodeFile(const char file[], SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 345 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 346 | Format* format = NULL); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 347 | static bool DecodeFile(const char file[], SkBitmap* bitmap) { |
| 348 | return DecodeFile(file, bitmap, SkBitmap::kNo_Config, |
| 349 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | } |
| 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.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 354 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
| 355 | See NOTE ABOUT PREFERRED CONFIGS. |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 356 | |
| 357 | @param format On success, if format is non-null, it is set to the format |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 358 | of the decoded buffer. On failure it is ignored. |
| 359 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 361 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 362 | Format* format = NULL); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 363 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 365 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | } |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 367 | |
| 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.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 380 | * SkImageInfo info; |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 381 | * 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.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 392 | static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImageInfo* info, |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 393 | const SkBitmapFactory::Target* target); |
| 394 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 395 | /** Decode the image stored in the specified SkStreamRewindable, and store the result |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 396 | in bitmap. Return true for success or false on failure. |
| 397 | |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 398 | @param prefConfig If the PrefConfigTable is not set, prefer this config. |
| 399 | See NOTE ABOUT PREFERRED CONFIGS. |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 400 | |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 401 | @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.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 404 | static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 405 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 406 | Format* format = NULL); |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 407 | static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 409 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | } |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 411 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 412 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | protected: |
| 427 | // must be overridden in subclasses. This guy is called by decode(...) |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 428 | virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 430 | // If the decoder wants to support tiled based decoding, |
| 431 | // this method must be overridden. This guy is called by buildTileIndex(...) |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 432 | virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 433 | 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.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 438 | virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 439 | 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.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 453 | * @param (srcX, srcY) the upper-left point of the src bitmap in terms of |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 454 | * the coordinate in the original bitmap. |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 455 | * @return bool Whether or not it succeeded. |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 456 | */ |
scroggo@google.com | 7e6fcee | 2013-05-03 20:14:28 +0000 | [diff] [blame] | 457 | bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 458 | int dstX, int dstY, int width, int height, |
| 459 | int srcX, int srcY); |
| 460 | |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 461 | /** |
| 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.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 466 | |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 467 | /** |
| 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.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 472 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 473 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 478 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | Calling this outside of onDecode() may return undefined values. |
| 480 | */ |
| 481 | |
| 482 | public: |
| 483 | bool shouldCancelDecode() const { return fShouldCancelDecode; } |
| 484 | |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 485 | protected: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | 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.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 498 | /** |
| 499 | * The raw data of the src image. |
| 500 | */ |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 501 | enum SrcDepth { |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 502 | // Color-indexed. |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 503 | kIndex_SrcDepth, |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 504 | // 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.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 508 | }; |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | private: |
| 520 | Peeker* fPeeker; |
| 521 | Chooser* fChooser; |
| 522 | SkBitmap::Allocator* fAllocator; |
| 523 | int fSampleSize; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 524 | SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false |
scroggo@google.com | f698c82 | 2013-07-18 19:34:49 +0000 | [diff] [blame] | 525 | PrefConfigTable fPrefTable; // use if fUsePrefTable is true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | bool fDitherImage; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 527 | bool fUsePrefTable; |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 528 | bool fSkipWritingZeroes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | mutable bool fShouldCancelDecode; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 530 | bool fPreferQualityOverSpeed; |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 531 | bool fRequireUnpremultipliedColors; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 534 | /** 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 | */ |
| 540 | class SkImageDecoderFactory : public SkRefCnt { |
| 541 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 542 | SK_DECLARE_INST_COUNT(SkImageDecoderFactory) |
| 543 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 544 | virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 545 | |
| 546 | private: |
| 547 | typedef SkRefCnt INHERITED; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 548 | }; |
| 549 | |
| 550 | class SkDefaultImageDecoderFactory : SkImageDecoderFactory { |
| 551 | public: |
| 552 | // calls SkImageDecoder::Factory(stream) |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 553 | virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) { |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 554 | return SkImageDecoder::Factory(stream); |
| 555 | } |
| 556 | }; |
| 557 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 558 | // 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 |
| 572 | DECLARE_DECODER_CREATOR(BMPImageDecoder); |
| 573 | DECLARE_DECODER_CREATOR(GIFImageDecoder); |
| 574 | DECLARE_DECODER_CREATOR(ICOImageDecoder); |
| 575 | DECLARE_DECODER_CREATOR(JPEGImageDecoder); |
| 576 | DECLARE_DECODER_CREATOR(PNGImageDecoder); |
| 577 | DECLARE_DECODER_CREATOR(WBMPImageDecoder); |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 578 | DECLARE_DECODER_CREATOR(WEBPImageDecoder); |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 579 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 580 | |
| 581 | // Typedefs to make registering decoder and formatter callbacks easier. |
| 582 | // These have to be defined outside SkImageDecoder. :( |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 583 | typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecoder_DecodeReg; |
| 584 | typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecoder_FormatReg; |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 585 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 586 | #endif |