blob: f7b3306b98fa935b02b5cda64c1caf6506c3d442 [file] [log] [blame]
msarett829caa22016-02-11 14:17:17 -08001/*
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
8#include "SkCodec.h"
9#include "Resources.h"
10#include "SkStream.h"
11#include "SkTemplates.h"
msarett4984c3c2016-03-10 05:44:43 -080012#include "SkYUVSizeInfo.h"
msarett829caa22016-02-11 14:17:17 -080013#include "Test.h"
14
15static SkStreamAsset* resource(const char path[]) {
16 SkString fullPath = GetResourcePath(path);
17 return SkStream::NewFromFile(fullPath.c_str());
18}
19
20static void codec_yuv(skiatest::Reporter* reporter,
21 const char path[],
22 SkISize expectedSizes[3]) {
23 SkAutoTDelete<SkStream> stream(resource(path));
24 if (!stream) {
halcanary7d571242016-02-24 17:59:16 -080025 INFOF(reporter, "Missing resource '%s'\n", path);
msarett829caa22016-02-11 14:17:17 -080026 return;
27 }
mtklein18300a32016-03-16 13:53:35 -070028 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett829caa22016-02-11 14:17:17 -080029 REPORTER_ASSERT(reporter, codec);
30 if (!codec) {
31 return;
32 }
33
34 // Test queryYUV8()
msarett4984c3c2016-03-10 05:44:43 -080035 SkYUVSizeInfo info;
msarett829caa22016-02-11 14:17:17 -080036 bool success = codec->queryYUV8(nullptr, nullptr);
37 REPORTER_ASSERT(reporter, !success);
38 success = codec->queryYUV8(&info, nullptr);
39 REPORTER_ASSERT(reporter, (expectedSizes == nullptr) == !success);
40 if (!success) {
41 return;
42 }
43 REPORTER_ASSERT(reporter,
44 0 == memcmp((const void*) &info, (const void*) expectedSizes, 3 * sizeof(SkISize)));
msarett4984c3c2016-03-10 05:44:43 -080045 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kY] ==
46 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kY].width()));
47 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kU] ==
48 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kU].width()));
49 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kV] ==
50 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kV].width()));
msarett829caa22016-02-11 14:17:17 -080051 SkYUVColorSpace colorSpace;
52 success = codec->queryYUV8(&info, &colorSpace);
53 REPORTER_ASSERT(reporter,
54 0 == memcmp((const void*) &info, (const void*) expectedSizes, 3 * sizeof(SkISize)));
msarett4984c3c2016-03-10 05:44:43 -080055 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kY] ==
56 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kY].width()));
57 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kU] ==
58 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kU].width()));
59 REPORTER_ASSERT(reporter, info.fWidthBytes[SkYUVSizeInfo::kV] ==
60 (uint32_t) SkAlign8(info.fSizes[SkYUVSizeInfo::kV].width()));
msarett829caa22016-02-11 14:17:17 -080061 REPORTER_ASSERT(reporter, kJPEG_SkYUVColorSpace == colorSpace);
62
63 // Allocate the memory for the YUV decode
msarett4984c3c2016-03-10 05:44:43 -080064 size_t totalBytes =
65 info.fWidthBytes[SkYUVSizeInfo::kY] * info.fSizes[SkYUVSizeInfo::kY].height() +
66 info.fWidthBytes[SkYUVSizeInfo::kU] * info.fSizes[SkYUVSizeInfo::kU].height() +
67 info.fWidthBytes[SkYUVSizeInfo::kV] * info.fSizes[SkYUVSizeInfo::kV].height();
msarett829caa22016-02-11 14:17:17 -080068 SkAutoMalloc storage(totalBytes);
69 void* planes[3];
70 planes[0] = storage.get();
msarett4984c3c2016-03-10 05:44:43 -080071 planes[1] = SkTAddOffset<void>(planes[0],
72 info.fWidthBytes[SkYUVSizeInfo::kY] * info.fSizes[SkYUVSizeInfo::kY].height());
73 planes[2] = SkTAddOffset<void>(planes[1],
74 info.fWidthBytes[SkYUVSizeInfo::kU] * info.fSizes[SkYUVSizeInfo::kU].height());
msarett829caa22016-02-11 14:17:17 -080075
76 // Test getYUV8Planes()
77 REPORTER_ASSERT(reporter, SkCodec::kInvalidInput ==
78 codec->getYUV8Planes(info, nullptr));
79 REPORTER_ASSERT(reporter, SkCodec::kSuccess ==
80 codec->getYUV8Planes(info, planes));
81}
82
83DEF_TEST(Jpeg_YUV_Codec, r) {
84 SkISize sizes[3];
85
86 sizes[0].set(128, 128);
87 sizes[1].set(64, 64);
88 sizes[2].set(64, 64);
89 codec_yuv(r, "color_wheel.jpg", sizes);
90
91 // H2V2
92 sizes[0].set(512, 512);
93 sizes[1].set(256, 256);
94 sizes[2].set(256, 256);
95 codec_yuv(r, "mandrill_512_q075.jpg", sizes);
96
97 // H1V1
98 sizes[1].set(512, 512);
99 sizes[2].set(512, 512);
100 codec_yuv(r, "mandrill_h1v1.jpg", sizes);
101
102 // H2V1
103 sizes[1].set(256, 512);
104 sizes[2].set(256, 512);
105 codec_yuv(r, "mandrill_h2v1.jpg", sizes);
106
107 // Non-power of two dimensions
108 sizes[0].set(439, 154);
109 sizes[1].set(220, 77);
110 sizes[2].set(220, 77);
111 codec_yuv(r, "cropped_mandrill.jpg", sizes);
112
113 sizes[0].set(8, 8);
114 sizes[1].set(4, 4);
115 sizes[2].set(4, 4);
116 codec_yuv(r, "randPixels.jpg", sizes);
117
118 // Progressive images
119 sizes[0].set(512, 512);
120 sizes[1].set(512, 512);
121 sizes[2].set(512, 512);
122 codec_yuv(r, "brickwork-texture.jpg", sizes);
123 codec_yuv(r, "brickwork_normal-map.jpg", sizes);
124
125 // A CMYK encoded image should fail.
126 codec_yuv(r, "CMYK.jpg", nullptr);
127 // A grayscale encoded image should fail.
128 codec_yuv(r, "grayscale.jpg", nullptr);
129 // A PNG should fail.
130 codec_yuv(r, "arrow.png", nullptr);
131}