michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkColorPriv.h" michael@0: #include "SkImageDecoder.h" michael@0: #include "SkStream.h" michael@0: #include "SkStreamHelpers.h" michael@0: #include "SkTypes.h" michael@0: michael@0: class SkICOImageDecoder : public SkImageDecoder { michael@0: public: michael@0: SkICOImageDecoder(); michael@0: michael@0: virtual Format getFormat() const SK_OVERRIDE { michael@0: return kICO_Format; michael@0: } michael@0: michael@0: protected: michael@0: virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; michael@0: michael@0: private: michael@0: typedef SkImageDecoder INHERITED; michael@0: }; michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: //read bytes starting from the begin-th index in the buffer michael@0: //read in Intel order, and return an integer michael@0: michael@0: #define readByte(buffer,begin) buffer[begin] michael@0: #define read2Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8) michael@0: #define read4Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8)+(buffer[begin+2]<<16)+(buffer[begin+3]<<24) michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkICOImageDecoder::SkICOImageDecoder() michael@0: { michael@0: } michael@0: michael@0: //helpers - my function pointer will call one of these, depending on the bitCount, each time through the inner loop michael@0: static void editPixelBit1(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); michael@0: static void editPixelBit4(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); michael@0: static void editPixelBit8(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); michael@0: static void editPixelBit24(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); michael@0: static void editPixelBit32(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); michael@0: michael@0: michael@0: static int calculateRowBytesFor8888(int w, int bitCount) michael@0: { michael@0: // Default rowBytes is w << 2 for kARGB_8888 michael@0: // In the case of a 4 bit image with an odd width, we need to add some michael@0: // so we can go off the end of the drawn bitmap. michael@0: // Add 4 to ensure that it is still a multiple of 4. michael@0: if (4 == bitCount && (w & 0x1)) { michael@0: return (w + 1) << 2; michael@0: } michael@0: // Otherwise return 0, which will allow it to be calculated automatically. michael@0: return 0; michael@0: } michael@0: michael@0: bool SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) michael@0: { michael@0: SkAutoMalloc autoMal; michael@0: const size_t length = CopyStreamToStorage(&autoMal, stream); michael@0: if (0 == length) { michael@0: return false; michael@0: } michael@0: michael@0: unsigned char* buf = (unsigned char*)autoMal.get(); michael@0: michael@0: //these should always be the same - should i use for error checking? - what about files that have some michael@0: //incorrect values, but still decode properly? michael@0: int reserved = read2Bytes(buf, 0); // 0 michael@0: int type = read2Bytes(buf, 2); // 1 michael@0: if (reserved != 0 || type != 1) michael@0: return false; michael@0: int count = read2Bytes(buf, 4); michael@0: michael@0: //need to at least have enough space to hold the initial table of info michael@0: if (length < (size_t)(6 + count*16)) michael@0: return false; michael@0: michael@0: int choice; michael@0: Chooser* chooser = this->getChooser(); michael@0: //FIXME:if no chooser, consider providing the largest color image michael@0: //what are the odds that the largest image would be monochrome? michael@0: if (NULL == chooser) { michael@0: choice = 0; michael@0: } else { michael@0: chooser->begin(count); michael@0: for (int i = 0; i < count; i++) michael@0: { michael@0: //need to find out the config, width, and height from the stream michael@0: int width = readByte(buf, 6 + i*16); michael@0: int height = readByte(buf, 7 + i*16); michael@0: int offset = read4Bytes(buf, 18 + i*16); michael@0: int bitCount = read2Bytes(buf, offset+14); michael@0: SkBitmap::Config c; michael@0: //currently only provide ARGB_8888_, but maybe we want kIndex8_Config for 1 and 4, and possibly 8? michael@0: //or maybe we'll determine this based on the provided config michael@0: switch (bitCount) michael@0: { michael@0: case 1: michael@0: case 4: michael@0: // In reality, at least for the moment, these will be decoded into kARGB_8888 bitmaps. michael@0: // However, this will be used to distinguish between the lower quality 1bpp and 4 bpp michael@0: // images and the higher quality images. michael@0: c = SkBitmap::kIndex8_Config; michael@0: break; michael@0: case 8: michael@0: case 24: michael@0: case 32: michael@0: c = SkBitmap::kARGB_8888_Config; michael@0: break; michael@0: default: michael@0: SkDEBUGF(("Image with %ibpp not supported\n", bitCount)); michael@0: continue; michael@0: } michael@0: chooser->inspect(i, c, width, height); michael@0: } michael@0: choice = chooser->choose(); michael@0: } michael@0: michael@0: //you never know what the chooser is going to supply michael@0: if (choice >= count || choice < 0) michael@0: return false; michael@0: michael@0: //skip ahead to the correct header michael@0: //commented out lines are not used, but if i switch to other read method, need to know how many to skip michael@0: //otherwise, they could be used for error checking michael@0: int w = readByte(buf, 6 + choice*16); michael@0: int h = readByte(buf, 7 + choice*16); michael@0: int colorCount = readByte(buf, 8 + choice*16); michael@0: //int reservedToo = readByte(buf, 9 + choice*16); //0 michael@0: //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0 michael@0: //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usually 0 michael@0: int size = read4Bytes(buf, 14 + choice*16); //matters? michael@0: int offset = read4Bytes(buf, 18 + choice*16); michael@0: if ((size_t)(offset + size) > length) michael@0: return false; michael@0: michael@0: // Check to see if this is a PNG image inside the ICO michael@0: { michael@0: SkMemoryStream subStream(buf + offset, size, false); michael@0: SkAutoTDelete otherDecoder(SkImageDecoder::Factory(&subStream)); michael@0: if (otherDecoder.get() != NULL) { michael@0: // Set fields on the other decoder to be the same as this one. michael@0: this->copyFieldsToOther(otherDecoder.get()); michael@0: if(otherDecoder->decode(&subStream, bm, this->getDefaultPref(), mode)) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: //int infoSize = read4Bytes(buf, offset); //40 michael@0: //int width = read4Bytes(buf, offset+4); //should == w michael@0: //int height = read4Bytes(buf, offset+8); //should == 2*h michael@0: //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it?) michael@0: int bitCount = read2Bytes(buf, offset+14); michael@0: michael@0: void (*placePixel)(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) = NULL; michael@0: switch (bitCount) michael@0: { michael@0: case 1: michael@0: placePixel = &editPixelBit1; michael@0: colorCount = 2; michael@0: break; michael@0: case 4: michael@0: placePixel = &editPixelBit4; michael@0: colorCount = 16; michael@0: break; michael@0: case 8: michael@0: placePixel = &editPixelBit8; michael@0: colorCount = 256; michael@0: break; michael@0: case 24: michael@0: placePixel = &editPixelBit24; michael@0: colorCount = 0; michael@0: break; michael@0: case 32: michael@0: placePixel = &editPixelBit32; michael@0: colorCount = 0; michael@0: break; michael@0: default: michael@0: SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount)); michael@0: return false; michael@0: } michael@0: michael@0: //these should all be zero, but perhaps are not - need to check michael@0: //int compression = read4Bytes(buf, offset+16); //0 michael@0: //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value michael@0: //int xPixels = read4Bytes(buf, offset+24); //0 michael@0: //int yPixels = read4Bytes(buf, offset+28); //0 michael@0: //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an actual value though michael@0: //int colorsImportant = read4Bytes(buf, offset+36); //0 michael@0: michael@0: int begin = offset + 40; michael@0: //this array represents the colortable michael@0: //if i allow other types of bitmaps, it may actually be used as a part of the bitmap michael@0: SkPMColor* colors = NULL; michael@0: int blue, green, red; michael@0: if (colorCount) michael@0: { michael@0: colors = new SkPMColor[colorCount]; michael@0: for (int j = 0; j < colorCount; j++) michael@0: { michael@0: //should this be a function - maybe a #define? michael@0: blue = readByte(buf, begin + 4*j); michael@0: green = readByte(buf, begin + 4*j + 1); michael@0: red = readByte(buf, begin + 4*j + 2); michael@0: colors[j] = SkPackARGB32(0xFF, red & 0xFF, green & 0xFF, blue & 0xFF); michael@0: } michael@0: } michael@0: int bitWidth = w*bitCount; michael@0: int test = bitWidth & 0x1F; michael@0: int mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 michael@0: int lineBitWidth = (bitWidth & 0xFFFFFFE0) + (0x20 & mask); michael@0: int lineWidth = lineBitWidth/bitCount; michael@0: michael@0: int xorOffset = begin + colorCount*4; //beginning of the color bitmap michael@0: //other read method means we will just be here already michael@0: int andOffset = xorOffset + ((lineWidth*h*bitCount) >> 3); michael@0: michael@0: /*int */test = w & 0x1F; //the low 5 bits - we are rounding up to the next 32 (2^5) michael@0: /*int */mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 michael@0: int andLineWidth = (w & 0xFFFFFFE0) + (0x20 & mask); michael@0: //if we allow different Configs, everything is the same til here michael@0: //change the config, and use different address getter, and place index vs color, and add the color table michael@0: //FIXME: what is the tradeoff in size? michael@0: //if the andbitmap (mask) is all zeroes, then we can easily do an index bitmap michael@0: //however, with small images with large colortables, maybe it's better to still do argb_8888 michael@0: michael@0: bm->setConfig(SkBitmap::kARGB_8888_Config, w, h, calculateRowBytesFor8888(w, bitCount)); michael@0: michael@0: if (SkImageDecoder::kDecodeBounds_Mode == mode) { michael@0: delete[] colors; michael@0: return true; michael@0: } michael@0: michael@0: if (!this->allocPixelRef(bm, NULL)) michael@0: { michael@0: delete[] colors; michael@0: return false; michael@0: } michael@0: michael@0: SkAutoLockPixels alp(*bm); michael@0: michael@0: for (int y = 0; y < h; y++) michael@0: { michael@0: for (int x = 0; x < w; x++) michael@0: { michael@0: //U32* address = bm->getAddr32(x, y); michael@0: michael@0: //check the alpha bit first, but pass it along to the function to figure out how to deal with it michael@0: int andPixelNo = andLineWidth*(h-y-1)+x; michael@0: //only need to get a new alphaByte when x %8 == 0 michael@0: //but that introduces an if and a mod - probably much slower michael@0: //that's ok, it's just a read of an array, not a stream michael@0: int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3)); michael@0: int shift = 7 - (andPixelNo & 0x7); michael@0: int m = 1 << shift; michael@0: michael@0: int pixelNo = lineWidth*(h-y-1)+x; michael@0: placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift, colors); michael@0: michael@0: } michael@0: } michael@0: michael@0: delete [] colors; michael@0: //ensure we haven't read off the end? michael@0: //of course this doesn't help us if the andOffset was a lie... michael@0: //return andOffset + (andLineWidth >> 3) <= length; michael@0: return true; michael@0: } //onDecode michael@0: michael@0: //function to place the pixel, determined by the bitCount michael@0: static void editPixelBit1(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) michael@0: { michael@0: // note that this should be the same as/similar to the AND bitmap michael@0: SkPMColor* address = bm->getAddr32(x,y); michael@0: int byte = readByte(buf, xorOffset + (pixelNo >> 3)); michael@0: int colorBit; michael@0: int alphaBit; michael@0: // Read all of the bits in this byte. michael@0: int i = x + 8; michael@0: // Pin to the width so we do not write outside the bounds of michael@0: // our color table. michael@0: i = i > w ? w : i; michael@0: // While loop to check all 8 bits individually. michael@0: while (x < i) michael@0: { michael@0: michael@0: colorBit = (byte & m) >> shift; michael@0: alphaBit = (alphaByte & m) >> shift; michael@0: *address = (alphaBit-1)&(colors[colorBit]); michael@0: x++; michael@0: // setup for the next pixel michael@0: address = address + 1; michael@0: m = m >> 1; michael@0: shift -= 1; michael@0: } michael@0: x--; michael@0: } michael@0: static void editPixelBit4(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) michael@0: { michael@0: SkPMColor* address = bm->getAddr32(x, y); michael@0: int byte = readByte(buf, xorOffset + (pixelNo >> 1)); michael@0: int pixel = (byte >> 4) & 0xF; michael@0: int alphaBit = (alphaByte & m) >> shift; michael@0: *address = (alphaBit-1)&(colors[pixel]); michael@0: x++; michael@0: //if w is odd, x may be the same as w, which means we are writing to an unused portion of the bitmap michael@0: //but that's okay, since i've added an extra rowByte for just this purpose michael@0: address = address + 1; michael@0: pixel = byte & 0xF; michael@0: m = m >> 1; michael@0: alphaBit = (alphaByte & m) >> (shift-1); michael@0: //speed up trick here michael@0: *address = (alphaBit-1)&(colors[pixel]); michael@0: } michael@0: michael@0: static void editPixelBit8(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) michael@0: { michael@0: SkPMColor* address = bm->getAddr32(x, y); michael@0: int pixel = readByte(buf, xorOffset + pixelNo); michael@0: int alphaBit = (alphaByte & m) >> shift; michael@0: *address = (alphaBit-1)&(colors[pixel]); michael@0: } michael@0: michael@0: static void editPixelBit24(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) michael@0: { michael@0: SkPMColor* address = bm->getAddr32(x, y); michael@0: int blue = readByte(buf, xorOffset + 3*pixelNo); michael@0: int green = readByte(buf, xorOffset + 3*pixelNo + 1); michael@0: int red = readByte(buf, xorOffset + 3*pixelNo + 2); michael@0: int alphaBit = (alphaByte & m) >> shift; michael@0: //alphaBit == 1 => alpha = 0 michael@0: int alpha = (alphaBit-1) & 0xFF; michael@0: *address = SkPreMultiplyARGB(alpha, red, green, blue); michael@0: } michael@0: michael@0: static void editPixelBit32(const int pixelNo, const unsigned char* buf, michael@0: const int xorOffset, int& x, int y, const int w, michael@0: SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) michael@0: { michael@0: SkPMColor* address = bm->getAddr32(x, y); michael@0: int blue = readByte(buf, xorOffset + 4*pixelNo); michael@0: int green = readByte(buf, xorOffset + 4*pixelNo + 1); michael@0: int red = readByte(buf, xorOffset + 4*pixelNo + 2); michael@0: int alphaBit = (alphaByte & m) >> shift; michael@0: #if 1 // don't trust the alphaBit for 32bit images michael@0: alphaBit = 0; michael@0: #endif michael@0: int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); michael@0: *address = SkPreMultiplyARGB(alpha, red, green, blue); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: DEFINE_DECODER_CREATOR(ICOImageDecoder); michael@0: ///////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool is_ico(SkStreamRewindable* stream) { michael@0: // Check to see if the first four bytes are 0,0,1,0 michael@0: // FIXME: Is that required and sufficient? michael@0: SkAutoMalloc autoMal(4); michael@0: unsigned char* buf = (unsigned char*)autoMal.get(); michael@0: stream->read((void*)buf, 4); michael@0: int reserved = read2Bytes(buf, 0); michael@0: int type = read2Bytes(buf, 2); michael@0: if (reserved != 0 || type != 1) { michael@0: // This stream does not represent an ICO image. michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static SkImageDecoder* sk_libico_dfactory(SkStreamRewindable* stream) { michael@0: if (is_ico(stream)) { michael@0: return SkNEW(SkICOImageDecoder); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory); michael@0: michael@0: static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) { michael@0: if (is_ico(stream)) { michael@0: return SkImageDecoder::kICO_Format; michael@0: } michael@0: return SkImageDecoder::kUnknown_Format; michael@0: } michael@0: michael@0: static SkImageDecoder_FormatReg gFormatReg(get_format_ico);