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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } else { | 179 } else { |
180 config = fDefaultPref; | 180 config = fDefaultPref; |
181 } | 181 } |
182 | 182 |
183 if (SkBitmap::kNo_Config == config) { | 183 if (SkBitmap::kNo_Config == config) { |
184 config = SkImageDecoder::GetDeviceConfig(); | 184 config = SkImageDecoder::GetDeviceConfig(); |
185 } | 185 } |
186 return config; | 186 return config; |
187 } | 187 } |
188 | 188 |
189 bool SkImageDecoder::decode(SkStream* stream, SkBitmap* bm, | 189 bool SkImageDecoder::decode(SkStreamRewindable* stream, SkBitmap* bm, |
190 SkBitmap::Config pref, Mode mode) { | 190 SkBitmap::Config pref, Mode mode) { |
191 // we reset this to false before calling onDecode | 191 // we reset this to false before calling onDecode |
192 fShouldCancelDecode = false; | 192 fShouldCancelDecode = false; |
193 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false | 193 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false |
194 fDefaultPref = pref; | 194 fDefaultPref = pref; |
195 | 195 |
196 // pass a temporary bitmap, so that if we return false, we are assured of | 196 // pass a temporary bitmap, so that if we return false, we are assured of |
197 // leaving the caller's bitmap untouched. | 197 // leaving the caller's bitmap untouched. |
198 SkBitmap tmp; | 198 SkBitmap tmp; |
199 if (!this->onDecode(stream, &tmp, mode)) { | 199 if (!this->onDecode(stream, &tmp, mode)) { |
200 return false; | 200 return false; |
201 } | 201 } |
202 bm->swap(tmp); | 202 bm->swap(tmp); |
203 return true; | 203 return true; |
204 } | 204 } |
205 | 205 |
206 bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, | 206 bool SkImageDecoder::decodeSubset(SkBitmap* bm, const SkIRect& rect, |
207 SkBitmap::Config pref) { | 207 SkBitmap::Config pref) { |
208 // we reset this to false before calling onDecodeSubset | 208 // we reset this to false before calling onDecodeSubset |
209 fShouldCancelDecode = false; | 209 fShouldCancelDecode = false; |
210 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false | 210 // assign this, for use by getPrefConfig(), in case fUsePrefTable is false |
211 fDefaultPref = pref; | 211 fDefaultPref = pref; |
212 | 212 |
213 return this->onDecodeSubset(bm, rect); | 213 return this->onDecodeSubset(bm, rect); |
214 } | 214 } |
215 | 215 |
216 bool SkImageDecoder::buildTileIndex(SkStream* stream, | 216 bool SkImageDecoder::buildTileIndex(SkStreamRewindable* stream, |
217 int *width, int *height) { | 217 int *width, int *height) { |
218 // we reset this to false before calling onBuildTileIndex | 218 // we reset this to false before calling onBuildTileIndex |
219 fShouldCancelDecode = false; | 219 fShouldCancelDecode = false; |
220 | 220 |
221 return this->onBuildTileIndex(stream, width, height); | 221 return this->onBuildTileIndex(stream, width, height); |
222 } | 222 } |
223 | 223 |
224 bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, | 224 bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
225 int dstX, int dstY, int width, int height, | 225 int dstX, int dstY, int width, int height, |
226 int srcX, int srcY) { | 226 int srcX, int srcY) { |
227 int w = width / sampleSize; | 227 int w = width / sampleSize; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 return true; | 269 return true; |
270 } | 270 } |
271 | 271 |
272 /////////////////////////////////////////////////////////////////////////////// | 272 /////////////////////////////////////////////////////////////////////////////// |
273 | 273 |
274 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, | 274 bool SkImageDecoder::DecodeFile(const char file[], SkBitmap* bm, |
275 SkBitmap::Config pref, Mode mode, Format* format) { | 275 SkBitmap::Config pref, Mode mode, Format* format) { |
276 SkASSERT(file); | 276 SkASSERT(file); |
277 SkASSERT(bm); | 277 SkASSERT(bm); |
278 | 278 |
279 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file)); | 279 SkAutoTUnref<SkStreamRewindable> stream(SkStream::NewFromFile(file)); |
280 if (stream.get()) { | 280 if (stream.get()) { |
281 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) { | 281 if (SkImageDecoder::DecodeStream(stream, bm, pref, mode, format)) { |
282 bm->pixelRef()->setURI(file); | 282 bm->pixelRef()->setURI(file); |
283 return true; | 283 return true; |
284 } | 284 } |
285 } | 285 } |
286 return false; | 286 return false; |
287 } | 287 } |
288 | 288 |
289 bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, | 289 bool SkImageDecoder::DecodeMemory(const void* buffer, size_t size, SkBitmap* bm, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 /** | 330 /** |
331 * Helper function for DecodeMemoryToTarget. DecodeMemoryToTarget wants | 331 * Helper function for DecodeMemoryToTarget. DecodeMemoryToTarget wants |
332 * 8888, so set the config to it. All parameters must not be null. | 332 * 8888, so set the config to it. All parameters must not be null. |
333 * @param decoder Decoder appropriate for this stream. | 333 * @param decoder Decoder appropriate for this stream. |
334 * @param stream Rewound stream to the encoded data. | 334 * @param stream Rewound stream to the encoded data. |
335 * @param bitmap On success, will have its bounds set to the bounds of the | 335 * @param bitmap On success, will have its bounds set to the bounds of the |
336 * encoded data, and its config set to 8888. | 336 * encoded data, and its config set to 8888. |
337 * @return True if the bounds were decoded and the bitmap is 8888 or can be | 337 * @return True if the bounds were decoded and the bitmap is 8888 or can be |
338 * copied to 8888. | 338 * copied to 8888. |
339 */ | 339 */ |
340 static bool decode_bounds_to_8888(SkImageDecoder* decoder, SkStream* stream, | 340 static bool decode_bounds_to_8888(SkImageDecoder* decoder, SkStreamRewindable* s
tream, |
341 SkBitmap* bitmap) { | 341 SkBitmap* bitmap) { |
342 SkASSERT(decoder != NULL); | 342 SkASSERT(decoder != NULL); |
343 SkASSERT(stream != NULL); | 343 SkASSERT(stream != NULL); |
344 SkASSERT(bitmap != NULL); | 344 SkASSERT(bitmap != NULL); |
345 | 345 |
346 if (!decoder->decode(stream, bitmap, SkImageDecoder::kDecodeBounds_Mode)) { | 346 if (!decoder->decode(stream, bitmap, SkImageDecoder::kDecodeBounds_Mode)) { |
347 return false; | 347 return false; |
348 } | 348 } |
349 | 349 |
350 if (bitmap->config() == SkBitmap::kARGB_8888_Config) { | 350 if (bitmap->config() == SkBitmap::kARGB_8888_Config) { |
(...skipping 12 matching lines...) Expand all Loading... |
363 * Helper function for DecodeMemoryToTarget. Decodes the stream into bitmap, an
d if | 363 * Helper function for DecodeMemoryToTarget. Decodes the stream into bitmap, an
d if |
364 * the bitmap is not 8888, then it is copied to 8888. Either way, the end resul
t has | 364 * the bitmap is not 8888, then it is copied to 8888. Either way, the end resul
t has |
365 * its pixels stored in target. All parameters must not be null. | 365 * its pixels stored in target. All parameters must not be null. |
366 * @param decoder Decoder appropriate for this stream. | 366 * @param decoder Decoder appropriate for this stream. |
367 * @param stream Rewound stream to the encoded data. | 367 * @param stream Rewound stream to the encoded data. |
368 * @param bitmap On success, will contain the decoded image, with its pixels st
ored | 368 * @param bitmap On success, will contain the decoded image, with its pixels st
ored |
369 * at target. | 369 * at target. |
370 * @param target Preallocated memory for storing pixels. | 370 * @param target Preallocated memory for storing pixels. |
371 * @return bool Whether the decode (and copy, if necessary) succeeded. | 371 * @return bool Whether the decode (and copy, if necessary) succeeded. |
372 */ | 372 */ |
373 static bool decode_pixels_to_8888(SkImageDecoder* decoder, SkStream* stream, | 373 static bool decode_pixels_to_8888(SkImageDecoder* decoder, SkStreamRewindable* s
tream, |
374 SkBitmap* bitmap, void* target) { | 374 SkBitmap* bitmap, void* target) { |
375 SkASSERT(decoder != NULL); | 375 SkASSERT(decoder != NULL); |
376 SkASSERT(stream != NULL); | 376 SkASSERT(stream != NULL); |
377 SkASSERT(bitmap != NULL); | 377 SkASSERT(bitmap != NULL); |
378 SkASSERT(target != NULL); | 378 SkASSERT(target != NULL); |
379 | 379 |
380 TargetAllocator allocator(target); | 380 TargetAllocator allocator(target); |
381 decoder->setAllocator(&allocator); | 381 decoder->setAllocator(&allocator); |
382 | 382 |
383 bool success = decoder->decode(stream, bitmap, SkImageDecoder::kDecodePixels
_Mode); | 383 bool success = decoder->decode(stream, bitmap, SkImageDecoder::kDecodePixels
_Mode); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 } | 438 } |
439 bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes); | 439 bm.setConfig(bm.config(), bm.width(), bm.height(), target->fRowBytes); |
440 } | 440 } |
441 | 441 |
442 // SkMemoryStream.rewind() will always return true. | 442 // SkMemoryStream.rewind() will always return true. |
443 SkAssertResult(stream.rewind()); | 443 SkAssertResult(stream.rewind()); |
444 return decode_pixels_to_8888(decoder.get(), &stream, &bm, target->fAddr); | 444 return decode_pixels_to_8888(decoder.get(), &stream, &bm, target->fAddr); |
445 } | 445 } |
446 | 446 |
447 | 447 |
448 bool SkImageDecoder::DecodeStream(SkStream* stream, SkBitmap* bm, | 448 bool SkImageDecoder::DecodeStream(SkStreamRewindable* stream, SkBitmap* bm, |
449 SkBitmap::Config pref, Mode mode, Format* format) { | 449 SkBitmap::Config pref, Mode mode, |
| 450 Format* format) { |
450 SkASSERT(stream); | 451 SkASSERT(stream); |
451 SkASSERT(bm); | 452 SkASSERT(bm); |
452 | 453 |
453 bool success = false; | 454 bool success = false; |
454 SkImageDecoder* codec = SkImageDecoder::Factory(stream); | 455 SkImageDecoder* codec = SkImageDecoder::Factory(stream); |
455 | 456 |
456 if (NULL != codec) { | 457 if (NULL != codec) { |
457 success = codec->decode(stream, bm, pref, mode); | 458 success = codec->decode(stream, bm, pref, mode); |
458 if (success && format) { | 459 if (success && format) { |
459 *format = codec->getFormat(); | 460 *format = codec->getFormat(); |
460 if (kUnknown_Format == *format) { | 461 if (kUnknown_Format == *format) { |
461 if (stream->rewind()) { | 462 if (stream->rewind()) { |
462 *format = GetStreamFormat(stream); | 463 *format = GetStreamFormat(stream); |
463 } | 464 } |
464 } | 465 } |
465 } | 466 } |
466 delete codec; | 467 delete codec; |
467 } | 468 } |
468 return success; | 469 return success; |
469 } | 470 } |
OLD | NEW |