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 | |
Leon Scroggins III | 65f4aea | 2018-10-24 12:17:22 -0400 | [diff] [blame] | 192 | fSwizzler = SkSwizzler::Make(encodedInfo, colorPtr, swizzlerInfo, swizzlerOptions); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 193 | SkASSERT(fSwizzler); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 196 | SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 197 | const SkCodec::Options& options) { |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 198 | if (this->xformOnDecode()) { |
| 199 | this->resetXformBuffer(dstInfo.width()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 200 | } |
| 201 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 202 | // Create the color table if necessary and prepare the stream for decode |
| 203 | // Note that if it is non-NULL, inputColorCount will be modified |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 204 | if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType())) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 205 | SkCodecPrintf("Error: could not create color table.\n"); |
| 206 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 207 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 208 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 209 | // Initialize a swizzler |
| 210 | this->initializeSwizzler(dstInfo, options); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 211 | return SkCodec::kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Performs the bitmap decoding for standard input format |
| 216 | */ |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 217 | int SkBmpStandardCodec::decodeRows(const SkImageInfo& dstInfo, void* dst, size_t dstRowBytes, |
| 218 | const Options& opts) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 219 | // Iterate over rows of the image |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 220 | const int height = dstInfo.height(); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 221 | for (int y = 0; y < height; y++) { |
| 222 | // Read a row of the input |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 223 | if (this->stream()->read(this->srcBuffer(), this->srcRowBytes()) != this->srcRowBytes()) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 224 | SkCodecPrintf("Warning: incomplete input stream.\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 225 | return y; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Decode the row in destination format |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 229 | uint32_t row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 230 | |
| 231 | void* dstRow = SkTAddOffset<void>(dst, row * dstRowBytes); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 232 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 233 | if (this->xformOnDecode()) { |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 234 | SkASSERT(this->colorXform()); |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 235 | fSwizzler->swizzle(this->xformBuffer(), this->srcBuffer()); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 236 | this->applyColorXform(dstRow, this->xformBuffer(), fSwizzler->swizzleWidth()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 237 | } else { |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 238 | fSwizzler->swizzle(dstRow, this->srcBuffer()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 239 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 240 | } |
| 241 | |
msarett | 1088db9 | 2016-03-22 08:58:35 -0700 | [diff] [blame] | 242 | if (fInIco && fIsOpaque) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 243 | const int startScanline = this->currScanline(); |
| 244 | if (startScanline < 0) { |
| 245 | // We are not performing a scanline decode. |
| 246 | // Just decode the entire ICO mask and return. |
| 247 | decodeIcoMask(this->stream(), dstInfo, dst, dstRowBytes); |
| 248 | return height; |
| 249 | } |
| 250 | |
| 251 | // In order to perform a scanline ICO decode, we must be able |
| 252 | // to skip ahead in the stream in order to apply the AND mask |
| 253 | // to the requested scanlines. |
| 254 | // We will do this by taking advantage of the fact that |
| 255 | // SkIcoCodec always uses a SkMemoryStream as its underlying |
| 256 | // representation of the stream. |
| 257 | const void* memoryBase = this->stream()->getMemoryBase(); |
| 258 | SkASSERT(nullptr != memoryBase); |
| 259 | SkASSERT(this->stream()->hasLength()); |
| 260 | SkASSERT(this->stream()->hasPosition()); |
| 261 | |
| 262 | const size_t length = this->stream()->getLength(); |
| 263 | const size_t currPosition = this->stream()->getPosition(); |
| 264 | |
| 265 | // 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] | 266 | const int remainingScanlines = this->dimensions().height() - startScanline - height; |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 267 | const size_t bytesToSkip = remainingScanlines * this->srcRowBytes() + |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 268 | startScanline * fAndMaskRowBytes; |
| 269 | const size_t subStreamStartPosition = currPosition + bytesToSkip; |
| 270 | if (subStreamStartPosition >= length) { |
| 271 | // FIXME: How can we indicate that this decode was actually incomplete? |
| 272 | return height; |
| 273 | } |
| 274 | |
| 275 | // Create a subStream to pass to decodeIcoMask(). It is useful to encapsulate |
| 276 | // the memory base into a stream in order to safely handle incomplete images |
| 277 | // without reading out of bounds memory. |
| 278 | const void* subStreamMemoryBase = SkTAddOffset<const void>(memoryBase, |
| 279 | subStreamStartPosition); |
| 280 | const size_t subStreamLength = length - subStreamStartPosition; |
| 281 | // This call does not transfer ownership of the subStreamMemoryBase. |
| 282 | SkMemoryStream subStream(subStreamMemoryBase, subStreamLength, false); |
| 283 | |
| 284 | // FIXME: If decodeIcoMask does not succeed, is there a way that we can |
| 285 | // indicate the decode was incomplete? |
| 286 | decodeIcoMask(&subStream, dstInfo, dst, dstRowBytes); |
| 287 | } |
| 288 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 289 | return height; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 290 | } |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 291 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 292 | void SkBmpStandardCodec::decodeIcoMask(SkStream* stream, const SkImageInfo& dstInfo, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 293 | void* dst, size_t dstRowBytes) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 294 | // BMP in ICO have transparency, so this cannot be 565. The below code depends |
| 295 | // on the output being an SkPMColor. |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 296 | SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() || |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 297 | kBGRA_8888_SkColorType == dstInfo.colorType() || |
| 298 | kRGBA_F16_SkColorType == dstInfo.colorType()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 299 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 300 | // If we are sampling, make sure that we only mask the sampled pixels. |
| 301 | // We do not need to worry about sampling in the y-dimension because that |
| 302 | // should be handled by SkSampledCodec. |
| 303 | const int sampleX = fSwizzler->sampleX(); |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 304 | const int sampledWidth = get_scaled_dimension(this->dimensions().width(), sampleX); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 305 | const int srcStartX = get_start_coord(sampleX); |
| 306 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 307 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 308 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 309 | for (int y = 0; y < dstInfo.height(); y++) { |
| 310 | // The srcBuffer will at least be large enough |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 311 | if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 312 | SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 313 | return; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 314 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 315 | |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 316 | auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) { |
| 317 | if (kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 318 | uint64_t* dst64 = (uint64_t*) dstRow; |
| 319 | dst64[x] &= bit - 1; |
| 320 | } else { |
| 321 | uint32_t* dst32 = (uint32_t*) dstRow; |
| 322 | dst32[x] &= bit - 1; |
| 323 | } |
| 324 | }; |
| 325 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 326 | int row = this->getDstRow(y, dstInfo.height()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 327 | |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 328 | void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 329 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 330 | int srcX = srcStartX; |
| 331 | for (int dstX = 0; dstX < sampledWidth; dstX++) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 332 | int quotient; |
| 333 | int modulus; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 334 | SkTDivMod(srcX, 8, "ient, &modulus); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 335 | uint32_t shift = 7 - modulus; |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 336 | uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1; |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 337 | applyMask(dstRow, dstX, alphaBit); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 338 | srcX += sampleX; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 341 | } |