msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 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 "SkBmpStandardCodec.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | |
| 13 | /* |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 14 | * Creates an instance of the decoder |
| 15 | * Called only by NewFromStream |
| 16 | */ |
| 17 | SkBmpStandardCodec::SkBmpStandardCodec(const SkImageInfo& info, SkStream* stream, |
| 18 | uint16_t bitsPerPixel, uint32_t numColors, |
| 19 | uint32_t bytesPerColor, uint32_t offset, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 20 | SkCodec::SkScanlineOrder rowOrder, bool inIco) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 21 | : INHERITED(info, stream, bitsPerPixel, rowOrder) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | , fColorTable(nullptr) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 23 | , fNumColors(this->computeNumColors(numColors)) |
| 24 | , fBytesPerColor(bytesPerColor) |
| 25 | , fOffset(offset) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 26 | , fSwizzler(nullptr) |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 27 | , fSrcRowBytes(SkAlign4(compute_row_bytes(this->getInfo().width(), this->bitsPerPixel()))) |
| 28 | , fSrcBuffer(new uint8_t [fSrcRowBytes]) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 29 | , fInIco(inIco) |
| 30 | {} |
| 31 | |
| 32 | /* |
| 33 | * Initiates the bitmap decode |
| 34 | */ |
| 35 | SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 36 | void* dst, size_t dstRowBytes, |
| 37 | const Options& opts, |
| 38 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 39 | int* inputColorCount, |
| 40 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 41 | if (opts.fSubset) { |
| 42 | // Subsets are not supported. |
| 43 | return kUnimplemented; |
| 44 | } |
| 45 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 46 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 47 | return kInvalidScale; |
| 48 | } |
| 49 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 50 | SkCodecPrintf("Error: cannot convert input type to output type.\n"); |
| 51 | return kInvalidConversion; |
| 52 | } |
| 53 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 54 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 55 | if (kSuccess != result) { |
| 56 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 57 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 58 | uint32_t rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
| 59 | if (rows != dstInfo.height()) { |
| 60 | *rowsDecoded = rows; |
| 61 | return kIncompleteInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 62 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 63 | if (fInIco) { |
| 64 | return this->decodeIcoMask(dstInfo, dst, dstRowBytes); |
| 65 | } |
| 66 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Process the color table for the bmp input |
| 71 | */ |
| 72 | bool SkBmpStandardCodec::createColorTable(SkAlphaType alphaType, int* numColors) { |
| 73 | // Allocate memory for color table |
| 74 | uint32_t colorBytes = 0; |
| 75 | SkPMColor colorTable[256]; |
| 76 | if (this->bitsPerPixel() <= 8) { |
| 77 | // Inform the caller of the number of colors |
| 78 | uint32_t maxColors = 1 << this->bitsPerPixel(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | if (nullptr != numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 80 | // We set the number of colors to maxColors in order to ensure |
| 81 | // safe memory accesses. Otherwise, an invalid pixel could |
| 82 | // access memory outside of our color table array. |
| 83 | *numColors = maxColors; |
| 84 | } |
| 85 | |
| 86 | // Read the color table from the stream |
| 87 | colorBytes = fNumColors * fBytesPerColor; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 88 | SkAutoTDeleteArray<uint8_t> cBuffer(new uint8_t[colorBytes]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 89 | if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
| 90 | SkCodecPrintf("Error: unable to read color table.\n"); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // Choose the proper packing function |
| 95 | SkPMColor (*packARGB) (uint32_t, uint32_t, uint32_t, uint32_t); |
| 96 | switch (alphaType) { |
| 97 | case kOpaque_SkAlphaType: |
| 98 | case kUnpremul_SkAlphaType: |
| 99 | packARGB = &SkPackARGB32NoCheck; |
| 100 | break; |
| 101 | case kPremul_SkAlphaType: |
| 102 | packARGB = &SkPreMultiplyARGB; |
| 103 | break; |
| 104 | default: |
| 105 | // This should not be reached because conversion possible |
| 106 | // should fail if the alpha type is not one of the above |
| 107 | // values. |
| 108 | SkASSERT(false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | packARGB = nullptr; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | |
| 113 | // Fill in the color table |
| 114 | uint32_t i = 0; |
| 115 | for (; i < fNumColors; i++) { |
| 116 | uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 117 | uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 118 | uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 119 | uint8_t alpha; |
| 120 | if (kOpaque_SkAlphaType == alphaType) { |
| 121 | alpha = 0xFF; |
| 122 | } else { |
| 123 | alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3); |
| 124 | } |
| 125 | colorTable[i] = packARGB(alpha, red, green, blue); |
| 126 | } |
| 127 | |
| 128 | // To avoid segmentation faults on bad pixel data, fill the end of the |
| 129 | // color table with black. This is the same the behavior as the |
| 130 | // chromium decoder. |
| 131 | for (; i < maxColors; i++) { |
| 132 | colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 133 | } |
| 134 | |
| 135 | // Set the color table |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 136 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
| 140 | // begins. Pixel data always begins immediately after the color table. |
| 141 | if (!fInIco) { |
| 142 | // Check that we have not read past the pixel array offset |
| 143 | if(fOffset < colorBytes) { |
| 144 | // This may occur on OS 2.1 and other old versions where the color |
| 145 | // table defaults to max size, and the bmp tries to use a smaller |
| 146 | // color table. This is invalid, and our decision is to indicate |
| 147 | // an error, rather than try to guess the intended size of the |
| 148 | // color table. |
| 149 | SkCodecPrintf("Error: pixel data offset less than color table size.\n"); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // After reading the color table, skip to the start of the pixel array |
| 154 | if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 155 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 156 | return false; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // Return true on success |
| 161 | return true; |
| 162 | } |
| 163 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 164 | bool SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 165 | // Get swizzler configuration |
| 166 | SkSwizzler::SrcConfig config; |
| 167 | switch (this->bitsPerPixel()) { |
| 168 | case 1: |
| 169 | config = SkSwizzler::kIndex1; |
| 170 | break; |
| 171 | case 2: |
| 172 | config = SkSwizzler::kIndex2; |
| 173 | break; |
| 174 | case 4: |
| 175 | config = SkSwizzler::kIndex4; |
| 176 | break; |
| 177 | case 8: |
| 178 | config = SkSwizzler::kIndex; |
| 179 | break; |
| 180 | case 24: |
| 181 | config = SkSwizzler::kBGR; |
| 182 | break; |
| 183 | case 32: |
| 184 | if (kOpaque_SkAlphaType == dstInfo.alphaType()) { |
| 185 | config = SkSwizzler::kBGRX; |
| 186 | } else { |
| 187 | config = SkSwizzler::kBGRA; |
| 188 | } |
| 189 | break; |
| 190 | default: |
| 191 | SkASSERT(false); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | // Get a pointer to the color table if it exists |
| 196 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 197 | |
| 198 | // Create swizzler |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 199 | fSwizzler.reset(SkSwizzler::CreateSwizzler(config, colorPtr, dstInfo, opts)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 200 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | if (nullptr == fSwizzler.get()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 207 | SkCodec::Result SkBmpStandardCodec::prepareToDecode(const SkImageInfo& dstInfo, |
| 208 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
| 209 | // Create the color table if necessary and prepare the stream for decode |
| 210 | // Note that if it is non-NULL, inputColorCount will be modified |
| 211 | if (!this->createColorTable(dstInfo.alphaType(), inputColorCount)) { |
| 212 | SkCodecPrintf("Error: could not create color table.\n"); |
| 213 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 214 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 215 | |
| 216 | // Copy the color table to the client if necessary |
| 217 | copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount); |
| 218 | |
| 219 | // Initialize a swizzler if necessary |
| 220 | if (!this->initializeSwizzler(dstInfo, options)) { |
| 221 | SkCodecPrintf("Error: cannot initialize swizzler.\n"); |
| 222 | return SkCodec::kInvalidConversion; |
| 223 | } |
| 224 | return SkCodec::kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Performs the bitmap decoding for standard input format |
| 229 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 230 | int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 231 | void* dst, size_t dstRowBytes, |
| 232 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 233 | // Iterate over rows of the image |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 234 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 235 | for (int y = 0; y < height; y++) { |
| 236 | // Read a row of the input |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 237 | if (this->stream()->read(fSrcBuffer.get(), fSrcRowBytes) != fSrcRowBytes) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 238 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 239 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 243 | uint32_t row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 244 | |
| 245 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
| 246 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 247 | } |
| 248 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 249 | // Finished decoding the entire image |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 250 | return height; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 251 | } |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 252 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 253 | // TODO (msarett): This function will need to be modified in order to perform row by row decodes |
| 254 | // when the Ico scanline decoder is implemented. |
| 255 | SkCodec::Result SkBmpStandardCodec::decodeIcoMask(const SkImageInfo& dstInfo, |
| 256 | void* dst, size_t dstRowBytes) { |
| 257 | // BMP in ICO have transparency, so this cannot be 565, and this mask |
| 258 | // prevents us from using kIndex8. The below code depends on the output |
| 259 | // being an SkPMColor. |
| 260 | SkASSERT(dstInfo.colorType() == kN32_SkColorType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 261 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 262 | // The AND mask is always 1 bit per pixel |
| 263 | const int width = this->getInfo().width(); |
| 264 | const size_t rowBytes = SkAlign4(compute_row_bytes(width, 1)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 265 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 266 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 267 | for (int y = 0; y < dstInfo.height(); y++) { |
| 268 | // The srcBuffer will at least be large enough |
| 269 | if (stream()->read(fSrcBuffer.get(), rowBytes) != rowBytes) { |
| 270 | SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
| 271 | return kIncompleteInput; |
| 272 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 273 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 274 | int row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 275 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 276 | SkPMColor* dstRow = |
| 277 | SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
| 278 | |
| 279 | for (int x = 0; x < width; x++) { |
| 280 | int quotient; |
| 281 | int modulus; |
| 282 | SkTDivMod(x, 8, "ient, &modulus); |
| 283 | uint32_t shift = 7 - modulus; |
| 284 | uint32_t alphaBit = |
| 285 | (fSrcBuffer.get()[quotient] >> shift) & 0x1; |
| 286 | dstRow[x] &= alphaBit - 1; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 289 | return kSuccess; |
| 290 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 291 | |
| 292 | uint32_t SkBmpStandardCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const { |
| 293 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 294 | if (colorPtr) { |
| 295 | return get_color_table_fill_value(colorType, colorPtr, 0); |
| 296 | } |
| 297 | return INHERITED::onGetFillValue(colorType, alphaType); |
| 298 | } |