blob: fcd31893d267e5515859bbf68546703b7cfb5f56 [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
msarettb46e5e22015-07-30 11:36:40 -07008#include "SkBmpCodec.h"
msarett9bde9182015-03-25 05:27:48 -07009#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
msarett9bde9182015-03-25 05:27:48 -070011#include "SkData.h"
msarett1a464672016-01-07 13:17:19 -080012#include "SkIcoCodec.h"
msarettbe1d5552016-01-21 09:05:23 -080013#include "SkPngCodec.h"
msarett9bde9182015-03-25 05:27:48 -070014#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 */
scroggodb30be22015-12-08 18:54:13 -080021bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) {
msarett9bde9182015-03-25 05:27:48 -070022 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' };
23 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' };
scroggodb30be22015-12-08 18:54:13 -080024 return bytesRead >= sizeof(icoSig) &&
msarett9bde9182015-03-25 05:27:48 -070025 (!memcmp(buffer, icoSig, sizeof(icoSig)) ||
26 !memcmp(buffer, curSig, sizeof(curSig)));
27}
28
Mike Reedede7bac2017-07-23 15:30:02 -040029std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
30 Result* result) {
msarett9bde9182015-03-25 05:27:48 -070031 // Header size constants
Leon Scroggins III862c1962017-10-02 16:28:49 -040032 constexpr uint32_t kIcoDirectoryBytes = 6;
33 constexpr uint32_t kIcoDirEntryBytes = 16;
msarett9bde9182015-03-25 05:27:48 -070034
35 // Read the directory header
Ben Wagner7ecc5962016-11-02 17:07:33 -040036 std::unique_ptr<uint8_t[]> dirBuffer(new uint8_t[kIcoDirectoryBytes]);
Mike Reedede7bac2017-07-23 15:30:02 -040037 if (stream->read(dirBuffer.get(), kIcoDirectoryBytes) != kIcoDirectoryBytes) {
scroggo230d4ac2015-03-26 07:15:55 -070038 SkCodecPrintf("Error: unable to read ico directory header.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040039 *result = kIncompleteInput;
halcanary96fcdcc2015-08-27 07:41:13 -070040 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070041 }
42
43 // Process the directory header
44 const uint16_t numImages = get_short(dirBuffer.get(), 4);
45 if (0 == numImages) {
scroggo230d4ac2015-03-26 07:15:55 -070046 SkCodecPrintf("Error: No images embedded in ico.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040047 *result = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070048 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070049 }
50
msarett9bde9182015-03-25 05:27:48 -070051 // 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 };
Leon Scroggins III005a9702017-06-29 15:41:32 -040058 SkAutoFree dirEntryBuffer(sk_malloc_flags(sizeof(Entry) * numImages,
59 SK_MALLOC_TEMP));
60 if (!dirEntryBuffer) {
61 SkCodecPrintf("Error: OOM allocating ICO directory for %i images.\n",
62 numImages);
Leon Scroggins III588fb042017-07-14 16:32:31 -040063 *result = kInternalError;
Leon Scroggins III005a9702017-06-29 15:41:32 -040064 return nullptr;
65 }
66 auto* directoryEntries = reinterpret_cast<Entry*>(dirEntryBuffer.get());
msarett9bde9182015-03-25 05:27:48 -070067
68 // Iterate over directory entries
69 for (uint32_t i = 0; i < numImages; i++) {
Leon Scroggins III005a9702017-06-29 15:41:32 -040070 uint8_t entryBuffer[kIcoDirEntryBytes];
Mike Reedede7bac2017-07-23 15:30:02 -040071 if (stream->read(entryBuffer, kIcoDirEntryBytes) != kIcoDirEntryBytes) {
Leon Scroggins III005a9702017-06-29 15:41:32 -040072 SkCodecPrintf("Error: Dir entries truncated in ico.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040073 *result = kIncompleteInput;
Leon Scroggins III005a9702017-06-29 15:41:32 -040074 return nullptr;
75 }
76
msarett9bde9182015-03-25 05:27:48 -070077 // The directory entry contains information such as width, height,
78 // bits per pixel, and number of colors in the color palette. We will
79 // ignore these fields since they are repeated in the header of the
80 // embedded image. In the event of an inconsistency, we would always
81 // defer to the value in the embedded header anyway.
82
83 // Specifies the size of the embedded image, including the header
Leon Scroggins III005a9702017-06-29 15:41:32 -040084 uint32_t size = get_int(entryBuffer, 8);
msarett9bde9182015-03-25 05:27:48 -070085
86 // Specifies the offset of the embedded image from the start of file.
87 // It does not indicate the start of the pixel data, but rather the
88 // start of the embedded image header.
Leon Scroggins III005a9702017-06-29 15:41:32 -040089 uint32_t offset = get_int(entryBuffer, 12);
msarett9bde9182015-03-25 05:27:48 -070090
91 // Save the vital fields
Leon Scroggins III005a9702017-06-29 15:41:32 -040092 directoryEntries[i].offset = offset;
93 directoryEntries[i].size = size;
msarett9bde9182015-03-25 05:27:48 -070094 }
95
Leon Scroggins III588fb042017-07-14 16:32:31 -040096 // Default Result, if no valid embedded codecs are found.
97 *result = kInvalidInput;
98
msarett9bde9182015-03-25 05:27:48 -070099 // It is "customary" that the embedded images will be stored in order of
100 // increasing offset. However, the specification does not indicate that
101 // they must be stored in this order, so we will not trust that this is the
102 // case. Here we sort the embedded images by increasing offset.
103 struct EntryLessThan {
104 bool operator() (Entry a, Entry b) const {
105 return a.offset < b.offset;
106 }
107 };
108 EntryLessThan lessThan;
Leon Scroggins III005a9702017-06-29 15:41:32 -0400109 SkTQSort(directoryEntries, &directoryEntries[numImages - 1], lessThan);
msarett9bde9182015-03-25 05:27:48 -0700110
111 // Now will construct a candidate codec for each of the embedded images
112 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400113 std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs(
114 new (SkTArray<std::unique_ptr<SkCodec>, true>)(numImages));
msarett9bde9182015-03-25 05:27:48 -0700115 for (uint32_t i = 0; i < numImages; i++) {
Leon Scroggins III005a9702017-06-29 15:41:32 -0400116 uint32_t offset = directoryEntries[i].offset;
117 uint32_t size = directoryEntries[i].size;
msarette6dd0042015-10-09 11:07:34 -0700118
msarett9bde9182015-03-25 05:27:48 -0700119 // Ensure that the offset is valid
120 if (offset < bytesRead) {
scroggo230d4ac2015-03-26 07:15:55 -0700121 SkCodecPrintf("Warning: invalid ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700122 continue;
123 }
124
125 // If we cannot skip, assume we have reached the end of the stream and
126 // stop trying to make codecs
Mike Reedede7bac2017-07-23 15:30:02 -0400127 if (stream->skip(offset - bytesRead) != offset - bytesRead) {
scroggo230d4ac2015-03-26 07:15:55 -0700128 SkCodecPrintf("Warning: could not skip to ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700129 break;
130 }
131 bytesRead = offset;
132
133 // Create a new stream for the embedded codec
Leon Scroggins III12a4dc92017-06-05 14:06:57 -0400134 SkAutoFree buffer(sk_malloc_flags(size, 0));
135 if (!buffer) {
136 SkCodecPrintf("Warning: OOM trying to create embedded stream.\n");
137 break;
138 }
139
Mike Reedede7bac2017-07-23 15:30:02 -0400140 if (stream->read(buffer.get(), size) != size) {
scroggo230d4ac2015-03-26 07:15:55 -0700141 SkCodecPrintf("Warning: could not create embedded stream.\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -0400142 *result = kIncompleteInput;
msarett9bde9182015-03-25 05:27:48 -0700143 break;
144 }
Leon Scroggins III12a4dc92017-06-05 14:06:57 -0400145
146 sk_sp<SkData> data(SkData::MakeFromMalloc(buffer.release(), size));
Mike Reed847068c2017-07-26 11:35:53 -0400147 auto embeddedStream = SkMemoryStream::Make(data);
msarett9bde9182015-03-25 05:27:48 -0700148 bytesRead += size;
149
150 // Check if the embedded codec is bmp or png and create the codec
Mike Reedede7bac2017-07-23 15:30:02 -0400151 std::unique_ptr<SkCodec> codec;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400152 Result dummyResult;
scroggodb30be22015-12-08 18:54:13 -0800153 if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
Mike Reedede7bac2017-07-23 15:30:02 -0400154 codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &dummyResult);
msarett9bde9182015-03-25 05:27:48 -0700155 } else {
Mike Reedede7bac2017-07-23 15:30:02 -0400156 codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &dummyResult);
msarett9bde9182015-03-25 05:27:48 -0700157 }
158
159 // Save a valid codec
halcanary96fcdcc2015-08-27 07:41:13 -0700160 if (nullptr != codec) {
Mike Reedede7bac2017-07-23 15:30:02 -0400161 codecs->push_back().reset(codec.release());
msarett9bde9182015-03-25 05:27:48 -0700162 }
163 }
164
165 // Recognize if there are no valid codecs
166 if (0 == codecs->count()) {
scroggo230d4ac2015-03-26 07:15:55 -0700167 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700168 return nullptr;
msarett9bde9182015-03-25 05:27:48 -0700169 }
170
171 // Use the largest codec as a "suggestion" for image info
Matt Sarett29121eb2016-10-17 14:32:46 -0400172 size_t maxSize = 0;
173 int maxIndex = 0;
174 for (int i = 0; i < codecs->count(); i++) {
msarett9bde9182015-03-25 05:27:48 -0700175 SkImageInfo info = codecs->operator[](i)->getInfo();
Mike Reed5a2e50e2017-10-02 13:44:11 +0000176 size_t size = info.getSafeSize(info.minRowBytes());
Matt Sarett29121eb2016-10-17 14:32:46 -0400177
msarett9bde9182015-03-25 05:27:48 -0700178 if (size > maxSize) {
179 maxSize = size;
180 maxIndex = i;
181 }
182 }
msarettc30c4182016-04-20 11:53:35 -0700183 int width = codecs->operator[](maxIndex)->getInfo().width();
184 int height = codecs->operator[](maxIndex)->getInfo().height();
185 SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo();
Matt Sarett7f650bd2016-10-30 21:25:34 -0400186 SkColorSpace* colorSpace = codecs->operator[](maxIndex)->getInfo().colorSpace();
msarett9bde9182015-03-25 05:27:48 -0700187
Leon Scroggins III588fb042017-07-14 16:32:31 -0400188 *result = kSuccess;
189 // The original stream is no longer needed, because the embedded codecs own their
190 // own streams.
Mike Reedede7bac2017-07-23 15:30:02 -0400191 return std::unique_ptr<SkCodec>(new SkIcoCodec(width, height, info, codecs.release(),
192 sk_ref_sp(colorSpace)));
msarett9bde9182015-03-25 05:27:48 -0700193}
194
195/*
196 * Creates an instance of the decoder
197 * Called only by NewFromStream
198 */
msarettc30c4182016-04-20 11:53:35 -0700199SkIcoCodec::SkIcoCodec(int width, int height, const SkEncodedInfo& info,
Ben Wagner145dbcd2016-11-03 14:40:50 -0400200 SkTArray<std::unique_ptr<SkCodec>, true>* codecs,
Matt Sarett7f650bd2016-10-30 21:25:34 -0400201 sk_sp<SkColorSpace> colorSpace)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400202 // The source SkColorSpaceXform::ColorFormat will not be used. The embedded
203 // codec's will be used instead.
204 : INHERITED(width, height, info, SkColorSpaceXform::ColorFormat(), nullptr,
205 std::move(colorSpace))
msarett9bde9182015-03-25 05:27:48 -0700206 , fEmbeddedCodecs(codecs)
nagarajan.n477e9d42017-09-25 18:13:06 +0530207 , fCurrCodec(nullptr)
msarett9bde9182015-03-25 05:27:48 -0700208{}
209
210/*
211 * Chooses the best dimensions given the desired scale
212 */
Chris Blume66f23322017-04-19 12:40:46 -0700213SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const {
msarett9bde9182015-03-25 05:27:48 -0700214 // We set the dimensions to the largest candidate image by default.
215 // Regardless of the scale request, this is the largest image that we
216 // will decode.
msarett9bde9182015-03-25 05:27:48 -0700217 int origWidth = this->getInfo().width();
218 int origHeight = this->getInfo().height();
219 float desiredSize = desiredScale * origWidth * origHeight;
220 // At least one image will have smaller error than this initial value
221 float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f;
222 int32_t minIndex = -1;
223 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) {
224 int width = fEmbeddedCodecs->operator[](i)->getInfo().width();
225 int height = fEmbeddedCodecs->operator[](i)->getInfo().height();
226 float error = SkTAbs(((float) (width * height)) - desiredSize);
227 if (error < minError) {
228 minError = error;
229 minIndex = i;
230 }
231 }
232 SkASSERT(minIndex >= 0);
233
234 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions();
235}
236
msarettbe8216a2015-12-04 08:00:50 -0800237int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) {
238 SkASSERT(startIndex >= 0);
239
scroggoe7fc14b2015-10-02 13:14:46 -0700240 // FIXME: Cache the index from onGetScaledDimensions?
msarettbe8216a2015-12-04 08:00:50 -0800241 for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) {
242 if (fEmbeddedCodecs->operator[](i)->getInfo().dimensions() == requestedSize) {
243 return i;
scroggoe7fc14b2015-10-02 13:14:46 -0700244 }
245 }
246
msarettbe8216a2015-12-04 08:00:50 -0800247 return -1;
248}
249
250bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) {
251 return this->chooseCodec(dim, 0) >= 0;
scroggoe7fc14b2015-10-02 13:14:46 -0700252}
253
msarett9bde9182015-03-25 05:27:48 -0700254/*
255 * Initiates the Ico decode
256 */
257SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
258 void* dst, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000259 const Options& opts,
260 int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700261 if (opts.fSubset) {
262 // Subsets are not supported.
263 return kUnimplemented;
264 }
scroggocc2feb12015-08-14 08:32:46 -0700265
msarettbe8216a2015-12-04 08:00:50 -0800266 int index = 0;
267 SkCodec::Result result = kInvalidScale;
268 while (true) {
269 index = this->chooseCodec(dstInfo.dimensions(), index);
270 if (index < 0) {
271 break;
msarett1603e932015-12-04 05:43:09 -0800272 }
msarettbe8216a2015-12-04 08:00:50 -0800273
Ben Wagner145dbcd2016-11-03 14:40:50 -0400274 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000275 result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts);
msarettbe8216a2015-12-04 08:00:50 -0800276 switch (result) {
277 case kSuccess:
278 case kIncompleteInput:
279 // The embedded codec will handle filling incomplete images, so we will indicate
280 // that all of the rows are initialized.
msarettf4004f92016-02-11 10:49:31 -0800281 *rowsDecoded = dstInfo.height();
msarettbe8216a2015-12-04 08:00:50 -0800282 return result;
283 default:
284 // Continue trying to find a valid embedded codec on a failed decode.
285 break;
286 }
287
288 index++;
msarett1603e932015-12-04 05:43:09 -0800289 }
290
291 SkCodecPrintf("Error: No matching candidate image in ico.\n");
292 return result;
293}
msarettbe8216a2015-12-04 08:00:50 -0800294
295SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000296 const SkCodec::Options& options) {
msarettbe8216a2015-12-04 08:00:50 -0800297 int index = 0;
298 SkCodec::Result result = kInvalidScale;
299 while (true) {
300 index = this->chooseCodec(dstInfo.dimensions(), index);
301 if (index < 0) {
302 break;
303 }
304
Ben Wagner145dbcd2016-11-03 14:40:50 -0400305 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
Leon Scroggins571b30f2017-07-11 17:35:31 +0000306 result = embeddedCodec->startScanlineDecode(dstInfo, &options);
msarettbe8216a2015-12-04 08:00:50 -0800307 if (kSuccess == result) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530308 fCurrCodec = embeddedCodec;
msarettbe8216a2015-12-04 08:00:50 -0800309 return result;
310 }
311
312 index++;
313 }
314
315 SkCodecPrintf("Error: No matching candidate image in ico.\n");
316 return result;
317}
318
319int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530320 SkASSERT(fCurrCodec);
321 return fCurrCodec->getScanlines(dst, count, rowBytes);
msarettbe8216a2015-12-04 08:00:50 -0800322}
323
324bool SkIcoCodec::onSkipScanlines(int count) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530325 SkASSERT(fCurrCodec);
326 return fCurrCodec->skipScanlines(count);
msarettbe8216a2015-12-04 08:00:50 -0800327}
328
scroggo8e6c7ad2016-09-16 08:20:38 -0700329SkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000330 void* pixels, size_t rowBytes, const SkCodec::Options& options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700331 int index = 0;
332 while (true) {
333 index = this->chooseCodec(dstInfo.dimensions(), index);
334 if (index < 0) {
335 break;
336 }
337
Ben Wagner145dbcd2016-11-03 14:40:50 -0400338 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
scroggo8e6c7ad2016-09-16 08:20:38 -0700339 switch (embeddedCodec->startIncrementalDecode(dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000340 pixels, rowBytes, &options)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700341 case kSuccess:
nagarajan.n477e9d42017-09-25 18:13:06 +0530342 fCurrCodec = embeddedCodec;
scroggo8e6c7ad2016-09-16 08:20:38 -0700343 return kSuccess;
344 case kUnimplemented:
345 // FIXME: embeddedCodec is a BMP. If scanline decoding would work,
346 // return kUnimplemented so that SkSampledCodec will fall through
347 // to use the scanline decoder.
348 // Note that calling startScanlineDecode will require an extra
349 // rewind. The embedded codec has an SkMemoryStream, which is
350 // cheap to rewind, though it will do extra work re-reading the
351 // header.
352 // Also note that we pass nullptr for Options. This is because
353 // Options that are valid for incremental decoding may not be
354 // valid for scanline decoding.
355 // Once BMP supports incremental decoding this workaround can go
356 // away.
Leon Scroggins571b30f2017-07-11 17:35:31 +0000357 if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700358 return kUnimplemented;
359 }
360 // Move on to the next embedded codec.
361 break;
362 default:
363 break;
364 }
365
366 index++;
367 }
368
369 SkCodecPrintf("Error: No matching candidate image in ico.\n");
370 return kInvalidScale;
371}
372
373SkCodec::Result SkIcoCodec::onIncrementalDecode(int* rowsDecoded) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530374 SkASSERT(fCurrCodec);
375 return fCurrCodec->incrementalDecode(rowsDecoded);
scroggo8e6c7ad2016-09-16 08:20:38 -0700376}
377
msarettbe8216a2015-12-04 08:00:50 -0800378SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const {
379 // FIXME: This function will possibly return the wrong value if it is called
scroggo8e6c7ad2016-09-16 08:20:38 -0700380 // before startScanlineDecode()/startIncrementalDecode().
nagarajan.n477e9d42017-09-25 18:13:06 +0530381 if (fCurrCodec) {
382 return fCurrCodec->getScanlineOrder();
scroggo8e6c7ad2016-09-16 08:20:38 -0700383 }
384
385 return INHERITED::onGetScanlineOrder();
msarettbe8216a2015-12-04 08:00:50 -0800386}
387
388SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) {
nagarajan.n477e9d42017-09-25 18:13:06 +0530389 if (fCurrCodec) {
390 return fCurrCodec->getSampler(createIfNecessary);
scroggo8e6c7ad2016-09-16 08:20:38 -0700391 }
392
393 return nullptr;
msarettbe8216a2015-12-04 08:00:50 -0800394}