skia2/src/images/SkImageDecoder.cpp

308 lines
9.5 KiB
C++
Raw Normal View History

Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkImageDecoder.h"
#include "SkBitmap.h"
#include "SkImagePriv.h"
#include "SkPixelRef.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkCanvas.h"
static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config;
SkBitmap::Config SkImageDecoder::GetDeviceConfig()
{
return gDeviceConfig;
}
void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config)
{
gDeviceConfig = config;
}
///////////////////////////////////////////////////////////////////////////////
SkImageDecoder::SkImageDecoder()
: fPeeker(NULL)
, fChooser(NULL)
, fAllocator(NULL)
, fSampleSize(1)
, fDefaultPref(SkBitmap::kNo_Config)
, fDitherImage(true)
, fUsePrefTable(false)
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
, fSkipWritingZeroes(false)
, fPreferQualityOverSpeed(false)
, fRequireUnpremultipliedColors(false) {
}
SkImageDecoder::~SkImageDecoder() {
SkSafeUnref(fPeeker);
SkSafeUnref(fChooser);
SkSafeUnref(fAllocator);
}
void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) {
if (NULL == other) {
return;
}
other->setPeeker(fPeeker);
other->setChooser(fChooser);
other->setAllocator(fAllocator);
other->setSampleSize(fSampleSize);
if (fUsePrefTable) {
other->setPrefConfigTable(fPrefTable);
} else {
other->fDefaultPref = fDefaultPref;
}
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
other->setDitherImage(fDitherImage);
other->setSkipWritingZeroes(fSkipWritingZeroes);
other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
}
SkImageDecoder::Format SkImageDecoder::getFormat() const {
return kUnknown_Format;
}
const char* SkImageDecoder::getFormatName() const {
return GetFormatName(this->getFormat());
}
const char* SkImageDecoder::GetFormatName(Format format) {
switch (format) {
case kUnknown_Format:
return "Unknown Format";
case kBMP_Format:
return "BMP";
case kGIF_Format:
return "GIF";
case kICO_Format:
return "ICO";
case kJPEG_Format:
return "JPEG";
case kPNG_Format:
return "PNG";
case kWBMP_Format:
return "WBMP";
case kWEBP_Format:
return "WEBP";
default:
SkDEBUGFAIL("Invalid format type!");
}
return "Unknown Format";
}
SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
SkRefCnt_SafeAssign(fPeeker, peeker);
return peeker;
}
SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
SkRefCnt_SafeAssign(fChooser, chooser);
return chooser;
}
SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
SkRefCnt_SafeAssign(fAllocator, alloc);
return alloc;
}
void SkImageDecoder::setSampleSize(int size) {
if (size < 1) {
size = 1;
}
fSampleSize = size;
}
bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width,
int height) const {
Chooser* chooser = fChooser;
if (NULL == chooser) { // no chooser, we just say YES to decoding :)
return true;
}
chooser->begin(1);
chooser->inspect(0, config, width, height);
return chooser->choose() == 0;
}
bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
SkColorTable* ctable) const {
return bitmap->allocPixels(fAllocator, ctable);
}
///////////////////////////////////////////////////////////////////////////////
void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) {
fUsePrefTable = true;
fPrefTable = prefTable;
}
SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth,
bool srcHasAlpha) const {
SkBitmap::Config config = SkBitmap::kNo_Config;
if (fUsePrefTable) {
switch (srcDepth) {
case kIndex_SrcDepth:
config = srcHasAlpha ? fPrefTable.fPrefFor_8Index_YesAlpha_src
: fPrefTable.fPrefFor_8Index_NoAlpha_src;
break;
case k8BitGray_SrcDepth:
config = fPrefTable.fPrefFor_8Gray_src;
break;
case k32Bit_SrcDepth:
config = srcHasAlpha ? fPrefTable.fPrefFor_8bpc_YesAlpha_src
: fPrefTable.fPrefFor_8bpc_NoAlpha_src;
break;
}
} else {
config = fDefaultPref;
}
if (SkBitmap::kNo_Config == config) {
config = SkImageDecoder::GetDeviceConfig();
}
return config;
}
bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode) {
// we reset this to false before calling onDecode
fShouldCancelDecode = false;
// assign this, for use by getPrefConfig(), in case fUsePrefTable is false
fDefaultPref = pref;
// pass a temporary bitmap, so that if we return false, we are assured of
// leaving the caller's bitmap untouched.
SkBitmap tmp;
if (!this->onDecode(stream, &tmp, mode)) {
return false;
}
bm->swap(tmp);
return true;
}
bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect,
SkBitmap::Config pref) {
// we reset this to false before calling onDecodeSubset
fShouldCancelDecode = false;
// assign this, for use by getPrefConfig(), in case fUsePrefTable is false
fDefaultPref = pref;
return this->onDecodeSubset(bm, rect);
}
bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream,
int *width, int *height) {
// we reset this to false before calling onBuildTileIndex
fShouldCancelDecode = false;
return this->onBuildTileIndex(stream, width, height);
}
bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
int dstX, int dstY, int width, int height,
int srcX, int srcY) {
int w = width / sampleSize;
int h = height / sampleSize;
if (src->config() == SkBitmap::kIndex8_Config) {
// kIndex8 does not allow drawing via an SkCanvas, as is done below.
// Instead, use extractSubset. Note that this shares the SkPixelRef and
// SkColorTable.
// FIXME: Since src is discarded in practice, this holds on to more
// pixels than is strictly necessary. Switch to a copy if memory
// savings are more important than speed here. This also means
// that the pixels in dst can not be reused (though there is no
// allocation, which was already done on src).
int x = (dstX - srcX) / sampleSize;
int y = (dstY - srcY) / sampleSize;
SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
return src->extractSubset(dst, subset);
}
// if the destination has no pixels then we must allocate them.
if (dst->isNull()) {
dst->setConfig(src->config(), w, h, 0, src->alphaType());
if (!this->allocPixelRef(dst, NULL)) {
SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
return false;
}
}
// check to see if the destination is large enough to decode the desired
// region. If this assert fails we will just draw as much of the source
// into the destination that we can.
if (dst->width() < w || dst->height() < h) {
SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
}
// Set the Src_Mode for the paint to prevent transparency issue in the
// dest in the event that the dest was being re-used.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
SkCanvas canvas(*dst);
canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
(srcY - dstY) / sampleSize,
&paint);
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
SkASSERT(file);
SkASSERT(bm);
SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
if (stream.get()) {
if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
bm->pixelRef()->setURI(file);
return true;
}
}
return false;
}
bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
SkBitmap::Config pref, Mode mode, Format* format) {
if (0 == size) {
return false;
}
SkASSERT(buffer);
SkMemoryStream stream(buffer, size);
return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
}
bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm,
SkBitmap::Config pref, Mode mode,
Format* format) {
SkASSERT(stream);
SkASSERT(bm);
bool success = false;
SkImageDecoder* codec = SkImageDecoder::Factory(stream);
if (NULL != codec) {
success = codec->decode(stream, bm, pref, mode);
if (success && format) {
*format = codec->getFormat();
if (kUnknown_Format == *format) {
if (stream->rewind()) {
*format = GetStreamFormat(stream);
}
}
}
delete codec;
}
return success;
}