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 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 17 | SkBmpStandardCodec::SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info, |
| 18 | SkStream* stream, uint16_t bitsPerPixel, uint32_t numColors, |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 19 | uint32_t bytesPerColor, uint32_t offset, |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 20 | SkCodec::SkScanlineOrder rowOrder, |
| 21 | bool isOpaque, bool inIco) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 22 | : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | , fColorTable(nullptr) |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 24 | , fNumColors(numColors) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 25 | , fBytesPerColor(bytesPerColor) |
| 26 | , fOffset(offset) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | , fSwizzler(nullptr) |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 28 | , fSrcBuffer(new uint8_t[this->srcRowBytes()]) |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 29 | , fIsOpaque(isOpaque) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 30 | , fInIco(inIco) |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 31 | , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->getInfo().width(), 1)) : 0) |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 32 | , fXformOnDecode(false) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 33 | {} |
| 34 | |
| 35 | /* |
| 36 | * Initiates the bitmap decode |
| 37 | */ |
| 38 | SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 39 | void* dst, size_t dstRowBytes, |
| 40 | const Options& opts, |
| 41 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 42 | int* inputColorCount, |
| 43 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 44 | if (opts.fSubset) { |
| 45 | // Subsets are not supported. |
| 46 | return kUnimplemented; |
| 47 | } |
| 48 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 49 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 50 | return kInvalidScale; |
| 51 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 52 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 53 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
| 54 | if (kSuccess != result) { |
| 55 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 56 | } |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 57 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 58 | if (rows != dstInfo.height()) { |
| 59 | *rowsDecoded = rows; |
| 60 | return kIncompleteInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 61 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 62 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Process the color table for the bmp input |
| 67 | */ |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 68 | bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType, |
| 69 | int* numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 70 | // Allocate memory for color table |
| 71 | uint32_t colorBytes = 0; |
| 72 | SkPMColor colorTable[256]; |
| 73 | if (this->bitsPerPixel() <= 8) { |
| 74 | // Inform the caller of the number of colors |
| 75 | uint32_t maxColors = 1 << this->bitsPerPixel(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | if (nullptr != numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 77 | // We set the number of colors to maxColors in order to ensure |
| 78 | // safe memory accesses. Otherwise, an invalid pixel could |
| 79 | // access memory outside of our color table array. |
| 80 | *numColors = maxColors; |
| 81 | } |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 82 | // Don't bother reading more than maxColors. |
| 83 | const uint32_t numColorsToRead = |
| 84 | fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 85 | |
| 86 | // Read the color table from the stream |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 87 | colorBytes = numColorsToRead * fBytesPerColor; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 88 | std::unique_ptr<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 | |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 94 | SkColorType packColorType = dstColorType; |
| 95 | SkAlphaType packAlphaType = dstAlphaType; |
| 96 | if (this->colorXform()) { |
| 97 | packColorType = kBGRA_8888_SkColorType; |
| 98 | packAlphaType = kUnpremul_SkAlphaType; |
| 99 | } |
| 100 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 101 | // Choose the proper packing function |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 102 | bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque; |
| 103 | PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 104 | |
| 105 | // Fill in the color table |
| 106 | uint32_t i = 0; |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 107 | for (; i < numColorsToRead; i++) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 108 | uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 109 | uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 110 | uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 111 | uint8_t alpha; |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 112 | if (fIsOpaque) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 113 | alpha = 0xFF; |
| 114 | } else { |
| 115 | alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3); |
| 116 | } |
| 117 | colorTable[i] = packARGB(alpha, red, green, blue); |
| 118 | } |
| 119 | |
| 120 | // To avoid segmentation faults on bad pixel data, fill the end of the |
| 121 | // color table with black. This is the same the behavior as the |
| 122 | // chromium decoder. |
| 123 | for (; i < maxColors; i++) { |
| 124 | colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 125 | } |
| 126 | |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 127 | if (this->colorXform() && !fXformOnDecode) { |
Matt Sarett | 19aff5d | 2017-04-03 16:01:10 -0400 | [diff] [blame] | 128 | SkColorSpaceXform::ColorFormat dstFormat = select_xform_format_ct(dstColorType); |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 129 | SkColorSpaceXform::ColorFormat srcFormat = SkColorSpaceXform::kBGRA_8888_ColorFormat; |
| 130 | SkAlphaType xformAlphaType = select_xform_alpha(dstAlphaType, |
| 131 | this->getInfo().alphaType()); |
| 132 | SkAssertResult(this->colorXform()->apply(dstFormat, colorTable, srcFormat, colorTable, |
| 133 | maxColors, xformAlphaType)); |
| 134 | } |
| 135 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 136 | // Set the color table |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 137 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
| 141 | // begins. Pixel data always begins immediately after the color table. |
| 142 | if (!fInIco) { |
| 143 | // Check that we have not read past the pixel array offset |
| 144 | if(fOffset < colorBytes) { |
| 145 | // This may occur on OS 2.1 and other old versions where the color |
| 146 | // table defaults to max size, and the bmp tries to use a smaller |
| 147 | // color table. This is invalid, and our decision is to indicate |
| 148 | // an error, rather than try to guess the intended size of the |
| 149 | // color table. |
| 150 | SkCodecPrintf("Error: pixel data offset less than color table size.\n"); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | // After reading the color table, skip to the start of the pixel array |
| 155 | if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 156 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Return true on success |
| 162 | return true; |
| 163 | } |
| 164 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 165 | void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) { |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 166 | // In the case of bmp-in-icos, we will report BGRA to the client, |
| 167 | // since we may be required to apply an alpha mask after the decode. |
| 168 | // However, the swizzler needs to know the actual format of the bmp. |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 169 | SkEncodedInfo encodedInfo = this->getEncodedInfo(); |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 170 | if (fInIco) { |
| 171 | if (this->bitsPerPixel() <= 8) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 172 | encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, |
| 173 | encodedInfo.alpha(), this->bitsPerPixel()); |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 174 | } else if (this->bitsPerPixel() == 24) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 175 | encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kBGR_Color, |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 176 | SkEncodedInfo::kOpaque_Alpha, 8); |
| 177 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Get a pointer to the color table if it exists |
| 181 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 182 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 183 | SkImageInfo swizzlerInfo = dstInfo; |
| 184 | SkCodec::Options swizzlerOptions = opts; |
| 185 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 186 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 187 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 188 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 189 | } |
| 190 | |
| 191 | swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | fSwizzler.reset(SkSwizzler::CreateSwizzler(encodedInfo, colorPtr, swizzlerInfo, |
| 196 | swizzlerOptions)); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 197 | SkASSERT(fSwizzler); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 200 | SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 201 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 202 | fXformOnDecode = false; |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 203 | if (this->colorXform()) { |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 204 | fXformOnDecode = apply_xform_on_decode(dstInfo.colorType(), this->getEncodedInfo().color()); |
| 205 | if (fXformOnDecode) { |
| 206 | this->resetXformBuffer(dstInfo.width()); |
| 207 | } |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 208 | } |
| 209 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 210 | // Create the color table if necessary and prepare the stream for decode |
| 211 | // Note that if it is non-NULL, inputColorCount will be modified |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 212 | if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 213 | SkCodecPrintf("Error: could not create color table.\n"); |
| 214 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 215 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 216 | |
| 217 | // Copy the color table to the client if necessary |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 218 | copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 219 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 220 | // Initialize a swizzler |
| 221 | this->initializeSwizzler(dstInfo, options); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 222 | return SkCodec::kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Performs the bitmap decoding for standard input format |
| 227 | */ |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 228 | int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
| 229 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 230 | // Iterate over rows of the image |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 231 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 232 | for (int y = 0; y < height; y++) { |
| 233 | // Read a row of the input |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 234 | if (this->stream()->read(fSrcBuffer.get(), this->srcRowBytes()) != this->srcRowBytes()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 235 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 236 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 240 | uint32_t row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 241 | |
| 242 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 243 | |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 244 | if (fXformOnDecode) { |
| 245 | SkASSERT(this->colorXform()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 246 | SkImageInfo xformInfo = dstInfo.makeWH(fSwizzler->swizzleWidth(), dstInfo.height()); |
| 247 | fSwizzler->swizzle(this->xformBuffer(), fSrcBuffer.get()); |
| 248 | this->applyColorXform(xformInfo, dstRow, this->xformBuffer()); |
| 249 | } else { |
| 250 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
| 251 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 252 | } |
| 253 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 254 | if (fInIco && fIsOpaque) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 255 | const int startScanline = this->currScanline(); |
| 256 | if (startScanline < 0) { |
| 257 | // We are not performing a scanline decode. |
| 258 | // Just decode the entire ICO mask and return. |
| 259 | decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes); |
| 260 | return height; |
| 261 | } |
| 262 | |
| 263 | // In order to perform a scanline ICO decode, we must be able |
| 264 | // to skip ahead in the stream in order to apply the AND mask |
| 265 | // to the requested scanlines. |
| 266 | // We will do this by taking advantage of the fact that |
| 267 | // SkIcoCodec always uses a SkMemoryStream as its underlying |
| 268 | // representation of the stream. |
| 269 | const void* memoryBase = this->stream()->getMemoryBase(); |
| 270 | SkASSERT(nullptr != memoryBase); |
| 271 | SkASSERT(this->stream()->hasLength()); |
| 272 | SkASSERT(this->stream()->hasPosition()); |
| 273 | |
| 274 | const size_t length = this->stream()->getLength(); |
| 275 | const size_t currPosition = this->stream()->getPosition(); |
| 276 | |
| 277 | // Calculate how many bytes we must skip to reach the AND mask. |
| 278 | const int remainingScanlines = this->getInfo().height() - startScanline - height; |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 279 | const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() + |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 280 | startScanline * fAndMaskRowBytes; |
| 281 | const size_t subStreamStartPosition = currPosition + bytesToSkip; |
| 282 | if (subStreamStartPosition >= length) { |
| 283 | // FIXME: How can we indicate that this decode was actually incomplete? |
| 284 | return height; |
| 285 | } |
| 286 | |
| 287 | // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate |
| 288 | // the memory base into a stream in order to safely handle incomplete images |
| 289 | // without reading out of bounds memory. |
| 290 | const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase, |
| 291 | subStreamStartPosition); |
| 292 | const size_t subStreamLength = length - subStreamStartPosition; |
| 293 | // This call does not transfer ownership of the subStreamMemoryBase. |
| 294 | SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false); |
| 295 | |
| 296 | // FIXME: If decodeIcoMask does not succeed, is there a way that we can |
| 297 | // indicate the decode was incomplete? |
| 298 | decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes); |
| 299 | } |
| 300 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 301 | return height; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 302 | } |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 303 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 304 | void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 305 | void* dst, size_t dstRowBytes) { |
| 306 | // BMP in ICO have transparency, so this cannot be 565, and this mask |
| 307 | // prevents us from using kIndex8. The below code depends on the output |
| 308 | // being an SkPMColor. |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 309 | SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() || |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 310 | kBGRA_8888_SkColorType == dstInfo.colorType() || |
| 311 | kRGBA_F16_SkColorType == dstInfo.colorType()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 312 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 313 | // If we are sampling, make sure that we only mask the sampled pixels. |
| 314 | // We do not need to worry about sampling in the y-dimension because that |
| 315 | // should be handled by SkSampledCodec. |
| 316 | const int sampleX = fSwizzler->sampleX(); |
| 317 | const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX); |
| 318 | const int srcStartX = get_start_coord(sampleX); |
| 319 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 320 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 321 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 322 | for (int y = 0; y < dstInfo.height(); y++) { |
| 323 | // The srcBuffer will at least be large enough |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 324 | if (stream->read(fSrcBuffer.get(), fAndMaskRowBytes) != fAndMaskRowBytes) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 325 | SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 326 | return; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 327 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 328 | |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 329 | auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) { |
| 330 | if (kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 331 | uint64_t* dst64 = (uint64_t*) dstRow; |
| 332 | dst64[x] &= bit - 1; |
| 333 | } else { |
| 334 | uint32_t* dst32 = (uint32_t*) dstRow; |
| 335 | dst32[x] &= bit - 1; |
| 336 | } |
| 337 | }; |
| 338 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 339 | int row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 340 | |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 341 | void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 342 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 343 | int srcX = srcStartX; |
| 344 | for (int dstX = 0; dstX < sampledWidth; dstX++) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 345 | int quotient; |
| 346 | int modulus; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 347 | SkTDivMod(srcX, 8, "ient, &modulus); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 348 | uint32_t shift = 7 - modulus; |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 349 | uint64_t alphaBit = (fSrcBuffer.get()[quotient] >> shift) & 0x1; |
| 350 | applyMask(dstRow, dstX, alphaBit); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 351 | srcX += sampleX; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 354 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 355 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 356 | uint64_t SkBmpStandardCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 357 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 358 | if (colorPtr) { |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 359 | return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(), colorPtr, 0, |
Matt Sarett | 19aff5d | 2017-04-03 16:01:10 -0400 | [diff] [blame] | 360 | this->colorXform(), false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 361 | } |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 362 | return INHERITED::onGetFillValue(dstInfo); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 363 | } |