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