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