| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkImageDecoder_DEFINED | 10 #ifndef SkImageDecoder_DEFINED |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 | 232 |
| 233 This state is automatically reset at the beginning of decode(). | 233 This state is automatically reset at the beginning of decode(). |
| 234 */ | 234 */ |
| 235 void cancelDecode() { | 235 void cancelDecode() { |
| 236 // now the subclass must query shouldCancelDecode() to be informed | 236 // now the subclass must query shouldCancelDecode() to be informed |
| 237 // of the request | 237 // of the request |
| 238 fShouldCancelDecode = true; | 238 fShouldCancelDecode = true; |
| 239 } | 239 } |
| 240 | 240 |
| 241 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then | 241 /** Passed to the decode method. If kDecodeBounds_Mode is passed, then |
| 242 only the bitmap's width/height/config need be set. If kDecodePixels_Mode | 242 only the bitmap's info need be set. If kDecodePixels_Mode |
| 243 is passed, then the bitmap must have pixels or a pixelRef. | 243 is passed, then the bitmap must have pixels or a pixelRef. |
| 244 */ | 244 */ |
| 245 enum Mode { | 245 enum Mode { |
| 246 kDecodeBounds_Mode, //!< only return width/height/config in bitmap | 246 kDecodeBounds_Mode, //!< only return info in bitmap |
| 247 kDecodePixels_Mode //!< return entire bitmap (including pixels) | 247 kDecodePixels_Mode //!< return entire bitmap (including pixels) |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 /** Given a stream, decode it into the specified bitmap. | 250 /** Given a stream, decode it into the specified bitmap. |
| 251 If the decoder can decompress the image, it calls bitmap.setConfig(), | 251 If the decoder can decompress the image, it calls bitmap.setInfo(), |
| 252 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(), | 252 and then if the Mode is kDecodePixels_Mode, call allocPixelRef(), |
| 253 which will allocated a pixelRef. To access the pixel memory, the codec | 253 which will allocated a pixelRef. To access the pixel memory, the codec |
| 254 needs to call lockPixels/unlockPixels on the | 254 needs to call lockPixels/unlockPixels on the |
| 255 bitmap. It can then set the pixels with the decompressed image. | 255 bitmap. It can then set the pixels with the decompressed image. |
| 256 * If the image cannot be decompressed, return false. After the | 256 * If the image cannot be decompressed, return false. After the |
| 257 * decoding, the function converts the decoded config in bitmap | 257 * decoding, the function converts the decoded colortype in bitmap |
| 258 * to pref if possible. Whether a conversion is feasible is | 258 * to pref if possible. Whether a conversion is feasible is |
| 259 * tested by Bitmap::canCopyTo(pref). | 259 * tested by Bitmap::canCopyTo(pref). |
| 260 | 260 |
| 261 If an SkBitmap::Allocator is installed via setAllocator, it will be | 261 If an SkBitmap::Allocator is installed via setAllocator, it will be |
| 262 used to allocate the pixel memory. A clever allocator can be used | 262 used to allocate the pixel memory. A clever allocator can be used |
| 263 to allocate the memory from a cache, volatile memory, or even from | 263 to allocate the memory from a cache, volatile memory, or even from |
| 264 an existing bitmap's memory. | 264 an existing bitmap's memory. |
| 265 | 265 |
| 266 If a Peeker is installed via setPeeker, it may be used to peek into | 266 If a Peeker is installed via setPeeker, it may be used to peek into |
| 267 meta data during the decode. | 267 meta data during the decode. |
| 268 | |
| 269 If a Chooser is installed via setChooser, it may be used to select | |
| 270 which image to return from a format that contains multiple images. | |
| 271 */ | 268 */ |
| 272 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode); | 269 bool decode(SkStream*, SkBitmap* bitmap, SkColorType pref, Mode); |
| 273 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { | 270 bool decode(SkStream* stream, SkBitmap* bitmap, Mode mode) { |
| 274 return this->decode(stream, bitmap, SkBitmap::kNo_Config, mode); | 271 return this->decode(stream, bitmap, kUnknown_SkColorType, mode); |
| 275 } | 272 } |
| 276 | 273 |
| 277 /** | 274 /** |
| 278 * Given a stream, build an index for doing tile-based decode. | 275 * Given a stream, build an index for doing tile-based decode. |
| 279 * The built index will be saved in the decoder, and the image size will | 276 * The built index will be saved in the decoder, and the image size will |
| 280 * be returned in width and height. | 277 * be returned in width and height. |
| 281 * | 278 * |
| 282 * Return true for success or false on failure. | 279 * Return true for success or false on failure. |
| 283 */ | 280 */ |
| 284 bool buildTileIndex(SkStreamRewindable*, int *width, int *height); | 281 bool buildTileIndex(SkStreamRewindable*, int *width, int *height); |
| 285 | 282 |
| 286 /** | 283 /** |
| 287 * Decode a rectangle subset in the image. | 284 * Decode a rectangle subset in the image. |
| 288 * The method can only be called after buildTileIndex(). | 285 * The method can only be called after buildTileIndex(). |
| 289 * | 286 * |
| 290 * Return true for success. | 287 * Return true for success. |
| 291 * Return false if the index is never built or failing in decoding. | 288 * Return false if the index is never built or failing in decoding. |
| 292 */ | 289 */ |
| 293 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref
); | 290 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkColorType pref); |
| 294 | 291 |
| 295 /** Given a stream, this will try to find an appropriate decoder object. | 292 /** Given a stream, this will try to find an appropriate decoder object. |
| 296 If none is found, the method returns NULL. | 293 If none is found, the method returns NULL. |
| 297 */ | 294 */ |
| 298 static SkImageDecoder* Factory(SkStreamRewindable*); | 295 static SkImageDecoder* Factory(SkStreamRewindable*); |
| 299 | 296 |
| 300 /** Decode the image stored in the specified file, and store the result | 297 /** Decode the image stored in the specified file, and store the result |
| 301 in bitmap. Return true for success or false on failure. | 298 in bitmap. Return true for success or false on failure. |
| 302 | 299 |
| 303 @param prefConfig If the PrefConfigTable is not set, prefer this config. | 300 @param pref If the PrefConfigTable is not set, prefer this colortype. |
| 304 See NOTE ABOUT PREFERRED CONFIGS. | 301 See NOTE ABOUT PREFERRED CONFIGS. |
| 305 | 302 |
| 306 @param format On success, if format is non-null, it is set to the format | 303 @param format On success, if format is non-null, it is set to the format |
| 307 of the decoded file. On failure it is ignored. | 304 of the decoded file. On failure it is ignored. |
| 308 */ | 305 */ |
| 309 static bool DecodeFile(const char file[], SkBitmap* bitmap, | 306 static bool DecodeFile(const char file[], SkBitmap* bitmap, SkColorType pref
, Mode, |
| 310 SkBitmap::Config prefConfig, Mode, | |
| 311 Format* format = NULL); | 307 Format* format = NULL); |
| 312 static bool DecodeFile(const char file[], SkBitmap* bitmap) { | 308 static bool DecodeFile(const char file[], SkBitmap* bitmap) { |
| 313 return DecodeFile(file, bitmap, SkBitmap::kNo_Config, | 309 return DecodeFile(file, bitmap, kUnknown_SkColorType, kDecodePixels_Mode
, NULL); |
| 314 kDecodePixels_Mode, NULL); | |
| 315 } | 310 } |
| 311 |
| 316 /** Decode the image stored in the specified memory buffer, and store the | 312 /** Decode the image stored in the specified memory buffer, and store the |
| 317 result in bitmap. Return true for success or false on failure. | 313 result in bitmap. Return true for success or false on failure. |
| 318 | 314 |
| 319 @param prefConfig If the PrefConfigTable is not set, prefer this config. | 315 @param pref If the PrefConfigTable is not set, prefer this colortype. |
| 320 See NOTE ABOUT PREFERRED CONFIGS. | 316 See NOTE ABOUT PREFERRED CONFIGS. |
| 321 | 317 |
| 322 @param format On success, if format is non-null, it is set to the format | 318 @param format On success, if format is non-null, it is set to the format |
| 323 of the decoded buffer. On failure it is ignored. | 319 of the decoded buffer. On failure it is ignored. |
| 324 */ | 320 */ |
| 325 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, | 321 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap,
SkColorType pref, |
| 326 SkBitmap::Config prefConfig, Mode, | 322 Mode, Format* format = NULL); |
| 327 Format* format = NULL); | |
| 328 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ | 323 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){ |
| 329 return DecodeMemory(buffer, size, bitmap, SkBitmap::kNo_Config, | 324 return DecodeMemory(buffer, size, bitmap, kUnknown_SkColorType, kDecodeP
ixels_Mode, NULL); |
| 330 kDecodePixels_Mode, NULL); | |
| 331 } | 325 } |
| 332 | 326 |
| 333 /** | 327 /** |
| 334 * Struct containing information about a pixel destination. | 328 * Struct containing information about a pixel destination. |
| 335 */ | 329 */ |
| 336 struct Target { | 330 struct Target { |
| 337 /** | 331 /** |
| 338 * Pre-allocated memory. | 332 * Pre-allocated memory. |
| 339 */ | 333 */ |
| 340 void* fAddr; | 334 void* fAddr; |
| 341 | 335 |
| 342 /** | 336 /** |
| 343 * Rowbytes of the allocated memory. | 337 * Rowbytes of the allocated memory. |
| 344 */ | 338 */ |
| 345 size_t fRowBytes; | 339 size_t fRowBytes; |
| 346 }; | 340 }; |
| 347 | 341 |
| 348 /** Decode the image stored in the specified SkStreamRewindable, and store t
he result | 342 /** Decode the image stored in the specified SkStreamRewindable, and store t
he result |
| 349 in bitmap. Return true for success or false on failure. | 343 in bitmap. Return true for success or false on failure. |
| 350 | 344 |
| 351 @param prefConfig If the PrefConfigTable is not set, prefer this config. | 345 @param pref If the PrefConfigTable is not set, prefer this colortype. |
| 352 See NOTE ABOUT PREFERRED CONFIGS. | 346 See NOTE ABOUT PREFERRED CONFIGS. |
| 353 | 347 |
| 354 @param format On success, if format is non-null, it is set to the format | 348 @param format On success, if format is non-null, it is set to the format |
| 355 of the decoded stream. On failure it is ignored. | 349 of the decoded stream. On failure it is ignored. |
| 356 */ | 350 */ |
| 357 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, | 351 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, SkCol
orType pref, Mode, |
| 358 SkBitmap::Config prefConfig, Mode, | |
| 359 Format* format = NULL); | 352 Format* format = NULL); |
| 360 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) { | 353 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) { |
| 361 return DecodeStream(stream, bitmap, SkBitmap::kNo_Config, | 354 return DecodeStream(stream, bitmap, kUnknown_SkColorType, kDecodePixels_
Mode, NULL); |
| 362 kDecodePixels_Mode, NULL); | |
| 363 } | 355 } |
| 364 | 356 |
| 357 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CONFIG |
| 358 bool decode(SkStream*, SkBitmap* bitmap, SkBitmap::Config pref, Mode mode) { |
| 359 return this->decode(stream, bitmap, SkBitmapConfigToColorType(pref), mod
e); |
| 360 } |
| 361 bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkBitmap::Config pref
) { |
| 362 return this->decodeSubset(bm, subset, SkBitmapConfigToColorType(pref)); |
| 363 } |
| 364 static bool DecodeFile(const char file[], SkBitmap* bitmap, SkBitmapConfig p
ref, Mode mode, |
| 365 Format* format = NULL) { |
| 366 return DecodeFile(file, bitmap, SkBitmapConfigToColorType(pref), mode, f
ormat); |
| 367 } |
| 368 static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, |
| 369 SkBitmap::Config pref, Mode mode, Format* format =
NULL) { |
| 370 return DecodeMemory(buffer, size, bitmap, SkBitmapConfigToColorType(pref
), mode, format); |
| 371 } |
| 372 static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, SkBit
map::Config pref, |
| 373 Mode mode, Format* format = NULL) { |
| 374 return DecodeStream(stream, bitmap, SkBitmapConfigToColorType(pref), mod
e, format); |
| 375 } |
| 376 #endif |
| 377 |
| 365 protected: | 378 protected: |
| 366 // must be overridden in subclasses. This guy is called by decode(...) | 379 // must be overridden in subclasses. This guy is called by decode(...) |
| 367 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; | 380 virtual bool onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0; |
| 368 | 381 |
| 369 // If the decoder wants to support tiled based decoding, | 382 // If the decoder wants to support tiled based decoding, |
| 370 // this method must be overridden. This guy is called by buildTileIndex(...) | 383 // this method must be overridden. This guy is called by buildTileIndex(...) |
| 371 virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height)
{ | 384 virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height)
{ |
| 372 return false; | 385 return false; |
| 373 } | 386 } |
| 374 | 387 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 396 bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, | 409 bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
| 397 int dstX, int dstY, int width, int height, | 410 int dstX, int dstY, int width, int height, |
| 398 int srcX, int srcY); | 411 int srcX, int srcY); |
| 399 | 412 |
| 400 /** | 413 /** |
| 401 * Copy all fields on this decoder to the other decoder. Used by subclasses | 414 * Copy all fields on this decoder to the other decoder. Used by subclasses |
| 402 * to decode a subimage using a different decoder, but with the same settin
gs. | 415 * to decode a subimage using a different decoder, but with the same settin
gs. |
| 403 */ | 416 */ |
| 404 void copyFieldsToOther(SkImageDecoder* other); | 417 void copyFieldsToOther(SkImageDecoder* other); |
| 405 | 418 |
| 406 /** | |
| 407 * Return the default preference being used by the current or latest call t
o | |
| 408 * decode. | |
| 409 */ | |
| 410 SkBitmap::Config getDefaultPref() { return fDefaultPref; } | |
| 411 | |
| 412 /** Can be queried from within onDecode, to see if the user (possibly in | 419 /** Can be queried from within onDecode, to see if the user (possibly in |
| 413 a different thread) has requested the decode to cancel. If this returns | 420 a different thread) has requested the decode to cancel. If this returns |
| 414 true, your onDecode() should stop and return false. | 421 true, your onDecode() should stop and return false. |
| 415 Each subclass needs to decide how often it can query this, to balance | 422 Each subclass needs to decide how often it can query this, to balance |
| 416 responsiveness with performance. | 423 responsiveness with performance. |
| 417 | 424 |
| 418 Calling this outside of onDecode() may return undefined values. | 425 Calling this outside of onDecode() may return undefined values. |
| 419 */ | 426 */ |
| 420 | 427 |
| 421 public: | 428 public: |
| 422 bool shouldCancelDecode() const { return fShouldCancelDecode; } | 429 bool shouldCancelDecode() const { return fShouldCancelDecode; } |
| 423 | 430 |
| 424 protected: | 431 protected: |
| 425 SkImageDecoder(); | 432 SkImageDecoder(); |
| 426 | 433 |
| 434 /** |
| 435 * Return the default preference being used by the current or latest call t
o decode. |
| 436 */ |
| 437 SkColorType getDefaultPref() { return fDefaultPref; } |
| 438 |
| 427 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER | 439 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
| 428 // helper function for decoders to handle the (common) case where there is o
nly | 440 // helper function for decoders to handle the (common) case where there is o
nly |
| 429 // once choice available in the image file. | 441 // once choice available in the image file. |
| 430 bool chooseFromOneChoice(SkColorType, int width, int height) const; | 442 bool chooseFromOneChoice(SkColorType, int width, int height) const; |
| 431 #endif | 443 #endif |
| 432 | 444 |
| 433 /* Helper for subclasses. Call this to allocate the pixel memory given the
bitmap's | 445 /* Helper for subclasses. Call this to allocate the pixel memory given the
bitmap's info. |
| 434 width/height/rowbytes/config. Returns true on success. This method handl
es checking | 446 Returns true on success. This method handles checking for an optional Al
locator. |
| 435 for an optional Allocator. | |
| 436 */ | 447 */ |
| 437 bool allocPixelRef(SkBitmap*, SkColorTable*) const; | 448 bool allocPixelRef(SkBitmap*, SkColorTable*) const; |
| 438 | 449 |
| 439 /** | 450 /** |
| 440 * The raw data of the src image. | 451 * The raw data of the src image. |
| 441 */ | 452 */ |
| 442 enum SrcDepth { | 453 enum SrcDepth { |
| 443 // Color-indexed. | 454 // Color-indexed. |
| 444 kIndex_SrcDepth, | 455 kIndex_SrcDepth, |
| 445 // Grayscale in 8 bits. | 456 // Grayscale in 8 bits. |
| 446 k8BitGray_SrcDepth, | 457 k8BitGray_SrcDepth, |
| 447 // 8 bits per component. Used for 24 bit if there is no alpha. | 458 // 8 bits per component. Used for 24 bit if there is no alpha. |
| 448 k32Bit_SrcDepth, | 459 k32Bit_SrcDepth, |
| 449 }; | 460 }; |
| 450 /** The subclass, inside onDecode(), calls this to determine the colorType o
f | 461 /** The subclass, inside onDecode(), calls this to determine the colorType o
f |
| 451 the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the | 462 the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the |
| 452 src image. This routine returns the caller's preference given | 463 src image. This routine returns the caller's preference given |
| 453 srcDepth and hasAlpha, or kUnknown_SkColorType if there is no preference
. | 464 srcDepth and hasAlpha, or kUnknown_SkColorType if there is no preference
. |
| 454 */ | 465 */ |
| 455 SkColorType getPrefColorType(SrcDepth, bool hasAlpha) const; | 466 SkColorType getPrefColorType(SrcDepth, bool hasAlpha) const; |
| 456 | 467 |
| 457 private: | 468 private: |
| 458 Peeker* fPeeker; | 469 Peeker* fPeeker; |
| 459 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER | 470 #ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER |
| 460 Chooser* fChooser; | 471 Chooser* fChooser; |
| 461 #endif | 472 #endif |
| 462 SkBitmap::Allocator* fAllocator; | 473 SkBitmap::Allocator* fAllocator; |
| 463 int fSampleSize; | 474 int fSampleSize; |
| 464 SkBitmap::Config fDefaultPref; // use if fUsePrefTable is false | 475 SkColorType fDefaultPref; // use if fUsePrefTable is false |
| 465 PrefConfigTable fPrefTable; // use if fUsePrefTable is true | 476 PrefConfigTable fPrefTable; // use if fUsePrefTable is true |
| 466 bool fDitherImage; | 477 bool fDitherImage; |
| 467 bool fUsePrefTable; | 478 bool fUsePrefTable; |
| 468 bool fSkipWritingZeroes; | 479 bool fSkipWritingZeroes; |
| 469 mutable bool fShouldCancelDecode; | 480 mutable bool fShouldCancelDecode; |
| 470 bool fPreferQualityOverSpeed; | 481 bool fPreferQualityOverSpeed; |
| 471 bool fRequireUnpremultipliedColors; | 482 bool fRequireUnpremultipliedColors; |
| 472 }; | 483 }; |
| 473 | 484 |
| 474 /** Calling newDecoder with a stream returns a new matching imagedecoder | 485 /** Calling newDecoder with a stream returns a new matching imagedecoder |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 DECLARE_DECODER_CREATOR(WEBPImageDecoder); | 529 DECLARE_DECODER_CREATOR(WEBPImageDecoder); |
| 519 DECLARE_DECODER_CREATOR(PKMImageDecoder); | 530 DECLARE_DECODER_CREATOR(PKMImageDecoder); |
| 520 DECLARE_DECODER_CREATOR(KTXImageDecoder); | 531 DECLARE_DECODER_CREATOR(KTXImageDecoder); |
| 521 | 532 |
| 522 // Typedefs to make registering decoder and formatter callbacks easier. | 533 // Typedefs to make registering decoder and formatter callbacks easier. |
| 523 // These have to be defined outside SkImageDecoder. :( | 534 // These have to be defined outside SkImageDecoder. :( |
| 524 typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecod
er_DecodeReg; | 535 typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecod
er_DecodeReg; |
| 525 typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecod
er_FormatReg; | 536 typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecod
er_FormatReg; |
| 526 | 537 |
| 527 #endif | 538 #endif |
| OLD | NEW |