Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkImageDecoder Class Referenceabstract

Base class for decoding compressed images into a SkBitmap. More...

#include <SkImageDecoder.h>

Inheritance diagram for SkImageDecoder:

Public Types

enum  Format {
  kUnknown_Format, kBMP_Format, kGIF_Format, kICO_Format,
  kJPEG_Format, kPNG_Format, kWBMP_Format, kWEBP_Format,
  kPKM_Format, kKTX_Format, kASTC_Format, kLastKnownFormat = kKTX_Format
}
 
enum  Mode { kDecodeBounds_Mode, kDecodePixels_Mode }
 Passed to the decode method. More...
 
enum  Result { kFailure = 0, kPartialSuccess = 1, kSuccess = 2 }
 Result of a decode. More...
 

Public Member Functions

virtual Format getFormat () const
 Return the format of image this decoder can decode. More...
 
bool decodeYUV8Planes (SkStream *stream, SkISize componentSizes[3], void *planes[3], size_t rowBytes[3], SkYUVColorSpace *)
 If planes or rowBytes is NULL, decodes the header and computes componentSizes for memory allocation. More...
 
const char * getFormatName () const
 Return a readable string of the value returned by getFormat().
 
bool getSkipWritingZeroes () const
 Whether the decoder should skip writing zeroes to output if possible.
 
void setSkipWritingZeroes (bool skip)
 Set to true if the decoder should skip writing any zeroes when creating the output image. More...
 
bool getDitherImage () const
 Returns true if the decoder should try to dither the resulting image. More...
 
void setDitherImage (bool dither)
 Set to true if the the decoder should try to dither the resulting image. More...
 
bool getPreferQualityOverSpeed () const
 Returns true if the decoder should try to decode the resulting image to a higher quality even at the expense of the decoding speed.
 
void setPreferQualityOverSpeed (bool qualityOverSpeed)
 Set to true if the the decoder should try to decode the resulting image to a higher quality even at the expense of the decoding speed.
 
void setRequireUnpremultipliedColors (bool request)
 Set to true to require the decoder to return a bitmap with unpremultiplied colors. More...
 
bool getRequireUnpremultipliedColors () const
 Returns true if the decoder will only return bitmaps with unpremultiplied colors.
 
SkPngChunkReadergetPeeker () const
 
SkPngChunkReadersetPeeker (SkPngChunkReader *)
 
void setPreserveSrcDepth (bool preserve)
 By default, the codec will try to comply with the "pref" colortype that is passed to decode() or decodeSubset(). More...
 
SkBitmap::AllocatorgetAllocator () const
 
SkBitmap::AllocatorsetAllocator (SkBitmap::Allocator *)
 
int getSampleSize () const
 
void setSampleSize (int size)
 
void resetSampleSize ()
 Reset the sampleSize to its default of 1.
 
void cancelDecode ()
 Decoding is synchronous, but for long decodes, a different thread can call this method safely. More...
 
Result decode (SkStream *, SkBitmap *bitmap, SkColorType pref, Mode)
 Given a stream, decode it into the specified bitmap. More...
 
Result decode (SkStream *stream, SkBitmap *bitmap, Mode mode)
 
bool shouldCancelDecode () const
 Can be queried from within onDecode, to see if the user (possibly in a different thread) has requested the decode to cancel. More...
 

Static Public Member Functions

static Format GetStreamFormat (SkStreamRewindable *)
 Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined. More...
 
static const char * GetFormatName (Format)
 Return a readable string of the Format provided.
 
static SkImageDecoderFactory (SkStreamRewindable *)
 Given a stream, this will try to find an appropriate decoder object. More...
 
static bool DecodeFile (const char file[], SkBitmap *bitmap, SkColorType pref, Mode, Format *format=NULL)
 Decode the image stored in the specified file, and store the result in bitmap. More...
 
static bool DecodeFile (const char file[], SkBitmap *bitmap)
 
static bool DecodeMemory (const void *buffer, size_t size, SkBitmap *bitmap, SkColorType pref, Mode, Format *format=NULL)
 Decode the image stored in the specified memory buffer, and store the result in bitmap. More...
 
static bool DecodeMemory (const void *buffer, size_t size, SkBitmap *bitmap)
 
static bool DecodeStream (SkStreamRewindable *stream, SkBitmap *bitmap, SkColorType pref, Mode, Format *format=NULL)
 Decode the image stored in the specified SkStreamRewindable, and store the result in bitmap. More...
 
static bool DecodeStream (SkStreamRewindable *stream, SkBitmap *bitmap)
 

Protected Types

enum  SrcDepth { kIndex_SrcDepth, k8BitGray_SrcDepth, k32Bit_SrcDepth }
 The raw data of the src image.
 

Protected Member Functions

virtual Result onDecode (SkStream *, SkBitmap *bitmap, Mode)=0
 
virtual bool onDecodeYUV8Planes (SkStream *, SkISize[3], void *[3], size_t[3], SkYUVColorSpace *)
 If planes or rowBytes is NULL, decodes the header and computes componentSizes for memory allocation. More...
 
void copyFieldsToOther (SkImageDecoder *other)
 Copy all fields on this decoder to the other decoder. More...
 
SkColorType getDefaultPref ()
 Return the default preference being used by the current or latest call to decode.
 
bool allocPixelRef (SkBitmap *, SkColorTable *) const
 
SkColorType getPrefColorType (SrcDepth, bool hasAlpha) const
 The subclass, inside onDecode(), calls this to determine the colorType of the returned bitmap. More...
 

Private Attributes

SkPngChunkReaderfPeeker
 
SkBitmap::AllocatorfAllocator
 
int fSampleSize
 
SkColorType fDefaultPref
 
bool fPreserveSrcDepth
 
bool fDitherImage
 
bool fSkipWritingZeroes
 
bool fShouldCancelDecode
 
bool fPreferQualityOverSpeed
 
bool fRequireUnpremultipliedColors
 

Detailed Description

Base class for decoding compressed images into a SkBitmap.

Member Enumeration Documentation

Passed to the decode method.

If kDecodeBounds_Mode is passed, then only the bitmap's info need be set. If kDecodePixels_Mode is passed, then the bitmap must have pixels or a pixelRef.

Enumerator
kDecodeBounds_Mode 

only return info in bitmap

kDecodePixels_Mode 

return entire bitmap (including pixels)

Result of a decode.

If read as a boolean, a partial success is considered a success (true).

Enumerator
kFailure 

Image failed to decode. bitmap will be.

kPartialSuccess 

Part of the image decoded. The rest is.

kSuccess 

The entire image was decoded, if Mode is.

Member Function Documentation

void SkImageDecoder::cancelDecode ( )
inline

Decoding is synchronous, but for long decodes, a different thread can call this method safely.

This sets a state that the decoders will periodically check, and if they see it changed to cancel, they will cancel. This will result in decode() returning false. However, there is no guarantee that the decoder will see the state change in time, so it is possible that cancelDecode() will be called, but will be ignored and decode() will return true (assuming no other problems were encountered).

This state is automatically reset at the beginning of decode().

void SkImageDecoder::copyFieldsToOther ( SkImageDecoder other)
protected

Copy all fields on this decoder to the other decoder.

Used by subclasses to decode a subimage using a different decoder, but with the same settings.

Result SkImageDecoder::decode ( SkStream ,
SkBitmap bitmap,
SkColorType  pref,
Mode   
)

Given a stream, decode it into the specified bitmap.

If the decoder can decompress the image, it calls bitmap.setInfo(), and then if the Mode is kDecodePixels_Mode, call allocPixelRef(), which will allocated a pixelRef. To access the pixel memory, the codec needs to call lockPixels/unlockPixels on the bitmap. It can then set the pixels with the decompressed image. If the image cannot be decompressed, return kFailure. After the decoding, the function converts the decoded colortype in bitmap to pref if possible. Whether a conversion is feasible is tested by Bitmap::canCopyTo(pref).

If an SkBitmap::Allocator is installed via setAllocator, it will be used to allocate the pixel memory. A clever allocator can be used to allocate the memory from a cache, volatile memory, or even from an existing bitmap's memory.

If an SkPngChunkReader is installed via setPeeker, it may be used to peek into meta data during the decode.

static bool SkImageDecoder::DecodeFile ( const char  file[],
SkBitmap bitmap,
SkColorType  pref,
Mode  ,
Format *  format = NULL 
)
static

Decode the image stored in the specified file, and store the result in bitmap.

Return true for success or false on failure.

Parameters
prefPrefer this colortype.
formatOn success, if format is non-null, it is set to the format of the decoded file. On failure it is ignored.
static bool SkImageDecoder::DecodeMemory ( const void *  buffer,
size_t  size,
SkBitmap bitmap,
SkColorType  pref,
Mode  ,
Format *  format = NULL 
)
static

Decode the image stored in the specified memory buffer, and store the result in bitmap.

Return true for success or false on failure.

Parameters
prefPrefer this colortype.
formatOn success, if format is non-null, it is set to the format of the decoded buffer. On failure it is ignored.
static bool SkImageDecoder::DecodeStream ( SkStreamRewindable stream,
SkBitmap bitmap,
SkColorType  pref,
Mode  ,
Format *  format = NULL 
)
static

Decode the image stored in the specified SkStreamRewindable, and store the result in bitmap.

Return true for success or false on failure.

Parameters
prefPrefer this colortype.
formatOn success, if format is non-null, it is set to the format of the decoded stream. On failure it is ignored.
bool SkImageDecoder::decodeYUV8Planes ( SkStream stream,
SkISize  componentSizes[3],
void *  planes[3],
size_t  rowBytes[3],
SkYUVColorSpace *   
)

If planes or rowBytes is NULL, decodes the header and computes componentSizes for memory allocation.

Otherwise, decodes the YUV planes into the provided image planes and updates componentSizes to the final image size. Returns whether the decoding was successful.

static SkImageDecoder* SkImageDecoder::Factory ( SkStreamRewindable )
static

Given a stream, this will try to find an appropriate decoder object.

If none is found, the method returns NULL.

bool SkImageDecoder::getDitherImage ( ) const
inline

Returns true if the decoder should try to dither the resulting image.

The default setting is true.

virtual Format SkImageDecoder::getFormat ( ) const
virtual

Return the format of image this decoder can decode.

If this decoder can decode multiple formats, kUnknown_Format will be returned.

SkColorType SkImageDecoder::getPrefColorType ( SrcDepth  ,
bool  hasAlpha 
) const
protected

The subclass, inside onDecode(), calls this to determine the colorType of the returned bitmap.

SrcDepth and hasAlpha reflect the raw data of the src image. This routine returns the caller's preference given srcDepth and hasAlpha, or kUnknown_SkColorType if there is no preference.

static Format SkImageDecoder::GetStreamFormat ( SkStreamRewindable )
static

Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined.

Rewinds the stream before returning.

virtual bool SkImageDecoder::onDecodeYUV8Planes ( SkStream ,
SkISize  [3],
void *  [3],
size_t  [3],
SkYUVColorSpace *   
)
inlineprotectedvirtual

If planes or rowBytes is NULL, decodes the header and computes componentSizes for memory allocation.

Otherwise, decodes the YUV planes into the provided image planes and updates componentSizes to the final image size. Returns whether the decoding was successful.

void SkImageDecoder::setDitherImage ( bool  dither)
inline

Set to true if the the decoder should try to dither the resulting image.

The default setting is true.

void SkImageDecoder::setPreserveSrcDepth ( bool  preserve)
inline

By default, the codec will try to comply with the "pref" colortype that is passed to decode() or decodeSubset().

However, this can be called to override that, causing the codec to try to match the src depth instead (as shown below).

src_8Index  -> kIndex_8_SkColorType
src_8Gray   -> kN32_SkColorType
src_8bpc    -> kN32_SkColorType
void SkImageDecoder::setRequireUnpremultipliedColors ( bool  request)
inline

Set to true to require the decoder to return a bitmap with unpremultiplied colors.

The default is false, meaning the resulting bitmap will have its colors premultiplied. NOTE: Passing true to this function may result in a bitmap which cannot be properly used by Skia.

void SkImageDecoder::setSkipWritingZeroes ( bool  skip)
inline

Set to true if the decoder should skip writing any zeroes when creating the output image.

This is a hint that may not be respected by the decoder. It should only be used if it is known that the memory to write to has already been set to 0; otherwise the resulting image will have garbage. This is ideal for images that contain a lot of completely transparent pixels, but may be a performance hit for an image that has only a few transparent pixels. The default is false.

bool SkImageDecoder::shouldCancelDecode ( ) const
inline

Can be queried from within onDecode, to see if the user (possibly in a different thread) has requested the decode to cancel.

If this returns true, your onDecode() should stop and return false. Each subclass needs to decide how often it can query this, to balance responsiveness with performance.

Calling this outside of onDecode() may return undefined values.


The documentation for this class was generated from the following file: