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