| 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 "Resources.h" | 8 #include "Resources.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkCodec.h" | 10 #include "SkCodec.h" |
| 11 #include "SkMD5.h" | 11 #include "SkMD5.h" |
| 12 #include "SkRandom.h" | 12 #include "SkRandom.h" |
| 13 #include "SkScaledCodec.h" |
| 13 #include "SkScanlineDecoder.h" | 14 #include "SkScanlineDecoder.h" |
| 14 #include "Test.h" | 15 #include "Test.h" |
| 15 | 16 |
| 16 static SkStreamAsset* resource(const char path[]) { | 17 static SkStreamAsset* resource(const char path[]) { |
| 17 SkString fullPath = GetResourcePath(path); | 18 SkString fullPath = GetResourcePath(path); |
| 18 return SkStream::NewFromFile(fullPath.c_str()); | 19 return SkStream::NewFromFile(fullPath.c_str()); |
| 19 } | 20 } |
| 20 | 21 |
| 21 static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { | 22 static void md5(const SkBitmap& bm, SkMD5::Digest* digest) { |
| 22 SkAutoLockPixels autoLockPixels(bm); | 23 SkAutoLockPixels autoLockPixels(bm); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 test_invalid_stream(r, emptyGif, sizeof(emptyGif)); | 275 test_invalid_stream(r, emptyGif, sizeof(emptyGif)); |
| 275 } | 276 } |
| 276 | 277 |
| 277 static void test_dimensions(skiatest::Reporter* r, const char path[]) { | 278 static void test_dimensions(skiatest::Reporter* r, const char path[]) { |
| 278 // Create the codec from the resource file | 279 // Create the codec from the resource file |
| 279 SkAutoTDelete<SkStream> stream(resource(path)); | 280 SkAutoTDelete<SkStream> stream(resource(path)); |
| 280 if (!stream) { | 281 if (!stream) { |
| 281 SkDebugf("Missing resource '%s'\n", path); | 282 SkDebugf("Missing resource '%s'\n", path); |
| 282 return; | 283 return; |
| 283 } | 284 } |
| 284 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); | 285 SkAutoTDelete<SkCodec> codec(SkScaledCodec::NewFromStream(stream.detach())); |
| 285 if (!codec) { | 286 if (!codec) { |
| 286 ERRORF(r, "Unable to create codec '%s'", path); | 287 ERRORF(r, "Unable to create codec '%s'", path); |
| 287 return; | 288 return; |
| 288 } | 289 } |
| 289 | 290 |
| 290 // Check that the decode is successful for a variety of scales | 291 // Check that the decode is successful for a variety of scales |
| 291 for (float scale = -0.05f; scale < 2.0f; scale += 0.05f) { | 292 for (float scale = 0.05f; scale < 2.0f; scale += 0.05f) { |
| 292 // Scale the output dimensions | 293 // Scale the output dimensions |
| 293 SkISize scaledDims = codec->getScaledDimensions(scale); | 294 SkISize scaledDims = codec->getScaledDimensions(scale); |
| 294 SkImageInfo scaledInfo = codec->getInfo().makeWH(scaledDims.width(), sca
ledDims.height()); | 295 SkImageInfo scaledInfo = codec->getInfo() |
| 296 .makeWH(scaledDims.width(), scaledDims.height()) |
| 297 .makeColorType(kN32_SkColorType); |
| 295 | 298 |
| 296 // Set up for the decode | 299 // Set up for the decode |
| 297 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); | 300 size_t rowBytes = scaledDims.width() * sizeof(SkPMColor); |
| 298 size_t totalBytes = scaledInfo.getSafeSize(rowBytes); | 301 size_t totalBytes = scaledInfo.getSafeSize(rowBytes); |
| 299 SkAutoTMalloc<SkPMColor> pixels(totalBytes); | 302 SkAutoTMalloc<SkPMColor> pixels(totalBytes); |
| 300 | 303 |
| 301 SkCodec::Result result = | 304 SkCodec::Result result = |
| 302 codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL,
NULL); | 305 codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL,
NULL); |
| 303 REPORTER_ASSERT(r, SkCodec::kSuccess == result); | 306 REPORTER_ASSERT(r, SkCodec::kSuccess == result); |
| 304 } | 307 } |
| 305 } | 308 } |
| 306 | 309 |
| 307 // Ensure that onGetScaledDimensions returns valid image dimensions to use for d
ecodes | 310 // Ensure that onGetScaledDimensions returns valid image dimensions to use for d
ecodes |
| 308 DEF_TEST(Codec_Dimensions, r) { | 311 DEF_TEST(Codec_Dimensions, r) { |
| 309 // JPG | 312 // JPG |
| 310 test_dimensions(r, "CMYK.jpg"); | 313 test_dimensions(r, "CMYK.jpg"); |
| 311 test_dimensions(r, "color_wheel.jpg"); | 314 test_dimensions(r, "color_wheel.jpg"); |
| 312 test_dimensions(r, "grayscale.jpg"); | 315 test_dimensions(r, "grayscale.jpg"); |
| 313 test_dimensions(r, "mandrill_512_q075.jpg"); | 316 test_dimensions(r, "mandrill_512_q075.jpg"); |
| 314 test_dimensions(r, "randPixels.jpg"); | 317 test_dimensions(r, "randPixels.jpg"); |
| 318 |
| 319 // Decoding small images with very large scaling factors is a potential |
| 320 // source of bugs and crashes. We disable these tests in Gold because |
| 321 // tiny images are not very useful to look at. |
| 322 // Here we make sure that we do not crash or access illegal memory when |
| 323 // performing scaled decodes on small images. |
| 324 test_dimensions(r, "1x1.png"); |
| 325 test_dimensions(r, "2x2.png"); |
| 326 test_dimensions(r, "3x3.png"); |
| 327 test_dimensions(r, "3x1.png"); |
| 328 test_dimensions(r, "1x1.png"); |
| 329 test_dimensions(r, "16x1.png"); |
| 330 test_dimensions(r, "1x16.png"); |
| 331 test_dimensions(r, "mandrill_16.png"); |
| 332 |
| 315 } | 333 } |
| 316 | 334 |
| 317 static void test_invalid(skiatest::Reporter* r, const char path[]) { | 335 static void test_invalid(skiatest::Reporter* r, const char path[]) { |
| 318 SkAutoTDelete<SkStream> stream(resource(path)); | 336 SkAutoTDelete<SkStream> stream(resource(path)); |
| 319 if (!stream) { | 337 if (!stream) { |
| 320 SkDebugf("Missing resource '%s'\n", path); | 338 SkDebugf("Missing resource '%s'\n", path); |
| 321 return; | 339 return; |
| 322 } | 340 } |
| 323 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); | 341 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 324 REPORTER_ASSERT(r, NULL == codec); | 342 REPORTER_ASSERT(r, NULL == codec); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 385 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 368 result = decoder->start( | 386 result = decoder->start( |
| 369 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); | 387 decoder->getInfo().makeColorType(kIndex_8_SkColorType)); |
| 370 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); | 388 REPORTER_ASSERT(r, SkCodec::kInvalidParameters == result); |
| 371 } | 389 } |
| 372 | 390 |
| 373 DEF_TEST(Codec_Params, r) { | 391 DEF_TEST(Codec_Params, r) { |
| 374 test_invalid_parameters(r, "index8.png"); | 392 test_invalid_parameters(r, "index8.png"); |
| 375 test_invalid_parameters(r, "mandrill.wbmp"); | 393 test_invalid_parameters(r, "mandrill.wbmp"); |
| 376 } | 394 } |
| OLD | NEW |