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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 18 | Format getFormat() const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | return kICO_Format; |
| 20 | } |
| 21 | |
| 22 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | Result onDecode(SkStream* stream, SkBitmap* bm, Mode) 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 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 75 | SkImageDecoder::Result 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); |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 79 | // Check that the buffer is large enough to read the directory header |
| 80 | if (length < 6) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 81 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
scroggo@google.com | dbf9f88 | 2013-08-21 15:01:48 +0000 | [diff] [blame] | 84 | unsigned char* buf = (unsigned char*)autoMal.get(); |
| 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | //these should always be the same - should i use for error checking? - what about files that have some |
| 87 | //incorrect values, but still decode properly? |
| 88 | int reserved = read2Bytes(buf, 0); // 0 |
| 89 | int type = read2Bytes(buf, 2); // 1 |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 90 | if (reserved != 0 || type != 1) { |
| 91 | return kFailure; |
| 92 | } |
| 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | int count = read2Bytes(buf, 4); |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 95 | // Check that there are directory entries |
| 96 | if (count < 1) { |
| 97 | return kFailure; |
| 98 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 100 | // Check that buffer is large enough to read directory entries. |
| 101 | // We are guaranteed that count is at least 1. We might as well assume |
| 102 | // count is 1 because this deprecated decoder only looks at the first |
| 103 | // directory entry. |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 104 | if (length < (size_t)(6 + count*16)) { |
| 105 | return kFailure; |
| 106 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | //skip ahead to the correct header |
| 109 | //commented out lines are not used, but if i switch to other read method, need to know how many to skip |
| 110 | //otherwise, they could be used for error checking |
reed | 2d73d80 | 2014-12-22 07:37:29 -0800 | [diff] [blame] | 111 | int w = readByte(buf, 6); |
| 112 | int h = readByte(buf, 7); |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 113 | SkASSERT(w >= 0 && h >= 0); |
reed | 2d73d80 | 2014-12-22 07:37:29 -0800 | [diff] [blame] | 114 | int colorCount = readByte(buf, 8); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | //int reservedToo = readByte(buf, 9 + choice*16); //0 |
| 116 | //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0 |
| 117 | //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usually 0 |
reed | 2d73d80 | 2014-12-22 07:37:29 -0800 | [diff] [blame] | 118 | const size_t size = read4Bytes(buf, 14); //matters? |
| 119 | const size_t offset = read4Bytes(buf, 18); |
djsollen | 97b4947 | 2014-08-26 08:31:24 -0700 | [diff] [blame] | 120 | // promote the sum to 64-bits to avoid overflow |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 121 | // Check that buffer is large enough to read image data |
scroggo | b61e206 | 2014-11-10 13:12:25 -0800 | [diff] [blame] | 122 | if (offset > length || size > length || ((uint64_t)offset + size) > length) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 123 | return kFailure; |
scroggo | 57ad493 | 2014-07-09 15:04:20 -0700 | [diff] [blame] | 124 | } |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 125 | |
| 126 | // Check to see if this is a PNG image inside the ICO |
| 127 | { |
| 128 | SkMemoryStream subStream(buf + offset, size, false); |
| 129 | SkAutoTDelete<SkImageDecoder> otherDecoder(SkImageDecoder::Factory(&subStream)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | if (otherDecoder.get() != nullptr) { |
djsollen | 6a9c7b1 | 2014-08-26 11:35:14 -0700 | [diff] [blame] | 131 | // Disallow nesting ICO files within one another |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 132 | // FIXME: Can ICO files contain other formats besides PNG? |
djsollen | 6a9c7b1 | 2014-08-26 11:35:14 -0700 | [diff] [blame] | 133 | if (otherDecoder->getFormat() == SkImageDecoder::kICO_Format) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 134 | return kFailure; |
djsollen | 6a9c7b1 | 2014-08-26 11:35:14 -0700 | [diff] [blame] | 135 | } |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 136 | // Set fields on the other decoder to be the same as this one. |
| 137 | this->copyFieldsToOther(otherDecoder.get()); |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 138 | const Result result = otherDecoder->decode(&subStream, bm, this->getDefaultPref(), |
| 139 | mode); |
| 140 | // FIXME: Should we just return result here? Is it possible that data that looked like |
| 141 | // a subimage was not, but was actually a valid ICO? |
| 142 | if (result != kFailure) { |
| 143 | return result; |
scroggo@google.com | 468142b | 2013-07-09 15:48:24 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | //int infoSize = read4Bytes(buf, offset); //40 |
| 149 | //int width = read4Bytes(buf, offset+4); //should == w |
| 150 | //int height = read4Bytes(buf, offset+8); //should == 2*h |
| 151 | //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it?) |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 152 | |
| 153 | // For ico images, only a byte is used to store each dimension |
| 154 | // 0 is used to represent 256 |
| 155 | if (w == 0) { |
| 156 | w = 256; |
| 157 | } |
| 158 | if (h == 0) { |
| 159 | h = 256; |
| 160 | } |
| 161 | |
| 162 | // Check that buffer is large enough to read the bit depth |
| 163 | if (length < offset + 16) { |
| 164 | return kFailure; |
| 165 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | int bitCount = read2Bytes(buf, offset+14); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 167 | |
| 168 | void (*placePixel)(const int pixelNo, const unsigned char* buf, |
| 169 | const int xorOffset, int& x, int y, const int w, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | switch (bitCount) |
| 172 | { |
| 173 | case 1: |
| 174 | placePixel = &editPixelBit1; |
| 175 | colorCount = 2; |
| 176 | break; |
| 177 | case 4: |
| 178 | placePixel = &editPixelBit4; |
| 179 | colorCount = 16; |
| 180 | break; |
| 181 | case 8: |
| 182 | placePixel = &editPixelBit8; |
| 183 | colorCount = 256; |
| 184 | break; |
| 185 | case 24: |
| 186 | placePixel = &editPixelBit24; |
| 187 | colorCount = 0; |
| 188 | break; |
| 189 | case 32: |
| 190 | placePixel = &editPixelBit32; |
| 191 | colorCount = 0; |
| 192 | break; |
| 193 | default: |
| 194 | SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount)); |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 195 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 197 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | //these should all be zero, but perhaps are not - need to check |
| 199 | //int compression = read4Bytes(buf, offset+16); //0 |
| 200 | //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value |
| 201 | //int xPixels = read4Bytes(buf, offset+24); //0 |
| 202 | //int yPixels = read4Bytes(buf, offset+28); //0 |
| 203 | //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an actual value though |
| 204 | //int colorsImportant = read4Bytes(buf, offset+36); //0 |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 205 | |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 206 | int begin = SkToInt(offset + 40); |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 207 | // Check that the buffer is large enough to read the color table |
| 208 | // For bmp-in-icos, there should be 4 bytes per color |
| 209 | if (length < (size_t) (begin + 4*colorCount)) { |
| 210 | return kFailure; |
| 211 | } |
| 212 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | //this array represents the colortable |
| 214 | //if i allow other types of bitmaps, it may actually be used as a part of the bitmap |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | SkPMColor* colors = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | int blue, green, red; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 217 | if (colorCount) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | { |
| 219 | colors = new SkPMColor[colorCount]; |
| 220 | for (int j = 0; j < colorCount; j++) |
| 221 | { |
| 222 | //should this be a function - maybe a #define? |
| 223 | blue = readByte(buf, begin + 4*j); |
| 224 | green = readByte(buf, begin + 4*j + 1); |
| 225 | red = readByte(buf, begin + 4*j + 2); |
| 226 | colors[j] = SkPackARGB32(0xFF, red & 0xFF, green & 0xFF, blue & 0xFF); |
| 227 | } |
| 228 | } |
| 229 | int bitWidth = w*bitCount; |
| 230 | int test = bitWidth & 0x1F; |
| 231 | int mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 |
| 232 | int lineBitWidth = (bitWidth & 0xFFFFFFE0) + (0x20 & mask); |
| 233 | int lineWidth = lineBitWidth/bitCount; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 234 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | int xorOffset = begin + colorCount*4; //beginning of the color bitmap |
| 236 | //other read method means we will just be here already |
| 237 | int andOffset = xorOffset + ((lineWidth*h*bitCount) >> 3); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | /*int */test = w & 0x1F; //the low 5 bits - we are rounding up to the next 32 (2^5) |
| 240 | /*int */mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0 |
| 241 | int andLineWidth = (w & 0xFFFFFFE0) + (0x20 & mask); |
| 242 | //if we allow different Configs, everything is the same til here |
| 243 | //change the config, and use different address getter, and place index vs color, and add the color table |
| 244 | //FIXME: what is the tradeoff in size? |
| 245 | //if the andbitmap (mask) is all zeroes, then we can easily do an index bitmap |
| 246 | //however, with small images with large colortables, maybe it's better to still do argb_8888 |
| 247 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 248 | bm->setInfo(SkImageInfo::MakeN32Premul(w, h), calculateRowBytesFor8888(w, bitCount)); |
scroggo@google.com | bc69ce9 | 2013-07-09 15:45:14 +0000 | [diff] [blame] | 249 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 251 | delete[] colors; |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 252 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | } |
| 254 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 255 | if (!this->allocPixelRef(bm, nullptr)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | { |
| 257 | delete[] colors; |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 258 | return kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 260 | |
msarett | 6e8f903 | 2015-03-13 08:07:01 -0700 | [diff] [blame] | 261 | // The AND mask is a 1-bit alpha mask for each pixel that comes after the |
| 262 | // XOR mask in the bmp. If we check that the largest AND offset is safe, |
| 263 | // it should mean all other buffer accesses will be at smaller indices and |
| 264 | // will therefore be safe. |
| 265 | size_t maxAndOffset = andOffset + ((andLineWidth*(h-1)+(w-1)) >> 3); |
| 266 | if (length <= maxAndOffset) { |
| 267 | return kFailure; |
| 268 | } |
| 269 | |
| 270 | // Here we assert that all reads from the buffer using the XOR offset are |
| 271 | // less than the AND offset. This should be guaranteed based on the above |
| 272 | // calculations. |
| 273 | #ifdef SK_DEBUG |
| 274 | int maxPixelNum = lineWidth*(h-1)+w-1; |
| 275 | int maxByte; |
| 276 | switch (bitCount) { |
| 277 | case 1: |
| 278 | maxByte = maxPixelNum >> 3; |
| 279 | break; |
| 280 | case 4: |
| 281 | maxByte = maxPixelNum >> 1; |
| 282 | break; |
| 283 | case 8: |
| 284 | maxByte = maxPixelNum; |
| 285 | break; |
| 286 | case 24: |
| 287 | maxByte = maxPixelNum * 3 + 2; |
| 288 | break; |
| 289 | case 32: |
| 290 | maxByte = maxPixelNum * 4 + 3; |
| 291 | break; |
| 292 | default: |
| 293 | SkASSERT(false); |
| 294 | return kFailure; |
| 295 | } |
| 296 | int maxXOROffset = xorOffset + maxByte; |
| 297 | SkASSERT(maxXOROffset < andOffset); |
| 298 | #endif |
| 299 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | SkAutoLockPixels alp(*bm); |
| 301 | |
| 302 | for (int y = 0; y < h; y++) |
| 303 | { |
| 304 | for (int x = 0; x < w; x++) |
| 305 | { |
| 306 | //U32* address = bm->getAddr32(x, y); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 307 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | //check the alpha bit first, but pass it along to the function to figure out how to deal with it |
| 309 | int andPixelNo = andLineWidth*(h-y-1)+x; |
| 310 | //only need to get a new alphaByte when x %8 == 0 |
| 311 | //but that introduces an if and a mod - probably much slower |
| 312 | //that's ok, it's just a read of an array, not a stream |
| 313 | int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3)); |
| 314 | int shift = 7 - (andPixelNo & 0x7); |
| 315 | int m = 1 << shift; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 316 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | int pixelNo = lineWidth*(h-y-1)+x; |
| 318 | placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift, colors); |
| 319 | |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | delete [] colors; |
| 324 | //ensure we haven't read off the end? |
| 325 | //of course this doesn't help us if the andOffset was a lie... |
| 326 | //return andOffset + (andLineWidth >> 3) <= length; |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 327 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | } //onDecode |
| 329 | |
| 330 | //function to place the pixel, determined by the bitCount |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 331 | static void editPixelBit1(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 | // note that this should be the same as/similar to the AND bitmap |
| 336 | SkPMColor* address = bm->getAddr32(x,y); |
| 337 | int byte = readByte(buf, xorOffset + (pixelNo >> 3)); |
| 338 | int colorBit; |
| 339 | int alphaBit; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 340 | // Read all of the bits in this byte. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | int i = x + 8; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 342 | // 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] | 343 | // our color table. |
| 344 | i = i > w ? w : i; |
| 345 | // While loop to check all 8 bits individually. |
| 346 | while (x < i) |
| 347 | { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | colorBit = (byte & m) >> shift; |
| 350 | alphaBit = (alphaByte & m) >> shift; |
| 351 | *address = (alphaBit-1)&(colors[colorBit]); |
| 352 | x++; |
| 353 | // setup for the next pixel |
| 354 | address = address + 1; |
| 355 | m = m >> 1; |
| 356 | shift -= 1; |
| 357 | } |
| 358 | x--; |
| 359 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 360 | static void editPixelBit4(const int pixelNo, const unsigned char* buf, |
| 361 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 363 | { |
| 364 | SkPMColor* address = bm->getAddr32(x, y); |
| 365 | int byte = readByte(buf, xorOffset + (pixelNo >> 1)); |
| 366 | int pixel = (byte >> 4) & 0xF; |
| 367 | int alphaBit = (alphaByte & m) >> shift; |
| 368 | *address = (alphaBit-1)&(colors[pixel]); |
| 369 | x++; |
| 370 | //if w is odd, x may be the same as w, which means we are writing to an unused portion of the bitmap |
| 371 | //but that's okay, since i've added an extra rowByte for just this purpose |
| 372 | address = address + 1; |
| 373 | pixel = byte & 0xF; |
| 374 | m = m >> 1; |
| 375 | alphaBit = (alphaByte & m) >> (shift-1); |
| 376 | //speed up trick here |
| 377 | *address = (alphaBit-1)&(colors[pixel]); |
| 378 | } |
| 379 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 380 | static void editPixelBit8(const int pixelNo, const unsigned char* buf, |
| 381 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 382 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 383 | { |
| 384 | SkPMColor* address = bm->getAddr32(x, y); |
| 385 | int pixel = readByte(buf, xorOffset + pixelNo); |
| 386 | int alphaBit = (alphaByte & m) >> shift; |
| 387 | *address = (alphaBit-1)&(colors[pixel]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 388 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 390 | static void editPixelBit24(const int pixelNo, const unsigned char* buf, |
| 391 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 393 | { |
| 394 | SkPMColor* address = bm->getAddr32(x, y); |
| 395 | int blue = readByte(buf, xorOffset + 3*pixelNo); |
| 396 | int green = readByte(buf, xorOffset + 3*pixelNo + 1); |
| 397 | int red = readByte(buf, xorOffset + 3*pixelNo + 2); |
| 398 | int alphaBit = (alphaByte & m) >> shift; |
| 399 | //alphaBit == 1 => alpha = 0 |
| 400 | int alpha = (alphaBit-1) & 0xFF; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 401 | *address = SkPreMultiplyARGB(alpha, red, green, blue); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | } |
| 403 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 404 | static void editPixelBit32(const int pixelNo, const unsigned char* buf, |
| 405 | const int xorOffset, int& x, int y, const int w, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 406 | SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) |
| 407 | { |
| 408 | SkPMColor* address = bm->getAddr32(x, y); |
| 409 | int blue = readByte(buf, xorOffset + 4*pixelNo); |
| 410 | int green = readByte(buf, xorOffset + 4*pixelNo + 1); |
| 411 | int red = readByte(buf, xorOffset + 4*pixelNo + 2); |
| 412 | int alphaBit = (alphaByte & m) >> shift; |
reed@google.com | 3bbee32 | 2011-08-05 16:13:09 +0000 | [diff] [blame] | 413 | #if 1 // don't trust the alphaBit for 32bit images <mrr> |
| 414 | alphaBit = 0; |
| 415 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF); |
reed@android.com | ca77697 | 2010-04-13 14:03:27 +0000 | [diff] [blame] | 417 | *address = SkPreMultiplyARGB(alpha, red, green, blue); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 420 | /////////////////////////////////////////////////////////////////////////////// |
| 421 | DEFINE_DECODER_CREATOR(ICOImageDecoder); |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 422 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 423 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 424 | static bool is_ico(SkStreamRewindable* stream) { |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 425 | // Check to see if the first four bytes are 0,0,1,0 |
| 426 | // FIXME: Is that required and sufficient? |
scroggo | b752f9f | 2014-10-24 06:49:57 -0700 | [diff] [blame] | 427 | char buf[4]; |
| 428 | if (stream->read((void*)buf, 4) != 4) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 429 | return false; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 430 | } |
scroggo | b752f9f | 2014-10-24 06:49:57 -0700 | [diff] [blame] | 431 | int reserved = read2Bytes(buf, 0); |
| 432 | int type = read2Bytes(buf, 2); |
| 433 | return 0 == reserved && 1 == type; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 434 | } |
| 435 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 436 | static SkImageDecoder* sk_libico_dfactory(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 437 | if (is_ico(stream)) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 438 | return new SkICOImageDecoder; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 439 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | return nullptr; |
reed@android.com | 00bf85a | 2009-01-22 13:04:56 +0000 | [diff] [blame] | 441 | } |
| 442 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 443 | static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 444 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 445 | static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 446 | if (is_ico(stream)) { |
| 447 | return SkImageDecoder::kICO_Format; |
| 448 | } |
| 449 | return SkImageDecoder::kUnknown_Format; |
| 450 | } |
| 451 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 452 | static SkImageDecoder_FormatReg gFormatReg(get_format_ico); |