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
8#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"
16#include "src/core/SkStreamPriv.h"
17#include "src/core/SkTSort.h"
18
19/*
20 * Checks the start of the stream to see if the image is an Ico or Cur
21 */
22bool SkIcoCodec::IsIco(const void* buffer, size_t bytesRead) {
23 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' };
24 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' };
25 return bytesRead >= sizeof(icoSig) &&
26 (!memcmp(buffer, icoSig, sizeof(icoSig)) ||
27 !memcmp(buffer, curSig, sizeof(curSig)));
28}
29
30std::unique_ptr<SkCodec> SkIcoCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
31 Result* result) {
32 // 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
48 // Header size constants
49 constexpr uint32_t kIcoDirectoryBytes = 6;
50 constexpr uint32_t kIcoDirEntryBytes = 16;
51
52 // Read the directory header
53 if (data->size() < kIcoDirectoryBytes) {
54 SkCodecPrintf("Error: unable to read ico directory header.\n");
55 *result = kIncompleteInput;
56 return nullptr;
57 }
58
59 // Process the directory header
60 const uint16_t numImages = get_short(data->bytes(), 4);
61 if (0 == numImages) {
62 SkCodecPrintf("Error: No images embedded in ico.\n");
63 *result = kInvalidInput;
64 return nullptr;
65 }
66
67 // 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 };
74 SkAutoFree dirEntryBuffer(sk_malloc_canfail(sizeof(Entry) * numImages));
75 if (!dirEntryBuffer) {
76 SkCodecPrintf("Error: OOM allocating ICO directory for %i images.\n",
77 numImages);
78 *result = kInternalError;
79 return nullptr;
80 }
81 auto* directoryEntries = reinterpret_cast<Entry*>(dirEntryBuffer.get());
82
83 // Iterate over directory entries
84 for (uint32_t i = 0; i < numImages; i++) {
85 const uint8_t* entryBuffer = data->bytes() + kIcoDirectoryBytes + i * kIcoDirEntryBytes;
86 if (data->size() < kIcoDirectoryBytes + (i+1) * kIcoDirEntryBytes) {
87 SkCodecPrintf("Error: Dir entries truncated in ico.\n");
88 *result = kIncompleteInput;
89 return nullptr;
90 }
91
92 // 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
99 uint32_t size = get_int(entryBuffer, 8);
100
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.
104 uint32_t offset = get_int(entryBuffer, 12);
105
106 // Save the vital fields
107 directoryEntries[i].offset = offset;
108 directoryEntries[i].size = size;
109 }
110
111 // Default Result, if no valid embedded codecs are found.
112 *result = kInvalidInput;
113
114 // 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.
118 struct EntryLessThan {
119 bool operator() (Entry a, Entry b) const {
120 return a.offset < b.offset;
121 }
122 };
123 EntryLessThan lessThan;
124 SkTQSort(directoryEntries, directoryEntries + numImages, lessThan);
125
126 // Now will construct a candidate codec for each of the embedded images
127 uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes;
128 std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs(
129 new SkTArray<std::unique_ptr<SkCodec>, true>(numImages));
130 for (uint32_t i = 0; i < numImages; i++) {
131 uint32_t offset = directoryEntries[i].offset;
132 uint32_t size = directoryEntries[i].size;
133
134 // Ensure that the offset is valid
135 if (offset < bytesRead) {
136 SkCodecPrintf("Warning: invalid ico offset.\n");
137 continue;
138 }
139
140 // If we cannot skip, assume we have reached the end of the stream and
141 // stop trying to make codecs
142 if (offset >= data->size()) {
143 SkCodecPrintf("Warning: could not skip to ico offset.\n");
144 break;
145 }
146 bytesRead = offset;
147
148 if (offset + size > data->size()) {
149 SkCodecPrintf("Warning: could not create embedded stream.\n");
150 *result = kIncompleteInput;
151 break;
152 }
153
154 sk_sp<SkData> embeddedData(SkData::MakeSubset(data.get(), offset, size));
155 auto embeddedStream = SkMemoryStream::Make(embeddedData);
156 bytesRead += size;
157
158 // Check if the embedded codec is bmp or png and create the codec
159 std::unique_ptr<SkCodec> codec;
160 Result dummyResult;
161 if (SkPngCodec::IsPng(embeddedData->bytes(), embeddedData->size())) {
162 codec = SkPngCodec::MakeFromStream(std::move(embeddedStream), &dummyResult);
163 } else {
164 codec = SkBmpCodec::MakeFromIco(std::move(embeddedStream), &dummyResult);
165 }
166
167 if (nullptr != codec) {
168 codecs->push_back().reset(codec.release());
169 }
170 }
171
172 if (0 == codecs->count()) {
173 SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
174 return nullptr;
175 }
176
177 // Use the largest codec as a "suggestion" for image info
178 size_t maxSize = 0;
179 int maxIndex = 0;
180 for (int i = 0; i < codecs->count(); i++) {
181 SkImageInfo info = codecs->operator[](i)->getInfo();
182 size_t size = info.computeMinByteSize();
183
184 if (size > maxSize) {
185 maxSize = size;
186 maxIndex = i;
187 }
188 }
189
190 auto maxInfo = codecs->operator[](maxIndex)->getEncodedInfo().copy();
191
192 *result = kSuccess;
193 return std::unique_ptr<SkCodec>(new SkIcoCodec(std::move(maxInfo), std::move(stream),
194 codecs.release()));
195}
196
197SkIcoCodec::SkIcoCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
198 SkTArray<std::unique_ptr<SkCodec>, true>* codecs)
199 // The source skcms_PixelFormat will not be used. The embedded
200 // codec's will be used instead.
201 : INHERITED(std::move(info), skcms_PixelFormat(), std::move(stream))
202 , fEmbeddedCodecs(codecs)
203 , fCurrCodec(nullptr)
204{}
205
206/*
207 * Chooses the best dimensions given the desired scale
208 */
209SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const {
210 // 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.
213 int origWidth = this->dimensions().width();
214 int origHeight = this->dimensions().height();
215 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++) {
220 auto dimensions = fEmbeddedCodecs->operator[](i)->dimensions();
221 int width = dimensions.width();
222 int height = dimensions.height();
223 float error = SkTAbs(((float) (width * height)) - desiredSize);
224 if (error < minError) {
225 minError = error;
226 minIndex = i;
227 }
228 }
229 SkASSERT(minIndex >= 0);
230
231 return fEmbeddedCodecs->operator[](minIndex)->dimensions();
232}
233
234int SkIcoCodec::chooseCodec(const SkISize& requestedSize, int startIndex) {
235 SkASSERT(startIndex >= 0);
236
237 // FIXME: Cache the index from onGetScaledDimensions?
238 for (int i = startIndex; i < fEmbeddedCodecs->count(); i++) {
239 if (fEmbeddedCodecs->operator[](i)->dimensions() == requestedSize) {
240 return i;
241 }
242 }
243
244 return -1;
245}
246
247bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) {
248 return this->chooseCodec(dim, 0) >= 0;
249}
250
251/*
252 * Initiates the Ico decode
253 */
254SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
255 void* dst, size_t dstRowBytes,
256 const Options& opts,
257 int* rowsDecoded) {
258 if (opts.fSubset) {
259 // Subsets are not supported.
260 return kUnimplemented;
261 }
262
263 int index = 0;
264 SkCodec::Result result = kInvalidScale;
265 while (true) {
266 index = this->chooseCodec(dstInfo.dimensions(), index);
267 if (index < 0) {
268 break;
269 }
270
271 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
272 result = embeddedCodec->getPixels(dstInfo, dst, dstRowBytes, &opts);
273 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.
278 *rowsDecoded = dstInfo.height();
279 return result;
280 default:
281 // Continue trying to find a valid embedded codec on a failed decode.
282 break;
283 }
284
285 index++;
286 }
287
288 SkCodecPrintf("Error: No matching candidate image in ico.\n");
289 return result;
290}
291
292SkCodec::Result SkIcoCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
293 const SkCodec::Options& options) {
294 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
302 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
303 result = embeddedCodec->startScanlineDecode(dstInfo, &options);
304 if (kSuccess == result) {
305 fCurrCodec = embeddedCodec;
306 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) {
317 SkASSERT(fCurrCodec);
318 return fCurrCodec->getScanlines(dst, count, rowBytes);
319}
320
321bool SkIcoCodec::onSkipScanlines(int count) {
322 SkASSERT(fCurrCodec);
323 return fCurrCodec->skipScanlines(count);
324}
325
326SkCodec::Result SkIcoCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
327 void* pixels, size_t rowBytes, const SkCodec::Options& options) {
328 int index = 0;
329 while (true) {
330 index = this->chooseCodec(dstInfo.dimensions(), index);
331 if (index < 0) {
332 break;
333 }
334
335 SkCodec* embeddedCodec = fEmbeddedCodecs->operator[](index).get();
336 switch (embeddedCodec->startIncrementalDecode(dstInfo,
337 pixels, rowBytes, &options)) {
338 case kSuccess:
339 fCurrCodec = embeddedCodec;
340 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.
354 if (embeddedCodec->startScanlineDecode(dstInfo) == kSuccess) {
355 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) {
371 SkASSERT(fCurrCodec);
372 return fCurrCodec->incrementalDecode(rowsDecoded);
373}
374
375SkCodec::SkScanlineOrder SkIcoCodec::onGetScanlineOrder() const {
376 // FIXME: This function will possibly return the wrong value if it is called
377 // before startScanlineDecode()/startIncrementalDecode().
378 if (fCurrCodec) {
379 return fCurrCodec->getScanlineOrder();
380 }
381
382 return INHERITED::onGetScanlineOrder();
383}
384
385SkSampler* SkIcoCodec::getSampler(bool createIfNecessary) {
386 if (fCurrCodec) {
387 return fCurrCodec->getSampler(createIfNecessary);
388 }
389
390 return nullptr;
391}
392