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