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" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkRefCnt.h" |
| 17 | |
| 18 | class SkStream; |
| 19 | |
| 20 | /** \class SkImageDecoder |
| 21 | |
| 22 | Base class for decoding compressed images into a SkBitmap |
| 23 | */ |
| 24 | class SkImageDecoder { |
| 25 | public: |
| 26 | virtual ~SkImageDecoder(); |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 27 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | kLastKnownFormat = kWBMP_Format |
| 38 | }; |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 39 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 55 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 61 | SK_DECLARE_INST_COUNT(Peeker) |
| 62 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | /** 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 67 | private: |
| 68 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | Peeker* getPeeker() const { return fPeeker; } |
| 72 | Peeker* setPeeker(Peeker*); |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 73 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | /** \class Peeker |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 81 | SK_DECLARE_INST_COUNT(Chooser) |
| 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | 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.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | Chooser* getChooser() const { return fChooser; } |
| 94 | Chooser* setChooser(Chooser*); |
| 95 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 96 | /** 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 164 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | /** 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 171 | * 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | */ |
| 178 | bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode); |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 179 | bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { |
| 180 | return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); |
| 181 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | |
| 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.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 187 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | /** 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.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 197 | |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 198 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | */ |
| 201 | static bool DecodeFile(const char file[], SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 202 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 203 | Format* format = NULL); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 204 | static bool DecodeFile(const char file[], SkBitmap* bitmap) { |
| 205 | return DecodeFile(file, bitmap, SkBitmap::kNo_Config, |
| 206 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | } |
| 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.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 217 | |
| 218 | @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] | 219 | of the decoded buffer. On failure it is ignored. |
| 220 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 222 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 223 | Format* format = NULL); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 224 | static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 226 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | } |
scroggo@google.com | f8d7d27 | 2013-02-22 21:38:35 +0000 | [diff] [blame] | 228 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | /** 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.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 265 | |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 266 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | static bool DecodeStream(SkStream* stream, SkBitmap* bitmap, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 270 | SkBitmap::Config prefConfig, Mode, |
reed@android.com | 31d1c64 | 2009-06-15 18:45:19 +0000 | [diff] [blame] | 271 | Format* format = NULL); |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 272 | static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, |
reed@android.com | b3ade9d | 2009-06-15 13:04:45 +0000 | [diff] [blame] | 274 | kDecodePixels_Mode, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | /** 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 | |
| 295 | protected: |
| 296 | // must be overridden in subclasses. This guy is called by decode(...) |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 297 | virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | |
| 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.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 304 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | Calling this outside of onDecode() may return undefined values. |
| 306 | */ |
| 307 | |
| 308 | public: |
| 309 | bool shouldCancelDecode() const { return fShouldCancelDecode; } |
| 310 | |
weita@google.com | 25e9834 | 2009-05-11 16:06:22 +0000 | [diff] [blame] | 311 | protected: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 312 | 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.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 324 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | private: |
| 340 | Peeker* fPeeker; |
| 341 | Chooser* fChooser; |
| 342 | SkBitmap::Allocator* fAllocator; |
| 343 | int fSampleSize; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 344 | SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false |
| 345 | SkBitmap::Config fPrefTable[6]; // use if fUsePrefTable is true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | bool fDitherImage; |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 347 | bool fUsePrefTable; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | mutable bool fShouldCancelDecode; |
| 349 | |
| 350 | // illegal |
| 351 | SkImageDecoder(const SkImageDecoder&); |
| 352 | SkImageDecoder& operator=(const SkImageDecoder&); |
| 353 | }; |
| 354 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 355 | /** 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 | */ |
| 361 | class SkImageDecoderFactory : public SkRefCnt { |
| 362 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 363 | SK_DECLARE_INST_COUNT(SkImageDecoderFactory) |
| 364 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 365 | virtual SkImageDecoder* newDecoder(SkStream*) = 0; |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 366 | |
| 367 | private: |
| 368 | typedef SkRefCnt INHERITED; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
| 371 | class SkDefaultImageDecoderFactory : SkImageDecoderFactory { |
| 372 | public: |
| 373 | // calls SkImageDecoder::Factory(stream) |
| 374 | virtual SkImageDecoder* newDecoder(SkStream* stream) { |
| 375 | return SkImageDecoder::Factory(stream); |
| 376 | } |
| 377 | }; |
| 378 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 379 | // 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 |
| 393 | DECLARE_DECODER_CREATOR(BMPImageDecoder); |
| 394 | DECLARE_DECODER_CREATOR(GIFImageDecoder); |
| 395 | DECLARE_DECODER_CREATOR(ICOImageDecoder); |
| 396 | DECLARE_DECODER_CREATOR(JPEGImageDecoder); |
| 397 | DECLARE_DECODER_CREATOR(PNGImageDecoder); |
| 398 | DECLARE_DECODER_CREATOR(WBMPImageDecoder); |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 399 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | #endif |