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