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