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