OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBmpCodec.h" | 8 #include "SkBmpCodec.h" |
9 #include "SkCodec_libico.h" | 9 #include "SkCodec_libico.h" |
10 #include "SkCodec_libpng.h" | 10 #include "SkCodec_libpng.h" |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 , fEmbeddedCodecs(codecs) | 199 , fEmbeddedCodecs(codecs) |
200 {} | 200 {} |
201 | 201 |
202 /* | 202 /* |
203 * Chooses the best dimensions given the desired scale | 203 * Chooses the best dimensions given the desired scale |
204 */ | 204 */ |
205 SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { | 205 SkISize SkIcoCodec::onGetScaledDimensions(float desiredScale) const { |
206 // We set the dimensions to the largest candidate image by default. | 206 // We set the dimensions to the largest candidate image by default. |
207 // Regardless of the scale request, this is the largest image that we | 207 // Regardless of the scale request, this is the largest image that we |
208 // will decode. | 208 // will decode. |
209 if (desiredScale >= 1.0) { | |
210 return this->getInfo().dimensions(); | |
211 } | |
212 | |
213 int origWidth = this->getInfo().width(); | 209 int origWidth = this->getInfo().width(); |
214 int origHeight = this->getInfo().height(); | 210 int origHeight = this->getInfo().height(); |
215 float desiredSize = desiredScale * origWidth * origHeight; | 211 float desiredSize = desiredScale * origWidth * origHeight; |
216 // At least one image will have smaller error than this initial value | 212 // At least one image will have smaller error than this initial value |
217 float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; | 213 float minError = ((float) (origWidth * origHeight)) - desiredSize + 1.0f; |
218 int32_t minIndex = -1; | 214 int32_t minIndex = -1; |
219 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { | 215 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
220 int width = fEmbeddedCodecs->operator[](i)->getInfo().width(); | 216 int width = fEmbeddedCodecs->operator[](i)->getInfo().width(); |
221 int height = fEmbeddedCodecs->operator[](i)->getInfo().height(); | 217 int height = fEmbeddedCodecs->operator[](i)->getInfo().height(); |
222 float error = SkTAbs(((float) (width * height)) - desiredSize); | 218 float error = SkTAbs(((float) (width * height)) - desiredSize); |
223 if (error < minError) { | 219 if (error < minError) { |
224 minError = error; | 220 minError = error; |
225 minIndex = i; | 221 minIndex = i; |
226 } | 222 } |
227 } | 223 } |
228 SkASSERT(minIndex >= 0); | 224 SkASSERT(minIndex >= 0); |
229 | 225 |
230 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions(); | 226 return fEmbeddedCodecs->operator[](minIndex)->getInfo().dimensions(); |
231 } | 227 } |
232 | 228 |
| 229 bool SkIcoCodec::onDimensionsSupported(const SkISize& dim) { |
| 230 // FIXME: Cache the index from onGetScaledDimensions? |
| 231 for (int32_t i = 0; i < fEmbeddedCodecs->count(); i++) { |
| 232 if (fEmbeddedCodecs->operator[](i)->getInfo().dimensions() == dim) { |
| 233 return true; |
| 234 } |
| 235 } |
| 236 |
| 237 return false; |
| 238 } |
| 239 |
233 /* | 240 /* |
234 * Initiates the Ico decode | 241 * Initiates the Ico decode |
235 */ | 242 */ |
236 SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, | 243 SkCodec::Result SkIcoCodec::onGetPixels(const SkImageInfo& dstInfo, |
237 void* dst, size_t dstRowBytes, | 244 void* dst, size_t dstRowBytes, |
238 const Options& opts, SkPMColor* ct, | 245 const Options& opts, SkPMColor* ct, |
239 int* ptr) { | 246 int* ptr) { |
240 if (opts.fSubset) { | 247 if (opts.fSubset) { |
241 // Subsets are not supported. | 248 // Subsets are not supported. |
242 return kUnimplemented; | 249 return kUnimplemented; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 kInvalidScale == result) { | 286 kInvalidScale == result) { |
280 SkCodecPrintf("Warning: Attempt to decode candidate ico failed.\
n"); | 287 SkCodecPrintf("Warning: Attempt to decode candidate ico failed.\
n"); |
281 continue; | 288 continue; |
282 } | 289 } |
283 | 290 |
284 // On success or partial success, return the result | 291 // On success or partial success, return the result |
285 return result; | 292 return result; |
286 } | 293 } |
287 } | 294 } |
288 | 295 |
| 296 // This should never be reached, since onDimensionsSupported should have rej
ected the |
| 297 // dimensions sooner. |
| 298 SkASSERT(false); |
289 SkCodecPrintf("Error: No matching candidate image in ico.\n"); | 299 SkCodecPrintf("Error: No matching candidate image in ico.\n"); |
290 return result; | 300 return result; |
291 } | 301 } |
OLD | NEW |