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