msarett | 9bde918 | 2015-03-25 05:27:48 -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 | |
msarett | b46e5e2 | 2015-07-30 11:36:40 -0700 | [diff] [blame] | 8 | #include "SkBmpCodec.h" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkData.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 12 | #include "SkIcoCodec.h" |
msarett | be1d555 | 2016-01-21 09:05:23 -0800 | [diff] [blame] | 13 | #include "SkPngCodec.h" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkTDArray.h" |
| 16 | #include "SkTSort.h" |
| 17 | |
| 18 | /* |
| 19 | * Checks the start of the stream to see if the image is an Ico or Cur |
| 20 | */ |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 21 | bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 22 | const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 23 | const char curSig[] = { '\x00', '\x00', '\x02', '\x00' }; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 24 | return bytesRead >= sizeof(icoSig) && |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 25 | (!memcmp(buffer, icoSig, sizeof(icoSig)) || |
| 26 | !memcmp(buffer, curSig, sizeof(curSig))); |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Assumes IsIco was called and returned true |
| 31 | * Creates an Ico decoder |
| 32 | * Reads enough of the stream to determine the image format |
| 33 | */ |
| 34 | SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) { |
msarett | d0be5bb | 2015-03-25 06:29:18 -0700 | [diff] [blame] | 35 | // Ensure that we do not leak the input stream |
| 36 | SkAutoTDelete<SkStream> inputStream(stream); |
| 37 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 38 | // Header size constants |
| 39 | static const uint32_t kIcoDirectoryBytes = 6; |
| 40 | static const uint32_t kIcoDirEntryBytes = 16; |
| 41 | |
| 42 | // Read the directory header |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 43 | SkAutoTDeleteArray<uint8_t> dirBuffer(new uint8_t[kIcoDirectoryBytes]); |
msarett | d0be5bb | 2015-03-25 06:29:18 -0700 | [diff] [blame] | 44 | if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) != |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 45 | kIcoDirectoryBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 46 | SkCodecPrintf("Error: unable to read ico directory header.\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 47 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Process the directory header |
| 51 | const uint16_t numImages = get_short(dirBuffer.get(), 4); |
| 52 | if (0 == numImages) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 53 | SkCodecPrintf("Error: No images embedded in ico.\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Ensure that we can read all of indicated directory entries |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 58 | SkAutoTDeleteArray<uint8_t> entryBuffer(new uint8_t[numImages * kIcoDirEntryBytes]); |
msarett | d0be5bb | 2015-03-25 06:29:18 -0700 | [diff] [blame] | 59 | if (inputStream.get()->read(entryBuffer.get(), numImages*kIcoDirEntryBytes) != |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 60 | numImages*kIcoDirEntryBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 61 | SkCodecPrintf("Error: unable to read ico directory entries.\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // This structure is used to represent the vital information about entries |
| 66 | // in the directory header. We will obtain this information for each |
| 67 | // directory entry. |
| 68 | struct Entry { |
| 69 | uint32_t offset; |
| 70 | uint32_t size; |
| 71 | }; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 72 | SkAutoTDeleteArray<Entry> directoryEntries(new Entry[numImages]); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 73 | |
| 74 | // Iterate over directory entries |
| 75 | for (uint32_t i = 0; i < numImages; i++) { |
| 76 | // The directory entry contains information such as width, height, |
| 77 | // bits per pixel, and number of colors in the color palette. We will |
| 78 | // ignore these fields since they are repeated in the header of the |
| 79 | // embedded image. In the event of an inconsistency, we would always |
| 80 | // defer to the value in the embedded header anyway. |
| 81 | |
| 82 | // Specifies the size of the embedded image, including the header |
| 83 | uint32_t size = get_int(entryBuffer.get(), 8 + i*kIcoDirEntryBytes); |
| 84 | |
| 85 | // Specifies the offset of the embedded image from the start of file. |
| 86 | // It does not indicate the start of the pixel data, but rather the |
| 87 | // start of the embedded image header. |
| 88 | uint32_t offset = get_int(entryBuffer.get(), 12 + i*kIcoDirEntryBytes); |
| 89 | |
| 90 | // Save the vital fields |
| 91 | directoryEntries.get()[i].offset = offset; |
| 92 | directoryEntries.get()[i].size = size; |
| 93 | } |
| 94 | |
| 95 | // It is "customary" that the embedded images will be stored in order of |
| 96 | // increasing offset. However, the specification does not indicate that |
| 97 | // they must be stored in this order, so we will not trust that this is the |
| 98 | // case. Here we sort the embedded images by increasing offset. |
| 99 | struct EntryLessThan { |
| 100 | bool operator() (Entry a, Entry b) const { |
| 101 | return a.offset < b.offset; |
| 102 | } |
| 103 | }; |
| 104 | EntryLessThan lessThan; |
| 105 | SkTQSort(directoryEntries.get(), directoryEntries.get() + numImages - 1, |
| 106 | lessThan); |
| 107 | |
| 108 | // Now will construct a candidate codec for each of the embedded images |
| 109 | uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; |
| 110 | SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> codecs( |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 111 | new (SkTArray<SkAutoTDelete<SkCodec>, true>)(numImages)); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 112 | for (uint32_t i = 0; i < numImages; i++) { |
| 113 | uint32_t offset = directoryEntries.get()[i].offset; |
| 114 | uint32_t size = directoryEntries.get()[i].size; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 115 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 116 | // Ensure that the offset is valid |
| 117 | if (offset < bytesRead) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 118 | SkCodecPrintf("Warning: invalid ico offset.\n"); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | |
| 122 | // If we cannot skip, assume we have reached the end of the stream and |
| 123 | // stop trying to make codecs |
msarett | d0be5bb | 2015-03-25 06:29:18 -0700 | [diff] [blame] | 124 | if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 125 | SkCodecPrintf("Warning: could not skip to ico offset.\n"); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | bytesRead = offset; |
| 129 | |
| 130 | // Create a new stream for the embedded codec |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame^] | 131 | sk_sp<SkData> data(SkData::MakeFromStream(inputStream.get(), size)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | if (nullptr == data.get()) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 133 | SkCodecPrintf("Warning: could not create embedded stream.\n"); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 134 | break; |
| 135 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame^] | 136 | SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data)); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 137 | bytesRead += size; |
| 138 | |
| 139 | // Check if the embedded codec is bmp or png and create the codec |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | SkCodec* codec = nullptr; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 141 | if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 142 | codec = SkPngCodec::NewFromStream(embeddedStream.release()); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 143 | } else { |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 144 | codec = SkBmpCodec::NewFromIco(embeddedStream.release()); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Save a valid codec |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | if (nullptr != codec) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 149 | codecs->push_back().reset(codec); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Recognize if there are no valid codecs |
| 154 | if (0 == codecs->count()) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 155 | SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Use the largest codec as a "suggestion" for image info |
| 160 | uint32_t maxSize = 0; |
| 161 | uint32_t maxIndex = 0; |
| 162 | for (int32_t i = 0; i < codecs->count(); i++) { |
| 163 | SkImageInfo info = codecs->operator[](i)->getInfo(); |
| 164 | uint32_t size = info.width() * info.height(); |
| 165 | if (size > maxSize) { |
| 166 | maxSize = size; |
| 167 | maxIndex = i; |
| 168 | } |
| 169 | } |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 170 | int width = codecs->operator[](maxIndex)->getInfo().width(); |
| 171 | int height = codecs->operator[](maxIndex)->getInfo().height(); |
| 172 | SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo(); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 173 | |
| 174 | // Note that stream is owned by the embedded codec, the ico does not need |
| 175 | // direct access to the stream. |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 176 | return new SkIcoCodec(width, height, info, codecs.release()); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Creates an instance of the decoder |
| 181 | * Called only by NewFromStream |
| 182 | */ |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 183 | SkIcoCodec::SkIcoCodec(int width, int height, const SkEncodedInfo& info, |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 184 | SkTArray<SkAutoTDelete<SkCodec>, true>* codecs) |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 185 | : INHERITED(width, height, info, nullptr) |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 186 | , fEmbeddedCodecs(codecs) |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 187 | , fCurrScanlineCodec(nullptr) |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 188 | {} |
| 189 | |
| 190 | /* |
| 191 | * Chooses the best dimensions given the desired scale |
| 192 | */ |
| 193 | SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { |
| 194 | // We set the dimensions to the largest candidate image by default. |
| 195 | // Regardless of the scale request, this is the largest image that we |
| 196 | // will decode. |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 197 | int origWidth = this->getInfo().width(); |
| 198 | int origHeight = this->getInfo().height(); |
| 199 | float desiredSize = desiredScale * origWidth * origHeight; |
| 200 | // At least one image will have smaller error than this initial value |
| 201 | float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; |
| 202 | int32_t minIndex = -1; |
| 203 | for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
| 204 | int width = fEmbeddedCodecs->operator[](i)->getInfo().width(); |
| 205 | int height = fEmbeddedCodecs->operator[](i)->getInfo().height(); |
| 206 | float error = SkTAbs(((float) (width * height)) - desiredSize); |
| 207 | if (error < minError) { |
| 208 | minError = error; |
| 209 | minIndex = i; |
| 210 | } |
| 211 | } |
| 212 | SkASSERT(minIndex >= 0); |
| 213 | |
| 214 | return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions(); |
| 215 | } |
| 216 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 217 | int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) { |
| 218 | SkASSERT(startIndex >= 0); |
| 219 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 220 | // FIXME: Cache the index from onGetScaledDimensions? |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 221 | for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) { |
| 222 | if (fEmbeddedCodecs->operator[](i)->getInfo().dimensions() == requestedSize) { |
| 223 | return i; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) { |
| 231 | return this->chooseCodec(dim, 0) >= 0; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 232 | } |
| 233 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 234 | /* |
| 235 | * Initiates the Ico decode |
| 236 | */ |
| 237 | SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 238 | void* dst, size_t dstRowBytes, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 239 | const Options& opts, SkPMColor* colorTable, |
| 240 | int* colorCount, int* rowsDecoded) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 241 | if (opts.fSubset) { |
| 242 | // Subsets are not supported. |
| 243 | return kUnimplemented; |
| 244 | } |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 245 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 246 | int index = 0; |
| 247 | SkCodec::Result result = kInvalidScale; |
| 248 | while (true) { |
| 249 | index = this->chooseCodec(dstInfo.dimensions(), index); |
| 250 | if (index < 0) { |
| 251 | break; |
msarett | 1603e93 | 2015-12-04 05:43:09 -0800 | [diff] [blame] | 252 | } |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 253 | |
| 254 | SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index); |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 255 | result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts, colorTable, |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 256 | colorCount); |
| 257 | |
| 258 | switch (result) { |
| 259 | case kSuccess: |
| 260 | case kIncompleteInput: |
| 261 | // The embedded codec will handle filling incomplete images, so we will indicate |
| 262 | // that all of the rows are initialized. |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 263 | *rowsDecoded = dstInfo.height(); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 264 | return result; |
| 265 | default: |
| 266 | // Continue trying to find a valid embedded codec on a failed decode. |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | index++; |
msarett | 1603e93 | 2015-12-04 05:43:09 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 274 | return result; |
| 275 | } |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 276 | |
| 277 | SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 278 | const SkCodec::Options& options, SkPMColor colorTable[], int* colorCount) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 279 | int index = 0; |
| 280 | SkCodec::Result result = kInvalidScale; |
| 281 | while (true) { |
| 282 | index = this->chooseCodec(dstInfo.dimensions(), index); |
| 283 | if (index < 0) { |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index); |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 288 | result = embeddedCodec->startScanlineDecode(dstInfo, &options, colorTable, colorCount); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 289 | if (kSuccess == result) { |
| 290 | fCurrScanlineCodec = embeddedCodec; |
| 291 | return result; |
| 292 | } |
| 293 | |
| 294 | index++; |
| 295 | } |
| 296 | |
| 297 | SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 298 | return result; |
| 299 | } |
| 300 | |
| 301 | int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
| 302 | SkASSERT(fCurrScanlineCodec); |
| 303 | return fCurrScanlineCodec->getScanlines(dst, count, rowBytes); |
| 304 | } |
| 305 | |
| 306 | bool SkIcoCodec::onSkipScanlines(int count) { |
| 307 | SkASSERT(fCurrScanlineCodec); |
| 308 | return fCurrScanlineCodec->skipScanlines(count); |
| 309 | } |
| 310 | |
| 311 | SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const { |
| 312 | // FIXME: This function will possibly return the wrong value if it is called |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 313 | // before startScanlineDecode(). |
| 314 | return fCurrScanlineCodec ? fCurrScanlineCodec->getScanlineOrder() : |
| 315 | INHERITED::onGetScanlineOrder(); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 319 | return fCurrScanlineCodec ? fCurrScanlineCodec->getSampler(createIfNecessary) : nullptr; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 320 | } |