2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkImageDecoder_DEFINED
|
|
|
|
#define SkImageDecoder_DEFINED
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
2013-02-22 21:38:35 +00:00
|
|
|
#include "SkImage.h"
|
2013-03-14 14:42:18 +00:00
|
|
|
#include "SkRect.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRefCnt.h"
|
2013-09-04 17:20:18 +00:00
|
|
|
#include "SkTRegistry.h"
|
2013-05-31 14:00:10 +00:00
|
|
|
#include "SkTypes.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
class SkStream;
|
2013-09-25 21:34:24 +00:00
|
|
|
class SkStreamRewindable;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
/** \class SkImageDecoder
|
|
|
|
|
|
|
|
Base class for decoding compressed images into a SkBitmap
|
|
|
|
*/
|
2014-04-07 19:34:38 +00:00
|
|
|
class SkImageDecoder : SkNoncopyable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
virtual ~SkImageDecoder();
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
enum Format {
|
|
|
|
kUnknown_Format,
|
|
|
|
kBMP_Format,
|
|
|
|
kGIF_Format,
|
|
|
|
kICO_Format,
|
|
|
|
kJPEG_Format,
|
|
|
|
kPNG_Format,
|
|
|
|
kWBMP_Format,
|
2013-03-14 14:42:18 +00:00
|
|
|
kWEBP_Format,
|
2014-05-22 18:40:29 +00:00
|
|
|
kPKM_Format,
|
2014-06-03 20:04:35 +00:00
|
|
|
kKTX_Format,
|
2014-08-07 19:58:38 +00:00
|
|
|
kASTC_Format,
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2014-06-03 20:04:35 +00:00
|
|
|
kLastKnownFormat = kKTX_Format,
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2013-04-17 21:07:55 +00:00
|
|
|
/** Return the format of image this decoder can decode. If this decoder can decode multiple
|
|
|
|
formats, kUnknown_Format will be returned.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
virtual Format getFormat() const;
|
|
|
|
|
2014-10-16 20:10:57 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
bool decodeYUV8Planes(SkStream* stream, SkISize componentSizes[3], void* planes[3],
|
|
|
|
size_t rowBytes[3], SkYUVColorSpace*);
|
|
|
|
|
2013-09-25 21:34:24 +00:00
|
|
|
/** Return the format of the SkStreamRewindable or kUnknown_Format if it cannot be determined.
|
|
|
|
Rewinds the stream before returning.
|
2013-04-25 17:33:51 +00:00
|
|
|
*/
|
2013-09-25 21:34:24 +00:00
|
|
|
static Format GetStreamFormat(SkStreamRewindable*);
|
2013-04-25 17:33:51 +00:00
|
|
|
|
2013-05-15 14:53:49 +00:00
|
|
|
/** Return a readable string of the Format provided.
|
|
|
|
*/
|
|
|
|
static const char* GetFormatName(Format);
|
|
|
|
|
2013-04-17 21:07:55 +00:00
|
|
|
/** Return a readable string of the value returned by getFormat().
|
2013-03-14 14:42:18 +00:00
|
|
|
*/
|
|
|
|
const char* getFormatName() const;
|
|
|
|
|
Add an option on SkImageDecoder to skip writing 0s.
Only implemented for PNG.
Add a getter and setter, and sets the default to false in the
constructor. Also copies the setting in copyFieldsToOther.
Fix an indpendent bug where fDitherImage was not being copied in
copyFieldsToOther.
In SkScaledBitmapSampler::begin, consolidate the settings passed in
by passing a const reference to the decoder. The decoder can be
referenced for its settings of dither, unpremultiplied, and now
skipping writing zeroes. Update callers to use the new API. In png
decoder, rather than passing around a pointer to an initial
read of getDitherImage, and potentially changing it, look at the
field on the decoder itself, and modify it directly. This is a
change in behavior - now if that same decoder is used to decode
a different image, the dither setting has changed. I think this is
okay because A) the typical use case is to use a new decoder for
each decode, B) we do not make any promises that a decode does not
change the decoder and C) it makes the code in SkScaledBitmapSampler
much cleaner.
In SkScaledBitmapScampler, add new row procs for skipping zeroes. Now
that choosing the row proc has five dimensions (src config, dst config,
dither, skip writing zeroes, unpremultiplied), use a new method: each
src/dst combination has a function for choosing the right proc depending
on the decoder.
SkScaledBitmapScampler::RowProc is now public for convenience.
Remove Sample_Gray_D8888_Unpremul, which is effectively no different
from Sample_Gray_D8888.
In cases where unpremultiplied was trivial, such as 565 and when
sampling from gray, decoding may now succeed.
Add a benchmark (currently disabled) for comparing the speed of skipping
writing zeroes versus not skipping. For this particular image, which is
mostly transparent pixels, normal decoding took about 3.6 milliseconds,
while skipping zeroes in the decode took only about 2.5 milliseconds
(this is on a Nexus 4). Presumably it would be slower on an image
with a small amount of transparency, but there will be no slowdown
for an image which reports that it has no transparency.
In SkImageRef_ashmem, always skip writing zeroes, since ashmem
memory is guaranteed to be initialized to 0.
Add a flag to skip writing zeroes in skimage.
Add a regression test for choosing the rowproc to ensure I did not
change any behavior accidentally.
BUG=skia:1661
R=reed@google.com
Review URL: https://codereview.chromium.org/24269006
git-svn-id: http://skia.googlecode.com/svn/trunk@11558 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-10-01 17:27:15 +00:00
|
|
|
/** Whether the decoder should skip writing zeroes to output if possible.
|
|
|
|
*/
|
|
|
|
bool getSkipWritingZeroes() const { return fSkipWritingZeroes; }
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
void setSkipWritingZeroes(bool skip) { fSkipWritingZeroes = skip; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns true if the decoder should try to dither the resulting image.
|
|
|
|
The default setting is true.
|
|
|
|
*/
|
|
|
|
bool getDitherImage() const { return fDitherImage; }
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Set to true if the the decoder should try to dither the resulting image.
|
|
|
|
The default setting is true.
|
|
|
|
*/
|
|
|
|
void setDitherImage(bool dither) { fDitherImage = dither; }
|
|
|
|
|
2013-03-14 14:42:18 +00:00
|
|
|
/** Returns true if the decoder should try to decode the
|
|
|
|
resulting image to a higher quality even at the expense of
|
|
|
|
the decoding speed.
|
|
|
|
*/
|
|
|
|
bool getPreferQualityOverSpeed() const { return fPreferQualityOverSpeed; }
|
|
|
|
|
|
|
|
/** 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 setPreferQualityOverSpeed(bool qualityOverSpeed) {
|
|
|
|
fPreferQualityOverSpeed = qualityOverSpeed;
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:33:20 +00:00
|
|
|
/** 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 setRequireUnpremultipliedColors(bool request) {
|
|
|
|
fRequireUnpremultipliedColors = request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns true if the decoder will only return bitmaps with unpremultiplied
|
|
|
|
colors.
|
|
|
|
*/
|
|
|
|
bool getRequireUnpremultipliedColors() const { return fRequireUnpremultipliedColors; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class Peeker
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Base class for optional callbacks to retrieve meta/chunk data out of
|
|
|
|
an image as it is being decoded.
|
|
|
|
*/
|
|
|
|
class Peeker : public SkRefCnt {
|
|
|
|
public:
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(Peeker)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return true to continue decoding, or false to indicate an error, which
|
|
|
|
will cause the decoder to not return the image.
|
|
|
|
*/
|
|
|
|
virtual bool peek(const char tag[], const void* data, size_t length) = 0;
|
2012-06-21 20:25:03 +00:00
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Peeker* getPeeker() const { return fPeeker; }
|
|
|
|
Peeker* setPeeker(Peeker*);
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2014-06-11 17:33:13 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
|
2013-07-09 15:45:14 +00:00
|
|
|
/** \class Chooser
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2013-07-09 15:45:14 +00:00
|
|
|
Base class for optional callbacks to choose an image from a format that
|
|
|
|
contains multiple images.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
class Chooser : public SkRefCnt {
|
|
|
|
public:
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(Chooser)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void begin(int count) {}
|
|
|
|
virtual void inspect(int index, SkBitmap::Config config, int width, int height) {}
|
|
|
|
/** Return the index of the subimage you want, or -1 to choose none of them.
|
|
|
|
*/
|
|
|
|
virtual int choose() = 0;
|
2012-06-21 20:25:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Chooser* getChooser() const { return fChooser; }
|
|
|
|
Chooser* setChooser(Chooser*);
|
2014-06-11 17:33:13 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-07-08 18:15:59 +00:00
|
|
|
/**
|
|
|
|
* 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 setPreserveSrcDepth(bool preserve) {
|
|
|
|
fPreserveSrcDepth = preserve;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap::Allocator* getAllocator() const { return fAllocator; }
|
|
|
|
SkBitmap::Allocator* setAllocator(SkBitmap::Allocator*);
|
|
|
|
|
|
|
|
// sample-size, if set to > 1, tells the decoder to return a smaller than
|
|
|
|
// original bitmap, sampling 1 pixel for every size pixels. e.g. if sample
|
|
|
|
// size is set to 3, then the returned bitmap will be 1/3 as wide and high,
|
|
|
|
// and will contain 1/9 as many pixels as the original.
|
|
|
|
// Note: this is a hint, and the codec may choose to ignore this, or only
|
|
|
|
// approximate the sample size.
|
|
|
|
int getSampleSize() const { return fSampleSize; }
|
|
|
|
void setSampleSize(int size);
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Reset the sampleSize to its default of 1
|
|
|
|
*/
|
|
|
|
void resetSampleSize() { this->setSampleSize(1); }
|
|
|
|
|
|
|
|
/** 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).
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
This state is automatically reset at the beginning of decode().
|
|
|
|
*/
|
|
|
|
void cancelDecode() {
|
|
|
|
// now the subclass must query shouldCancelDecode() to be informed
|
|
|
|
// of the request
|
|
|
|
fShouldCancelDecode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Passed to the decode method. If kDecodeBounds_Mode is passed, then
|
2014-06-13 00:40:00 +00:00
|
|
|
only the bitmap's info need be set. If kDecodePixels_Mode
|
2008-12-17 15:59:43 +00:00
|
|
|
is passed, then the bitmap must have pixels or a pixelRef.
|
|
|
|
*/
|
|
|
|
enum Mode {
|
2014-06-13 00:40:00 +00:00
|
|
|
kDecodeBounds_Mode, //!< only return info in bitmap
|
2008-12-17 15:59:43 +00:00
|
|
|
kDecodePixels_Mode //!< return entire bitmap (including pixels)
|
|
|
|
};
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2014-10-22 19:07:00 +00:00
|
|
|
/** Result of a decode. If read as a boolean, a partial success is
|
|
|
|
considered a success (true).
|
|
|
|
*/
|
|
|
|
enum Result {
|
|
|
|
kFailure = 0, //!< Image failed to decode. bitmap will be
|
|
|
|
// unchanged.
|
|
|
|
kPartialSuccess = 1, //!< Part of the image decoded. The rest is
|
|
|
|
// filled in automatically
|
|
|
|
kSuccess = 2 //!< The entire image was decoded, if Mode is
|
|
|
|
// kDecodePixels_Mode, or the bounds were
|
|
|
|
// decoded, in kDecodeBounds_Mode.
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Given a stream, decode it into the specified bitmap.
|
2014-06-13 00:40:00 +00:00
|
|
|
If the decoder can decompress the image, it calls bitmap.setInfo(),
|
2008-12-17 15:59:43 +00:00
|
|
|
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.
|
2014-10-22 19:07:00 +00:00
|
|
|
* If the image cannot be decompressed, return kFailure. After the
|
2014-06-13 00:40:00 +00:00
|
|
|
* decoding, the function converts the decoded colortype in bitmap
|
2009-05-11 16:06:22 +00:00
|
|
|
* to pref if possible. Whether a conversion is feasible is
|
|
|
|
* tested by Bitmap::canCopyTo(pref).
|
|
|
|
|
2013-07-09 15:45:14 +00:00
|
|
|
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 a Peeker is installed via setPeeker, it may be used to peek into
|
|
|
|
meta data during the decode.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2014-10-22 19:07:00 +00:00
|
|
|
Result decode(SkStream*, SkBitmap* bitmap, SkColorType pref, Mode);
|
|
|
|
Result decode(SkStream* stream, SkBitmap* bitmap, Mode mode) {
|
2014-06-13 00:40:00 +00:00
|
|
|
return this->decode(stream, bitmap, kUnknown_SkColorType, mode);
|
2010-03-03 21:04:12 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-03-14 14:42:18 +00:00
|
|
|
/**
|
|
|
|
* Given a stream, build an index for doing tile-based decode.
|
|
|
|
* The built index will be saved in the decoder, and the image size will
|
|
|
|
* be returned in width and height.
|
|
|
|
*
|
|
|
|
* Return true for success or false on failure.
|
|
|
|
*/
|
2013-09-25 21:34:24 +00:00
|
|
|
bool buildTileIndex(SkStreamRewindable*, int *width, int *height);
|
2013-03-14 14:42:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-03 20:14:28 +00:00
|
|
|
* Decode a rectangle subset in the image.
|
2013-03-14 14:42:18 +00:00
|
|
|
* The method can only be called after buildTileIndex().
|
|
|
|
*
|
|
|
|
* Return true for success.
|
|
|
|
* Return false if the index is never built or failing in decoding.
|
|
|
|
*/
|
2014-06-13 00:40:00 +00:00
|
|
|
bool decodeSubset(SkBitmap* bm, const SkIRect& subset, SkColorType pref);
|
2013-05-03 20:14:28 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Given a stream, this will try to find an appropriate decoder object.
|
|
|
|
If none is found, the method returns NULL.
|
|
|
|
*/
|
2013-09-25 21:34:24 +00:00
|
|
|
static SkImageDecoder* Factory(SkStreamRewindable*);
|
2009-03-17 17:59:53 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Decode the image stored in the specified file, and store the result
|
|
|
|
in bitmap. Return true for success or false on failure.
|
|
|
|
|
2014-06-13 00:40:00 +00:00
|
|
|
@param pref If the PrefConfigTable is not set, prefer this colortype.
|
2013-07-18 19:34:49 +00:00
|
|
|
See NOTE ABOUT PREFERRED CONFIGS.
|
2009-06-15 18:45:19 +00:00
|
|
|
|
2009-06-15 13:04:45 +00:00
|
|
|
@param format On success, if format is non-null, it is set to the format
|
|
|
|
of the decoded file. On failure it is ignored.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2014-06-13 00:40:00 +00:00
|
|
|
static bool DecodeFile(const char file[], SkBitmap* bitmap, SkColorType pref, Mode,
|
2009-06-15 18:45:19 +00:00
|
|
|
Format* format = NULL);
|
2009-06-15 13:04:45 +00:00
|
|
|
static bool DecodeFile(const char file[], SkBitmap* bitmap) {
|
2014-06-13 00:40:00 +00:00
|
|
|
return DecodeFile(file, bitmap, kUnknown_SkColorType, kDecodePixels_Mode, NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2014-06-13 00:40:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Decode the image stored in the specified memory buffer, and store the
|
|
|
|
result in bitmap. Return true for success or false on failure.
|
|
|
|
|
2014-06-13 00:40:00 +00:00
|
|
|
@param pref If the PrefConfigTable is not set, prefer this colortype.
|
2013-07-18 19:34:49 +00:00
|
|
|
See NOTE ABOUT PREFERRED CONFIGS.
|
2009-06-15 18:45:19 +00:00
|
|
|
|
|
|
|
@param format On success, if format is non-null, it is set to the format
|
2009-06-15 13:04:45 +00:00
|
|
|
of the decoded buffer. On failure it is ignored.
|
|
|
|
*/
|
2014-06-13 00:40:00 +00:00
|
|
|
static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap, SkColorType pref,
|
|
|
|
Mode, Format* format = NULL);
|
2009-06-15 13:04:45 +00:00
|
|
|
static bool DecodeMemory(const void* buffer, size_t size, SkBitmap* bitmap){
|
2014-06-13 00:40:00 +00:00
|
|
|
return DecodeMemory(buffer, size, bitmap, kUnknown_SkColorType, kDecodePixels_Mode, NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-02-22 21:38:35 +00:00
|
|
|
|
2013-12-05 18:31:42 +00:00
|
|
|
/**
|
|
|
|
* Struct containing information about a pixel destination.
|
|
|
|
*/
|
|
|
|
struct Target {
|
|
|
|
/**
|
|
|
|
* Pre-allocated memory.
|
|
|
|
*/
|
|
|
|
void* fAddr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rowbytes of the allocated memory.
|
|
|
|
*/
|
|
|
|
size_t fRowBytes;
|
|
|
|
};
|
|
|
|
|
2013-09-25 21:34:24 +00:00
|
|
|
/** Decode the image stored in the specified SkStreamRewindable, and store the result
|
2008-12-17 15:59:43 +00:00
|
|
|
in bitmap. Return true for success or false on failure.
|
|
|
|
|
2014-06-13 00:40:00 +00:00
|
|
|
@param pref If the PrefConfigTable is not set, prefer this colortype.
|
2013-07-18 19:34:49 +00:00
|
|
|
See NOTE ABOUT PREFERRED CONFIGS.
|
2009-06-15 18:45:19 +00:00
|
|
|
|
2009-06-15 13:04:45 +00:00
|
|
|
@param format On success, if format is non-null, it is set to the format
|
|
|
|
of the decoded stream. On failure it is ignored.
|
|
|
|
*/
|
2014-06-13 00:40:00 +00:00
|
|
|
static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap, SkColorType pref, Mode,
|
2009-06-15 18:45:19 +00:00
|
|
|
Format* format = NULL);
|
2013-09-25 21:34:24 +00:00
|
|
|
static bool DecodeStream(SkStreamRewindable* stream, SkBitmap* bitmap) {
|
2014-06-13 00:40:00 +00:00
|
|
|
return DecodeStream(stream, bitmap, kUnknown_SkColorType, kDecodePixels_Mode, NULL);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
// must be overridden in subclasses. This guy is called by decode(...)
|
2014-10-22 19:07:00 +00:00
|
|
|
virtual Result onDecode(SkStream*, SkBitmap* bitmap, Mode) = 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-03-14 14:42:18 +00:00
|
|
|
// If the decoder wants to support tiled based decoding,
|
|
|
|
// this method must be overridden. This guy is called by buildTileIndex(...)
|
2013-09-25 21:34:24 +00:00
|
|
|
virtual bool onBuildTileIndex(SkStreamRewindable*, int *width, int *height) {
|
2013-03-14 14:42:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the decoder wants to support tiled based decoding,
|
|
|
|
// this method must be overridden. This guy is called by decodeRegion(...)
|
2013-05-03 20:14:28 +00:00
|
|
|
virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& rect) {
|
2013-03-14 14:42:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-16 20:10:57 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
virtual bool onDecodeYUV8Planes(SkStream* stream, SkISize componentSizes[3], void* planes[3],
|
|
|
|
size_t rowBytes[3], SkYUVColorSpace*) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-14 14:42:18 +00:00
|
|
|
/*
|
|
|
|
* Crop a rectangle from the src Bitmap to the dest Bitmap. src and dst are
|
|
|
|
* both sampled by sampleSize from an original Bitmap.
|
|
|
|
*
|
|
|
|
* @param dst the destination bitmap.
|
|
|
|
* @param src the source bitmap that is sampled by sampleSize from the
|
|
|
|
* original bitmap.
|
|
|
|
* @param sampleSize the sample size that src is sampled from the original bitmap.
|
|
|
|
* @param (dstX, dstY) the upper-left point of the dest bitmap in terms of
|
|
|
|
* the coordinate in the original bitmap.
|
|
|
|
* @param (width, height) the width and height of the unsampled dst.
|
2013-05-03 20:14:28 +00:00
|
|
|
* @param (srcX, srcY) the upper-left point of the src bitmap in terms of
|
2013-03-14 14:42:18 +00:00
|
|
|
* the coordinate in the original bitmap.
|
2013-05-03 20:14:28 +00:00
|
|
|
* @return bool Whether or not it succeeded.
|
2013-03-14 14:42:18 +00:00
|
|
|
*/
|
2013-05-03 20:14:28 +00:00
|
|
|
bool cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
|
2013-03-14 14:42:18 +00:00
|
|
|
int dstX, int dstY, int width, int height,
|
|
|
|
int srcX, int srcY);
|
|
|
|
|
2013-07-09 15:48:24 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
void copyFieldsToOther(SkImageDecoder* other);
|
2013-03-14 14:42:18 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** 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.
|
2009-05-11 16:06:22 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
Calling this outside of onDecode() may return undefined values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool shouldCancelDecode() const { return fShouldCancelDecode; }
|
|
|
|
|
2009-05-11 16:06:22 +00:00
|
|
|
protected:
|
2008-12-17 15:59:43 +00:00
|
|
|
SkImageDecoder();
|
|
|
|
|
2014-06-13 00:40:00 +00:00
|
|
|
/**
|
|
|
|
* Return the default preference being used by the current or latest call to decode.
|
|
|
|
*/
|
|
|
|
SkColorType getDefaultPref() { return fDefaultPref; }
|
|
|
|
|
2014-06-11 17:33:13 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
|
2008-12-17 15:59:43 +00:00
|
|
|
// helper function for decoders to handle the (common) case where there is only
|
|
|
|
// once choice available in the image file.
|
2014-06-10 02:52:07 +00:00
|
|
|
bool chooseFromOneChoice(SkColorType, int width, int height) const;
|
2014-06-11 17:33:13 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-06-13 00:40:00 +00:00
|
|
|
/* Helper for subclasses. Call this to allocate the pixel memory given the bitmap's info.
|
|
|
|
Returns true on success. This method handles checking for an optional Allocator.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
bool allocPixelRef(SkBitmap*, SkColorTable*) const;
|
|
|
|
|
2013-07-18 19:34:49 +00:00
|
|
|
/**
|
|
|
|
* The raw data of the src image.
|
|
|
|
*/
|
2010-03-03 21:04:12 +00:00
|
|
|
enum SrcDepth {
|
2013-07-18 19:34:49 +00:00
|
|
|
// Color-indexed.
|
2010-03-03 21:04:12 +00:00
|
|
|
kIndex_SrcDepth,
|
2013-07-18 19:34:49 +00:00
|
|
|
// Grayscale in 8 bits.
|
|
|
|
k8BitGray_SrcDepth,
|
|
|
|
// 8 bits per component. Used for 24 bit if there is no alpha.
|
|
|
|
k32Bit_SrcDepth,
|
2010-03-03 21:04:12 +00:00
|
|
|
};
|
2014-06-10 02:52:07 +00:00
|
|
|
/** The subclass, inside onDecode(), calls this to determine the colorType of
|
2010-03-03 21:04:12 +00:00
|
|
|
the returned bitmap. SrcDepth and hasAlpha reflect the raw data of the
|
|
|
|
src image. This routine returns the caller's preference given
|
2014-06-10 02:52:07 +00:00
|
|
|
srcDepth and hasAlpha, or kUnknown_SkColorType if there is no preference.
|
2010-03-03 21:04:12 +00:00
|
|
|
*/
|
2014-06-10 02:52:07 +00:00
|
|
|
SkColorType getPrefColorType(SrcDepth, bool hasAlpha) const;
|
2010-03-03 21:04:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
Peeker* fPeeker;
|
2014-06-11 17:33:13 +00:00
|
|
|
#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
|
2008-12-17 15:59:43 +00:00
|
|
|
Chooser* fChooser;
|
2014-06-11 17:33:13 +00:00
|
|
|
#endif
|
2008-12-17 15:59:43 +00:00
|
|
|
SkBitmap::Allocator* fAllocator;
|
|
|
|
int fSampleSize;
|
2014-06-13 00:40:00 +00:00
|
|
|
SkColorType fDefaultPref; // use if fUsePrefTable is false
|
2014-07-08 18:15:59 +00:00
|
|
|
bool fPreserveSrcDepth;
|
2014-06-17 15:38:31 +00:00
|
|
|
bool fDitherImage;
|
Add an option on SkImageDecoder to skip writing 0s.
Only implemented for PNG.
Add a getter and setter, and sets the default to false in the
constructor. Also copies the setting in copyFieldsToOther.
Fix an indpendent bug where fDitherImage was not being copied in
copyFieldsToOther.
In SkScaledBitmapSampler::begin, consolidate the settings passed in
by passing a const reference to the decoder. The decoder can be
referenced for its settings of dither, unpremultiplied, and now
skipping writing zeroes. Update callers to use the new API. In png
decoder, rather than passing around a pointer to an initial
read of getDitherImage, and potentially changing it, look at the
field on the decoder itself, and modify it directly. This is a
change in behavior - now if that same decoder is used to decode
a different image, the dither setting has changed. I think this is
okay because A) the typical use case is to use a new decoder for
each decode, B) we do not make any promises that a decode does not
change the decoder and C) it makes the code in SkScaledBitmapSampler
much cleaner.
In SkScaledBitmapScampler, add new row procs for skipping zeroes. Now
that choosing the row proc has five dimensions (src config, dst config,
dither, skip writing zeroes, unpremultiplied), use a new method: each
src/dst combination has a function for choosing the right proc depending
on the decoder.
SkScaledBitmapScampler::RowProc is now public for convenience.
Remove Sample_Gray_D8888_Unpremul, which is effectively no different
from Sample_Gray_D8888.
In cases where unpremultiplied was trivial, such as 565 and when
sampling from gray, decoding may now succeed.
Add a benchmark (currently disabled) for comparing the speed of skipping
writing zeroes versus not skipping. For this particular image, which is
mostly transparent pixels, normal decoding took about 3.6 milliseconds,
while skipping zeroes in the decode took only about 2.5 milliseconds
(this is on a Nexus 4). Presumably it would be slower on an image
with a small amount of transparency, but there will be no slowdown
for an image which reports that it has no transparency.
In SkImageRef_ashmem, always skip writing zeroes, since ashmem
memory is guaranteed to be initialized to 0.
Add a flag to skip writing zeroes in skimage.
Add a regression test for choosing the rowproc to ensure I did not
change any behavior accidentally.
BUG=skia:1661
R=reed@google.com
Review URL: https://codereview.chromium.org/24269006
git-svn-id: http://skia.googlecode.com/svn/trunk@11558 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-10-01 17:27:15 +00:00
|
|
|
bool fSkipWritingZeroes;
|
2008-12-17 15:59:43 +00:00
|
|
|
mutable bool fShouldCancelDecode;
|
2013-03-14 14:42:18 +00:00
|
|
|
bool fPreferQualityOverSpeed;
|
2013-06-14 15:33:20 +00:00
|
|
|
bool fRequireUnpremultipliedColors;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2009-03-17 17:59:53 +00:00
|
|
|
/** Calling newDecoder with a stream returns a new matching imagedecoder
|
|
|
|
instance, or NULL if none can be found. The caller must manage its ownership
|
|
|
|
of the stream as usual, calling unref() when it is done, as the returned
|
|
|
|
decoder may have called ref() (and if so, the decoder is responsible for
|
|
|
|
balancing its ownership when it is destroyed).
|
|
|
|
*/
|
|
|
|
class SkImageDecoderFactory : public SkRefCnt {
|
|
|
|
public:
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkImageDecoderFactory)
|
|
|
|
|
2013-09-25 21:34:24 +00:00
|
|
|
virtual SkImageDecoder* newDecoder(SkStreamRewindable*) = 0;
|
2012-06-21 20:25:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SkRefCnt INHERITED;
|
2009-03-17 17:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkDefaultImageDecoderFactory : SkImageDecoderFactory {
|
|
|
|
public:
|
|
|
|
// calls SkImageDecoder::Factory(stream)
|
2013-09-25 21:34:24 +00:00
|
|
|
virtual SkImageDecoder* newDecoder(SkStreamRewindable* stream) {
|
2009-03-17 17:59:53 +00:00
|
|
|
return SkImageDecoder::Factory(stream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-23 18:13:47 +00:00
|
|
|
// This macro declares a global (i.e., non-class owned) creation entry point
|
|
|
|
// for each decoder (e.g., CreateJPEGImageDecoder)
|
|
|
|
#define DECLARE_DECODER_CREATOR(codec) \
|
|
|
|
SkImageDecoder *Create ## codec ();
|
|
|
|
|
|
|
|
// This macro defines the global creation entry point for each decoder. Each
|
|
|
|
// decoder implementation that registers with the decoder factory must call it.
|
|
|
|
#define DEFINE_DECODER_CREATOR(codec) \
|
|
|
|
SkImageDecoder *Create ## codec () { \
|
|
|
|
return SkNEW( Sk ## codec ); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the decoders known by Skia. Note that, depending on the compiler settings,
|
|
|
|
// not all of these will be available
|
|
|
|
DECLARE_DECODER_CREATOR(BMPImageDecoder);
|
|
|
|
DECLARE_DECODER_CREATOR(GIFImageDecoder);
|
|
|
|
DECLARE_DECODER_CREATOR(ICOImageDecoder);
|
|
|
|
DECLARE_DECODER_CREATOR(JPEGImageDecoder);
|
|
|
|
DECLARE_DECODER_CREATOR(PNGImageDecoder);
|
|
|
|
DECLARE_DECODER_CREATOR(WBMPImageDecoder);
|
2013-03-14 14:42:18 +00:00
|
|
|
DECLARE_DECODER_CREATOR(WEBPImageDecoder);
|
2014-05-22 18:40:29 +00:00
|
|
|
DECLARE_DECODER_CREATOR(PKMImageDecoder);
|
2014-06-03 20:04:35 +00:00
|
|
|
DECLARE_DECODER_CREATOR(KTXImageDecoder);
|
2014-08-07 19:58:38 +00:00
|
|
|
DECLARE_DECODER_CREATOR(ASTCImageDecoder);
|
2013-09-04 17:20:18 +00:00
|
|
|
|
|
|
|
// Typedefs to make registering decoder and formatter callbacks easier.
|
|
|
|
// These have to be defined outside SkImageDecoder. :(
|
2013-09-25 21:34:24 +00:00
|
|
|
typedef SkTRegistry<SkImageDecoder*(*)(SkStreamRewindable*)> SkImageDecoder_DecodeReg;
|
|
|
|
typedef SkTRegistry<SkImageDecoder::Format(*)(SkStreamRewindable*)> SkImageDecoder_FormatReg;
|
2013-09-04 17:20:18 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|