Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1115)

Side by Side Diff: src/images/SkImageDecoder_libico.cpp

Issue 1828433004: Revert of Delete SkImageDecoder (Closed) Base URL: https://skia.googlesource.com/skia.git@fix-animator
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | src/images/SkImageDecoder_libjpeg.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "SkData.h"
10 #include "SkImageDecoder.h"
11 #include "SkStream.h"
12 #include "SkStreamPriv.h"
13 #include "SkTypes.h"
14
15 class SkICOImageDecoder : public SkImageDecoder {
16 public:
17 SkICOImageDecoder();
18
19 Format getFormat() const override {
20 return kICO_Format;
21 }
22
23 protected:
24 Result onDecode(SkStream* stream, SkBitmap* bm, Mode) override;
25
26 private:
27 typedef SkImageDecoder INHERITED;
28 };
29
30 //////////////////////////////////////////////////////////////////////////////// /////////
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]+SkLeftShift(buffer[begin+1],8)
37 #define read4Bytes(buffer,begin) buffer[begin]+SkLeftShift(buffer[begin+1],8)+Sk LeftShift(buffer[begin+2],16)+SkLeftShift(buffer[begin+3],24)
38
39 //////////////////////////////////////////////////////////////////////////////// /////////
40
41 SkICOImageDecoder::SkICOImageDecoder()
42 {
43 }
44
45 //helpers - my function pointer will call one of these, depending on the bitCoun t, each time through the inner loop
46 static void editPixelBit1(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);
49 static void editPixelBit4(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);
52 static void editPixelBit8(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);
55 static void editPixelBit24(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 static void editPixelBit32(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);
61
62
63 static 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
76 SkImageDecoder::Result SkICOImageDecoder::onDecode(SkStream* stream, SkBitmap* b m, Mode mode) {
77 auto data = SkCopyStreamToData(stream);
78 if (!data) {
79 return kFailure;
80 }
81
82 const size_t length = data->size();
83 // Check that the buffer is large enough to read the directory header
84 if (length < 6) {
85 return kFailure;
86 }
87
88 unsigned char* buf = (unsigned char*) data->data();
89
90 //these should always be the same - should i use for error checking? - what about files that have some
91 //incorrect values, but still decode properly?
92 int reserved = read2Bytes(buf, 0); // 0
93 int type = read2Bytes(buf, 2); // 1
94 if (reserved != 0 || type != 1) {
95 return kFailure;
96 }
97
98 int count = read2Bytes(buf, 4);
99 // Check that there are directory entries
100 if (count < 1) {
101 return kFailure;
102 }
103
104 // Check that buffer is large enough to read directory entries.
105 // We are guaranteed that count is at least 1. We might as well assume
106 // count is 1 because this deprecated decoder only looks at the first
107 // directory entry.
108 if (length < (size_t)(6 + count*16)) {
109 return kFailure;
110 }
111
112 //skip ahead to the correct header
113 //commented out lines are not used, but if i switch to other read method, ne ed to know how many to skip
114 //otherwise, they could be used for error checking
115 int w = readByte(buf, 6);
116 int h = readByte(buf, 7);
117 SkASSERT(w >= 0 && h >= 0);
118 int colorCount = readByte(buf, 8);
119 //int reservedToo = readByte(buf, 9 + choice*16); //0
120 //int planes = read2Bytes(buf, 10 + choice*16); //1 - but often 0
121 //int fakeBitCount = read2Bytes(buf, 12 + choice*16); //should be real - usu ally 0
122 const size_t size = read4Bytes(buf, 14); //matters?
123 const size_t offset = read4Bytes(buf, 18);
124 // promote the sum to 64-bits to avoid overflow
125 // Check that buffer is large enough to read image data
126 if (offset > length || size > length || ((uint64_t)offset + size) > length) {
127 return kFailure;
128 }
129
130 // Check to see if this is a PNG image inside the ICO
131 {
132 SkMemoryStream subStream(buf + offset, size, false);
133 SkAutoTDelete<SkImageDecoder> otherDecoder(SkImageDecoder::Factory(&subS tream));
134 if (otherDecoder.get() != nullptr) {
135 // Disallow nesting ICO files within one another
136 // FIXME: Can ICO files contain other formats besides PNG?
137 if (otherDecoder->getFormat() == SkImageDecoder::kICO_Format) {
138 return kFailure;
139 }
140 // Set fields on the other decoder to be the same as this one.
141 this->copyFieldsToOther(otherDecoder.get());
142 const Result result = otherDecoder->decode(&subStream, bm, this->get DefaultPref(),
143 mode);
144 // FIXME: Should we just return result here? Is it possible that dat a that looked like
145 // a subimage was not, but was actually a valid ICO?
146 if (result != kFailure) {
147 return result;
148 }
149 }
150 }
151
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
157 // For ico images, only a byte is used to store each dimension
158 // 0 is used to represent 256
159 if (w == 0) {
160 w = 256;
161 }
162 if (h == 0) {
163 h = 256;
164 }
165
166 // Check that buffer is large enough to read the bit depth
167 if (length < offset + 16) {
168 return kFailure;
169 }
170 int bitCount = read2Bytes(buf, offset+14);
171
172 void (*placePixel)(const int pixelNo, const unsigned char* buf,
173 const int xorOffset, int& x, int y, const int w,
174 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors) = null ptr;
175 switch (bitCount)
176 {
177 case 1:
178 placePixel = &editPixelBit1;
179 colorCount = 2;
180 break;
181 case 4:
182 placePixel = &editPixelBit4;
183 colorCount = 16;
184 break;
185 case 8:
186 placePixel = &editPixelBit8;
187 colorCount = 256;
188 break;
189 case 24:
190 placePixel = &editPixelBit24;
191 colorCount = 0;
192 break;
193 case 32:
194 placePixel = &editPixelBit32;
195 colorCount = 0;
196 break;
197 default:
198 SkDEBUGF(("Decoding %ibpp is unimplemented\n", bitCount));
199 return kFailure;
200 }
201
202 //these should all be zero, but perhaps are not - need to check
203 //int compression = read4Bytes(buf, offset+16); //0
204 //int imageSize = read4Bytes(buf, offset+20); //0 - sometimes has a value
205 //int xPixels = read4Bytes(buf, offset+24); //0
206 //int yPixels = read4Bytes(buf, offset+28); //0
207 //int colorsUsed = read4Bytes(buf, offset+32) //0 - might have an ac tual value though
208 //int colorsImportant = read4Bytes(buf, offset+36); //0
209
210 int begin = SkToInt(offset + 40);
211 // Check that the buffer is large enough to read the color table
212 // For bmp-in-icos, there should be 4 bytes per color
213 if (length < (size_t) (begin + 4*colorCount)) {
214 return kFailure;
215 }
216
217 //this array represents the colortable
218 //if i allow other types of bitmaps, it may actually be used as a part of th e bitmap
219 SkPMColor* colors = nullptr;
220 int blue, green, red;
221 if (colorCount)
222 {
223 colors = new SkPMColor[colorCount];
224 for (int j = 0; j < colorCount; j++)
225 {
226 //should this be a function - maybe a #define?
227 blue = readByte(buf, begin + 4*j);
228 green = readByte(buf, begin + 4*j + 1);
229 red = readByte(buf, begin + 4*j + 2);
230 colors[j] = SkPackARGB32(0xFF, red & 0xFF, green & 0xFF, blue & 0xFF );
231 }
232 }
233 int bitWidth = w*bitCount;
234 int test = bitWidth & 0x1F;
235 int mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | test) & 0x1); //either 0xFFFFFFFF or 0
236 int lineBitWidth = (bitWidth & 0xFFFFFFE0) + (0x20 & mask);
237 int lineWidth = lineBitWidth/bitCount;
238
239 int xorOffset = begin + colorCount*4; //beginning of the color bitmap
240 //other read method means we will ju st be here already
241 int andOffset = xorOffset + ((lineWidth*h*bitCount) >> 3);
242
243 /*int */test = w & 0x1F; //the low 5 bits - we are rounding up to the next 32 (2^5)
244 /*int */mask = -(((test >> 4) | (test >> 3) | (test >> 2) | (test >> 1) | te st) & 0x1); //either 0xFFFFFFFF or 0
245 int andLineWidth = (w & 0xFFFFFFE0) + (0x20 & mask);
246 //if we allow different Configs, everything is the same til here
247 //change the config, and use different address getter, and place index vs co lor, and add the color table
248 //FIXME: what is the tradeoff in size?
249 //if the andbitmap (mask) is all zeroes, then we can easily do an index bitm ap
250 //however, with small images with large colortables, maybe it's better to st ill do argb_8888
251
252 bm->setInfo(SkImageInfo::MakeN32Premul(w, h), calculateRowBytesFor8888(w, bi tCount));
253
254 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
255 delete[] colors;
256 return kSuccess;
257 }
258
259 if (!this->allocPixelRef(bm, nullptr))
260 {
261 delete[] colors;
262 return kFailure;
263 }
264
265 // The AND mask is a 1-bit alpha mask for each pixel that comes after the
266 // XOR mask in the bmp. If we check that the largest AND offset is safe,
267 // it should mean all other buffer accesses will be at smaller indices and
268 // will therefore be safe.
269 size_t maxAndOffset = andOffset + ((andLineWidth*(h-1)+(w-1)) >> 3);
270 if (length <= maxAndOffset) {
271 return kFailure;
272 }
273
274 // Here we assert that all reads from the buffer using the XOR offset are
275 // less than the AND offset. This should be guaranteed based on the above
276 // calculations.
277 #ifdef SK_DEBUG
278 int maxPixelNum = lineWidth*(h-1)+w-1;
279 int maxByte;
280 switch (bitCount) {
281 case 1:
282 maxByte = maxPixelNum >> 3;
283 break;
284 case 4:
285 maxByte = maxPixelNum >> 1;
286 break;
287 case 8:
288 maxByte = maxPixelNum;
289 break;
290 case 24:
291 maxByte = maxPixelNum * 3 + 2;
292 break;
293 case 32:
294 maxByte = maxPixelNum * 4 + 3;
295 break;
296 default:
297 SkASSERT(false);
298 return kFailure;
299 }
300 int maxXOROffset = xorOffset + maxByte;
301 SkASSERT(maxXOROffset < andOffset);
302 #endif
303
304 SkAutoLockPixels alp(*bm);
305
306 for (int y = 0; y < h; y++)
307 {
308 for (int x = 0; x < w; x++)
309 {
310 //U32* address = bm->getAddr32(x, y);
311
312 //check the alpha bit first, but pass it along to the function to fi gure out how to deal with it
313 int andPixelNo = andLineWidth*(h-y-1)+x;
314 //only need to get a new alphaByte when x %8 == 0
315 //but that introduces an if and a mod - probably much slower
316 //that's ok, it's just a read of an array, not a stream
317 int alphaByte = readByte(buf, andOffset + (andPixelNo >> 3));
318 int shift = 7 - (andPixelNo & 0x7);
319 int m = 1 << shift;
320
321 int pixelNo = lineWidth*(h-y-1)+x;
322 placePixel(pixelNo, buf, xorOffset, x, y, w, bm, alphaByte, m, shift , colors);
323
324 }
325 }
326
327 delete [] colors;
328 //ensure we haven't read off the end?
329 //of course this doesn't help us if the andOffset was a lie...
330 //return andOffset + (andLineWidth >> 3) <= length;
331 return kSuccess;
332 } //onDecode
333
334 //function to place the pixel, determined by the bitCount
335 static void editPixelBit1(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 // note that this should be the same as/similar to the AND bitmap
340 SkPMColor* address = bm->getAddr32(x,y);
341 int byte = readByte(buf, xorOffset + (pixelNo >> 3));
342 int colorBit;
343 int alphaBit;
344 // Read all of the bits in this byte.
345 int i = x + 8;
346 // Pin to the width so we do not write outside the bounds of
347 // our color table.
348 i = i > w ? w : i;
349 // While loop to check all 8 bits individually.
350 while (x < i)
351 {
352
353 colorBit = (byte & m) >> shift;
354 alphaBit = (alphaByte & m) >> shift;
355 *address = (alphaBit-1)&(colors[colorBit]);
356 x++;
357 // setup for the next pixel
358 address = address + 1;
359 m = m >> 1;
360 shift -= 1;
361 }
362 x--;
363 }
364 static void editPixelBit4(const int pixelNo, const unsigned char* buf,
365 const int xorOffset, int& x, int y, const int w,
366 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
367 {
368 SkPMColor* address = bm->getAddr32(x, y);
369 int byte = readByte(buf, xorOffset + (pixelNo >> 1));
370 int pixel = (byte >> 4) & 0xF;
371 int alphaBit = (alphaByte & m) >> shift;
372 *address = (alphaBit-1)&(colors[pixel]);
373 x++;
374 //if w is odd, x may be the same as w, which means we are writing to an unus ed portion of the bitmap
375 //but that's okay, since i've added an extra rowByte for just this purpose
376 address = address + 1;
377 pixel = byte & 0xF;
378 m = m >> 1;
379 alphaBit = (alphaByte & m) >> (shift-1);
380 //speed up trick here
381 *address = (alphaBit-1)&(colors[pixel]);
382 }
383
384 static void editPixelBit8(const int pixelNo, const unsigned char* buf,
385 const int xorOffset, int& x, int y, const int w,
386 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
387 {
388 SkPMColor* address = bm->getAddr32(x, y);
389 int pixel = readByte(buf, xorOffset + pixelNo);
390 int alphaBit = (alphaByte & m) >> shift;
391 *address = (alphaBit-1)&(colors[pixel]);
392 }
393
394 static void editPixelBit24(const int pixelNo, const unsigned char* buf,
395 const int xorOffset, int& x, int y, const int w,
396 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
397 {
398 SkPMColor* address = bm->getAddr32(x, y);
399 int blue = readByte(buf, xorOffset + 3*pixelNo);
400 int green = readByte(buf, xorOffset + 3*pixelNo + 1);
401 int red = readByte(buf, xorOffset + 3*pixelNo + 2);
402 int alphaBit = (alphaByte & m) >> shift;
403 //alphaBit == 1 => alpha = 0
404 int alpha = (alphaBit-1) & 0xFF;
405 *address = SkPreMultiplyARGB(alpha, red, green, blue);
406 }
407
408 static void editPixelBit32(const int pixelNo, const unsigned char* buf,
409 const int xorOffset, int& x, int y, const int w,
410 SkBitmap* bm, int alphaByte, int m, int shift, SkPMColor* colors)
411 {
412 SkPMColor* address = bm->getAddr32(x, y);
413 int blue = readByte(buf, xorOffset + 4*pixelNo);
414 int green = readByte(buf, xorOffset + 4*pixelNo + 1);
415 int red = readByte(buf, xorOffset + 4*pixelNo + 2);
416 int alphaBit = (alphaByte & m) >> shift;
417 #if 1 // don't trust the alphaBit for 32bit images <mrr>
418 alphaBit = 0;
419 #endif
420 int alpha = readByte(buf, xorOffset + 4*pixelNo + 3) & ((alphaBit-1)&0xFF);
421 *address = SkPreMultiplyARGB(alpha, red, green, blue);
422 }
423
424 ///////////////////////////////////////////////////////////////////////////////
425 DEFINE_DECODER_CREATOR(ICOImageDecoder);
426 //////////////////////////////////////////////////////////////////////////////// /////////
427
428 static bool is_ico(SkStreamRewindable* stream) {
429 // Check to see if the first four bytes are 0,0,1,0
430 // FIXME: Is that required and sufficient?
431 char buf[4];
432 if (stream->read((void*)buf, 4) != 4) {
433 return false;
434 }
435 int reserved = read2Bytes(buf, 0);
436 int type = read2Bytes(buf, 2);
437 return 0 == reserved && 1 == type;
438 }
439
440 static SkImageDecoder* sk_libico_dfactory(SkStreamRewindable* stream) {
441 if (is_ico(stream)) {
442 return new SkICOImageDecoder;
443 }
444 return nullptr;
445 }
446
447 static SkImageDecoder_DecodeReg gReg(sk_libico_dfactory);
448
449 static SkImageDecoder::Format get_format_ico(SkStreamRewindable* stream) {
450 if (is_ico(stream)) {
451 return SkImageDecoder::kICO_Format;
452 }
453 return SkImageDecoder::kUnknown_Format;
454 }
455
456 static SkImageDecoder_FormatReg gFormatReg(get_format_ico);
OLDNEW
« no previous file with comments | « src/images/SkImageDecoder_libgif.cpp ('k') | src/images/SkImageDecoder_libjpeg.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698