blob: aaac2b9f18f4d4132e95067abd2a6f367dd5d880 [file] [log] [blame]
halcanarydaefa5b2014-08-27 13:00:54 -07001/*
2 * Copyright 2014 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
halcanarydaefa5b2014-08-27 13:00:54 -07008#include "SkCanvas.h"
halcanarydaefa5b2014-08-27 13:00:54 -07009#include "SkData.h"
halcanary57f744e2016-09-09 11:41:59 -070010#include "SkDocument.h"
11#include "SkImageGenerator.h"
halcanarydaefa5b2014-08-27 13:00:54 -070012#include "SkStream.h"
halcanarydaefa5b2014-08-27 13:00:54 -070013
14#include "Resources.h"
15#include "Test.h"
16
halcanarydaefa5b2014-08-27 13:00:54 -070017static bool is_subset_of(SkData* smaller, SkData* larger) {
18 SkASSERT(smaller && larger);
19 if (smaller->size() > larger->size()) {
20 return false;
21 }
22 size_t size = smaller->size();
23 size_t size_diff = larger->size() - size;
24 for (size_t i = 0; i <= size_diff; ++i) {
25 if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
26 return true;
27 }
28 }
29 return false;
30}
31
32
bungeman38d909e2016-08-02 14:40:46 -070033static sk_sp<SkData> load_resource(
halcanarydaefa5b2014-08-27 13:00:54 -070034 skiatest::Reporter* r, const char* test, const char* filename) {
Mike Reed0933bc92017-12-09 01:27:41 +000035 sk_sp<SkData> data = GetResourceAsData(filename);
halcanary7d571242016-02-24 17:59:16 -080036 if (!data) {
37 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
38 test, filename);
halcanarydaefa5b2014-08-27 13:00:54 -070039 }
halcanary96fcdcc2015-08-27 07:41:13 -070040 return data; // May return nullptr.
halcanarydaefa5b2014-08-27 13:00:54 -070041}
42
43/**
44 * Test that for Jpeg files that use the JFIF colorspace, they are
45 * directly embedded into the PDF (without re-encoding) when that
46 * makes sense.
47 */
halcanary57f744e2016-09-09 11:41:59 -070048DEF_TEST(SkPDF_JpegEmbedTest, r) {
49 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
50 const char test[] = "SkPDF_JpegEmbedTest";
Hal Canaryc465d132017-12-08 10:21:31 -050051 sk_sp<SkData> mandrillData(load_resource(r, test, "images/mandrill_512_q075.jpg"));
52 sk_sp<SkData> cmykData(load_resource(r, test, "images/CMYK.jpg"));
halcanarydaefa5b2014-08-27 13:00:54 -070053 if (!mandrillData || !cmykData) {
54 return;
55 }
halcanary7a14b312015-10-01 07:28:13 -070056 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070057 SkDynamicMemoryWStream pdf;
halcanary4b656662016-04-27 07:45:18 -070058 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
halcanarydaefa5b2014-08-27 13:00:54 -070059 SkCanvas* canvas = document->beginPage(642, 1028);
60
61 canvas->clear(SK_ColorLTGRAY);
62
reed9ce9d672016-03-17 10:51:11 -070063 sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
64 canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
65 sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
66 canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);
halcanary7a14b312015-10-01 07:28:13 -070067
68 canvas->flush();
69 document->endPage();
70 document->close();
Mike Reed76147942016-10-25 09:57:13 -040071 sk_sp<SkData> pdfData = pdf.detachAsData();
halcanary7a14b312015-10-01 07:28:13 -070072 SkASSERT(pdfData);
halcanary7a14b312015-10-01 07:28:13 -070073
reed9ce9d672016-03-17 10:51:11 -070074 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -070075
76 // This JPEG uses a nonstandard colorspace - it can not be
77 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -070078 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -070079}
halcanary96287f72015-05-07 11:46:59 -070080
Hal Canary43fb7a02016-12-30 13:09:03 -050081#ifdef SK_SUPPORT_PDF
82
83#include "SkJpegInfo.h"
84
halcanary57f744e2016-09-09 11:41:59 -070085DEF_TEST(SkPDF_JpegIdentification, r) {
halcanary96287f72015-05-07 11:46:59 -070086 static struct {
87 const char* path;
88 bool isJfif;
89 SkJFIFInfo::Type type;
Hal Canaryc465d132017-12-08 10:21:31 -050090 } kTests[] = {{"images/CMYK.jpg", false, SkJFIFInfo::kGrayscale},
91 {"images/color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
92 {"images/grayscale.jpg", true, SkJFIFInfo::kGrayscale},
93 {"images/mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
94 {"images/randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
halcanary96287f72015-05-07 11:46:59 -070095 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
bungeman38d909e2016-08-02 14:40:46 -070096 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
halcanary96287f72015-05-07 11:46:59 -070097 if (!data) {
98 continue;
99 }
100 SkJFIFInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700101 bool isJfif = SkIsJFIF(data.get(), &info);
halcanary96287f72015-05-07 11:46:59 -0700102 if (isJfif != kTests[i].isJfif) {
103 ERRORF(r, "%s failed isJfif test", kTests[i].path);
104 continue;
105 }
106 if (!isJfif) {
107 continue; // not applicable
108 }
109 if (kTests[i].type != info.fType) {
110 ERRORF(r, "%s failed jfif type test", kTests[i].path);
111 continue;
112 }
halcanary7d571242016-02-24 17:59:16 -0800113 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
114 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700115 }
halcanary57f744e2016-09-09 11:41:59 -0700116
117 // Test several malformed jpegs.
118 SkJFIFInfo info;
119 {
120 static const char goodJpeg[] =
121 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
122 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
123 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
124 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
125 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
126 size_t goodJpegLength = 177;
127 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
128 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
129 REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512));
130 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
131
132 // Not long enough to read first (SOI) segment marker.
133 data = SkData::MakeWithoutCopy(goodJpeg, 1);
134 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
135
136 // Not long enough to read second segment (APP0) marker.
137 data = SkData::MakeWithoutCopy(goodJpeg, 3);
138 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
139
140 // Not long enough to read second segment's length.
141 data = SkData::MakeWithoutCopy(goodJpeg, 5);
142 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
143
144 // APP0 segment is truncated.
145 data = SkData::MakeWithoutCopy(goodJpeg, 7);
146 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
147
148 // Missing SOF segment.
149 data = SkData::MakeWithoutCopy(goodJpeg, 89);
150 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
151 }
152 {
153 // JFIF tag missing.
154 static const char jpeg[] =
155 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
156 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
157 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
158 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
159 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
160 size_t jpegLength = 177;
161 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
162 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
163 }
164 {
165 // APP0 segment short (byte 6 changed).
166 static const char jpeg[] =
167 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
168 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
169 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
170 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
171 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
172 size_t jpegLength = 177;
173 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
174 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
175 }
176 {
177 // SOF segment short. ('\21' replaced with '\5')
178 static const char jpeg[] =
179 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
180 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
181 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
182 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
183 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
184 size_t jpegLength = 177;
185 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
186 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
187 }
188 {
189 // Unsupported 12-bit components. ('\10' replaced with '\14')
190 static const char jpeg[] =
191 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
192 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
193 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
194 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
195 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
196 size_t jpegLength = 177;
197 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
198 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
199 }
200 {
201 // Two color channels. ('\3' replaced with '\2')
202 static const char jpeg[] =
203 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
204 "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
205 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
206 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
207 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
208 size_t jpegLength = 177;
209 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
210 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
211 }
halcanary96287f72015-05-07 11:46:59 -0700212}
Hal Canary43fb7a02016-12-30 13:09:03 -0500213#endif