| 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 #include "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
| 9 #include "SkImageDecoder.h" | 9 #include "SkImageDecoder.h" |
| 10 #include "SkStream.h" | 10 #include "SkStream.h" |
| 11 #include "SkStreamHelpers.h" | 11 #include "SkStreamHelpers.h" |
| 12 #include "SkTypes.h" | 12 #include "SkTypes.h" |
| 13 | 13 |
| 14 class SkICOImageDecoder : public SkImageDecoder { | 14 class SkICOImageDecoder : public SkImageDecoder { |
| 15 public: | 15 public: |
| 16 SkICOImageDecoder(); | 16 SkICOImageDecoder(); |
| 17 | 17 |
| 18 virtual Format getFormat() const SK_OVERRIDE { | 18 virtual Format getFormat() const SK_OVERRIDE { |
| 19 return kICO_Format; | 19 return kICO_Format; |
| 20 } | 20 } |
| 21 | 21 |
| 22 protected: | 22 protected: |
| 23 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 23 virtual bool onDecode(SkStreamRewindable* stream, SkBitmap* bm, Mode) SK_OVE
RRIDE; |
| 24 | 24 |
| 25 private: | 25 private: |
| 26 typedef SkImageDecoder INHERITED; | 26 typedef SkImageDecoder INHERITED; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 ////////////////////////////////////////////////////////////////////////////////
///////// | 29 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 30 | 30 |
| 31 //read bytes starting from the begin-th index in the buffer | 31 //read bytes starting from the begin-th index in the buffer |
| 32 //read in Intel order, and return an integer | 32 //read in Intel order, and return an integer |
| 33 | 33 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // In the case of a 4 bit image with an odd width, we need to add some | 65 // In the case of a 4 bit image with an odd width, we need to add some |
| 66 // so we can go off the end of the drawn bitmap. | 66 // so we can go off the end of the drawn bitmap. |
| 67 // Add 4 to ensure that it is still a multiple of 4. | 67 // Add 4 to ensure that it is still a multiple of 4. |
| 68 if (4 == bitCount && (w & 0x1)) { | 68 if (4 == bitCount && (w & 0x1)) { |
| 69 return (w + 1) << 2; | 69 return (w + 1) << 2; |
| 70 } | 70 } |
| 71 // Otherwise return 0, which will allow it to be calculated automatically. | 71 // Otherwise return 0, which will allow it to be calculated automatically. |
| 72 return 0; | 72 return 0; |
| 73 } | 73 } |
| 74 | 74 |
| 75 bool SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) | 75 bool SkICOImageDecoder::onDecode(SkStreamRewindable* stream, SkBitmap* bm, Mode
mode) |
| 76 { | 76 { |
| 77 SkAutoMalloc autoMal; | 77 SkAutoMalloc autoMal; |
| 78 const size_t length = CopyStreamToStorage(&autoMal, stream); | 78 const size_t length = CopyStreamToStorage(&autoMal, stream); |
| 79 if (0 == length) { | 79 if (0 == length) { |
| 80 return false; | 80 return false; |
| 81 } | 81 } |
| 82 | 82 |
| 83 unsigned char* buf = (unsigned char*)autoMal.get(); | 83 unsigned char* buf = (unsigned char*)autoMal.get(); |
| 84 | 84 |
| 85 //these should always be the same - should i use for error checking? - what
about files that have some | 85 //these should always be the same - should i use for error checking? - what
about files that have some |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 alphaBit = 0; | 376 alphaBit = 0; |
| 377 #endif | 377 #endif |
| 378 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); | 378 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); |
| 379 *address = SkPreMultiplyARGB(alpha, red, green, blue); | 379 *address = SkPreMultiplyARGB(alpha, red, green, blue); |
| 380 } | 380 } |
| 381 | 381 |
| 382 /////////////////////////////////////////////////////////////////////////////// | 382 /////////////////////////////////////////////////////////////////////////////// |
| 383 DEFINE_DECODER_CREATOR(ICOImageDecoder); | 383 DEFINE_DECODER_CREATOR(ICOImageDecoder); |
| 384 ////////////////////////////////////////////////////////////////////////////////
///////// | 384 ////////////////////////////////////////////////////////////////////////////////
///////// |
| 385 | 385 |
| 386 static bool is_ico(SkStream* stream) { | 386 static bool is_ico(SkStreamRewindable* stream) { |
| 387 // Check to see if the first four bytes are 0,0,1,0 | 387 // Check to see if the first four bytes are 0,0,1,0 |
| 388 // FIXME: Is that required and sufficient? | 388 // FIXME: Is that required and sufficient? |
| 389 SkAutoMalloc autoMal(4); | 389 SkAutoMalloc autoMal(4); |
| 390 unsigned char* buf = (unsigned char*)autoMal.get(); | 390 unsigned char* buf = (unsigned char*)autoMal.get(); |
| 391 stream->read((void*)buf, 4); | 391 stream->read((void*)buf, 4); |
| 392 int reserved = read2Bytes(buf, 0); | 392 int reserved = read2Bytes(buf, 0); |
| 393 int type = read2Bytes(buf, 2); | 393 int type = read2Bytes(buf, 2); |
| 394 if (reserved != 0 || type != 1) { | 394 if (reserved != 0 || type != 1) { |
| 395 // This stream does not represent an ICO image. | 395 // This stream does not represent an ICO image. |
| 396 return false; | 396 return false; |
| 397 } | 397 } |
| 398 return true; | 398 return true; |
| 399 } | 399 } |
| 400 | 400 |
| 401 #include "SkTRegistry.h" | 401 #include "SkTRegistry.h" |
| 402 | 402 |
| 403 static SkImageDecoder* sk_libico_dfactory(SkStream* stream) { | 403 static SkImageDecoder* sk_libico_dfactory(SkStreamRewindable* stream) { |
| 404 if (is_ico(stream)) { | 404 if (is_ico(stream)) { |
| 405 return SkNEW(SkICOImageDecoder); | 405 return SkNEW(SkICOImageDecoder); |
| 406 } | 406 } |
| 407 return NULL; | 407 return NULL; |
| 408 } | 408 } |
| 409 | 409 |
| 410 static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libico_dfactory); | 410 static SkTRegistry<SkImageDecoder*, SkStreamRewindable*> gReg(sk_libico_dfactory
); |
| 411 | 411 |
| 412 static SkImageDecoder::Format get_format_ico(SkStream* stream) { | 412 static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) { |
| 413 if (is_ico(stream)) { | 413 if (is_ico(stream)) { |
| 414 return SkImageDecoder::kICO_Format; | 414 return SkImageDecoder::kICO_Format; |
| 415 } | 415 } |
| 416 return SkImageDecoder::kUnknown_Format; | 416 return SkImageDecoder::kUnknown_Format; |
| 417 } | 417 } |
| 418 | 418 |
| 419 static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_ico)
; | 419 static SkTRegistry<SkImageDecoder::Format, SkStreamRewindable*> gFormatReg(get_f
ormat_ico); |
| OLD | NEW |