OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 | 27 |
28 void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config) | 28 void SkImageDecoder::SetDeviceConfig(SkBitmap::Config config) |
29 { | 29 { |
30 gDeviceConfig = config; | 30 gDeviceConfig = config; |
31 } | 31 } |
32 | 32 |
33 /////////////////////////////////////////////////////////////////////////////// | 33 /////////////////////////////////////////////////////////////////////////////// |
34 | 34 |
35 SkImageDecoder::SkImageDecoder() | 35 SkImageDecoder::SkImageDecoder() |
36 : fPeeker(NULL), fChooser(NULL), fAllocator(NULL), fSampleSize(1), | 36 : fPeeker(NULL) |
37 fDefaultPref(SkBitmap::kNo_Config), fDitherImage(true), | 37 , fChooser(NULL) |
38 fUsePrefTable(false),fPreferQualityOverSpeed(false) { | 38 , fAllocator(NULL) |
| 39 , fSampleSize(1) |
| 40 , fDefaultPref(SkBitmap::kNo_Config) |
| 41 , fDitherImage(true) |
| 42 , fUsePrefTable(false) |
| 43 , fPreferQualityOverSpeed(false) { |
39 } | 44 } |
40 | 45 |
41 SkImageDecoder::~SkImageDecoder() { | 46 SkImageDecoder::~SkImageDecoder() { |
42 SkSafeUnref(fPeeker); | 47 SkSafeUnref(fPeeker); |
43 SkSafeUnref(fChooser); | 48 SkSafeUnref(fChooser); |
44 SkSafeUnref(fAllocator); | 49 SkSafeUnref(fAllocator); |
45 } | 50 } |
46 | 51 |
47 SkImageDecoder::Format SkImageDecoder::getFormat() const { | 52 SkImageDecoder::Format SkImageDecoder::getFormat() const { |
48 return kUnknown_Format; | 53 return kUnknown_Format; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 // pass a temporary bitmap, so that if we return false, we are assured of | 175 // pass a temporary bitmap, so that if we return false, we are assured of |
171 // leaving the caller's bitmap untouched. | 176 // leaving the caller's bitmap untouched. |
172 SkBitmap tmp; | 177 SkBitmap tmp; |
173 if (!this->onDecode(stream, &tmp, mode)) { | 178 if (!this->onDecode(stream, &tmp, mode)) { |
174 return false; | 179 return false; |
175 } | 180 } |
176 bm->swap(tmp); | 181 bm->swap(tmp); |
177 return true; | 182 return true; |
178 } | 183 } |
179 | 184 |
180 bool SkImageDecoder::decodeRegion(SkBitmap* bm, const SkIRect& rect, | 185 bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, |
181 SkBitmap::Config pref) { | 186 SkBitmap::Config pref) { |
182 // we reset this to false before calling onDecodeRegion | 187 // we reset this to false before calling onDecodeSubset |
183 fShouldCancelDecode = false; | 188 fShouldCancelDecode = false; |
184 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false | 189 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false |
185 fDefaultPref = pref; | 190 fDefaultPref = pref; |
186 | 191 |
187 return this->onDecodeRegion(bm, rect); | 192 return this->onDecodeSubset(bm, rect); |
188 } | 193 } |
189 | 194 |
190 bool SkImageDecoder::buildTileIndex(SkStream* stream, | 195 bool SkImageDecoder::buildTileIndex(SkStream* stream, |
191 int *width, int *height) { | 196 int *width, int *height) { |
192 // we reset this to false before calling onBuildTileIndex | 197 // we reset this to false before calling onBuildTileIndex |
193 fShouldCancelDecode = false; | 198 fShouldCancelDecode = false; |
194 | 199 |
195 return this->onBuildTileIndex(stream, width, height); | 200 return this->onBuildTileIndex(stream, width, height); |
196 } | 201 } |
197 | 202 |
198 void SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, | 203 bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
199 int dstX, int dstY, int width, int height, | 204 int dstX, int dstY, int width, int height, |
200 int srcX, int srcY) { | 205 int srcX, int srcY) { |
201 int w = width / sampleSize; | 206 int w = width / sampleSize; |
202 int h = height / sampleSize; | 207 int h = height / sampleSize; |
| 208 if (src->getConfig() == SkBitmap::kIndex8_Config) { |
| 209 // kIndex8 does not allow drawing via an SkCanvas, as is done below. |
| 210 // Instead, use extractSubset. Note that this shares the SkPixelRef and |
| 211 // SkColorTable. |
| 212 // FIXME: Since src is discarded in practice, this holds on to more |
| 213 // pixels than is strictly necessary. Switch to a copy if memory |
| 214 // savings are more important than speed here. This also means |
| 215 // that the pixels in dst can not be reused (though there is no |
| 216 // allocation, which was already done on src). |
| 217 int x = (dstX - srcX) / sampleSize; |
| 218 int y = (dstY - srcY) / sampleSize; |
| 219 SkIRect subset = SkIRect::MakeXYWH(x, y, w, h); |
| 220 return src->extractSubset(dst, subset); |
| 221 } |
203 // if the destination has no pixels then we must allocate them. | 222 // if the destination has no pixels then we must allocate them. |
204 if (dst->isNull()) { | 223 if (dst->isNull()) { |
205 dst->setConfig(src->getConfig(), w, h); | 224 dst->setConfig(src->getConfig(), w, h); |
206 dst->setIsOpaque(src->isOpaque()); | 225 dst->setIsOpaque(src->isOpaque()); |
207 | 226 |
208 if (!this->allocPixelRef(dst, NULL)) { | 227 if (!this->allocPixelRef(dst, NULL)) { |
209 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap")); | 228 SkDEBUGF(("failed to allocate pixels needed to crop the bitmap")); |
210 return; | 229 return false; |
211 } | 230 } |
212 } | 231 } |
213 // check to see if the destination is large enough to decode the desired | 232 // check to see if the destination is large enough to decode the desired |
214 // region. If this assert fails we will just draw as much of the source | 233 // region. If this assert fails we will just draw as much of the source |
215 // into the destination that we can. | 234 // into the destination that we can. |
216 SkASSERT(dst->width() >= w && dst->height() >= h); | 235 if (dst->width() < w || dst->height() < h) { |
| 236 SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitma
p.\n")); |
| 237 } |
217 | 238 |
218 // Set the Src_Mode for the paint to prevent transparency issue in the | 239 // Set the Src_Mode for the paint to prevent transparency issue in the |
219 // dest in the event that the dest was being re-used. | 240 // dest in the event that the dest was being re-used. |
220 SkPaint paint; | 241 SkPaint paint; |
221 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 242 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
222 | 243 |
223 SkCanvas canvas(*dst); | 244 SkCanvas canvas(*dst); |
224 canvas.drawSprite(*src, (srcX - dstX) / sampleSize, | 245 canvas.drawSprite(*src, (srcX - dstX) / sampleSize, |
225 (srcY - dstY) / sampleSize, | 246 (srcY - dstY) / sampleSize, |
226 &paint); | 247 &paint); |
| 248 return true; |
227 } | 249 } |
228 | 250 |
229 /////////////////////////////////////////////////////////////////////////////// | 251 /////////////////////////////////////////////////////////////////////////////// |
230 | 252 |
231 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, | 253 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, |
232 SkBitmap::Config pref, Mode mode, Format* format) { | 254 SkBitmap::Config pref, Mode mode, Format* format) { |
233 SkASSERT(file); | 255 SkASSERT(file); |
234 SkASSERT(bm); | 256 SkASSERT(bm); |
235 | 257 |
236 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file)); | 258 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file)); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 CreateICOImageDecoder(); | 378 CreateICOImageDecoder(); |
357 CreateWBMPImageDecoder(); | 379 CreateWBMPImageDecoder(); |
358 // Only link GIF and PNG on platforms that build them. See images.gyp | 380 // Only link GIF and PNG on platforms that build them. See images.gyp |
359 #if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUIL
D_FOR_NACL) | 381 #if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUIL
D_FOR_NACL) |
360 CreateGIFImageDecoder(); | 382 CreateGIFImageDecoder(); |
361 #endif | 383 #endif |
362 #if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) | 384 #if !defined(SK_BUILD_FOR_MAC) && !defined(SK_BUILD_FOR_WIN) |
363 CreatePNGImageDecoder(); | 385 CreatePNGImageDecoder(); |
364 #endif | 386 #endif |
365 } | 387 } |
OLD | NEW |