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) |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 28 | , fIsOpaque(isOpaque) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 29 | , fInIco(inIco) |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 30 | , fAndMaskRowBytes(fInIco ? SkAlign4(compute_row_bytes(this->getInfo().width(), 1)) : 0) |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 31 | {} |
| 32 | |
| 33 | /* |
| 34 | * Initiates the bitmap decode |
| 35 | */ |
| 36 | SkCodec::Result SkBmpStandardCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 37 | void* dst, size_t dstRowBytes, |
| 38 | const Options& opts, |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 39 | SkPMColor* inputColorPtr, |
| 40 | int* inputColorCount, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 41 | int* rowsDecoded) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 42 | if (opts.fSubset) { |
| 43 | // Subsets are not supported. |
| 44 | return kUnimplemented; |
| 45 | } |
| 46 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 47 | SkCodecPrintf("Error: scaling not supported.\n"); |
| 48 | return kInvalidScale; |
| 49 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 50 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 51 | Result result = this->prepareToDecode(dstInfo, opts, inputColorPtr, inputColorCount); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 52 | if (kSuccess != result) { |
| 53 | return result; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 54 | } |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 55 | int rows = this->decodeRows(dstInfo, dst, dstRowBytes, opts); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 56 | if (rows != dstInfo.height()) { |
| 57 | *rowsDecoded = rows; |
| 58 | return kIncompleteInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 59 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 60 | return kSuccess; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Process the color table for the bmp input |
| 65 | */ |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 66 | bool SkBmpStandardCodec::createColorTable(SkColorType dstColorType, SkAlphaType dstAlphaType, |
| 67 | int* numColors) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 68 | // Allocate memory for color table |
| 69 | uint32_t colorBytes = 0; |
| 70 | SkPMColor colorTable[256]; |
| 71 | if (this->bitsPerPixel() <= 8) { |
| 72 | // Inform the caller of the number of colors |
| 73 | uint32_t maxColors = 1 << this->bitsPerPixel(); |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 74 | if (nullptr != numColors) { |
| 75 | // We set the number of colors to maxColors in order to ensure |
| 76 | // safe memory accesses. Otherwise, an invalid pixel could |
| 77 | // access memory outside of our color table array. |
| 78 | *numColors = maxColors; |
| 79 | } |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 80 | // Don't bother reading more than maxColors. |
| 81 | const uint32_t numColorsToRead = |
| 82 | fNumColors == 0 ? maxColors : SkTMin(fNumColors, maxColors); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 83 | |
| 84 | // Read the color table from the stream |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 85 | colorBytes = numColorsToRead * fBytesPerColor; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 86 | std::unique_ptr<uint8_t[]> cBuffer(new uint8_t[colorBytes]); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 87 | if (stream()->read(cBuffer.get(), colorBytes) != colorBytes) { |
| 88 | SkCodecPrintf("Error: unable to read color table.\n"); |
| 89 | return false; |
| 90 | } |
| 91 | |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 92 | SkColorType packColorType = dstColorType; |
| 93 | SkAlphaType packAlphaType = dstAlphaType; |
| 94 | if (this->colorXform()) { |
| 95 | packColorType = kBGRA_8888_SkColorType; |
| 96 | packAlphaType = kUnpremul_SkAlphaType; |
| 97 | } |
| 98 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 99 | // Choose the proper packing function |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 100 | bool isPremul = (kPremul_SkAlphaType == packAlphaType) && !fIsOpaque; |
| 101 | PackColorProc packARGB = choose_pack_color_proc(isPremul, packColorType); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 102 | |
| 103 | // Fill in the color table |
| 104 | uint32_t i = 0; |
benjaminwagner | 886e5e4 | 2015-12-04 08:48:26 -0800 | [diff] [blame] | 105 | for (; i < numColorsToRead; i++) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 106 | uint8_t blue = get_byte(cBuffer.get(), i*fBytesPerColor); |
| 107 | uint8_t green = get_byte(cBuffer.get(), i*fBytesPerColor + 1); |
| 108 | uint8_t red = get_byte(cBuffer.get(), i*fBytesPerColor + 2); |
| 109 | uint8_t alpha; |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 110 | if (fIsOpaque) { |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 111 | alpha = 0xFF; |
| 112 | } else { |
| 113 | alpha = get_byte(cBuffer.get(), i*fBytesPerColor + 3); |
| 114 | } |
| 115 | colorTable[i] = packARGB(alpha, red, green, blue); |
| 116 | } |
| 117 | |
| 118 | // To avoid segmentation faults on bad pixel data, fill the end of the |
| 119 | // color table with black. This is the same the behavior as the |
| 120 | // chromium decoder. |
| 121 | for (; i < maxColors; i++) { |
| 122 | colorTable[i] = SkPackARGB32NoCheck(0xFF, 0, 0, 0); |
| 123 | } |
| 124 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 125 | if (this->colorXform() && !this->xformOnDecode()) { |
| 126 | this->applyColorXform(colorTable, colorTable, maxColors); |
Matt Sarett | 1a85ca5 | 2016-11-04 11:52:48 -0400 | [diff] [blame] | 127 | } |
| 128 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 129 | // Set the color table |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 130 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Bmp-in-Ico files do not use an offset to indicate where the pixel data |
| 134 | // begins. Pixel data always begins immediately after the color table. |
| 135 | if (!fInIco) { |
| 136 | // Check that we have not read past the pixel array offset |
| 137 | if(fOffset < colorBytes) { |
| 138 | // This may occur on OS 2.1 and other old versions where the color |
| 139 | // table defaults to max size, and the bmp tries to use a smaller |
| 140 | // color table. This is invalid, and our decision is to indicate |
| 141 | // an error, rather than try to guess the intended size of the |
| 142 | // color table. |
| 143 | SkCodecPrintf("Error: pixel data offset less than color table size.\n"); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | // After reading the color table, skip to the start of the pixel array |
| 148 | if (stream()->skip(fOffset - colorBytes) != fOffset - colorBytes) { |
| 149 | SkCodecPrintf("Error: unable to skip to image data.\n"); |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Return true on success |
| 155 | return true; |
| 156 | } |
| 157 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 158 | void SkBmpStandardCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) { |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 159 | // In the case of bmp-in-icos, we will report BGRA to the client, |
| 160 | // since we may be required to apply an alpha mask after the decode. |
| 161 | // However, the swizzler needs to know the actual format of the bmp. |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 162 | SkEncodedInfo encodedInfo = this->getEncodedInfo(); |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 163 | if (fInIco) { |
| 164 | if (this->bitsPerPixel() <= 8) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 165 | encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, |
| 166 | encodedInfo.alpha(), this->bitsPerPixel()); |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 167 | } else if (this->bitsPerPixel() == 24) { |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 168 | encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kBGR_Color, |
msarett | 3e375b0 | 2016-05-04 13:03:48 -0700 | [diff] [blame] | 169 | SkEncodedInfo::kOpaque_Alpha, 8); |
| 170 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Get a pointer to the color table if it exists |
| 174 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 175 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 176 | SkImageInfo swizzlerInfo = dstInfo; |
| 177 | SkCodec::Options swizzlerOptions = opts; |
| 178 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 179 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 180 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 181 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 182 | } |
| 183 | |
| 184 | swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | fSwizzler.reset(SkSwizzler::CreateSwizzler(encodedInfo, colorPtr, swizzlerInfo, |
| 189 | swizzlerOptions)); |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 190 | SkASSERT(fSwizzler); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 193 | SkCodec::Result SkBmpStandardCodec::onPrepareToDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 194 | const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) { |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 195 | if (this->xformOnDecode()) { |
| 196 | this->resetXformBuffer(dstInfo.width()); |
Matt Sarett | 1b96c6f | 2016-11-03 16:15:20 -0400 | [diff] [blame] | 197 | } |
| 198 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 199 | // Create the color table if necessary and prepare the stream for decode |
| 200 | // Note that if it is non-NULL, inputColorCount will be modified |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 201 | if (!this->createColorTable(dstInfo.colorType(), dstInfo.alphaType(), inputColorCount)) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 202 | SkCodecPrintf("Error: could not create color table.\n"); |
| 203 | return SkCodec::kInvalidInput; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 204 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 205 | |
Leon Scroggins | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 206 | // Copy the color table to the client if necessary |
| 207 | copy_color_table(dstInfo, fColorTable.get(), inputColorPtr, inputColorCount); |
| 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. |
| 266 | const int remainingScanlines = this->getInfo().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 | 8321f75 | 2017-07-10 19:51:46 +0000 | [diff] [blame] | 294 | // BMP in ICO have transparency, so this cannot be 565, and this mask |
| 295 | // prevents us from using kIndex8. The below code depends on the output |
| 296 | // being an SkPMColor. |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 297 | SkASSERT(kRGBA_8888_SkColorType == dstInfo.colorType() || |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 298 | kBGRA_8888_SkColorType == dstInfo.colorType() || |
| 299 | kRGBA_F16_SkColorType == dstInfo.colorType()); |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 300 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 301 | // If we are sampling, make sure that we only mask the sampled pixels. |
| 302 | // We do not need to worry about sampling in the y-dimension because that |
| 303 | // should be handled by SkSampledCodec. |
| 304 | const int sampleX = fSwizzler->sampleX(); |
| 305 | const int sampledWidth = get_scaled_dimension(this->getInfo().width(), sampleX); |
| 306 | const int srcStartX = get_start_coord(sampleX); |
| 307 | |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 308 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 309 | SkPMColor* dstPtr = (SkPMColor*) dst; |
| 310 | for (int y = 0; y < dstInfo.height(); y++) { |
| 311 | // The srcBuffer will at least be large enough |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 312 | if (stream->read(this->srcBuffer(), fAndMaskRowBytes) != fAndMaskRowBytes) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 313 | SkCodecPrintf("Warning: incomplete AND mask for bmp-in-ico.\n"); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 314 | return; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 315 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 316 | |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 317 | auto applyMask = [dstInfo](void* dstRow, int x, uint64_t bit) { |
| 318 | if (kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 319 | uint64_t* dst64 = (uint64_t*) dstRow; |
| 320 | dst64[x] &= bit - 1; |
| 321 | } else { |
| 322 | uint32_t* dst32 = (uint32_t*) dstRow; |
| 323 | dst32[x] &= bit - 1; |
| 324 | } |
| 325 | }; |
| 326 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 327 | int row = this->getDstRow(y, dstInfo.height()); |
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 | void* dstRow = SkTAddOffset<SkPMColor>(dstPtr, row * dstRowBytes); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 330 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 331 | int srcX = srcStartX; |
| 332 | for (int dstX = 0; dstX < sampledWidth; dstX++) { |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 333 | int quotient; |
| 334 | int modulus; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 335 | SkTDivMod(srcX, 8, "ient, &modulus); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 336 | uint32_t shift = 7 - modulus; |
Leon Scroggins III | d81fed9 | 2017-06-01 13:42:28 -0400 | [diff] [blame] | 337 | uint64_t alphaBit = (this->srcBuffer()[quotient] >> shift) & 0x1; |
Matt Sarett | 09a1c08 | 2017-02-01 15:34:22 -0800 | [diff] [blame] | 338 | applyMask(dstRow, dstX, alphaBit); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 339 | srcX += sampleX; |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
msarett | 4ab9d5f | 2015-08-06 15:34:42 -0700 | [diff] [blame] | 342 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 343 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 344 | uint64_t SkBmpStandardCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 345 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 346 | if (colorPtr) { |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 347 | return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(), colorPtr, 0, |
Matt Sarett | 19aff5d | 2017-04-03 16:01:10 -0400 | [diff] [blame] | 348 | this->colorXform(), false); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 349 | } |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 350 | return INHERITED::onGetFillValue(dstInfo); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 351 | } |