gfx/skia/trunk/src/images/SkImageDecoder_libico.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial