blob: 9340744f76777d206ae5f2ab47f6bb902e7b8adc [file] [log] [blame]
msarett9bde9182015-03-25 05:27:48 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 III995b4672020-03-13 17:12:34 -040016#include "src/core/SkStreamPriv.h"
John Stiles6e9ead92020-07-14 00:13:51 +000017#include "src/core/SkTSort.h"
msarett9bde9182015-03-25 05:27:48 -070018
19/*
20 * Checks the start of the stream to see if the image is an Ico or Cur
21 */
scroggodb30be22015-12-08 18:54:13 -080022bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) {
msarett9bde9182015-03-25 05:27:48 -070023 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' };
24 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' };
scroggodb30be22015-12-08 18:54:13 -080025 return bytesRead >= sizeof(icoSig) &&
msarett9bde9182015-03-25 05:27:48 -070026 (!memcmp(buffer, icoSig, sizeof(icoSig)) ||
27 !memcmp(buffer, curSig, sizeof(curSig)));
28}
29
Mike Reedede7bac2017-07-23 15:30:02 -040030std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
31 Result* result) {
Leon Scroggins III995b4672020-03-13 17:12:34 -040032 // 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
msarett9bde9182015-03-25 05:27:48 -070048 // Header size constants
Leon Scroggins III862c1962017-10-02 16:28:49 -040049 constexpr uint32_t kIcoDirectoryBytes = 6;
50 constexpr uint32_t kIcoDirEntryBytes = 16;
msarett9bde9182015-03-25 05:27:48 -070051
52 // Read the directory header
Leon Scroggins III995b4672020-03-13 17:12:34 -040053 if (data->size() < kIcoDirectoryBytes) {
scroggo230d4ac2015-03-26 07:15:55 -070054 SkCodecPrintf("Error: unable to read ico directory header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040055 *result = kIncompleteInput;
halcanary96fcdcc2015-08-27 07:41:13 -070056 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070057 }
58
59 // Process the directory header
Leon Scroggins III995b4672020-03-13 17:12:34 -040060 const uint16_t numImages = get_short(data->bytes(), 4);
msarett9bde9182015-03-25 05:27:48 -070061 if (0 == numImages) {
scroggo230d4ac2015-03-26 07:15:55 -070062 SkCodecPrintf("Error: No images embedded in ico.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040063 *result = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070064 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070065 }
66
msarett9bde9182015-03-25 05:27:48 -070067 // 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 Reed8dc8dbc2018-01-05 11:20:10 -050074 SkAutoFree dirEntryBuffer(sk_malloc_canfail(sizeof(Entry) * numImages));
Leon Scroggins III005a9702017-06-29 15:41:32 -040075 if (!dirEntryBuffer) {
76 SkCodecPrintf("Error: OOM allocating ICO directory for %i images.\n",
77 numImages);
Leon Scroggins III588fb042017-07-14 16:32:31 -040078 *result = kInternalError;
Leon Scroggins III005a9702017-06-29 15:41:32 -040079 return nullptr;
80 }
81 auto* directoryEntries = reinterpret_cast<Entry*>(dirEntryBuffer.get());
msarett9bde9182015-03-25 05:27:48 -070082
83 // Iterate over directory entries
84 for (uint32_t i = 0; i < numImages; i++) {
Leon Scroggins III995b4672020-03-13 17:12:34 -040085 const uint8_t* entryBuffer = data->bytes() + kIcoDirectoryBytes + i * kIcoDirEntryBytes;
86 if (data->size() < kIcoDirectoryBytes + (i+1) * kIcoDirEntryBytes) {
Leon Scroggins III005a9702017-06-29 15:41:32 -040087 SkCodecPrintf("Error: Dir entries truncated in ico.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040088 *result = kIncompleteInput;
Leon Scroggins III005a9702017-06-29 15:41:32 -040089 return nullptr;
90 }
91
msarett9bde9182015-03-25 05:27:48 -070092 // 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 III005a9702017-06-29 15:41:32 -040099 uint32_t size = get_int(entryBuffer, 8);
msarett9bde9182015-03-25 05:27:48 -0700100
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 III005a9702017-06-29 15:41:32 -0400104 uint32_t offset = get_int(entryBuffer, 12);
msarett9bde9182015-03-25 05:27:48 -0700105
106 // Save the vital fields
Leon Scroggins III005a9702017-06-29 15:41:32 -0400107 directoryEntries[i].offset = offset;
108 directoryEntries[i].size = size;
msarett9bde9182015-03-25 05:27:48 -0700109 }
110
Leon Scroggins III588fb042017-07-14 16:32:31 -0400111 // Default Result, if no valid embedded codecs are found.
112 *result = kInvalidInput;
113
msarett9bde9182015-03-25 05:27:48 -0700114 // 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 Stiles6e9ead92020-07-14 00:13:51 +0000118 struct EntryLessThan {
119 bool operator() (Entry a, Entry b) const {
120 return a.offset < b.offset;
121 }
122 };
123 EntryLessThan lessThan;
John Stiles886a9042020-07-14 16:28:33 -0400124 SkTQSort(directoryEntries, directoryEntries + numImages, lessThan);
msarett9bde9182015-03-25 05:27:48 -0700125
126 // Now will construct a candidate codec for each of the embedded images
127 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400128 std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs(
Mike Klein79aea6a2018-06-11 10:45:26 -0400129 new SkTArray<std::unique_ptr<SkCodec>, true>(numImages));
msarett9bde9182015-03-25 05:27:48 -0700130 for (uint32_t i = 0; i < numImages; i++) {
Leon Scroggins III005a9702017-06-29 15:41:32 -0400131 uint32_t offset = directoryEntries[i].offset;
132 uint32_t size = directoryEntries[i].size;
msarette6dd0042015-10-09 11:07:34 -0700133
msarett9bde9182015-03-25 05:27:48 -0700134 // Ensure that the offset is valid
135 if (offset < bytesRead) {
scroggo230d4ac2015-03-26 07:15:55 -0700136 SkCodecPrintf("Warning: invalid ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700137 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 III995b4672020-03-13 17:12:34 -0400142 if (offset >= data->size()) {
scroggo230d4ac2015-03-26 07:15:55 -0700143 SkCodecPrintf("Warning: could not skip to ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700144 break;
145 }
146 bytesRead = offset;
147
Leon Scroggins III995b4672020-03-13 17:12:34 -0400148 if (offset + size > data->size()) {
scroggo230d4ac2015-03-26 07:15:55 -0700149 SkCodecPrintf("Warning: could not create embedded stream.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400150 *result = kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700151 break;
152 }
Leon Scroggins III12a4dc92017-06-05 14:06:57 -0400153
Leon Scroggins III995b4672020-03-13 17:12:34 -0400154 sk_sp<SkData> embeddedData(SkData::MakeSubset(data.get(), offset, size));
155 auto embeddedStream = SkMemoryStream::Make(embeddedData);
msarett9bde9182015-03-25 05:27:48 -0700156 bytesRead += size;
157
158 // Check if the embedded codec is bmp or png and create the codec
Mike Reedede7bac2017-07-23 15:30:02 -0400159 std::unique_ptr<SkCodec> codec;
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400160 Result ignoredResult;
Leon Scroggins III995b4672020-03-13 17:12:34 -0400161 if (SkPngCodec::IsPng(embeddedData->bytes(), embeddedData->size())) {
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400162 codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &ignoredResult);
msarett9bde9182015-03-25 05:27:48 -0700163 } else {
Kevin Lubickbe03ef12021-06-16 15:28:00 -0400164 codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &ignoredResult);
msarett9bde9182015-03-25 05:27:48 -0700165 }
166
halcanary96fcdcc2015-08-27 07:41:13 -0700167 if (nullptr != codec) {
Mike Reedede7bac2017-07-23 15:30:02 -0400168 codecs->push_back().reset(codec.release());
msarett9bde9182015-03-25 05:27:48 -0700169 }
170 }
171
msarett9bde9182015-03-25 05:27:48 -0700172 if (0 == codecs->count()) {
scroggo230d4ac2015-03-26 07:15:55 -0700173 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700174 return nullptr;
msarett9bde9182015-03-25 05:27:48 -0700175 }
176
177 // Use the largest codec as a "suggestion" for image info
Matt Sarett29121eb2016-10-17 14:32:46 -0400178 size_t maxSize = 0;
179 int maxIndex = 0;
180 for (int i = 0; i < codecs->count(); i++) {
msarett9bde9182015-03-25 05:27:48 -0700181 SkImageInfo info = codecs->operator[](i)->getInfo();
Mike Reedf0ffb892017-10-03 14:47:21 -0400182 size_t size = info.computeMinByteSize();
Matt Sarett29121eb2016-10-17 14:32:46 -0400183
msarett9bde9182015-03-25 05:27:48 -0700184 if (size > maxSize) {
185 maxSize = size;
186 maxIndex = i;
187 }
188 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400189
190 auto maxInfo = codecs->operator[](maxIndex)->getEncodedInfo().copy();
msarett9bde9182015-03-25 05:27:48 -0700191
Leon Scroggins III588fb042017-07-14 16:32:31 -0400192 *result = kSuccess;
Leon Scroggins III995b4672020-03-13 17:12:34 -0400193 return std::unique_ptr<SkCodec>(new SkIcoCodec(std::move(maxInfo), std::move(stream),
194 codecs.release()));
msarett9bde9182015-03-25 05:27:48 -0700195}
196
Leon Scroggins III995b4672020-03-13 17:12:34 -0400197SkIcoCodec::SkIcoCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
198 SkTArray<std::unique_ptr<SkCodec>, true>* codecs)
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400199 // The source skcms_PixelFormat will not be used. The embedded
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400200 // codec's will be used instead.
Leon Scroggins III995b4672020-03-13 17:12:34 -0400201 : INHERITED(std::move(info), skcms_PixelFormat(), std::move(stream))
msarett9bde9182015-03-25 05:27:48 -0700202 , fEmbeddedCodecs(codecs)
nagarajan.n477e9d42017-09-25 18:13:06 +0530203 , fCurrCodec(nullptr)
msarett9bde9182015-03-25 05:27:48 -0700204{}
205
206/*
207 * Chooses the best dimensions given the desired scale
208 */
Chris Blume66f23322017-04-19 12:40:46 -0700209SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const {
msarett9bde9182015-03-25 05:27:48 -0700210 // 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 III712476e2018-10-03 15:47:00 -0400213 int origWidth = this->dimensions().width();
214 int origHeight = this->dimensions().height();
msarett9bde9182015-03-25 05:27:48 -0700215 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 III712476e2018-10-03 15:47:00 -0400220 auto dimensions = fEmbeddedCodecs->operator[](i)->dimensions();
221 int width = dimensions.width();
222 int height = dimensions.height();
msarett9bde9182015-03-25 05:27:48 -0700223 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 III712476e2018-10-03 15:47:00 -0400231 return fEmbeddedCodecs->operator[](minIndex)->dimensions();
msarett9bde9182015-03-25 05:27:48 -0700232}
233
msarettbe8216a2015-12-04 08:00:50 -0800234int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) {
235 SkASSERT(startIndex >= 0);
236
scroggoe7fc14b2015-10-02 13:14:46 -0700237 // FIXME: Cache the index from onGetScaledDimensions?
msarettbe8216a2015-12-04 08:00:50 -0800238 for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400239 if (fEmbeddedCodecs->operator[](i)->dimensions() == requestedSize) {
msarettbe8216a2015-12-04 08:00:50 -0800240 return i;
scroggoe7fc14b2015-10-02 13:14:46 -0700241 }
242 }
243
msarettbe8216a2015-12-04 08:00:50 -0800244 return -1;
245}
246
247bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) {
248 return this->chooseCodec(dim, 0) >= 0;
scroggoe7fc14b2015-10-02 13:14:46 -0700249}
250
msarett9bde9182015-03-25 05:27:48 -0700251/*
252 * Initiates the Ico decode
253 */
254SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
255 void* dst, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000256 const Options& opts,
257 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700258 if (opts.fSubset) {
259 // Subsets are not supported.
260 return kUnimplemented;
261 }
scroggocc2feb12015-08-14 08:32:46 -0700262
msarettbe8216a2015-12-04 08:00:50 -0800263 int index = 0;
264 SkCodec::Result result = kInvalidScale;
265 while (true) {
266 index = this->chooseCodec(dstInfo.dimensions(), index);
267 if (index < 0) {
268 break;
msarett1603e932015-12-04 05:43:09 -0800269 }
msarettbe8216a2015-12-04 08:00:50 -0800270
Ben Wagner145dbcd2016-11-03 14:40:50 -0400271 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000272 result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts);
msarettbe8216a2015-12-04 08:00:50 -0800273 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.
msarettf4004f92016-02-11 10:49:31 -0800278 *rowsDecoded = dstInfo.height();
msarettbe8216a2015-12-04 08:00:50 -0800279 return result;
280 default:
281 // Continue trying to find a valid embedded codec on a failed decode.
282 break;
283 }
284
285 index++;
msarett1603e932015-12-04 05:43:09 -0800286 }
287
288 SkCodecPrintf("Error: No matching candidate image in ico.\n");
289 return result;
290}
msarettbe8216a2015-12-04 08:00:50 -0800291
292SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000293 const SkCodec::Options& options) {
msarettbe8216a2015-12-04 08:00:50 -0800294 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 Wagner145dbcd2016-11-03 14:40:50 -0400302 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000303 result = embeddedCodec->startScanlineDecode(dstInfo, &options);
msarettbe8216a2015-12-04 08:00:50 -0800304 if (kSuccess == result) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530305 fCurrCodec = embeddedCodec;
msarettbe8216a2015-12-04 08:00:50 -0800306 return result;
307 }
308
309 index++;
310 }
311
312 SkCodecPrintf("Error: No matching candidate image in ico.\n");
313 return result;
314}
315
316int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530317 SkASSERT(fCurrCodec);
318 return fCurrCodec->getScanlines(dst, count, rowBytes);
msarettbe8216a2015-12-04 08:00:50 -0800319}
320
321bool SkIcoCodec::onSkipScanlines(int count) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530322 SkASSERT(fCurrCodec);
323 return fCurrCodec->skipScanlines(count);
msarettbe8216a2015-12-04 08:00:50 -0800324}
325
scroggo8e6c7ad2016-09-16 08:20:38 -0700326SkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000327 void* pixels, size_t rowBytes, const SkCodec::Options& options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700328 int index = 0;
329 while (true) {
330 index = this->chooseCodec(dstInfo.dimensions(), index);
331 if (index < 0) {
332 break;
333 }
334
Ben Wagner145dbcd2016-11-03 14:40:50 -0400335 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
scroggo8e6c7ad2016-09-16 08:20:38 -0700336 switch (embeddedCodec->startIncrementalDecode(dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000337 pixels, rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700338 case kSuccess:
nagarajan.n477e9d42017-09-25 18:13:06 +0530339 fCurrCodec = embeddedCodec;
scroggo8e6c7ad2016-09-16 08:20:38 -0700340 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 Scroggins571b30f2017-07-11 17:35:31 +0000354 if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700355 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
370SkCodec::Result SkIcoCodec::onIncrementalDecode(int* rowsDecoded) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530371 SkASSERT(fCurrCodec);
372 return fCurrCodec->incrementalDecode(rowsDecoded);
scroggo8e6c7ad2016-09-16 08:20:38 -0700373}
374
msarettbe8216a2015-12-04 08:00:50 -0800375SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const {
376 // FIXME: This function will possibly return the wrong value if it is called
scroggo8e6c7ad2016-09-16 08:20:38 -0700377 // before startScanlineDecode()/startIncrementalDecode().
nagarajan.n477e9d42017-09-25 18:13:06 +0530378 if (fCurrCodec) {
379 return fCurrCodec->getScanlineOrder();
scroggo8e6c7ad2016-09-16 08:20:38 -0700380 }
381
382 return INHERITED::onGetScanlineOrder();
msarettbe8216a2015-12-04 08:00:50 -0800383}
384
385SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530386 if (fCurrCodec) {
387 return fCurrCodec->getSampler(createIfNecessary);
scroggo8e6c7ad2016-09-16 08:20:38 -0700388 }
389
390 return nullptr;
msarettbe8216a2015-12-04 08:00:50 -0800391}