msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 8 | #include "Resources.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 9 | #include "SkAutoMalloc.h" |
| 10 | #include "SkCodec.h" |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | #include "SkTemplates.h" |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 13 | #include "SkYUVASizeInfo.h" |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 14 | #include "Test.h" |
| 15 | |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 16 | static void codec_yuv(skiatest::Reporter* reporter, |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 17 | const char path[], |
| 18 | SkISize expectedSizes[4]) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 19 | std::unique_ptr<SkStream> stream(GetResourceAsStream(path)); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 20 | if (!stream) { |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 21 | return; |
| 22 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 23 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 24 | REPORTER_ASSERT(reporter, codec); |
| 25 | if (!codec) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | // Test queryYUV8() |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 30 | SkYUVASizeInfo info; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 31 | |
| 32 | { |
| 33 | bool success = codec->queryYUV8(nullptr, nullptr); |
| 34 | REPORTER_ASSERT(reporter, !success); |
| 35 | success = codec->queryYUV8(&info, nullptr); |
| 36 | REPORTER_ASSERT(reporter, (expectedSizes == nullptr) == !success); |
| 37 | if (!success) { |
| 38 | return; |
| 39 | } |
| 40 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 41 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, info.fSizes[i] == expectedSizes[i]); |
| 43 | REPORTER_ASSERT(reporter, |
| 44 | info.fWidthBytes[i] == (uint32_t) SkAlign8(info.fSizes[i].width())); |
| 45 | } |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 46 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 47 | |
| 48 | { |
| 49 | SkYUVColorSpace colorSpace; |
| 50 | bool success = codec->queryYUV8(&info, &colorSpace); |
| 51 | REPORTER_ASSERT(reporter, (expectedSizes == nullptr) == !success); |
| 52 | if (!success) { |
| 53 | return; |
| 54 | } |
| 55 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 56 | for (int i = 0; i < SkYUVASizeInfo::kMaxCount; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, info.fSizes[i] == expectedSizes[i]); |
| 58 | REPORTER_ASSERT(reporter, |
| 59 | info.fWidthBytes[i] == (uint32_t) SkAlign8(info.fSizes[i].width())); |
| 60 | } |
| 61 | REPORTER_ASSERT(reporter, kJPEG_SkYUVColorSpace == colorSpace); |
| 62 | } |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 63 | |
| 64 | // Allocate the memory for the YUV decode |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 65 | size_t totalBytes = info.computeTotalBytes(); |
| 66 | |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 67 | SkAutoMalloc storage(totalBytes); |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 68 | void* planes[SkYUVASizeInfo::kMaxCount]; |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 69 | |
| 70 | info.computePlanes(storage.get(), planes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 71 | |
| 72 | // Test getYUV8Planes() |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, SkCodec::kInvalidInput == codec->getYUV8Planes(info, nullptr)); |
| 74 | REPORTER_ASSERT(reporter, SkCodec::kSuccess == codec->getYUV8Planes(info, planes)); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | DEF_TEST(Jpeg_YUV_Codec, r) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 78 | SkISize sizes[4]; |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 79 | |
| 80 | sizes[0].set(128, 128); |
| 81 | sizes[1].set(64, 64); |
| 82 | sizes[2].set(64, 64); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 83 | sizes[3].set(0, 0); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 84 | codec_yuv(r, "images/color_wheel.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 85 | |
| 86 | // H2V2 |
| 87 | sizes[0].set(512, 512); |
| 88 | sizes[1].set(256, 256); |
| 89 | sizes[2].set(256, 256); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 90 | codec_yuv(r, "images/mandrill_512_q075.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 91 | |
| 92 | // H1V1 |
| 93 | sizes[1].set(512, 512); |
| 94 | sizes[2].set(512, 512); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 95 | codec_yuv(r, "images/mandrill_h1v1.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 96 | |
| 97 | // H2V1 |
| 98 | sizes[1].set(256, 512); |
| 99 | sizes[2].set(256, 512); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 100 | codec_yuv(r, "images/mandrill_h2v1.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 101 | |
| 102 | // Non-power of two dimensions |
| 103 | sizes[0].set(439, 154); |
| 104 | sizes[1].set(220, 77); |
| 105 | sizes[2].set(220, 77); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 106 | codec_yuv(r, "images/cropped_mandrill.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 107 | |
| 108 | sizes[0].set(8, 8); |
| 109 | sizes[1].set(4, 4); |
| 110 | sizes[2].set(4, 4); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 111 | codec_yuv(r, "images/randPixels.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 112 | |
| 113 | // Progressive images |
| 114 | sizes[0].set(512, 512); |
| 115 | sizes[1].set(512, 512); |
| 116 | sizes[2].set(512, 512); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 117 | codec_yuv(r, "images/brickwork-texture.jpg", sizes); |
| 118 | codec_yuv(r, "images/brickwork_normal-map.jpg", sizes); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 119 | |
| 120 | // A CMYK encoded image should fail. |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 121 | codec_yuv(r, "images/CMYK.jpg", nullptr); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 122 | // A grayscale encoded image should fail. |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 123 | codec_yuv(r, "images/grayscale.jpg", nullptr); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 124 | // A PNG should fail. |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 125 | codec_yuv(r, "images/arrow.png", nullptr); |
msarett | 829caa2 | 2016-02-11 14:17:17 -0800 | [diff] [blame] | 126 | } |