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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkData.h" |
| 9 | #include "include/core/SkStream.h" |
| 10 | #include "include/private/SkColorData.h" |
| 11 | #include "include/private/SkTDArray.h" |
| 12 | #include "src/codec/SkBmpCodec.h" |
| 13 | #include "src/codec/SkCodecPriv.h" |
| 14 | #include "src/codec/SkIcoCodec.h" |
| 15 | #include "src/codec/SkPngCodec.h" |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 16 | #include "src/core/SkStreamPriv.h" |
John Stiles | 6e9ead9 | 2020-07-14 00:13:51 +0000 | [diff] [blame] | 17 | #include "src/core/SkTSort.h" |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Checks the start of the stream to see if the image is an Ico or Cur |
| 21 | */ |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 22 | bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 23 | const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' }; |
| 24 | const char curSig[] = { '\x00', '\x00', '\x02', '\x00' }; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 25 | return bytesRead >= sizeof(icoSig) && |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 26 | (!memcmp(buffer, icoSig, sizeof(icoSig)) || |
| 27 | !memcmp(buffer, curSig, sizeof(curSig))); |
| 28 | } |
| 29 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 30 | std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 31 | Result* result) { |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 32 | // It is helpful to have the entire stream in a contiguous buffer. In some cases, |
| 33 | // this is already the case anyway, so this method is faster. In others, this is |
| 34 | // safer than the old method, which required allocating a block of memory whose |
| 35 | // byte size is stored in the stream as a uint32_t, and may result in a large or |
| 36 | // failed allocation. |
| 37 | sk_sp<SkData> data = nullptr; |
| 38 | if (stream->getMemoryBase()) { |
| 39 | // It is safe to make without copy because we'll hold onto the stream. |
| 40 | data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); |
| 41 | } else { |
| 42 | data = SkCopyStreamToData(stream.get()); |
| 43 | |
| 44 | // If we are forced to copy the stream to a data, we can go ahead and delete the stream. |
| 45 | stream.reset(nullptr); |
| 46 | } |
| 47 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 48 | // Header size constants |
Leon Scroggins III | 862c196 | 2017-10-02 16:28:49 -0400 | [diff] [blame] | 49 | constexpr uint32_t kIcoDirectoryBytes = 6; |
| 50 | constexpr uint32_t kIcoDirEntryBytes = 16; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 51 | |
| 52 | // Read the directory header |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 53 | if (data->size() < kIcoDirectoryBytes) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 54 | SkCodecPrintf("Error: unable to read ico directory header.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 55 | *result = kIncompleteInput; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Process the directory header |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 60 | const uint16_t numImages = get_short(data->bytes(), 4); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 61 | if (0 == numImages) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 62 | SkCodecPrintf("Error: No images embedded in ico.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 63 | *result = kInvalidInput; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 65 | } |
| 66 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 67 | // This structure is used to represent the vital information about entries |
| 68 | // in the directory header. We will obtain this information for each |
| 69 | // directory entry. |
| 70 | struct Entry { |
| 71 | uint32_t offset; |
| 72 | uint32_t size; |
| 73 | }; |
Mike Reed | 8dc8dbc | 2018-01-05 11:20:10 -0500 | [diff] [blame] | 74 | SkAutoFree dirEntryBuffer(sk_malloc_canfail(sizeof(Entry) * numImages)); |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 75 | if (!dirEntryBuffer) { |
| 76 | SkCodecPrintf("Error: OOM allocating ICO directory for %i images.\n", |
| 77 | numImages); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 78 | *result = kInternalError; |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 79 | return nullptr; |
| 80 | } |
| 81 | auto* directoryEntries = reinterpret_cast<Entry*>(dirEntryBuffer.get()); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 82 | |
| 83 | // Iterate over directory entries |
| 84 | for (uint32_t i = 0; i < numImages; i++) { |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 85 | const uint8_t* entryBuffer = data->bytes() + kIcoDirectoryBytes + i * kIcoDirEntryBytes; |
| 86 | if (data->size() < kIcoDirectoryBytes + (i+1) * kIcoDirEntryBytes) { |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 87 | SkCodecPrintf("Error: Dir entries truncated in ico.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 88 | *result = kIncompleteInput; |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| 91 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 92 | // The directory entry contains information such as width, height, |
| 93 | // bits per pixel, and number of colors in the color palette. We will |
| 94 | // ignore these fields since they are repeated in the header of the |
| 95 | // embedded image. In the event of an inconsistency, we would always |
| 96 | // defer to the value in the embedded header anyway. |
| 97 | |
| 98 | // Specifies the size of the embedded image, including the header |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 99 | uint32_t size = get_int(entryBuffer, 8); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 100 | |
| 101 | // Specifies the offset of the embedded image from the start of file. |
| 102 | // It does not indicate the start of the pixel data, but rather the |
| 103 | // start of the embedded image header. |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 104 | uint32_t offset = get_int(entryBuffer, 12); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 105 | |
| 106 | // Save the vital fields |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 107 | directoryEntries[i].offset = offset; |
| 108 | directoryEntries[i].size = size; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 111 | // Default Result, if no valid embedded codecs are found. |
| 112 | *result = kInvalidInput; |
| 113 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 114 | // It is "customary" that the embedded images will be stored in order of |
| 115 | // increasing offset. However, the specification does not indicate that |
| 116 | // they must be stored in this order, so we will not trust that this is the |
| 117 | // case. Here we sort the embedded images by increasing offset. |
John Stiles | 6e9ead9 | 2020-07-14 00:13:51 +0000 | [diff] [blame] | 118 | struct EntryLessThan { |
| 119 | bool operator() (Entry a, Entry b) const { |
| 120 | return a.offset < b.offset; |
| 121 | } |
| 122 | }; |
| 123 | EntryLessThan lessThan; |
John Stiles | 886a904 | 2020-07-14 16:28:33 -0400 | [diff] [blame] | 124 | SkTQSort(directoryEntries, directoryEntries + numImages, lessThan); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 125 | |
| 126 | // Now will construct a candidate codec for each of the embedded images |
| 127 | uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 128 | std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs( |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 129 | new SkTArray<std::unique_ptr<SkCodec>, true>(numImages)); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 130 | for (uint32_t i = 0; i < numImages; i++) { |
Leon Scroggins III | 005a970 | 2017-06-29 15:41:32 -0400 | [diff] [blame] | 131 | uint32_t offset = directoryEntries[i].offset; |
| 132 | uint32_t size = directoryEntries[i].size; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 133 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 134 | // Ensure that the offset is valid |
| 135 | if (offset < bytesRead) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 136 | SkCodecPrintf("Warning: invalid ico offset.\n"); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // If we cannot skip, assume we have reached the end of the stream and |
| 141 | // stop trying to make codecs |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 142 | if (offset >= data->size()) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 143 | SkCodecPrintf("Warning: could not skip to ico offset.\n"); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 144 | break; |
| 145 | } |
| 146 | bytesRead = offset; |
| 147 | |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 148 | if (offset + size > data->size()) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 149 | SkCodecPrintf("Warning: could not create embedded stream.\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 150 | *result = kIncompleteInput; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 151 | break; |
| 152 | } |
Leon Scroggins III | 12a4dc9 | 2017-06-05 14:06:57 -0400 | [diff] [blame] | 153 | |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 154 | sk_sp<SkData> embeddedData(SkData::MakeSubset(data.get(), offset, size)); |
| 155 | auto embeddedStream = SkMemoryStream::Make(embeddedData); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 156 | bytesRead += size; |
| 157 | |
| 158 | // Check if the embedded codec is bmp or png and create the codec |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 159 | std::unique_ptr<SkCodec> codec; |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 160 | Result ignoredResult; |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 161 | if (SkPngCodec::IsPng(embeddedData->bytes(), embeddedData->size())) { |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 162 | codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &ignoredResult); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 163 | } else { |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 164 | codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &ignoredResult); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 165 | } |
| 166 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | if (nullptr != codec) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 168 | codecs->push_back().reset(codec.release()); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 172 | if (0 == codecs->count()) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 173 | SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | return nullptr; |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Use the largest codec as a "suggestion" for image info |
Matt Sarett | 29121eb | 2016-10-17 14:32:46 -0400 | [diff] [blame] | 178 | size_t maxSize = 0; |
| 179 | int maxIndex = 0; |
| 180 | for (int i = 0; i < codecs->count(); i++) { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 181 | SkImageInfo info = codecs->operator[](i)->getInfo(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 182 | size_t size = info.computeMinByteSize(); |
Matt Sarett | 29121eb | 2016-10-17 14:32:46 -0400 | [diff] [blame] | 183 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 184 | if (size > maxSize) { |
| 185 | maxSize = size; |
| 186 | maxIndex = i; |
| 187 | } |
| 188 | } |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 189 | |
| 190 | auto maxInfo = codecs->operator[](maxIndex)->getEncodedInfo().copy(); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 191 | |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 192 | *result = kSuccess; |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 193 | return std::unique_ptr<SkCodec>(new SkIcoCodec(std::move(maxInfo), std::move(stream), |
| 194 | codecs.release())); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 197 | SkIcoCodec::SkIcoCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, |
| 198 | SkTArray<std::unique_ptr<SkCodec>, true>* codecs) |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 199 | // The source skcms_PixelFormat will not be used. The embedded |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 200 | // codec's will be used instead. |
Leon Scroggins III | 995b467 | 2020-03-13 17:12:34 -0400 | [diff] [blame] | 201 | : INHERITED(std::move(info), skcms_PixelFormat(), std::move(stream)) |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 202 | , fEmbeddedCodecs(codecs) |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 203 | , fCurrCodec(nullptr) |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 204 | {} |
| 205 | |
| 206 | /* |
| 207 | * Chooses the best dimensions given the desired scale |
| 208 | */ |
Chris Blume | 66f2332 | 2017-04-19 12:40:46 -0700 | [diff] [blame] | 209 | SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 210 | // We set the dimensions to the largest candidate image by default. |
| 211 | // Regardless of the scale request, this is the largest image that we |
| 212 | // will decode. |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 213 | int origWidth = this->dimensions().width(); |
| 214 | int origHeight = this->dimensions().height(); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 215 | float desiredSize = desiredScale * origWidth * origHeight; |
| 216 | // At least one image will have smaller error than this initial value |
| 217 | float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; |
| 218 | int32_t minIndex = -1; |
| 219 | for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 220 | auto dimensions = fEmbeddedCodecs->operator[](i)->dimensions(); |
| 221 | int width = dimensions.width(); |
| 222 | int height = dimensions.height(); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 223 | float error = SkTAbs(((float) (width * height)) - desiredSize); |
| 224 | if (error < minError) { |
| 225 | minError = error; |
| 226 | minIndex = i; |
| 227 | } |
| 228 | } |
| 229 | SkASSERT(minIndex >= 0); |
| 230 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 231 | return fEmbeddedCodecs->operator[](minIndex)->dimensions(); |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 232 | } |
| 233 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 234 | int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) { |
| 235 | SkASSERT(startIndex >= 0); |
| 236 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 237 | // FIXME: Cache the index from onGetScaledDimensions? |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 238 | for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) { |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 239 | if (fEmbeddedCodecs->operator[](i)->dimensions() == requestedSize) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 240 | return i; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) { |
| 248 | return this->chooseCodec(dim, 0) >= 0; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 249 | } |
| 250 | |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 251 | /* |
| 252 | * Initiates the Ico decode |
| 253 | */ |
| 254 | SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 255 | void* dst, size_t dstRowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 256 | const Options& opts, |
| 257 | int* rowsDecoded) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 258 | if (opts.fSubset) { |
| 259 | // Subsets are not supported. |
| 260 | return kUnimplemented; |
| 261 | } |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 262 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 263 | int index = 0; |
| 264 | SkCodec::Result result = kInvalidScale; |
| 265 | while (true) { |
| 266 | index = this->chooseCodec(dstInfo.dimensions(), index); |
| 267 | if (index < 0) { |
| 268 | break; |
msarett | 1603e93 | 2015-12-04 05:43:09 -0800 | [diff] [blame] | 269 | } |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 270 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 271 | SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 272 | result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 273 | switch (result) { |
| 274 | case kSuccess: |
| 275 | case kIncompleteInput: |
| 276 | // The embedded codec will handle filling incomplete images, so we will indicate |
| 277 | // that all of the rows are initialized. |
msarett | f4004f9 | 2016-02-11 10:49:31 -0800 | [diff] [blame] | 278 | *rowsDecoded = dstInfo.height(); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 279 | return result; |
| 280 | default: |
| 281 | // Continue trying to find a valid embedded codec on a failed decode. |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | index++; |
msarett | 1603e93 | 2015-12-04 05:43:09 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 289 | return result; |
| 290 | } |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 291 | |
| 292 | SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 293 | const SkCodec::Options& options) { |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 294 | int index = 0; |
| 295 | SkCodec::Result result = kInvalidScale; |
| 296 | while (true) { |
| 297 | index = this->chooseCodec(dstInfo.dimensions(), index); |
| 298 | if (index < 0) { |
| 299 | break; |
| 300 | } |
| 301 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 302 | SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 303 | result = embeddedCodec->startScanlineDecode(dstInfo, &options); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 304 | if (kSuccess == result) { |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 305 | fCurrCodec = embeddedCodec; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 306 | return result; |
| 307 | } |
| 308 | |
| 309 | index++; |
| 310 | } |
| 311 | |
| 312 | SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 313 | return result; |
| 314 | } |
| 315 | |
| 316 | int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 317 | SkASSERT(fCurrCodec); |
| 318 | return fCurrCodec->getScanlines(dst, count, rowBytes); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | bool SkIcoCodec::onSkipScanlines(int count) { |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 322 | SkASSERT(fCurrCodec); |
| 323 | return fCurrCodec->skipScanlines(count); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 324 | } |
| 325 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 326 | SkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 327 | void* pixels, size_t rowBytes, const SkCodec::Options& options) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 328 | int index = 0; |
| 329 | while (true) { |
| 330 | index = this->chooseCodec(dstInfo.dimensions(), index); |
| 331 | if (index < 0) { |
| 332 | break; |
| 333 | } |
| 334 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 335 | SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 336 | switch (embeddedCodec->startIncrementalDecode(dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 337 | pixels, rowBytes, &options)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 338 | case kSuccess: |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 339 | fCurrCodec = embeddedCodec; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 340 | return kSuccess; |
| 341 | case kUnimplemented: |
| 342 | // FIXME: embeddedCodec is a BMP. If scanline decoding would work, |
| 343 | // return kUnimplemented so that SkSampledCodec will fall through |
| 344 | // to use the scanline decoder. |
| 345 | // Note that calling startScanlineDecode will require an extra |
| 346 | // rewind. The embedded codec has an SkMemoryStream, which is |
| 347 | // cheap to rewind, though it will do extra work re-reading the |
| 348 | // header. |
| 349 | // Also note that we pass nullptr for Options. This is because |
| 350 | // Options that are valid for incremental decoding may not be |
| 351 | // valid for scanline decoding. |
| 352 | // Once BMP supports incremental decoding this workaround can go |
| 353 | // away. |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 354 | if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 355 | return kUnimplemented; |
| 356 | } |
| 357 | // Move on to the next embedded codec. |
| 358 | break; |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | index++; |
| 364 | } |
| 365 | |
| 366 | SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
| 367 | return kInvalidScale; |
| 368 | } |
| 369 | |
| 370 | SkCodec::Result SkIcoCodec::onIncrementalDecode(int* rowsDecoded) { |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 371 | SkASSERT(fCurrCodec); |
| 372 | return fCurrCodec->incrementalDecode(rowsDecoded); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 373 | } |
| 374 | |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 375 | SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const { |
| 376 | // FIXME: This function will possibly return the wrong value if it is called |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 377 | // before startScanlineDecode()/startIncrementalDecode(). |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 378 | if (fCurrCodec) { |
| 379 | return fCurrCodec->getScanlineOrder(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return INHERITED::onGetScanlineOrder(); |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) { |
nagarajan.n | 477e9d4 | 2017-09-25 18:13:06 +0530 | [diff] [blame] | 386 | if (fCurrCodec) { |
| 387 | return fCurrCodec->getSampler(createIfNecessary); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return nullptr; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 391 | } |