| OLD | NEW |
| (Empty) |
| 1 | |
| 2 /* | |
| 3 * Copyright 2006 The Android Open Source Project | |
| 4 * | |
| 5 * Use of this source code is governed by a BSD-style license that can be | |
| 6 * found in the LICENSE file. | |
| 7 */ | |
| 8 | |
| 9 | |
| 10 #ifndef SkImageDecoder_DEFINED | |
| 11 #define SkImageDecoder_DEFINED | |
| 12 | |
| 13 #include "SkBitmap.h" | |
| 14 #include "SkBitmapFactory.h" | |
| 15 #include "SkImage.h" | |
| 16 #include "SkRect.h" | |
| 17 #include "SkRefCnt.h" | |
| 18 | |
| 19 class SkStream; | |
| 20 | |
| 21 /** \class SkImageDecoder | |
| 22 | |
| 23 Base class for decoding compressed images into a SkBitmap | |
| 24 */ | |
| 25 class SkImageDecoder { | |
| 26 public: | |
| 27 virtual ~SkImageDecoder(); | |
| 28 | |
| 29 enum Format { | |
| 30 kUnknown_Format, | |
| 31 kBMP_Format, | |
| 32 kGIF_Format, | |
| 33 kICO_Format, | |
| 34 kJPEG_Format, | |
| 35 kPNG_Format, | |
| 36 kWBMP_Format, | |
| 37 kWEBP_Format, | |
| 38 | |
| 39 kLastKnownFormat = kWEBP_Format, | |
| 40 }; | |
| 41 | |
| 42 /** Return the format of image this decoder can decode. If this decoder can
decode multiple | |
| 43 formats, kUnknown_Format will be returned. | |
| 44 */ | |
| 45 virtual Format getFormat() const; | |
| 46 | |
| 47 /** Return the format of the SkStream or kUnknown_Format if it cannot be det
ermined. Rewinds the | |
| 48 stream before returning. | |
| 49 */ | |
| 50 static Format GetStreamFormat(SkStream*); | |
| 51 | |
| 52 /** Return a readable string of the Format provided. | |
| 53 */ | |
| 54 static const char* GetFormatName(Format); | |
| 55 | |
| 56 /** Return a readable string of the value returned by getFormat(). | |
| 57 */ | |
| 58 const char* getFormatName() const; | |
| 59 | |
| 60 /** Returns true if the decoder should try to dither the resulting image. | |
| 61 The default setting is true. | |
| 62 */ | |
| 63 bool getDitherImage() const { return fDitherImage; } | |
| 64 | |
| 65 /** Set to true if the the decoder should try to dither the resulting image. | |
| 66 The default setting is true. | |
| 67 */ | |
| 68 void setDitherImage(bool dither) { fDitherImage = dither; } | |
| 69 | |
| 70 /** Returns true if the decoder should try to decode the | |
| 71 resulting image to a higher quality even at the expense of | |
| 72 the decoding speed. | |
| 73 */ | |
| 74 bool getPreferQualityOverSpeed() const { return fPreferQualityOverSpeed; } | |
| 75 | |
| 76 /** Set to true if the the decoder should try to decode the | |
| 77 resulting image to a higher quality even at the expense of | |
| 78 the decoding speed. | |
| 79 */ | |
| 80 void setPreferQualityOverSpeed(bool qualityOverSpeed) { | |
| 81 fPreferQualityOverSpeed = qualityOverSpeed; | |
| 82 } | |
| 83 | |
| 84 /** \class Peeker | |
| 85 | |
| 86 Base class for optional callbacks to retrieve meta/chunk data out of | |
| 87 an image as it is being decoded. | |
| 88 */ | |
| 89 class Peeker : public SkRefCnt { | |
| 90 public: | |
| 91 SK_DECLARE_INST_COUNT(Peeker) | |
| 92 | |
| 93 /** Return true to continue decoding, or false to indicate an error, whi
ch | |
| 94 will cause the decoder to not return the image. | |
| 95 */ | |
| 96 virtual bool peek(const char tag[], const void* data, size_t length) = 0
; | |
| 97 private: | |
| 98 typedef SkRefCnt INHERITED; | |
| 99 }; | |
| 100 | |
| 101 Peeker* getPeeker() const { return fPeeker; } | |
| 102 Peeker* setPeeker(Peeker*); | |
| 103 | |
| 104 /** \class Peeker | |
| 105 | |
| 106 Base class for optional callbacks to retrieve meta/chunk data out of | |
| 107 an image as it is being decoded. | |
| 108 */ | |
| 109 class Chooser : public SkRefCnt { | |
| 110 public: | |
| 111 SK_DECLARE_INST_COUNT(Chooser) | |
| 112 | |
| 113 virtual void begin(int count) {} | |
| 114 virtual void inspect(int index, SkBitmap::Config config, int width, int
height) {} | |
| 115 /** Return the index of the subimage you want, or -1 to choose none of t
hem. | |
| 116 */ | |
| 117 virtual int choose() = 0; | |
| 118 | |
| 119 private: | |
| 120 typedef SkRefCnt INHERITED; | |
| 121 }; | |
| 122 | |
| 123 Chooser* getChooser() const { return fChooser; } | |
| 124 Chooser* setChooser(Chooser*); | |
| 125 | |
| 126 /** This optional table describes the caller's preferred config based on | |
| 127 information about the src data. For this table, the src attributes are | |
| 128 described in terms of depth (index (8), 16, 32/24) and if there is | |
| 129 per-pixel alpha. These inputs combine to create an index into the | |
| 130 pref[] table, which contains the caller's preferred config for that | |
| 131 input, or kNo_Config if there is no preference. | |
| 132 | |
| 133 To specify no preferrence, call setPrefConfigTable(NULL), which is | |
| 134 the default. | |
| 135 | |
| 136 Note, it is still at the discretion of the codec as to what output | |
| 137 config is actually returned, as it may not be able to support the | |
| 138 caller's preference. | |
| 139 | |
| 140 Here is how the index into the table is computed from the src: | |
| 141 depth [8, 16, 32/24] -> 0, 2, 4 | |
| 142 alpha [no, yes] -> 0, 1 | |
| 143 The two index values are OR'd together. | |
| 144 src: 8-index, no-alpha -> 0 | |
| 145 src: 8-index, yes-alpha -> 1 | |
| 146 src: 16bit, no-alpha -> 2 // e.g. 565 | |
| 147 src: 16bit, yes-alpha -> 3 // e.g. 1555 | |
| 148 src: 32/24, no-alpha -> 4 | |
| 149 src: 32/24, yes-alpha -> 5 | |
| 150 */ | |
| 151 void setPrefConfigTable(const SkBitmap::Config pref[6]); | |
| 152 | |
| 153 SkBitmap::Allocator* getAllocator() const { return fAllocator; } | |
| 154 SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*); | |
| 155 | |
| 156 // sample-size, if set to > 1, tells the decoder to return a smaller than | |
| 157 // original bitmap, sampling 1 pixel for every size pixels. e.g. if sample | |
| 158 // size is set to 3, then the returned bitmap will be 1/3 as wide and high, | |
| 159 // and will contain 1/9 as many pixels as the original. | |
| 160 // Note: this is a hint, and the codec may choose to ignore this, or only | |
| 161 // approximate the sample size. | |
| 162 int getSampleSize() const { return fSampleSize; } | |
| 163 void setSampleSize(int size); | |
| 164 | |
| 165 /** Reset the sampleSize to its default of 1 | |
| 166 */ | |
| 167 void resetSampleSize() { this->setSampleSize(1); } | |
| 168 | |
| 169 /** Decoding is synchronous, but for long decodes, a different thread can | |
| 170 call this method safely. This sets a state that the decoders will | |
| 171 periodically check, and if they see it changed to cancel, they will | |
| 172 cancel. This will result in decode() returning false. However, there is | |
| 173 no guarantee that the decoder will see the state change in time, so | |
| 174 it is possible that cancelDecode() will be called, but will be ignored | |
| 175 and decode() will return true (assuming no other problems were | |
| 176 encountered). | |
| 177 | |
| 178 This state is automatically reset at the beginning of decode(). | |
| 179 */ | |
| 180 void cancelDecode() { | |
| 181 // now the subclass must query shouldCancelDecode() to be informed | |
| 182 // of the request | |
| 183 fShouldCancelDecode = true; | |
| 184 } | |
| 185 | |
| 186 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then | |
| 187 only the bitmap's width/height/config need be set. If kDecodePixels_Mode | |
| 188 is passed, then the bitmap must have pixels or a pixelRef. | |
| 189 */ | |
| 190 enum Mode { | |
| 191 kDecodeBounds_Mode, //!< only return width/height/config in bitmap | |
| 192 kDecodePixels_Mode //!< return entire bitmap (including pixels) | |
| 193 }; | |
| 194 | |
| 195 /** Given a stream, decode it into the specified bitmap. | |
| 196 If the decoder can decompress the image, it calls bitmap.setConfig(), | |
| 197 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(), | |
| 198 which will allocated a pixelRef. To access the pixel memory, the codec | |
| 199 needs to call lockPixels/unlockPixels on the | |
| 200 bitmap. It can then set the pixels with the decompressed image. | |
| 201 * If the image cannot be decompressed, return false. After the | |
| 202 * decoding, the function converts the decoded config in bitmap | |
| 203 * to pref if possible. Whether a conversion is feasible is | |
| 204 * tested by Bitmap::canCopyTo(pref). | |
| 205 | |
| 206 note: document use of Allocator, Peeker and Chooser | |
| 207 */ | |
| 208 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode, bool r
euseBitmap = false); | |
| 209 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode, bool reuseBitmap
= false) { | |
| 210 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode, reuseBit
map); | |
| 211 } | |
| 212 | |
| 213 /** | |
| 214 * Given a stream, build an index for doing tile-based decode. | |
| 215 * The built index will be saved in the decoder, and the image size will | |
| 216 * be returned in width and height. | |
| 217 * | |
| 218 * Return true for success or false on failure. | |
| 219 */ | |
| 220 bool buildTileIndex(SkStream*, int *width, int *height); | |
| 221 | |
| 222 /** | |
| 223 * Decode a rectangle subset in the image. | |
| 224 * The method can only be called after buildTileIndex(). | |
| 225 * | |
| 226 * Return true for success. | |
| 227 * Return false if the index is never built or failing in decoding. | |
| 228 */ | |
| 229 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref
); | |
| 230 | |
| 231 /** | |
| 232 * @Deprecated | |
| 233 * Use decodeSubset instead. | |
| 234 */ | |
| 235 bool decodeRegion(SkBitmap* bitmap, const SkIRect& rect, SkBitmap::Config pr
ef) { | |
| 236 return this->decodeSubset(bitmap, rect, pref); | |
| 237 } | |
| 238 | |
| 239 /** Given a stream, this will try to find an appropriate decoder object. | |
| 240 If none is found, the method returns NULL. | |
| 241 */ | |
| 242 static SkImageDecoder* Factory(SkStream*); | |
| 243 | |
| 244 /** Decode the image stored in the specified file, and store the result | |
| 245 in bitmap. Return true for success or false on failure. | |
| 246 | |
| 247 If pref is kNo_Config, then the decoder is free to choose the most natur
al | |
| 248 config given the image data. If pref something other than kNo_Config, | |
| 249 the decoder will attempt to decode the image into that format, unless | |
| 250 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's | |
| 251 config does not support that), in which case the decoder will choose a | |
| 252 closest match configuration. | |
| 253 | |
| 254 @param format On success, if format is non-null, it is set to the format | |
| 255 of the decoded file. On failure it is ignored. | |
| 256 */ | |
| 257 static bool DecodeFile(const char file[], SkBitmap* bitmap, | |
| 258 SkBitmap::Config prefConfig, Mode, | |
| 259 Format* format = NULL); | |
| 260 static bool DecodeFile(const char file[], SkBitmap* bitmap) { | |
| 261 return DecodeFile(file, bitmap, SkBitmap::kNo_Config, | |
| 262 kDecodePixels_Mode, NULL); | |
| 263 } | |
| 264 /** Decode the image stored in the specified memory buffer, and store the | |
| 265 result in bitmap. Return true for success or false on failure. | |
| 266 | |
| 267 If pref is kNo_Config, then the decoder is free to choose the most natur
al | |
| 268 config given the image data. If pref something other than kNo_Config, | |
| 269 the decoder will attempt to decode the image into that format, unless | |
| 270 there is a conflict (e.g. the image has per-pixel alpha and the bitmap's | |
| 271 config does not support that), in which case the decoder will choose a | |
| 272 closest match configuration. | |
| 273 | |
| 274 @param format On success, if format is non-null, it is set to the format | |
| 275 of the decoded buffer. On failure it is ignored. | |
| 276 */ | |
| 277 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, | |
| 278 SkBitmap::Config prefConfig, Mode, | |
| 279 Format* format = NULL); | |
| 280 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ | |
| 281 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config, | |
| 282 kDecodePixels_Mode, NULL); | |
| 283 } | |
| 284 | |
| 285 /** | |
| 286 * Decode memory. | |
| 287 * @param info Output parameter. Returns info about the encoded image. | |
| 288 * @param target Contains the address of pixel memory to decode into | |
| 289 * (which must be large enough to hold the width in info) and | |
| 290 * the row bytes to use. If NULL, returns info and does not | |
| 291 * decode pixels. | |
| 292 * @return bool Whether the function succeeded. | |
| 293 * | |
| 294 * Sample usage: | |
| 295 * <code> | |
| 296 * // Determine the image's info: width/height/config | |
| 297 * SkImage::Info info; | |
| 298 * bool success = DecodeMemoryToTarget(src, size, &info, NULL); | |
| 299 * if (!success) return; | |
| 300 * // Allocate space for the result: | |
| 301 * SkBitmapFactory::Target target; | |
| 302 * target.fAddr = malloc/other allocation | |
| 303 * target.fRowBytes = ... | |
| 304 * // Now decode the actual pixels into target. &info is optional, | |
| 305 * // and could be NULL | |
| 306 * success = DecodeMemoryToTarget(src, size, &info, &target); | |
| 307 * </code> | |
| 308 */ | |
| 309 static bool DecodeMemoryToTarget(const void* buffer, size_t size, SkImage::I
nfo* info, | |
| 310 const SkBitmapFactory::Target* target); | |
| 311 | |
| 312 /** Decode the image stored in the specified SkStream, and store the result | |
| 313 in bitmap. Return true for success or false on failure. | |
| 314 | |
| 315 If pref is kNo_Config, then the decoder is free to choose the most | |
| 316 natural config given the image data. If pref something other than | |
| 317 kNo_Config, the decoder will attempt to decode the image into that | |
| 318 format, unless there is a conflict (e.g. the image has per-pixel alpha | |
| 319 and the bitmap's config does not support that), in which case the | |
| 320 decoder will choose a closest match configuration. | |
| 321 | |
| 322 @param format On success, if format is non-null, it is set to the format | |
| 323 of the decoded stream. On failure it is ignored. | |
| 324 */ | |
| 325 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap, | |
| 326 SkBitmap::Config prefConfig, Mode, | |
| 327 Format* format = NULL); | |
| 328 static bool DecodeStream(SkStream* stream, SkBitmap* bitmap) { | |
| 329 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, | |
| 330 kDecodePixels_Mode, NULL); | |
| 331 } | |
| 332 | |
| 333 /** Return the default config for the running device. | |
| 334 Currently this used as a suggestion to image decoders that need to guess | |
| 335 what config they should decode into. | |
| 336 Default is kNo_Config, but this can be changed with SetDeviceConfig() | |
| 337 */ | |
| 338 static SkBitmap::Config GetDeviceConfig(); | |
| 339 /** Set the default config for the running device. | |
| 340 Currently this used as a suggestion to image decoders that need to guess | |
| 341 what config they should decode into. | |
| 342 Default is kNo_Config. | |
| 343 This can be queried with GetDeviceConfig() | |
| 344 */ | |
| 345 static void SetDeviceConfig(SkBitmap::Config); | |
| 346 | |
| 347 protected: | |
| 348 // must be overridden in subclasses. This guy is called by decode(...) | |
| 349 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; | |
| 350 | |
| 351 // If the decoder wants to support tiled based decoding, | |
| 352 // this method must be overridden. This guy is called by buildTileIndex(...) | |
| 353 virtual bool onBuildTileIndex(SkStream*, int *width, int *height) { | |
| 354 return false; | |
| 355 } | |
| 356 | |
| 357 // If the decoder wants to support tiled based decoding, | |
| 358 // this method must be overridden. This guy is called by decodeRegion(...) | |
| 359 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) { | |
| 360 return false; | |
| 361 } | |
| 362 | |
| 363 /* | |
| 364 * Crop a rectangle from the src Bitmap to the dest Bitmap. src and dst are | |
| 365 * both sampled by sampleSize from an original Bitmap. | |
| 366 * | |
| 367 * @param dst the destination bitmap. | |
| 368 * @param src the source bitmap that is sampled by sampleSize from the | |
| 369 * original bitmap. | |
| 370 * @param sampleSize the sample size that src is sampled from the original b
itmap. | |
| 371 * @param (dstX, dstY) the upper-left point of the dest bitmap in terms of | |
| 372 * the coordinate in the original bitmap. | |
| 373 * @param (width, height) the width and height of the unsampled dst. | |
| 374 * @param (srcX, srcY) the upper-left point of the src bitmap in terms of | |
| 375 * the coordinate in the original bitmap. | |
| 376 * @return bool Whether or not it succeeded. | |
| 377 */ | |
| 378 bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, | |
| 379 int dstX, int dstY, int width, int height, | |
| 380 int srcX, int srcY); | |
| 381 | |
| 382 | |
| 383 | |
| 384 /** Can be queried from within onDecode, to see if the user (possibly in | |
| 385 a different thread) has requested the decode to cancel. If this returns | |
| 386 true, your onDecode() should stop and return false. | |
| 387 Each subclass needs to decide how often it can query this, to balance | |
| 388 responsiveness with performance. | |
| 389 | |
| 390 Calling this outside of onDecode() may return undefined values. | |
| 391 */ | |
| 392 | |
| 393 public: | |
| 394 bool shouldCancelDecode() const { return fShouldCancelDecode; } | |
| 395 | |
| 396 protected: | |
| 397 SkImageDecoder(); | |
| 398 | |
| 399 // helper function for decoders to handle the (common) case where there is o
nly | |
| 400 // once choice available in the image file. | |
| 401 bool chooseFromOneChoice(SkBitmap::Config config, int width, int height) con
st; | |
| 402 | |
| 403 /* Helper for subclasses. Call this to allocate the pixel memory given the
bitmap's | |
| 404 width/height/rowbytes/config. Returns true on success. This method handl
es checking | |
| 405 for an optional Allocator. | |
| 406 */ | |
| 407 bool allocPixelRef(SkBitmap*, SkColorTable*) const; | |
| 408 | |
| 409 enum SrcDepth { | |
| 410 kIndex_SrcDepth, | |
| 411 k16Bit_SrcDepth, | |
| 412 k32Bit_SrcDepth | |
| 413 }; | |
| 414 /** The subclass, inside onDecode(), calls this to determine the config of | |
| 415 the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the | |
| 416 src image. This routine returns the caller's preference given | |
| 417 srcDepth and hasAlpha, or kNo_Config if there is no preference. | |
| 418 | |
| 419 Note: this also takes into account GetDeviceConfig(), so the subclass | |
| 420 need not call that. | |
| 421 */ | |
| 422 SkBitmap::Config getPrefConfig(SrcDepth, bool hasAlpha) const; | |
| 423 | |
| 424 private: | |
| 425 Peeker* fPeeker; | |
| 426 Chooser* fChooser; | |
| 427 SkBitmap::Allocator* fAllocator; | |
| 428 int fSampleSize; | |
| 429 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false | |
| 430 SkBitmap::Config fPrefTable[6]; // use if fUsePrefTable is true | |
| 431 bool fDitherImage; | |
| 432 bool fUsePrefTable; | |
| 433 mutable bool fShouldCancelDecode; | |
| 434 bool fPreferQualityOverSpeed; | |
| 435 | |
| 436 // illegal | |
| 437 SkImageDecoder(const SkImageDecoder&); | |
| 438 SkImageDecoder& operator=(const SkImageDecoder&); | |
| 439 }; | |
| 440 | |
| 441 /** Calling newDecoder with a stream returns a new matching imagedecoder | |
| 442 instance, or NULL if none can be found. The caller must manage its ownership | |
| 443 of the stream as usual, calling unref() when it is done, as the returned | |
| 444 decoder may have called ref() (and if so, the decoder is responsible for | |
| 445 balancing its ownership when it is destroyed). | |
| 446 */ | |
| 447 class SkImageDecoderFactory : public SkRefCnt { | |
| 448 public: | |
| 449 SK_DECLARE_INST_COUNT(SkImageDecoderFactory) | |
| 450 | |
| 451 virtual SkImageDecoder* newDecoder(SkStream*) = 0; | |
| 452 | |
| 453 private: | |
| 454 typedef SkRefCnt INHERITED; | |
| 455 }; | |
| 456 | |
| 457 class SkDefaultImageDecoderFactory : SkImageDecoderFactory { | |
| 458 public: | |
| 459 // calls SkImageDecoder::Factory(stream) | |
| 460 virtual SkImageDecoder* newDecoder(SkStream* stream) { | |
| 461 return SkImageDecoder::Factory(stream); | |
| 462 } | |
| 463 }; | |
| 464 | |
| 465 // This macro declares a global (i.e., non-class owned) creation entry point | |
| 466 // for each decoder (e.g., CreateJPEGImageDecoder) | |
| 467 #define DECLARE_DECODER_CREATOR(codec) \ | |
| 468 SkImageDecoder *Create ## codec (); | |
| 469 | |
| 470 // This macro defines the global creation entry point for each decoder. Each | |
| 471 // decoder implementation that registers with the decoder factory must call it. | |
| 472 #define DEFINE_DECODER_CREATOR(codec) \ | |
| 473 SkImageDecoder *Create ## codec () { \ | |
| 474 return SkNEW( Sk ## codec ); \ | |
| 475 } | |
| 476 | |
| 477 // All the decoders known by Skia. Note that, depending on the compiler settings
, | |
| 478 // not all of these will be available | |
| 479 DECLARE_DECODER_CREATOR(BMPImageDecoder); | |
| 480 DECLARE_DECODER_CREATOR(GIFImageDecoder); | |
| 481 DECLARE_DECODER_CREATOR(ICOImageDecoder); | |
| 482 DECLARE_DECODER_CREATOR(JPEGImageDecoder); | |
| 483 DECLARE_DECODER_CREATOR(PNGImageDecoder); | |
| 484 DECLARE_DECODER_CREATOR(WBMPImageDecoder); | |
| 485 DECLARE_DECODER_CREATOR(WEBPImageDecoder); | |
| 486 | |
| 487 #endif | |
| OLD | NEW |