2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkImageDecoder.h"
11 #include "SkImagePriv.h"
12 #include "SkPixelRef.h"
14 #include "SkTemplates.h"
17 static SkBitmap::Config gDeviceConfig = SkBitmap::kNo_Config;
19 SkBitmap::Config SkImageDecoder::GetDeviceConfig()
24 void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config)
26 gDeviceConfig = config;
29 ///////////////////////////////////////////////////////////////////////////////
31 SkImageDecoder::SkImageDecoder()
36 , fDefaultPref(SkBitmap::kNo_Config)
38 , fUsePrefTable(false)
39 , fSkipWritingZeroes(false)
40 , fPreferQualityOverSpeed(false)
41 , fRequireUnpremultipliedColors(false) {
44 SkImageDecoder::~SkImageDecoder() {
46 SkSafeUnref(fChooser);
47 SkSafeUnref(fAllocator);
50 void SkImageDecoder::copyFieldsToOther(SkImageDecoder* other) {
54 other->setPeeker(fPeeker);
55 other->setChooser(fChooser);
56 other->setAllocator(fAllocator);
57 other->setSampleSize(fSampleSize);
59 other->setPrefConfigTable(fPrefTable);
61 other->fDefaultPref = fDefaultPref;
63 other->setDitherImage(fDitherImage);
64 other->setSkipWritingZeroes(fSkipWritingZeroes);
65 other->setPreferQualityOverSpeed(fPreferQualityOverSpeed);
66 other->setRequireUnpremultipliedColors(fRequireUnpremultipliedColors);
69 SkImageDecoder::Format SkImageDecoder::getFormat() const {
70 return kUnknown_Format;
73 const char* SkImageDecoder::getFormatName() const {
74 return GetFormatName(this->getFormat());
77 const char* SkImageDecoder::GetFormatName(Format format) {
80 return "Unknown Format";
96 SkDEBUGFAIL("Invalid format type!");
98 return "Unknown Format";
101 SkImageDecoder::Peeker* SkImageDecoder::setPeeker(Peeker* peeker) {
102 SkRefCnt_SafeAssign(fPeeker, peeker);
106 SkImageDecoder::Chooser* SkImageDecoder::setChooser(Chooser* chooser) {
107 SkRefCnt_SafeAssign(fChooser, chooser);
111 SkBitmap::Allocator* SkImageDecoder::setAllocator(SkBitmap::Allocator* alloc) {
112 SkRefCnt_SafeAssign(fAllocator, alloc);
116 void SkImageDecoder::setSampleSize(int size) {
123 bool SkImageDecoder::chooseFromOneChoice(SkBitmap::Config config, int width,
125 Chooser* chooser = fChooser;
127 if (NULL == chooser) { // no chooser, we just say YES to decoding :)
131 chooser->inspect(0, config, width, height);
132 return chooser->choose() == 0;
135 bool SkImageDecoder::allocPixelRef(SkBitmap* bitmap,
136 SkColorTable* ctable) const {
137 return bitmap->allocPixels(fAllocator, ctable);
140 ///////////////////////////////////////////////////////////////////////////////
142 void SkImageDecoder::setPrefConfigTable(const PrefConfigTable& prefTable) {
143 fUsePrefTable = true;
144 fPrefTable = prefTable;
147 SkBitmap::Config SkImageDecoder::getPrefConfig(SrcDepth srcDepth,
148 bool srcHasAlpha) const {
149 SkBitmap::Config config = SkBitmap::kNo_Config;
153 case kIndex_SrcDepth:
154 config = srcHasAlpha ? fPrefTable.fPrefFor_8Index_YesAlpha_src
155 : fPrefTable.fPrefFor_8Index_NoAlpha_src;
157 case k8BitGray_SrcDepth:
158 config = fPrefTable.fPrefFor_8Gray_src;
160 case k32Bit_SrcDepth:
161 config = srcHasAlpha ? fPrefTable.fPrefFor_8bpc_YesAlpha_src
162 : fPrefTable.fPrefFor_8bpc_NoAlpha_src;
166 config = fDefaultPref;
169 if (SkBitmap::kNo_Config == config) {
170 config = SkImageDecoder::GetDeviceConfig();
175 bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm,
176 SkBitmap::Config pref, Mode mode) {
177 // we reset this to false before calling onDecode
178 fShouldCancelDecode = false;
179 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
182 // pass a temporary bitmap, so that if we return false, we are assured of
183 // leaving the caller's bitmap untouched.
185 if (!this->onDecode(stream, &tmp, mode)) {
192 bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect,
193 SkBitmap::Config pref) {
194 // we reset this to false before calling onDecodeSubset
195 fShouldCancelDecode = false;
196 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false
199 return this->onDecodeSubset(bm, rect);
202 bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream,
203 int *width, int *height) {
204 // we reset this to false before calling onBuildTileIndex
205 fShouldCancelDecode = false;
207 return this->onBuildTileIndex(stream, width, height);
210 bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize,
211 int dstX, int dstY, int width, int height,
212 int srcX, int srcY) {
213 int w = width / sampleSize;
214 int h = height / sampleSize;
215 if (src->config() == SkBitmap::kIndex8_Config) {
216 // kIndex8 does not allow drawing via an SkCanvas, as is done below.
217 // Instead, use extractSubset. Note that this shares the SkPixelRef and
219 // FIXME: Since src is discarded in practice, this holds on to more
220 // pixels than is strictly necessary. Switch to a copy if memory
221 // savings are more important than speed here. This also means
222 // that the pixels in dst can not be reused (though there is no
223 // allocation, which was already done on src).
224 int x = (dstX - srcX) / sampleSize;
225 int y = (dstY - srcY) / sampleSize;
226 SkIRect subset = SkIRect::MakeXYWH(x, y, w, h);
227 return src->extractSubset(dst, subset);
229 // if the destination has no pixels then we must allocate them.
231 dst->setConfig(src->config(), w, h, 0, src->alphaType());
233 if (!this->allocPixelRef(dst, NULL)) {
234 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap"));
238 // check to see if the destination is large enough to decode the desired
239 // region. If this assert fails we will just draw as much of the source
240 // into the destination that we can.
241 if (dst->width() < w || dst->height() < h) {
242 SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n"));
245 // Set the Src_Mode for the paint to prevent transparency issue in the
246 // dest in the event that the dest was being re-used.
248 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
250 SkCanvas canvas(*dst);
251 canvas.drawSprite(*src, (srcX - dstX) / sampleSize,
252 (srcY - dstY) / sampleSize,
257 ///////////////////////////////////////////////////////////////////////////////
259 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm,
260 SkBitmap::Config pref, Mode mode, Format* format) {
264 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file));
266 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) {
267 bm->pixelRef()->setURI(file);
274 bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm,
275 SkBitmap::Config pref, Mode mode, Format* format) {
281 SkMemoryStream stream(buffer, size);
282 return SkImageDecoder::DecodeStream(&stream, bm, pref, mode, format);
285 bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm,
286 SkBitmap::Config pref, Mode mode,
291 bool success = false;
292 SkImageDecoder* codec = SkImageDecoder::Factory(stream);
295 success = codec->decode(stream, bm, pref, mode);
296 if (success && format) {
297 *format = codec->getFormat();
298 if (kUnknown_Format == *format) {
299 if (stream->rewind()) {
300 *format = GetStreamFormat(stream);