Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(337)

Side by Side Diff: src/codec/SkCodec_libico.cpp

Issue 1011343003: Enabling ico decoding with use of png and bmp decoders (Closed) Base URL: https://skia.googlesource.com/skia.git@swizzle
Patch Set: Using appropriate Sk data types for array of codecs, removing getOriginalInfo Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "SkCodec_libbmp.h"
9 #include "SkCodec_libico.h"
10 #include "SkCodec_libpng.h"
11 #include "SkCodecPriv.h"
12 #include "SkColorPriv.h"
13 #include "SkData.h"
14 #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 */
21 bool SkIcoCodec::IsIco(SkStream* stream) {
22 const char icoSig[] = { '\x00', '\x00', '\x01', '\x00' };
23 const char curSig[] = { '\x00', '\x00', '\x02', '\x00' };
24 char buffer[sizeof(icoSig)];
25 return stream->read(buffer, sizeof(icoSig)) == sizeof(icoSig) &&
26 (!memcmp(buffer, icoSig, sizeof(icoSig)) ||
27 !memcmp(buffer, curSig, sizeof(curSig)));
28 }
29
30 /*
31 * Assumes IsIco was called and returned true
32 * Creates an Ico decoder
33 * Reads enough of the stream to determine the image format
34 */
35 SkCodec* SkIcoCodec::NewFromStream(SkStream* stream) {
36 // Header size constants
37 static const uint32_t kIcoDirectoryBytes = 6;
38 static const uint32_t kIcoDirEntryBytes = 16;
39
40 // Read the directory header
41 SkAutoTDeleteArray<uint8_t> dirBuffer(
42 SkNEW_ARRAY(uint8_t, kIcoDirectoryBytes));
43 if (stream->read(dirBuffer.get(), kIcoDirectoryBytes) !=
44 kIcoDirectoryBytes) {
45 SkDebugf("Error: unable to read ico directory header.\n");
46 return NULL;
47 }
48
49 // Process the directory header
50 const uint16_t numImages = get_short(dirBuffer.get(), 4);
51 if (0 == numImages) {
52 SkDebugf("Error: No images embedded in ico.\n");
53 return NULL;
54 }
55
56 // Ensure that we can read all of indicated directory entries
57 SkAutoTDeleteArray<uint8_t> entryBuffer(
58 SkNEW_ARRAY(uint8_t, numImages*kIcoDirEntryBytes));
59 if (stream->read(entryBuffer.get(), numImages*kIcoDirEntryBytes) !=
60 numImages*kIcoDirEntryBytes) {
61 SkDebugf("Error: unable to read ico directory entries.\n");
62 return NULL;
63 }
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 };
72 SkAutoTDeleteArray<Entry> directoryEntries(SkNEW_ARRAY(Entry, numImages));
73
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(
111 SkNEW_ARGS((SkTArray<SkAutoTDelete<SkCodec>, true>), (numImages)));
112 for (uint32_t i = 0; i < numImages; i++) {
113 uint32_t offset = directoryEntries.get()[i].offset;
114 uint32_t size = directoryEntries.get()[i].size;
115
116 // Ensure that the offset is valid
117 if (offset < bytesRead) {
118 SkDebugf("Warning: invalid ico offset.\n");
119 continue;
120 }
121
122 // If we cannot skip, assume we have reached the end of the stream and
123 // stop trying to make codecs
124 if (stream->skip(offset - bytesRead) != offset - bytesRead) {
125 SkDebugf("Warning: could not skip to ico offset.\n");
126 break;
127 }
128 bytesRead = offset;
129
130 // Create a new stream for the embedded codec
131 SkAutoTUnref<SkData> data(SkData::NewFromStream(stream, size));
132 if (NULL == data.get()) {
133 SkDebugf("Warning: could not create embedded stream.\n");
134 break;
135 }
136 SkAutoTDelete<SkMemoryStream>
137 embeddedStream(SkNEW_ARGS(SkMemoryStream, (data.get())));
138 bytesRead += size;
139
140 // Check if the embedded codec is bmp or png and create the codec
141 const bool isPng = SkPngCodec::IsPng(embeddedStream);
142 SkAssertResult(embeddedStream->rewind());
143 SkCodec* codec = NULL;
144 if (isPng) {
145 codec = SkPngCodec::NewFromStream(embeddedStream.detach());
146 } else {
147 codec = SkBmpCodec::NewFromIco(embeddedStream.detach());
148 }
149
150 // Save a valid codec
151 if (NULL != codec) {
152 codecs->push_back();
153 codecs->operator[](codecs->count() - 1).reset(codec);
scroggo 2015/03/23 20:34:51 push_back returns a T&, so you can combine these t
msarett 2015/03/24 13:08:37 Nice, that's convenient!
154 }
155 }
156
157 // Recognize if there are no valid codecs
158 if (0 == codecs->count()) {
159 SkDebugf("Error: could not find any valid embedded ico codecs.\n");
160 return NULL;
161 }
162
163 // Use the largest codec as a "suggestion" for image info
164 uint32_t maxSize = 0;
165 uint32_t maxIndex = 0;
166 for (int32_t i = 0; i < codecs->count(); i++) {
167 SkImageInfo info = codecs->operator[](i)->getInfo();
168 uint32_t size = info.width() * info.height();
169 if (size > maxSize) {
170 maxSize = size;
171 maxIndex = i;
172 }
173 }
174 SkImageInfo info = codecs->operator[](maxIndex)->getInfo();
175
176 // Note that stream is owned by the embedded codec, the ico does not need
177 // direct access to the stream.
178 return SkNEW_ARGS(SkIcoCodec, (info, codecs.detach()));
179 }
180
181 /*
182 * Creates an instance of the decoder
183 * Called only by NewFromStream
184 */
185 SkIcoCodec::SkIcoCodec(const SkImageInfo& info,
186 SkTArray<SkAutoTDelete<SkCodec>, true>* codecs)
187 : INHERITED(info, NULL)
188 , fEmbeddedCodecs(codecs)
189 {}
190
191 /*
192 * Chooses the best dimensions given the desired scale
193 */
194 SkISize 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.
198 if (desiredScale >= 1.0) {
199 return this->getInfo().dimensions();
200 }
201
202 int origWidth = this->getInfo().width();
203 int origHeight = this->getInfo().height();
204 float desiredSize = desiredScale * origWidth * origHeight;
205 // At least one image will have smaller error than this initial value
206 float minError = origWidth * origHeight - desiredSize + 1.0;
207 int32_t minIndex = -1;
208 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) {
209 int width = fEmbeddedCodecs->operator[](i)->getInfo().width();
210 int height = fEmbeddedCodecs->operator[](i)->getInfo().height();
211 float error = SkTAbs(((float) (width * height)) - desiredSize);
212 if (error < minError) {
213 minError = error;
214 minIndex = i;
215 }
216 }
217 SkASSERT(minIndex >= 0);
218
219 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions();
220 }
221
222 /*
223 * Initiates the Ico decode
224 */
225 SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo,
226 void* dst, size_t dstRowBytes,
227 const Options& opts, SkPMColor* ct,
228 int* ptr) {
229 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) {
230 // If the dimensions match, try to decode
231 if (dstInfo.dimensions() ==
232 fEmbeddedCodecs->operator[](i)->getInfo().dimensions()) {
233
234 // Perform the decode
235 Result result = fEmbeddedCodecs->operator[](i)->getPixels(dstInfo,
236 dst, dstRowBytes, &opts, ct, ptr);
237
238 // On a fatal error, keep trying to find an image to decode
239 if (kInvalidConversion == result || kInvalidInput == result) {
scroggo 2015/03/23 20:34:51 Why did you pick these two? I would think if we go
msarett 2015/03/24 13:08:37 I absolutely agree. I assumed that since we were
scroggo 2015/03/24 15:24:42 Oh right, I wasn't thinking. I'm fine with or with
240 SkDebugf("Warning: Attempt to decode candidate ico failed.\n");
241 continue;
242 }
243
244 // On success or partial success, return the result
245 return result;
246 }
247 }
248
249 SkDebugf("Error: No ico candidate image with matching dimensions.\n");
250 return kInvalidScale;
251 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698