blob: 0e81b72407218778b9d469e72b95941149b83309 [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
msarettd0be5bb2015-03-25 06:29:18 -0700131 SkAutoTUnref<SkData> data(
132 SkData::NewFromStream(inputStream.get(), size));
halcanary96fcdcc2015-08-27 07:41:13 -0700133 if (nullptr == data.get()) {
scroggo230d4ac2015-03-26 07:15:55 -0700134 SkCodecPrintf("Warning: could not create embedded stream.\n");
msarett9bde9182015-03-25 05:27:48 -0700135 break;
136 }
halcanary385fe4d2015-08-26 13:07:48 -0700137 SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data.get()));
msarett9bde9182015-03-25 05:27:48 -0700138 bytesRead += size;
139
140 // Check if the embedded codec is bmp or png and create the codec
halcanary96fcdcc2015-08-27 07:41:13 -0700141 SkCodec* codec = nullptr;
scroggodb30be22015-12-08 18:54:13 -0800142 if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
mtklein18300a32016-03-16 13:53:35 -0700143 codec = SkPngCodec::NewFromStream(embeddedStream.release());
msarett9bde9182015-03-25 05:27:48 -0700144 } else {
mtklein18300a32016-03-16 13:53:35 -0700145 codec = SkBmpCodec::NewFromIco(embeddedStream.release());
msarett9bde9182015-03-25 05:27:48 -0700146 }
147
148 // Save a valid codec
halcanary96fcdcc2015-08-27 07:41:13 -0700149 if (nullptr != codec) {
msarett9bde9182015-03-25 05:27:48 -0700150 codecs->push_back().reset(codec);
151 }
152 }
153
154 // Recognize if there are no valid codecs
155 if (0 == codecs->count()) {
scroggo230d4ac2015-03-26 07:15:55 -0700156 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700157 return nullptr;
msarett9bde9182015-03-25 05:27:48 -0700158 }
159
160 // Use the largest codec as a "suggestion" for image info
161 uint32_t maxSize = 0;
162 uint32_t maxIndex = 0;
163 for (int32_t i = 0; i < codecs->count(); i++) {
164 SkImageInfo info = codecs->operator[](i)->getInfo();
165 uint32_t size = info.width() * info.height();
166 if (size > maxSize) {
167 maxSize = size;
168 maxIndex = i;
169 }
170 }
msarettc30c4182016-04-20 11:53:35 -0700171 int width = codecs->operator[](maxIndex)->getInfo().width();
172 int height = codecs->operator[](maxIndex)->getInfo().height();
173 SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo();
msarett9bde9182015-03-25 05:27:48 -0700174
175 // Note that stream is owned by the embedded codec, the ico does not need
176 // direct access to the stream.
msarettc30c4182016-04-20 11:53:35 -0700177 return new SkIcoCodec(width, height, info, codecs.release());
msarett9bde9182015-03-25 05:27:48 -0700178}
179
180/*
181 * Creates an instance of the decoder
182 * Called only by NewFromStream
183 */
msarettc30c4182016-04-20 11:53:35 -0700184SkIcoCodec::SkIcoCodec(int width, int height, const SkEncodedInfo& info,
msarett9bde9182015-03-25 05:27:48 -0700185 SkTArray<SkAutoTDelete<SkCodec>, true>* codecs)
msarettc30c4182016-04-20 11:53:35 -0700186 : INHERITED(width, height, info, nullptr)
msarett9bde9182015-03-25 05:27:48 -0700187 , fEmbeddedCodecs(codecs)
msarettbe8216a2015-12-04 08:00:50 -0800188 , fCurrScanlineCodec(nullptr)
msarett9bde9182015-03-25 05:27:48 -0700189{}
190
191/*
192 * Chooses the best dimensions given the desired scale
193 */
194SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const {
195 // We set the dimensions to the largest candidate image by default.
196 // Regardless of the scale request, this is the largest image that we
197 // will decode.
msarett9bde9182015-03-25 05:27:48 -0700198 int origWidth = this->getInfo().width();
199 int origHeight = this->getInfo().height();
200 float desiredSize = desiredScale * origWidth * origHeight;
201 // At least one image will have smaller error than this initial value
202 float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f;
203 int32_t minIndex = -1;
204 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) {
205 int width = fEmbeddedCodecs->operator[](i)->getInfo().width();
206 int height = fEmbeddedCodecs->operator[](i)->getInfo().height();
207 float error = SkTAbs(((float) (width * height)) - desiredSize);
208 if (error < minError) {
209 minError = error;
210 minIndex = i;
211 }
212 }
213 SkASSERT(minIndex >= 0);
214
215 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions();
216}
217
msarettbe8216a2015-12-04 08:00:50 -0800218int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) {
219 SkASSERT(startIndex >= 0);
220
scroggoe7fc14b2015-10-02 13:14:46 -0700221 // FIXME: Cache the index from onGetScaledDimensions?
msarettbe8216a2015-12-04 08:00:50 -0800222 for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) {
223 if (fEmbeddedCodecs->operator[](i)->getInfo().dimensions() == requestedSize) {
224 return i;
scroggoe7fc14b2015-10-02 13:14:46 -0700225 }
226 }
227
msarettbe8216a2015-12-04 08:00:50 -0800228 return -1;
229}
230
231bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) {
232 return this->chooseCodec(dim, 0) >= 0;
scroggoe7fc14b2015-10-02 13:14:46 -0700233}
234
msarett9bde9182015-03-25 05:27:48 -0700235/*
236 * Initiates the Ico decode
237 */
238SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
239 void* dst, size_t dstRowBytes,
msarette6dd0042015-10-09 11:07:34 -0700240 const Options& opts, SkPMColor* colorTable,
241 int* colorCount, int* rowsDecoded) {
scroggob636b452015-07-22 07:16:20 -0700242 if (opts.fSubset) {
243 // Subsets are not supported.
244 return kUnimplemented;
245 }
scroggocc2feb12015-08-14 08:32:46 -0700246
msarettbe8216a2015-12-04 08:00:50 -0800247 int index = 0;
248 SkCodec::Result result = kInvalidScale;
249 while (true) {
250 index = this->chooseCodec(dstInfo.dimensions(), index);
251 if (index < 0) {
252 break;
msarett1603e932015-12-04 05:43:09 -0800253 }
msarettbe8216a2015-12-04 08:00:50 -0800254
255 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index);
msarettf4004f92016-02-11 10:49:31 -0800256 result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts, colorTable,
msarettbe8216a2015-12-04 08:00:50 -0800257 colorCount);
258
259 switch (result) {
260 case kSuccess:
261 case kIncompleteInput:
262 // The embedded codec will handle filling incomplete images, so we will indicate
263 // that all of the rows are initialized.
msarettf4004f92016-02-11 10:49:31 -0800264 *rowsDecoded = dstInfo.height();
msarettbe8216a2015-12-04 08:00:50 -0800265 return result;
266 default:
267 // Continue trying to find a valid embedded codec on a failed decode.
268 break;
269 }
270
271 index++;
msarett1603e932015-12-04 05:43:09 -0800272 }
273
274 SkCodecPrintf("Error: No matching candidate image in ico.\n");
275 return result;
276}
msarettbe8216a2015-12-04 08:00:50 -0800277
278SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
279 const SkCodec::Options& options, SkPMColor colorTable[], int* colorCount) {
msarettbe8216a2015-12-04 08:00:50 -0800280 int index = 0;
281 SkCodec::Result result = kInvalidScale;
282 while (true) {
283 index = this->chooseCodec(dstInfo.dimensions(), index);
284 if (index < 0) {
285 break;
286 }
287
288 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index);
msarettf4004f92016-02-11 10:49:31 -0800289 result = embeddedCodec->startScanlineDecode(dstInfo, &options, colorTable, colorCount);
msarettbe8216a2015-12-04 08:00:50 -0800290 if (kSuccess == result) {
291 fCurrScanlineCodec = embeddedCodec;
292 return result;
293 }
294
295 index++;
296 }
297
298 SkCodecPrintf("Error: No matching candidate image in ico.\n");
299 return result;
300}
301
302int SkIcoCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
303 SkASSERT(fCurrScanlineCodec);
304 return fCurrScanlineCodec->getScanlines(dst, count, rowBytes);
305}
306
307bool SkIcoCodec::onSkipScanlines(int count) {
308 SkASSERT(fCurrScanlineCodec);
309 return fCurrScanlineCodec->skipScanlines(count);
310}
311
312SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const {
313 // FIXME: This function will possibly return the wrong value if it is called
scroggo26694c32016-06-01 12:08:23 -0700314 // before startScanlineDecode().
315 return fCurrScanlineCodec ? fCurrScanlineCodec->getScanlineOrder() :
316 INHERITED::onGetScanlineOrder();
msarettbe8216a2015-12-04 08:00:50 -0800317}
318
319SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) {
scroggo26694c32016-06-01 12:08:23 -0700320 return fCurrScanlineCodec ? fCurrScanlineCodec->getSampler(createIfNecessary) : nullptr;
msarettbe8216a2015-12-04 08:00:50 -0800321}