Chromium Code Reviews| 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 "SkImageDecoder_libico.h" | |
| 9 | |
| 8 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" | 
| 9 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" | 
| 10 #include "SkStream.h" | 12 #include "SkStream.h" | 
| 11 #include "SkStreamPriv.h" | 13 #include "SkStreamPriv.h" | 
| 12 #include "SkTypes.h" | 14 #include "SkTypes.h" | 
| 13 | 15 | 
| 14 class SkICOImageDecoder : public SkImageDecoder { | 16 class SkICOImageDecoder : public SkImageDecoder { | 
| 15 public: | 17 public: | 
| 16 SkICOImageDecoder(); | 18 SkICOImageDecoder(); | 
| 17 | 19 | 
| 18 virtual Format getFormat() const SK_OVERRIDE { | 20 virtual Format getFormat() const SK_OVERRIDE { | 
| 19 return kICO_Format; | 21 return kICO_Format; | 
| 20 } | 22 } | 
| 21 | 23 | 
| 22 protected: | 24 protected: | 
| 23 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 25 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 
| 24 | 26 | 
| 25 private: | 27 private: | 
| 26 typedef SkImageDecoder INHERITED; | 28 typedef SkImageDecoder INHERITED; | 
| 27 }; | 29 }; | 
| 28 | |
| 
 
scroggo
2014/11/12 18:00:12
Why the delete?
 
Kimmo Kinnunen
2014/11/18 08:29:45
Done.
 
 | |
| 29 //////////////////////////////////////////////////////////////////////////////// ///////// | 30 //////////////////////////////////////////////////////////////////////////////// ///////// | 
| 30 | 31 | 
| 31 //read bytes starting from the begin-th index in the buffer | 32 //read bytes starting from the begin-th index in the buffer | 
| 32 //read in Intel order, and return an integer | 33 //read in Intel order, and return an integer | 
| 33 | 34 | 
| 34 #define readByte(buffer,begin) buffer[begin] | 35 #define readByte(buffer,begin) buffer[begin] | 
| 35 #define read2Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8) | 36 #define read2Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8) | 
| 36 #define read4Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8)+(buffer[begi n+2]<<16)+(buffer[begin+3]<<24) | 37 #define read4Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8)+(buffer[begi n+2]<<16)+(buffer[begin+3]<<24) | 
| 37 | 38 | 
| 38 //////////////////////////////////////////////////////////////////////////////// ///////// | 39 //////////////////////////////////////////////////////////////////////////////// ///////// | 
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 int green = readByte(buf, xorOffset + 4*pixelNo + 1); | 383 int green = readByte(buf, xorOffset + 4*pixelNo + 1); | 
| 383 int red = readByte(buf, xorOffset + 4*pixelNo + 2); | 384 int red = readByte(buf, xorOffset + 4*pixelNo + 2); | 
| 384 int alphaBit = (alphaByte & m) >> shift; | 385 int alphaBit = (alphaByte & m) >> shift; | 
| 385 #if 1 // don't trust the alphaBit for 32bit images <mrr> | 386 #if 1 // don't trust the alphaBit for 32bit images <mrr> | 
| 386 alphaBit = 0; | 387 alphaBit = 0; | 
| 387 #endif | 388 #endif | 
| 388 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); | 389 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); | 
| 389 *address = SkPreMultiplyARGB(alpha, red, green, blue); | 390 *address = SkPreMultiplyARGB(alpha, red, green, blue); | 
| 390 } | 391 } | 
| 391 | 392 | 
| 392 /////////////////////////////////////////////////////////////////////////////// | 393 SkImageDecoder::Format SkDetectFormatICOImageDecoder(SkStreamRewindable* stream) { | 
| 393 DEFINE_DECODER_CREATOR(ICOImageDecoder); | |
| 394 //////////////////////////////////////////////////////////////////////////////// ///////// | |
| 395 | |
| 396 static bool is_ico(SkStreamRewindable* stream) { | |
| 397 // Check to see if the first four bytes are 0,0,1,0 | 394 // Check to see if the first four bytes are 0,0,1,0 | 
| 398 // FIXME: Is that required and sufficient? | 395 // FIXME: Is that required and sufficient? | 
| 399 SkAutoMalloc autoMal(4); | 396 SkAutoMalloc autoMal(4); | 
| 400 unsigned char* buf = (unsigned char*)autoMal.get(); | 397 unsigned char* buf = (unsigned char*)autoMal.get(); | 
| 401 stream->read((void*)buf, 4); | 398 stream->read((void*)buf, 4); | 
| 402 int reserved = read2Bytes(buf, 0); | 399 int reserved = read2Bytes(buf, 0); | 
| 403 int type = read2Bytes(buf, 2); | 400 int type = read2Bytes(buf, 2); | 
| 404 if (reserved != 0 || type != 1) { | 401 if (reserved != 0 || type != 1) { | 
| 405 // This stream does not represent an ICO image. | 402 // This stream does not represent an ICO image. | 
| 406 return false; | 403 return SkImageDecoder::kUnknown_Format; | 
| 407 } | 404 } | 
| 408 return true; | 405 return SkImageDecoder::kICO_Format; | 
| 409 } | 406 } | 
| 410 | 407 | 
| 411 static SkImageDecoder* sk_libico_dfactory(SkStreamRewindable* stream) { | 408 SkImageDecoder* SkCreateICOImageDecoder(SkStreamRewindable* stream) { | 
| 412 if (is_ico(stream)) { | 409 if (SkDetectFormatICOImageDecoder(stream) == SkImageDecoder::kICO_Format) { | 
| 413 return SkNEW(SkICOImageDecoder); | 410 return SkNEW(SkICOImageDecoder); | 
| 414 } | 411 } | 
| 415 return NULL; | 412 return NULL; | 
| 416 } | 413 } | 
| 417 | 414 | 
| 418 static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory); | |
| 419 | |
| 420 static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) { | |
| 421 if (is_ico(stream)) { | |
| 422 return SkImageDecoder::kICO_Format; | |
| 423 } | |
| 424 return SkImageDecoder::kUnknown_Format; | |
| 425 } | |
| 426 | |
| 427 static SkImageDecoder_FormatReg gFormatReg(get_format_ico); | |
| OLD | NEW |