blob: 45000b636fe4b04700633cbb0cb947de61607d3b [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"
10#include "SkColorPriv.h"
11#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
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 */
34SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) {
msarettd0be5bb2015-03-25 06:29:18 -070035 // Ensure that we do not leak the input stream
36 SkAutoTDelete<SkStream> inputStream(stream);
37
msarett9bde9182015-03-25 05:27:48 -070038 // Header size constants
39 static const uint32_t kIcoDirectoryBytes = 6;
40 static const uint32_t kIcoDirEntryBytes = 16;
41
42 // Read the directory header
halcanary385fe4d2015-08-26 13:07:48 -070043 SkAutoTDeleteArray<uint8_t> dirBuffer(new uint8_t[kIcoDirectoryBytes]);
msarettd0be5bb2015-03-25 06:29:18 -070044 if (inputStream.get()->read(dirBuffer.get(), kIcoDirectoryBytes) !=
msarett9bde9182015-03-25 05:27:48 -070045 kIcoDirectoryBytes) {
scroggo230d4ac2015-03-26 07:15:55 -070046 SkCodecPrintf("Error: unable to read ico directory header.\n");
halcanary96fcdcc2015-08-27 07:41:13 -070047 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070048 }
49
50 // Process the directory header
51 const uint16_t numImages = get_short(dirBuffer.get(), 4);
52 if (0 == numImages) {
scroggo230d4ac2015-03-26 07:15:55 -070053 SkCodecPrintf("Error: No images embedded in ico.\n");
halcanary96fcdcc2015-08-27 07:41:13 -070054 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070055 }
56
57 // Ensure that we can read all of indicated directory entries
halcanary385fe4d2015-08-26 13:07:48 -070058 SkAutoTDeleteArray<uint8_t> entryBuffer(new uint8_t[numImages * kIcoDirEntryBytes]);
msarettd0be5bb2015-03-25 06:29:18 -070059 if (inputStream.get()->read(entryBuffer.get(), numImages*kIcoDirEntryBytes) !=
msarett9bde9182015-03-25 05:27:48 -070060 numImages*kIcoDirEntryBytes) {
scroggo230d4ac2015-03-26 07:15:55 -070061 SkCodecPrintf("Error: unable to read ico directory entries.\n");
halcanary96fcdcc2015-08-27 07:41:13 -070062 return nullptr;
msarett9bde9182015-03-25 05:27:48 -070063 }
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 };
halcanary385fe4d2015-08-26 13:07:48 -070072 SkAutoTDeleteArray<Entry> directoryEntries(new Entry[numImages]);
msarett9bde9182015-03-25 05:27:48 -070073
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(
halcanary385fe4d2015-08-26 13:07:48 -0700111 new (SkTArray<SkAutoTDelete<SkCodec>, true>)(numImages));
msarett9bde9182015-03-25 05:27:48 -0700112 for (uint32_t i = 0; i < numImages; i++) {
113 uint32_t offset = directoryEntries.get()[i].offset;
114 uint32_t size = directoryEntries.get()[i].size;
msarette6dd0042015-10-09 11:07:34 -0700115
msarett9bde9182015-03-25 05:27:48 -0700116 // Ensure that the offset is valid
117 if (offset < bytesRead) {
scroggo230d4ac2015-03-26 07:15:55 -0700118 SkCodecPrintf("Warning: invalid ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700119 continue;
120 }
121
122 // If we cannot skip, assume we have reached the end of the stream and
123 // stop trying to make codecs
msarettd0be5bb2015-03-25 06:29:18 -0700124 if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) {
scroggo230d4ac2015-03-26 07:15:55 -0700125 SkCodecPrintf("Warning: could not skip to ico offset.\n");
msarett9bde9182015-03-25 05:27:48 -0700126 break;
127 }
128 bytesRead = offset;
129
130 // Create a new stream for the embedded codec
bungeman38d909e2016-08-02 14:40:46 -0700131 sk_sp<SkData> data(SkData::MakeFromStream(inputStream.get(), size));
halcanary96fcdcc2015-08-27 07:41:13 -0700132 if (nullptr == data.get()) {
scroggo230d4ac2015-03-26 07:15:55 -0700133 SkCodecPrintf("Warning: could not create embedded stream.\n");
msarett9bde9182015-03-25 05:27:48 -0700134 break;
135 }
bungeman38d909e2016-08-02 14:40:46 -0700136 SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data));
msarett9bde9182015-03-25 05:27:48 -0700137 bytesRead += size;
138
139 // Check if the embedded codec is bmp or png and create the codec
halcanary96fcdcc2015-08-27 07:41:13 -0700140 SkCodec* codec = nullptr;
scroggodb30be22015-12-08 18:54:13 -0800141 if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
mtklein18300a32016-03-16 13:53:35 -0700142 codec = SkPngCodec::NewFromStream(embeddedStream.release());
msarett9bde9182015-03-25 05:27:48 -0700143 } else {
mtklein18300a32016-03-16 13:53:35 -0700144 codec = SkBmpCodec::NewFromIco(embeddedStream.release());
msarett9bde9182015-03-25 05:27:48 -0700145 }
146
147 // Save a valid codec
halcanary96fcdcc2015-08-27 07:41:13 -0700148 if (nullptr != codec) {
msarett9bde9182015-03-25 05:27:48 -0700149 codecs->push_back().reset(codec);
150 }
151 }
152
153 // Recognize if there are no valid codecs
154 if (0 == codecs->count()) {
scroggo230d4ac2015-03-26 07:15:55 -0700155 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700156 return nullptr;
msarett9bde9182015-03-25 05:27:48 -0700157 }
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 }
msarettc30c4182016-04-20 11:53:35 -0700170 int width = codecs->operator[](maxIndex)->getInfo().width();
171 int height = codecs->operator[](maxIndex)->getInfo().height();
172 SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo();
msarett9bde9182015-03-25 05:27:48 -0700173
174 // Note that stream is owned by the embedded codec, the ico does not need
175 // direct access to the stream.
msarettc30c4182016-04-20 11:53:35 -0700176 return new SkIcoCodec(width, height, info, codecs.release());
msarett9bde9182015-03-25 05:27:48 -0700177}
178
179/*
180 * Creates an instance of the decoder
181 * Called only by NewFromStream
182 */
msarettc30c4182016-04-20 11:53:35 -0700183SkIcoCodec::SkIcoCodec(int width, int height, const SkEncodedInfo& info,
msarett9bde9182015-03-25 05:27:48 -0700184 SkTArray<SkAutoTDelete<SkCodec>, true>* codecs)
msarettc30c4182016-04-20 11:53:35 -0700185 : INHERITED(width, height, info, nullptr)
msarett9bde9182015-03-25 05:27:48 -0700186 , fEmbeddedCodecs(codecs)
msarettbe8216a2015-12-04 08:00:50 -0800187 , fCurrScanlineCodec(nullptr)
msarett9bde9182015-03-25 05:27:48 -0700188{}
189
190/*
191 * Chooses the best dimensions given the desired scale
192 */
193SkISize 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.
msarett9bde9182015-03-25 05:27:48 -0700197 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
msarettbe8216a2015-12-04 08:00:50 -0800217int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) {
218 SkASSERT(startIndex >= 0);
219
scroggoe7fc14b2015-10-02 13:14:46 -0700220 // FIXME: Cache the index from onGetScaledDimensions?
msarettbe8216a2015-12-04 08:00:50 -0800221 for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) {
222 if (fEmbeddedCodecs->operator[](i)->getInfo().dimensions() == requestedSize) {
223 return i;
scroggoe7fc14b2015-10-02 13:14:46 -0700224 }
225 }
226
msarettbe8216a2015-12-04 08:00:50 -0800227 return -1;
228}
229
230bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) {
231 return this->chooseCodec(dim, 0) >= 0;
scroggoe7fc14b2015-10-02 13:14:46 -0700232}
233
msarett9bde9182015-03-25 05:27:48 -0700234/*
235 * Initiates the Ico decode
236 */
237SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
238 void* dst, size_t dstRowBytes,
msarette6dd0042015-10-09 11:07:34 -0700239 const Options& opts, SkPMColor* colorTable,
240 int* colorCount, int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700241 if (opts.fSubset) {
242 // Subsets are not supported.
243 return kUnimplemented;
244 }
scroggocc2feb12015-08-14 08:32:46 -0700245
msarettbe8216a2015-12-04 08:00:50 -0800246 int index = 0;
247 SkCodec::Result result = kInvalidScale;
248 while (true) {
249 index = this->chooseCodec(dstInfo.dimensions(), index);
250 if (index < 0) {
251 break;
msarett1603e932015-12-04 05:43:09 -0800252 }
msarettbe8216a2015-12-04 08:00:50 -0800253
254 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index);
msarettf4004f92016-02-11 10:49:31 -0800255 result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts, colorTable,
msarettbe8216a2015-12-04 08:00:50 -0800256 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.
msarettf4004f92016-02-11 10:49:31 -0800263 *rowsDecoded = dstInfo.height();
msarettbe8216a2015-12-04 08:00:50 -0800264 return result;
265 default:
266 // Continue trying to find a valid embedded codec on a failed decode.
267 break;
268 }
269
270 index++;
msarett1603e932015-12-04 05:43:09 -0800271 }
272
273 SkCodecPrintf("Error: No matching candidate image in ico.\n");
274 return result;
275}
msarettbe8216a2015-12-04 08:00:50 -0800276
277SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
278 const SkCodec::Options& options, SkPMColor colorTable[], int* colorCount) {
msarettbe8216a2015-12-04 08:00:50 -0800279 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);
msarettf4004f92016-02-11 10:49:31 -0800288 result = embeddedCodec->startScanlineDecode(dstInfo, &options, colorTable, colorCount);
msarettbe8216a2015-12-04 08:00:50 -0800289 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
301int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
302 SkASSERT(fCurrScanlineCodec);
303 return fCurrScanlineCodec->getScanlines(dst, count, rowBytes);
304}
305
306bool SkIcoCodec::onSkipScanlines(int count) {
307 SkASSERT(fCurrScanlineCodec);
308 return fCurrScanlineCodec->skipScanlines(count);
309}
310
311SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const {
312 // FIXME: This function will possibly return the wrong value if it is called
scroggod8d68552016-06-06 11:26:17 -0700313 // before startScanlineDecode().
314 return fCurrScanlineCodec ? fCurrScanlineCodec->getScanlineOrder() :
315 INHERITED::onGetScanlineOrder();
msarettbe8216a2015-12-04 08:00:50 -0800316}
317
318SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) {
scroggod8d68552016-06-06 11:26:17 -0700319 return fCurrScanlineCodec ? fCurrScanlineCodec->getSampler(createIfNecessary) : nullptr;
msarettbe8216a2015-12-04 08:00:50 -0800320}