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