blob: 14f575babab4162f9db3706e6350af9479692a1c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkImageDecoder.h"
11#include "SkStream.h"
12#include "SkColorPriv.h"
13#include "SkTypes.h"
14
15class SkICOImageDecoder : public SkImageDecoder {
16public:
17 SkICOImageDecoder();
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000019 virtual Format getFormat() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 return kICO_Format;
21 }
22
23protected:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000024 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000026private:
27 typedef SkImageDecoder INHERITED;
28};
reed@google.com3bbee322011-08-05 16:13:09 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030/////////////////////////////////////////////////////////////////////////////////////////
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]+(buffer[begin+1]<<8)
37#define read4Bytes(buffer,begin) buffer[begin]+(buffer[begin+1]<<8)+(buffer[begin+2]<<16)+(buffer[begin+3]<<24)
38
39/////////////////////////////////////////////////////////////////////////////////////////
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041SkICOImageDecoder::SkICOImageDecoder()
42{
43}
44
45//helpers - my function pointer will call one of these, depending on the bitCount, each time through the inner loop
rmistry@google.comd6176b02012-08-23 18:14:13 +000046static void editPixelBit1(const int pixelNo, const unsigned char* buf,
47 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors);
rmistry@google.comd6176b02012-08-23 18:14:13 +000049static void editPixelBit4(const int pixelNo, const unsigned char* buf,
50 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors);
rmistry@google.comd6176b02012-08-23 18:14:13 +000052static void editPixelBit8(const int pixelNo, const unsigned char* buf,
53 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors);
rmistry@google.comd6176b02012-08-23 18:14:13 +000055static void editPixelBit24(const int pixelNo, const unsigned char* buf,
56 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors);
rmistry@google.comd6176b02012-08-23 18:14:13 +000058static void editPixelBit32(const int pixelNo, const unsigned char* buf,
59 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors);
rmistry@google.comd6176b02012-08-23 18:14:13 +000061
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063static 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
reed@android.com3f1f06a2010-03-03 21:04:12 +000076bool SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode)
reed@android.com8a1c16f2008-12-17 15:59:43 +000077{
78 size_t length = stream->read(NULL, 0);
79 SkAutoMalloc autoMal(length);
80 unsigned char* buf = (unsigned char*)autoMal.get();
81 if (stream->read((void*)buf, length) != length) {
82 return false;
83 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000084
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 //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.comd6176b02012-08-23 18:14:13 +000092
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 //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.comd6176b02012-08-23 18:14:13 +000096
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 int choice;
98 Chooser* chooser = this->getChooser();
99 //FIXME:if no chooser, consider providing the largest color image
100 //what are the odds that the largest image would be monochrome?
101 if (NULL == chooser) {
102 choice = 0;
103 } else {
104 chooser->begin(count);
105 for (int i = 0; i < count; i++)
106 {
107 //need to find out the config, width, and height from the stream
108 int width = readByte(buf, 6 + i*16);
109 int height = readByte(buf, 7 + i*16);
110 int offset = read4Bytes(buf, 18 + i*16);
111 int bitCount = read2Bytes(buf, offset+14);
112 SkBitmap::Config c;
113 //currently only provide ARGB_8888_, but maybe we want kIndex8_Config for 1 and 4, and possibly 8?
114 //or maybe we'll determine this based on the provided config
115 switch (bitCount)
116 {
117 case 1:
118 case 4:
119 // In reality, at least for the moment, these will be decoded into kARGB_8888 bitmaps.
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120 // However, this will be used to distinguish between the lower quality 1bpp and 4 bpp
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 // images and the higher quality images.
122 c = SkBitmap::kIndex8_Config;
123 break;
124 case 8:
125 case 24:
126 case 32:
127 c = SkBitmap::kARGB_8888_Config;
128 break;
129 default:
130 SkDEBUGF(("Image with %ibpp not supported\n", bitCount));
131 continue;
132 }
133 chooser->inspect(i, c, width, height);
134 }
135 choice = chooser->choose();
136 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000137
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138 //you never know what the chooser is going to supply
rmistry@google.comd6176b02012-08-23 18:14:13 +0000139 if (choice >= count || choice < 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 return false;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 //skip ahead to the correct header
143 //commented out lines are not used, but if i switch to other read method, need to know how many to skip
144 //otherwise, they could be used for error checking
145 int w = readByte(buf, 6 + choice*16);
146 int h = readByte(buf, 7 + choice*16);
147 int colorCount = readByte(buf, 8 + choice*16);
148 //int reservedToo = readByte(buf, 9 + choice*16); //0
149 //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0
150 //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usually 0
151 int size = read4Bytes(buf, 14 + choice*16); //matters?
152 int offset = read4Bytes(buf, 18 + choice*16);
153 if ((size_t)(offset + size) > length)
154 return false;
155 //int infoSize = read4Bytes(buf, offset); //40
156 //int width = read4Bytes(buf, offset+4); //should == w
157 //int height = read4Bytes(buf, offset+8); //should == 2*h
158 //int planesToo = read2Bytes(buf, offset+12); //should == 1 (does it?)
159 int bitCount = read2Bytes(buf, offset+14);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000160
161 void (*placePixel)(const int pixelNo, const unsigned char* buf,
162 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) = NULL;
164 switch (bitCount)
165 {
166 case 1:
167 placePixel = &editPixelBit1;
168 colorCount = 2;
169 break;
170 case 4:
171 placePixel = &editPixelBit4;
172 colorCount = 16;
173 break;
174 case 8:
175 placePixel = &editPixelBit8;
176 colorCount = 256;
177 break;
178 case 24:
179 placePixel = &editPixelBit24;
180 colorCount = 0;
181 break;
182 case 32:
183 placePixel = &editPixelBit32;
184 colorCount = 0;
185 break;
186 default:
187 SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount));
188 return false;
189 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
reed@android.com8a1c16f2008-12-17 15:59:43 +0000191 //these should all be zero, but perhaps are not - need to check
192 //int compression = read4Bytes(buf, offset+16); //0
193 //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value
194 //int xPixels = read4Bytes(buf, offset+24); //0
195 //int yPixels = read4Bytes(buf, offset+28); //0
196 //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an actual value though
197 //int colorsImportant = read4Bytes(buf, offset+36); //0
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
reed@android.com8a1c16f2008-12-17 15:59:43 +0000199 int begin = offset + 40;
200 //this array represents the colortable
201 //if i allow other types of bitmaps, it may actually be used as a part of the bitmap
202 SkPMColor* colors = NULL;
203 int blue, green, red;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204 if (colorCount)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 {
206 colors = new SkPMColor[colorCount];
207 for (int j = 0; j < colorCount; j++)
208 {
209 //should this be a function - maybe a #define?
210 blue = readByte(buf, begin + 4*j);
211 green = readByte(buf, begin + 4*j + 1);
212 red = readByte(buf, begin + 4*j + 2);
213 colors[j] = SkPackARGB32(0xFF, red & 0xFF, green & 0xFF, blue & 0xFF);
214 }
215 }
216 int bitWidth = w*bitCount;
217 int test = bitWidth & 0x1F;
218 int mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0
219 int lineBitWidth = (bitWidth & 0xFFFFFFE0) + (0x20 & mask);
220 int lineWidth = lineBitWidth/bitCount;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 int xorOffset = begin + colorCount*4; //beginning of the color bitmap
223 //other read method means we will just be here already
224 int andOffset = xorOffset + ((lineWidth*h*bitCount) >> 3);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 /*int */test = w & 0x1F; //the low 5 bits - we are rounding up to the next 32 (2^5)
227 /*int */mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0
228 int andLineWidth = (w & 0xFFFFFFE0) + (0x20 & mask);
229 //if we allow different Configs, everything is the same til here
230 //change the config, and use different address getter, and place index vs color, and add the color table
231 //FIXME: what is the tradeoff in size?
232 //if the andbitmap (mask) is all zeroes, then we can easily do an index bitmap
233 //however, with small images with large colortables, maybe it's better to still do argb_8888
234
reed@android.com8a1c16f2008-12-17 15:59:43 +0000235 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000236 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h, calculateRowBytesFor8888(w, bitCount));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 delete[] colors;
238 return true;
239 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000240 // No Bitmap reuse supported for this format
241 if (!bm->isNull()) {
242 return false;
243 }
244 bm->setConfig(SkBitmap::kARGB_8888_Config, w, h, calculateRowBytesFor8888(w, bitCount));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245
246 if (!this->allocPixelRef(bm, NULL))
247 {
248 delete[] colors;
249 return false;
250 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000251
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252 SkAutoLockPixels alp(*bm);
253
254 for (int y = 0; y < h; y++)
255 {
256 for (int x = 0; x < w; x++)
257 {
258 //U32* address = bm->getAddr32(x, y);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000259
reed@android.com8a1c16f2008-12-17 15:59:43 +0000260 //check the alpha bit first, but pass it along to the function to figure out how to deal with it
261 int andPixelNo = andLineWidth*(h-y-1)+x;
262 //only need to get a new alphaByte when x %8 == 0
263 //but that introduces an if and a mod - probably much slower
264 //that's ok, it's just a read of an array, not a stream
265 int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3));
266 int shift = 7 - (andPixelNo & 0x7);
267 int m = 1 << shift;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 int pixelNo = lineWidth*(h-y-1)+x;
270 placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift, colors);
271
272 }
273 }
274
275 delete [] colors;
276 //ensure we haven't read off the end?
277 //of course this doesn't help us if the andOffset was a lie...
278 //return andOffset + (andLineWidth >> 3) <= length;
279 return true;
280} //onDecode
281
282//function to place the pixel, determined by the bitCount
rmistry@google.comd6176b02012-08-23 18:14:13 +0000283static void editPixelBit1(const int pixelNo, const unsigned char* buf,
284 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
286{
287 // note that this should be the same as/similar to the AND bitmap
288 SkPMColor* address = bm->getAddr32(x,y);
289 int byte = readByte(buf, xorOffset + (pixelNo >> 3));
290 int colorBit;
291 int alphaBit;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000292 // Read all of the bits in this byte.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 int i = x + 8;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000294 // Pin to the width so we do not write outside the bounds of
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 // our color table.
296 i = i > w ? w : i;
297 // While loop to check all 8 bits individually.
298 while (x < i)
299 {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000300
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 colorBit = (byte & m) >> shift;
302 alphaBit = (alphaByte & m) >> shift;
303 *address = (alphaBit-1)&(colors[colorBit]);
304 x++;
305 // setup for the next pixel
306 address = address + 1;
307 m = m >> 1;
308 shift -= 1;
309 }
310 x--;
311}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000312static void editPixelBit4(const int pixelNo, const unsigned char* buf,
313 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
315{
316 SkPMColor* address = bm->getAddr32(x, y);
317 int byte = readByte(buf, xorOffset + (pixelNo >> 1));
318 int pixel = (byte >> 4) & 0xF;
319 int alphaBit = (alphaByte & m) >> shift;
320 *address = (alphaBit-1)&(colors[pixel]);
321 x++;
322 //if w is odd, x may be the same as w, which means we are writing to an unused portion of the bitmap
323 //but that's okay, since i've added an extra rowByte for just this purpose
324 address = address + 1;
325 pixel = byte & 0xF;
326 m = m >> 1;
327 alphaBit = (alphaByte & m) >> (shift-1);
328 //speed up trick here
329 *address = (alphaBit-1)&(colors[pixel]);
330}
331
rmistry@google.comd6176b02012-08-23 18:14:13 +0000332static void editPixelBit8(const int pixelNo, const unsigned char* buf,
333 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
335{
336 SkPMColor* address = bm->getAddr32(x, y);
337 int pixel = readByte(buf, xorOffset + pixelNo);
338 int alphaBit = (alphaByte & m) >> shift;
339 *address = (alphaBit-1)&(colors[pixel]);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000340}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341
rmistry@google.comd6176b02012-08-23 18:14:13 +0000342static void editPixelBit24(const int pixelNo, const unsigned char* buf,
343 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
345{
346 SkPMColor* address = bm->getAddr32(x, y);
347 int blue = readByte(buf, xorOffset + 3*pixelNo);
348 int green = readByte(buf, xorOffset + 3*pixelNo + 1);
349 int red = readByte(buf, xorOffset + 3*pixelNo + 2);
350 int alphaBit = (alphaByte & m) >> shift;
351 //alphaBit == 1 => alpha = 0
352 int alpha = (alphaBit-1) & 0xFF;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000353 *address = SkPreMultiplyARGB(alpha, red, green, blue);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000354}
355
rmistry@google.comd6176b02012-08-23 18:14:13 +0000356static void editPixelBit32(const int pixelNo, const unsigned char* buf,
357 const int xorOffset, int& x, int y, const int w,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
359{
360 SkPMColor* address = bm->getAddr32(x, y);
361 int blue = readByte(buf, xorOffset + 4*pixelNo);
362 int green = readByte(buf, xorOffset + 4*pixelNo + 1);
363 int red = readByte(buf, xorOffset + 4*pixelNo + 2);
364 int alphaBit = (alphaByte & m) >> shift;
reed@google.com3bbee322011-08-05 16:13:09 +0000365#if 1 // don't trust the alphaBit for 32bit images <mrr>
366 alphaBit = 0;
367#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF);
reed@android.comca776972010-04-13 14:03:27 +0000369 *address = SkPreMultiplyARGB(alpha, red, green, blue);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370}
371
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000372///////////////////////////////////////////////////////////////////////////////
373DEFINE_DECODER_CREATOR(ICOImageDecoder);
reed@android.com00bf85a2009-01-22 13:04:56 +0000374/////////////////////////////////////////////////////////////////////////////////////////
375
376#include "SkTRegistry.h"
377
caryclark@google.com2a2cc202012-06-06 12:04:36 +0000378static SkImageDecoder* sk_libico_dfactory(SkStream* stream) {
reed@android.com00bf85a2009-01-22 13:04:56 +0000379 // Check to see if the first four bytes are 0,0,1,0
380 // FIXME: Is that required and sufficient?
381 SkAutoMalloc autoMal(4);
382 unsigned char* buf = (unsigned char*)autoMal.get();
383 stream->read((void*)buf, 4);
384 int reserved = read2Bytes(buf, 0);
385 int type = read2Bytes(buf, 2);
386 if (reserved != 0 || type != 1) {
387 // This stream does not represent an ICO image.
388 return NULL;
389 }
390 return SkNEW(SkICOImageDecoder);
391}
392
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000393static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libico_dfactory);