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

Side by Side Diff: src/codec/SkBmpRLECodec.cpp

Issue 1258863008: Split SkBmpCodec into three separate classes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 4 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/codec/SkBmpRLECodec.h ('k') | src/codec/SkBmpStandardCodec.h » ('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 2015 Google Inc.
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 "SkBmpRLECodec.h"
9 #include "SkCodecPriv.h"
10 #include "SkColorPriv.h"
11 #include "SkScanlineDecoder.h"
12 #include "SkStream.h"
13
14 /*
15 * Checks if the conversion between the input image and the requested output
16 * image has been implemented
17 */
18 static bool conversion_possible(const SkImageInfo& dst,
19 const SkImageInfo& src) {
20 // Ensure that the profile type is unchanged
21 if (dst.profileType() != src.profileType()) {
22 return false;
23 }
24
25 // Ensure the alpha type is valid
26 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
27 return false;
28 }
29
30 // Check for supported color types
31 switch (dst.colorType()) {
32 // Allow output to kN32 from any type of input
33 case kN32_SkColorType:
34 return true;
35 // Allow output to kIndex_8 from compatible inputs
36 case kIndex_8_SkColorType:
37 return kIndex_8_SkColorType == src.colorType();
38 default:
39 return false;
40 }
41 }
42
43 /*
44 * Creates an instance of the decoder
45 * Called only by NewFromStream
46 */
47 SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream,
48 uint16_t bitsPerPixel, uint32_t numColors,
49 uint32_t bytesPerColor, uint32_t offset,
50 SkBmpCodec::RowOrder rowOrder, size_t RLEBytes)
51 : INHERITED(info, stream, bitsPerPixel, rowOrder)
52 , fColorTable(NULL)
53 , fNumColors(this->computeNumColors(numColors))
54 , fBytesPerColor(bytesPerColor)
55 , fOffset(offset)
56 , fStreamBuffer(SkNEW_ARRAY(uint8_t, RLEBytes))
57 , fRLEBytes(RLEBytes)
58 , fCurrRLEByte(0)
59 {}
60
61 /*
62 * Initiates the bitmap decode
63 */
64 SkCodec::Result SkBmpRLECodec::onGetPixels(const SkImageInfo& dstInfo,
65 void* dst, size_t dstRowBytes,
66 const Options& opts,
67 SkPMColor* inputColorPtr,
68 int* inputColorCount) {
69 if (!this->handleRewind(false)) {
70 return kCouldNotRewind;
71 }
72 if (opts.fSubset) {
73 // Subsets are not supported.
74 return kUnimplemented;
75 }
76 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
77 SkCodecPrintf("Error: scaling not supported.\n");
78 return kInvalidScale;
79 }
80 if (!conversion_possible(dstInfo, this->getInfo())) {
81 SkCodecPrintf("Error: cannot convert input type to output type.\n");
82 return kInvalidConversion;
83 }
84
85 // Create the color table if necessary and prepare the stream for decode
86 // Note that if it is non-NULL, inputColorCount will be modified
87 if (!this->createColorTable(inputColorCount)) {
88 SkCodecPrintf("Error: could not create color table.\n");
89 return kInvalidInput;
90 }
91
92 // Copy the color table to the client if necessary
93 copy_color_table(dstInfo, fColorTable, inputColorPtr, inputColorCount);
94
95 // Initialize a swizzler if necessary
96 if (!this->initializeStreamBuffer()) {
97 SkCodecPrintf("Error: cannot initialize swizzler.\n");
98 return kInvalidConversion;
99 }
100
101 // Perform the decode
102 return decode(dstInfo, dst, dstRowBytes, opts);
103 }
104
105 /*
106 * Process the color table for the bmp input
107 */
108 bool SkBmpRLECodec::createColorTable(int* numColors) {
109 // Allocate memory for color table
110 uint32_t colorBytes = 0;
111 SkPMColor colorTable[256];
112 if (this->bitsPerPixel() <= 8) {
113 // Inform the caller of the number of colors
114 uint32_t maxColors = 1 << this->bitsPerPixel();
115 if (NULL != numColors) {
116 // We set the number of colors to maxColors in order to ensure
117 // safe memory accesses. Otherwise, an invalid pixel could
118 // access memory outside of our color table array.
119 *numColors = maxColors;
120 }
121
122 // Read the color table from the stream
123 colorBytes = fNumColors * fBytesPerColor;
124 SkAutoTDeleteArray<uint8_t> cBuffer(SkNEW_ARRAY(uint8_t, colorBytes));
125 if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) {
126 SkCodecPrintf("Error: unable to read color table.\n");
127 return false;
128 }
129
130 // Fill in the color table
131 uint32_t i = 0;
132 for (; i < fNumColors; i++) {
133 uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor);
134 uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1);
135 uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2);
136 colorTable[i] = SkPackARGB32NoCheck(0xFF, red, green, blue);
137 }
138
139 // To avoid segmentation faults on bad pixel data, fill the end of the
140 // color table with black. This is the same the behavior as the
141 // chromium decoder.
142 for (; i < maxColors; i++) {
143 colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0);
144 }
145
146 // Set the color table
147 fColorTable.reset(SkNEW_ARGS(SkColorTable, (colorTable, maxColors)));
148 }
149
150 // Check that we have not read past the pixel array offset
151 if(fOffset < colorBytes) {
152 // This may occur on OS 2.1 and other old versions where the color
153 // table defaults to max size, and the bmp tries to use a smaller
154 // color table. This is invalid, and our decision is to indicate
155 // an error, rather than try to guess the intended size of the
156 // color table.
157 SkCodecPrintf("Error: pixel data offset less than color table size.\n");
158 return false;
159 }
160
161 // After reading the color table, skip to the start of the pixel array
162 if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) {
163 SkCodecPrintf("Error: unable to skip to image data.\n");
164 return false;
165 }
166
167 // Return true on success
168 return true;
169 }
170
171 bool SkBmpRLECodec::initializeStreamBuffer() {
172 // Setup a buffer to contain the full input stream
173 size_t totalBytes = this->stream()->read(fStreamBuffer.get(), fRLEBytes);
174 if (totalBytes < fRLEBytes) {
175 fRLEBytes = totalBytes;
176 SkCodecPrintf("Warning: incomplete RLE file.\n");
177 }
178 if (fRLEBytes == 0) {
179 SkCodecPrintf("Error: could not read RLE image data.\n");
180 return false;
181 }
182 return true;
183 }
184
185 /*
186 * Set an RLE pixel using the color table
187 */
188 void SkBmpRLECodec::setPixel(void* dst, size_t dstRowBytes,
189 const SkImageInfo& dstInfo, uint32_t x, uint32_t y,
190 uint8_t index) {
191 // Set the row
192 int height = dstInfo.height();
193 int row;
194 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) {
195 row = height - y - 1;
196 } else {
197 row = y;
198 }
199
200 // Set the pixel based on destination color type
201 switch (dstInfo.colorType()) {
202 case kN32_SkColorType: {
203 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst,
204 row * (int) dstRowBytes);
205 dstRow[x] = fColorTable->operator[](index);
206 break;
207 }
208 default:
209 // This case should not be reached. We should catch an invalid
210 // color type when we check that the conversion is possible.
211 SkASSERT(false);
212 break;
213 }
214 }
215
216 /*
217 * Set an RLE pixel from R, G, B values
218 */
219 void SkBmpRLECodec::setRGBPixel(void* dst, size_t dstRowBytes,
220 const SkImageInfo& dstInfo, uint32_t x,
221 uint32_t y, uint8_t red, uint8_t green,
222 uint8_t blue) {
223 // Set the row
224 int height = dstInfo.height();
225 int row;
226 if (SkBmpCodec::kBottomUp_RowOrder == this->rowOrder()) {
227 row = height - y - 1;
228 } else {
229 row = y;
230 }
231
232 // Set the pixel based on destination color type
233 switch (dstInfo.colorType()) {
234 case kN32_SkColorType: {
235 SkPMColor* dstRow = SkTAddOffset<SkPMColor>((SkPMColor*) dst,
236 row * (int) dstRowBytes);
237 dstRow[x] = SkPackARGB32NoCheck(0xFF, red, green, blue);
238 break;
239 }
240 default:
241 // This case should not be reached. We should catch an invalid
242 // color type when we check that the conversion is possible.
243 SkASSERT(false);
244 break;
245 }
246 }
247
248 /*
249 * Performs the bitmap decoding for RLE input format
250 * RLE decoding is performed all at once, rather than a one row at a time
251 */
252 SkCodec::Result SkBmpRLECodec::decode(const SkImageInfo& dstInfo,
253 void* dst, size_t dstRowBytes,
254 const Options& opts) {
255 // Set RLE flags
256 static const uint8_t RLE_ESCAPE = 0;
257 static const uint8_t RLE_EOL = 0;
258 static const uint8_t RLE_EOF = 1;
259 static const uint8_t RLE_DELTA = 2;
260
261 // Set constant values
262 const int width = dstInfo.width();
263 const int height = dstInfo.height();
264
265 // Destination parameters
266 int x = 0;
267 int y = 0;
268
269 // Set the background as transparent. Then, if the RLE code skips pixels,
270 // the skipped pixels will be transparent.
271 // Because of the need for transparent pixels, kN32 is the only color
272 // type that makes sense for the destination format.
273 SkASSERT(kN32_SkColorType == dstInfo.colorType());
274 if (kNo_ZeroInitialized == opts.fZeroInitialized) {
275 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, height, SK_ColorTRANSPARENT, NULL);
276 }
277
278 while (true) {
279 // If we have reached a row that is beyond the requested height, we have
280 // succeeded.
281 if (y >= height) {
282 // It would be better to check for the EOF marker before returning
283 // success, but we may be performing a scanline decode, which
284 // may require us to stop before decoding the full height.
285 return kSuccess;
286 }
287
288 // Every entry takes at least two bytes
289 if ((int) fRLEBytes - fCurrRLEByte < 2) {
290 SkCodecPrintf("Warning: incomplete RLE input.\n");
291 return kIncompleteInput;
292 }
293
294 // Read the next two bytes. These bytes have different meanings
295 // depending on their values. In the first interpretation, the first
296 // byte is an escape flag and the second byte indicates what special
297 // task to perform.
298 const uint8_t flag = fStreamBuffer.get()[fCurrRLEByte++];
299 const uint8_t task = fStreamBuffer.get()[fCurrRLEByte++];
300
301 // Perform decoding
302 if (RLE_ESCAPE == flag) {
303 switch (task) {
304 case RLE_EOL:
305 x = 0;
306 y++;
307 break;
308 case RLE_EOF:
309 return kSuccess;
310 case RLE_DELTA: {
311 // Two bytes are needed to specify delta
312 if ((int) fRLEBytes - fCurrRLEByte < 2) {
313 SkCodecPrintf("Warning: incomplete RLE input\n");
314 return kIncompleteInput;
315 }
316 // Modify x and y
317 const uint8_t dx = fStreamBuffer.get()[fCurrRLEByte++];
318 const uint8_t dy = fStreamBuffer.get()[fCurrRLEByte++];
319 x += dx;
320 y += dy;
321 if (x > width || y > height) {
322 SkCodecPrintf("Warning: invalid RLE input 1.\n");
323 return kIncompleteInput;
324 }
325 break;
326 }
327 default: {
328 // If task does not match any of the above signals, it
329 // indicates that we have a sequence of non-RLE pixels.
330 // Furthermore, the value of task is equal to the number
331 // of pixels to interpret.
332 uint8_t numPixels = task;
333 const size_t rowBytes = compute_row_bytes(numPixels,
334 this->bitsPerPixel());
335 // Abort if setting numPixels moves us off the edge of the
336 // image. Also abort if there are not enough bytes
337 // remaining in the stream to set numPixels.
338 if (x + numPixels > width ||
339 (int) fRLEBytes - fCurrRLEByte < SkAlign2(rowBytes)) {
340 SkCodecPrintf("Warning: invalid RLE input 2.\n");
341 return kIncompleteInput;
342 }
343 // Set numPixels number of pixels
344 while (numPixels > 0) {
345 switch(this->bitsPerPixel()) {
346 case 4: {
347 SkASSERT(fCurrRLEByte < fRLEBytes);
348 uint8_t val = fStreamBuffer.get()[fCurrRLEByte++ ];
349 setPixel(dst, dstRowBytes, dstInfo, x++,
350 y, val >> 4);
351 numPixels--;
352 if (numPixels != 0) {
353 setPixel(dst, dstRowBytes, dstInfo,
354 x++, y, val & 0xF);
355 numPixels--;
356 }
357 break;
358 }
359 case 8:
360 SkASSERT(fCurrRLEByte < fRLEBytes);
361 setPixel(dst, dstRowBytes, dstInfo, x++,
362 y, fStreamBuffer.get()[fCurrRLEByte++]);
363 numPixels--;
364 break;
365 case 24: {
366 SkASSERT(fCurrRLEByte + 2 < fRLEBytes);
367 uint8_t blue = fStreamBuffer.get()[fCurrRLEByte+ +];
368 uint8_t green = fStreamBuffer.get()[fCurrRLEByte ++];
369 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++ ];
370 setRGBPixel(dst, dstRowBytes, dstInfo,
371 x++, y, red, green, blue);
372 numPixels--;
373 }
374 default:
375 SkASSERT(false);
376 return kInvalidInput;
377 }
378 }
379 // Skip a byte if necessary to maintain alignment
380 if (!SkIsAlign2(rowBytes)) {
381 fCurrRLEByte++;
382 }
383 break;
384 }
385 }
386 } else {
387 // If the first byte read is not a flag, it indicates the number of
388 // pixels to set in RLE mode.
389 const uint8_t numPixels = flag;
390 const int endX = SkTMin<int>(x + numPixels, width);
391
392 if (24 == this->bitsPerPixel()) {
393 // In RLE24, the second byte read is part of the pixel color.
394 // There are two more required bytes to finish encoding the
395 // color.
396 if ((int) fRLEBytes - fCurrRLEByte < 2) {
397 SkCodecPrintf("Warning: incomplete RLE input\n");
398 return kIncompleteInput;
399 }
400
401 // Fill the pixels up to endX with the specified color
402 uint8_t blue = task;
403 uint8_t green = fStreamBuffer.get()[fCurrRLEByte++];
404 uint8_t red = fStreamBuffer.get()[fCurrRLEByte++];
405 while (x < endX) {
406 setRGBPixel(dst, dstRowBytes, dstInfo, x++, y, red,
407 green, blue);
408 }
409 } else {
410 // In RLE8 or RLE4, the second byte read gives the index in the
411 // color table to look up the pixel color.
412 // RLE8 has one color index that gets repeated
413 // RLE4 has two color indexes in the upper and lower 4 bits of
414 // the bytes, which are alternated
415 uint8_t indices[2] = { task, task };
416 if (4 == this->bitsPerPixel()) {
417 indices[0] >>= 4;
418 indices[1] &= 0xf;
419 }
420
421 // Set the indicated number of pixels
422 for (int which = 0; x < endX; x++) {
423 setPixel(dst, dstRowBytes, dstInfo, x, y,
424 indices[which]);
425 which = !which;
426 }
427 }
428 }
429 }
430 }
OLDNEW
« no previous file with comments | « src/codec/SkBmpRLECodec.h ('k') | src/codec/SkBmpStandardCodec.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698