epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkImageDecoder.h" |
| 11 | #include "SkStream.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkTypes.h" |
| 14 | |
| 15 | class SkICOImageDecoder : public SkImageDecoder { |
| 16 | public: |
| 17 | SkICOImageDecoder(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 18 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | virtual Format getFormat() const { |
| 20 | return kICO_Format; |
| 21 | } |
| 22 | |
| 23 | protected: |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 24 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 27 | #if 0 // UNUSED |
reed@google.com | 3bbee32 | 2011-08-05 16:13:09 +0000 | [diff] [blame] | 28 | SkImageDecoder* SkCreateICOImageDecoder() { |
| 29 | return new SkICOImageDecoder; |
| 30 | } |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 31 | #endif |
reed@google.com | 3bbee32 | 2011-08-05 16:13:09 +0000 | [diff] [blame] | 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 34 | |
| 35 | //read bytes starting from the begin-th index in the buffer |
| 36 | //read in Intel order, and return an integer |
| 37 | |
| 38 | #define readByte(buffer,begin) buffer[begin] |
| 39 | #define read2Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8) |
| 40 | #define read4Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8)+(buffer[begin+2]<<16)+(buffer[begin+3]<<24) |
| 41 | |
| 42 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | SkICOImageDecoder::SkICOImageDecoder() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | //helpers - my function pointer will call one of these, depending on the bitCount, each time through the inner loop |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 49 | static void editPixelBit1(const int pixelNo, const unsigned char* buf, |
| 50 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 52 | static void editPixelBit4(const int pixelNo, const unsigned char* buf, |
| 53 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 55 | static void editPixelBit8(const int pixelNo, const unsigned char* buf, |
| 56 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 58 | static void editPixelBit24(const int pixelNo, const unsigned char* buf, |
| 59 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 61 | static void editPixelBit32(const int pixelNo, const unsigned char* buf, |
| 62 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 64 | |
| 65 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | static int calculateRowBytesFor8888(int w, int bitCount) |
| 67 | { |
| 68 | // Default rowBytes is w << 2 for kARGB_8888 |
| 69 | // In the case of a 4 bit image with an odd width, we need to add some |
| 70 | // so we can go off the end of the drawn bitmap. |
| 71 | // Add 4 to ensure that it is still a multiple of 4. |
| 72 | if (4 == bitCount && (w & 0x1)) { |
| 73 | return (w + 1) << 2; |
| 74 | } |
| 75 | // Otherwise return 0, which will allow it to be calculated automatically. |
| 76 | return 0; |
| 77 | } |
| 78 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 79 | bool SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | { |
| 81 | size_t length = stream->read(NULL, 0); |
| 82 | SkAutoMalloc autoMal(length); |
| 83 | unsigned char* buf = (unsigned char*)autoMal.get(); |
| 84 | if (stream->read((void*)buf, length) != length) { |
| 85 | return false; |
| 86 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | //these should always be the same - should i use for error checking? - what about files that have some |
| 89 | //incorrect values, but still decode properly? |
| 90 | int reserved = read2Bytes(buf, 0); // 0 |
| 91 | int type = read2Bytes(buf, 2); // 1 |
| 92 | if (reserved != 0 || type != 1) |
| 93 | return false; |
| 94 | int count = read2Bytes(buf, 4); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 95 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | //need to at least have enough space to hold the initial table of info |
| 97 | if (length < (size_t)(6 + count*16)) |
| 98 | return false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 99 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | int choice; |
| 101 | Chooser* chooser = this->getChooser(); |
| 102 | //FIXME:if no chooser, consider providing the largest color image |
| 103 | //what are the odds that the largest image would be monochrome? |
| 104 | if (NULL == chooser) { |
| 105 | choice = 0; |
| 106 | } else { |
| 107 | chooser->begin(count); |
| 108 | for (int i = 0; i < count; i++) |
| 109 | { |
| 110 | //need to find out the config, width, and height from the stream |
| 111 | int width = readByte(buf, 6 + i*16); |
| 112 | int height = readByte(buf, 7 + i*16); |
| 113 | int offset = read4Bytes(buf, 18 + i*16); |
| 114 | int bitCount = read2Bytes(buf, offset+14); |
| 115 | SkBitmap::Config c; |
| 116 | //currently only provide ARGB_8888_, but maybe we want kIndex8_Config for 1 and 4, and possibly 8? |
| 117 | //or maybe we'll determine this based on the provided config |
| 118 | switch (bitCount) |
| 119 | { |
| 120 | case 1: |
| 121 | case 4: |
| 122 | // In reality, at least for the moment, these will be decoded into kARGB_8888 bitmaps. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 123 | // However, this will be used to distinguish between the lower quality 1bpp and 4 bpp |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | // images and the higher quality images. |
| 125 | c = SkBitmap::kIndex8_Config; |
| 126 | break; |
| 127 | case 8: |
| 128 | case 24: |
| 129 | case 32: |
| 130 | c = SkBitmap::kARGB_8888_Config; |
| 131 | break; |
| 132 | default: |
| 133 | SkDEBUGF(("Image with %ibpp not supported\n", bitCount)); |
| 134 | continue; |
| 135 | } |
| 136 | chooser->inspect(i, c, width, height); |
| 137 | } |
| 138 | choice = chooser->choose(); |
| 139 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | //you never know what the chooser is going to supply |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 142 | if (choice >= count || choice < 0) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | return false; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 144 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | //skip ahead to the correct header |
| 146 | //commented out lines are not used, but if i switch to other read method, need to know how many to skip |
| 147 | //otherwise, they could be used for error checking |
| 148 | int w = readByte(buf, 6 + choice*16); |
| 149 | int h = readByte(buf, 7 + choice*16); |
| 150 | int colorCount = readByte(buf, 8 + choice*16); |
| 151 | //int reservedToo = readByte(buf, 9 + choice*16); //0 |
| 152 | //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0 |
| 153 | //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usually 0 |
| 154 | int size = read4Bytes(buf, 14 + choice*16); //matters? |
| 155 | int offset = read4Bytes(buf, 18 + choice*16); |
| 156 | if ((size_t)(offset + size) > length) |
| 157 | return false; |
| 158 | //int infoSize = read4Bytes(buf, offset); //40 |
| 159 | //int width = read4Bytes(buf, offset+4); //should == w |
| 160 | //int height = read4Bytes(buf, offset+8); //should == 2*h |
| 161 | //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it?) |
| 162 | int bitCount = read2Bytes(buf, offset+14); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 163 | |
| 164 | void (*placePixel)(const int pixelNo, const unsigned char* buf, |
| 165 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) = NULL; |
| 167 | switch (bitCount) |
| 168 | { |
| 169 | case 1: |
| 170 | placePixel = &editPixelBit1; |
| 171 | colorCount = 2; |
| 172 | break; |
| 173 | case 4: |
| 174 | placePixel = &editPixelBit4; |
| 175 | colorCount = 16; |
| 176 | break; |
| 177 | case 8: |
| 178 | placePixel = &editPixelBit8; |
| 179 | colorCount = 256; |
| 180 | break; |
| 181 | case 24: |
| 182 | placePixel = &editPixelBit24; |
| 183 | colorCount = 0; |
| 184 | break; |
| 185 | case 32: |
| 186 | placePixel = &editPixelBit32; |
| 187 | colorCount = 0; |
| 188 | break; |
| 189 | default: |
| 190 | SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount)); |
| 191 | return false; |
| 192 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | //these should all be zero, but perhaps are not - need to check |
| 195 | //int compression = read4Bytes(buf, offset+16); //0 |
| 196 | //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value |
| 197 | //int xPixels = read4Bytes(buf, offset+24); //0 |
| 198 | //int yPixels = read4Bytes(buf, offset+28); //0 |
| 199 | //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an actual value though |
| 200 | //int colorsImportant = read4Bytes(buf, offset+36); //0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | int begin = offset + 40; |
| 203 | //this array represents the colortable |
| 204 | //if i allow other types of bitmaps, it may actually be used as a part of the bitmap |
| 205 | SkPMColor* colors = NULL; |
| 206 | int blue, green, red; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 207 | if (colorCount) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | { |
| 209 | colors = new SkPMColor[colorCount]; |
| 210 | for (int j = 0; j < colorCount; j++) |
| 211 | { |
| 212 | //should this be a function - maybe a #define? |
| 213 | blue = readByte(buf, begin + 4*j); |
| 214 | green = readByte(buf, begin + 4*j + 1); |
| 215 | red = readByte(buf, begin + 4*j + 2); |
| 216 | colors[j] = SkPackARGB32(0xFF, red & 0xFF, green & 0xFF, blue & 0xFF); |
| 217 | } |
| 218 | } |
| 219 | int bitWidth = w*bitCount; |
| 220 | int test = bitWidth & 0x1F; |
| 221 | int mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 |
| 222 | int lineBitWidth = (bitWidth & 0xFFFFFFE0) + (0x20 & mask); |
| 223 | int lineWidth = lineBitWidth/bitCount; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 224 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | int xorOffset = begin + colorCount*4; //beginning of the color bitmap |
| 226 | //other read method means we will just be here already |
| 227 | int andOffset = xorOffset + ((lineWidth*h*bitCount) >> 3); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 228 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | /*int */test = w & 0x1F; //the low 5 bits - we are rounding up to the next 32 (2^5) |
| 230 | /*int */mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 |
| 231 | int andLineWidth = (w & 0xFFFFFFE0) + (0x20 & mask); |
| 232 | //if we allow different Configs, everything is the same til here |
| 233 | //change the config, and use different address getter, and place index vs color, and add the color table |
| 234 | //FIXME: what is the tradeoff in size? |
| 235 | //if the andbitmap (mask) is all zeroes, then we can easily do an index bitmap |
| 236 | //however, with small images with large colortables, maybe it's better to still do argb_8888 |
| 237 | |
| 238 | bm->setConfig(SkBitmap::kARGB_8888_Config, w, h, calculateRowBytesFor8888(w, bitCount)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 239 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 241 | delete[] colors; |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | if (!this->allocPixelRef(bm, NULL)) |
| 246 | { |
| 247 | delete[] colors; |
| 248 | return false; |
| 249 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 250 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | SkAutoLockPixels alp(*bm); |
| 252 | |
| 253 | for (int y = 0; y < h; y++) |
| 254 | { |
| 255 | for (int x = 0; x < w; x++) |
| 256 | { |
| 257 | //U32* address = bm->getAddr32(x, y); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 258 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | //check the alpha bit first, but pass it along to the function to figure out how to deal with it |
| 260 | int andPixelNo = andLineWidth*(h-y-1)+x; |
| 261 | //only need to get a new alphaByte when x %8 == 0 |
| 262 | //but that introduces an if and a mod - probably much slower |
| 263 | //that's ok, it's just a read of an array, not a stream |
| 264 | int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3)); |
| 265 | int shift = 7 - (andPixelNo & 0x7); |
| 266 | int m = 1 << shift; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 267 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | int pixelNo = lineWidth*(h-y-1)+x; |
| 269 | placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift, colors); |
| 270 | |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | delete [] colors; |
| 275 | //ensure we haven't read off the end? |
| 276 | //of course this doesn't help us if the andOffset was a lie... |
| 277 | //return andOffset + (andLineWidth >> 3) <= length; |
| 278 | return true; |
| 279 | } //onDecode |
| 280 | |
| 281 | //function to place the pixel, determined by the bitCount |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 282 | static void editPixelBit1(const int pixelNo, const unsigned char* buf, |
| 283 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 285 | { |
| 286 | // note that this should be the same as/similar to the AND bitmap |
| 287 | SkPMColor* address = bm->getAddr32(x,y); |
| 288 | int byte = readByte(buf, xorOffset + (pixelNo >> 3)); |
| 289 | int colorBit; |
| 290 | int alphaBit; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 291 | // Read all of the bits in this byte. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | int i = x + 8; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 293 | // Pin to the width so we do not write outside the bounds of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | // our color table. |
| 295 | i = i > w ? w : i; |
| 296 | // While loop to check all 8 bits individually. |
| 297 | while (x < i) |
| 298 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 299 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | colorBit = (byte & m) >> shift; |
| 301 | alphaBit = (alphaByte & m) >> shift; |
| 302 | *address = (alphaBit-1)&(colors[colorBit]); |
| 303 | x++; |
| 304 | // setup for the next pixel |
| 305 | address = address + 1; |
| 306 | m = m >> 1; |
| 307 | shift -= 1; |
| 308 | } |
| 309 | x--; |
| 310 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 311 | static void editPixelBit4(const int pixelNo, const unsigned char* buf, |
| 312 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 313 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 314 | { |
| 315 | SkPMColor* address = bm->getAddr32(x, y); |
| 316 | int byte = readByte(buf, xorOffset + (pixelNo >> 1)); |
| 317 | int pixel = (byte >> 4) & 0xF; |
| 318 | int alphaBit = (alphaByte & m) >> shift; |
| 319 | *address = (alphaBit-1)&(colors[pixel]); |
| 320 | x++; |
| 321 | //if w is odd, x may be the same as w, which means we are writing to an unused portion of the bitmap |
| 322 | //but that's okay, since i've added an extra rowByte for just this purpose |
| 323 | address = address + 1; |
| 324 | pixel = byte & 0xF; |
| 325 | m = m >> 1; |
| 326 | alphaBit = (alphaByte & m) >> (shift-1); |
| 327 | //speed up trick here |
| 328 | *address = (alphaBit-1)&(colors[pixel]); |
| 329 | } |
| 330 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 331 | static void editPixelBit8(const int pixelNo, const unsigned char* buf, |
| 332 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 334 | { |
| 335 | SkPMColor* address = bm->getAddr32(x, y); |
| 336 | int pixel = readByte(buf, xorOffset + pixelNo); |
| 337 | int alphaBit = (alphaByte & m) >> shift; |
| 338 | *address = (alphaBit-1)&(colors[pixel]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 339 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 341 | static void editPixelBit24(const int pixelNo, const unsigned char* buf, |
| 342 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 343 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 344 | { |
| 345 | SkPMColor* address = bm->getAddr32(x, y); |
| 346 | int blue = readByte(buf, xorOffset + 3*pixelNo); |
| 347 | int green = readByte(buf, xorOffset + 3*pixelNo + 1); |
| 348 | int red = readByte(buf, xorOffset + 3*pixelNo + 2); |
| 349 | int alphaBit = (alphaByte & m) >> shift; |
| 350 | //alphaBit == 1 => alpha = 0 |
| 351 | int alpha = (alphaBit-1) & 0xFF; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 352 | *address = SkPreMultiplyARGB(alpha, red, green, blue); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | } |
| 354 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 355 | static void editPixelBit32(const int pixelNo, const unsigned char* buf, |
| 356 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 358 | { |
| 359 | SkPMColor* address = bm->getAddr32(x, y); |
| 360 | int blue = readByte(buf, xorOffset + 4*pixelNo); |
| 361 | int green = readByte(buf, xorOffset + 4*pixelNo + 1); |
| 362 | int red = readByte(buf, xorOffset + 4*pixelNo + 2); |
| 363 | int alphaBit = (alphaByte & m) >> shift; |
reed@google.com | 3bbee32 | 2011-08-05 16:13:09 +0000 | [diff] [blame] | 364 | #if 1 // don't trust the alphaBit for 32bit images <mrr> |
| 365 | alphaBit = 0; |
| 366 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); |
reed@android.com | ca77697 | 2010-04-13 14:03:27 +0000 | [diff] [blame] | 368 | *address = SkPreMultiplyARGB(alpha, red, green, blue); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 371 | /////////////////////////////////////////////////////////////////////////////// |
| 372 | DEFINE_DECODER_CREATOR(ICOImageDecoder); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 373 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 374 | |
| 375 | #include "SkTRegistry.h" |
| 376 | |
caryclark@google.com | 2a2cc20 | 2012-06-06 12:04:36 +0000 | [diff] [blame] | 377 | static SkImageDecoder* sk_libico_dfactory(SkStream* stream) { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 378 | // Check to see if the first four bytes are 0,0,1,0 |
| 379 | // FIXME: Is that required and sufficient? |
| 380 | SkAutoMalloc autoMal(4); |
| 381 | unsigned char* buf = (unsigned char*)autoMal.get(); |
| 382 | stream->read((void*)buf, 4); |
| 383 | int reserved = read2Bytes(buf, 0); |
| 384 | int type = read2Bytes(buf, 2); |
| 385 | if (reserved != 0 || type != 1) { |
| 386 | // This stream does not represent an ICO image. |
| 387 | return NULL; |
| 388 | } |
| 389 | return SkNEW(SkICOImageDecoder); |
| 390 | } |
| 391 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 392 | static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libico_dfactory); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 393 | |