blob: 87577ac6dc2f304ee8b2fad3b971bfbeb333ff13 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkData.h"
10#include "include/core/SkImageGenerator.h"
11#include "include/core/SkStream.h"
12#include "include/docs/SkPDFDocument.h"
halcanarydaefa5b2014-08-27 13:00:54 -070013
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tests/Test.h"
15#include "tools/Resources.h"
halcanarydaefa5b2014-08-27 13:00:54 -070016
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;
Hal Canary3026d4b2019-01-07 10:00:48 -050058 auto document = SkPDF::MakeDocument(&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
halcanary7a14b312015-10-01 07:28:13 -070068 document->endPage();
69 document->close();
Mike Reed76147942016-10-25 09:57:13 -040070 sk_sp<SkData> pdfData = pdf.detachAsData();
halcanary7a14b312015-10-01 07:28:13 -070071 SkASSERT(pdfData);
halcanary7a14b312015-10-01 07:28:13 -070072
Hal Canaryd104cc42018-12-20 16:15:17 -050073 #ifndef SK_PDF_BASE85_BINARY
reed9ce9d672016-03-17 10:51:11 -070074 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
Hal Canaryd104cc42018-12-20 16:15:17 -050075 #endif
halcanary7a14b312015-10-01 07:28:13 -070076
77 // This JPEG uses a nonstandard colorspace - it can not be
78 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -070079 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -070080}
halcanary96287f72015-05-07 11:46:59 -070081
Hal Canary43fb7a02016-12-30 13:09:03 -050082#ifdef SK_SUPPORT_PDF
83
Mike Kleinc0bd9f92019-04-23 12:05:21 -050084#include "src/pdf/SkJpegInfo.h"
Hal Canary43fb7a02016-12-30 13:09:03 -050085
Hal Canary83e0f1b2018-04-05 16:58:41 -040086struct SkJFIFInfo {
87 SkISize fSize;
88 enum Type {
89 kGrayscale,
90 kYCbCr,
91 } fType;
92};
93bool SkIsJFIF(const SkData* data, SkJFIFInfo* info) {
94 SkISize jpegSize;
95 SkEncodedInfo::Color jpegColorType;
96 SkEncodedOrigin exifOrientation;
97 if (data && SkGetJpegInfo(data->data(), data->size(), &jpegSize,
98 &jpegColorType, &exifOrientation)) {
99 bool yuv = jpegColorType == SkEncodedInfo::kYUV_Color;
100 bool goodColorType = yuv || jpegColorType == SkEncodedInfo::kGray_Color;
101 if (goodColorType && kTopLeft_SkEncodedOrigin == exifOrientation) {
102 if (info) {
103 *info = {jpegSize, yuv ? SkJFIFInfo::kYCbCr : SkJFIFInfo::kGrayscale};
104 }
105 return true;
106 }
107 }
108 return false;
109}
110
halcanary57f744e2016-09-09 11:41:59 -0700111DEF_TEST(SkPDF_JpegIdentification, r) {
halcanary96287f72015-05-07 11:46:59 -0700112 static struct {
113 const char* path;
114 bool isJfif;
115 SkJFIFInfo::Type type;
Hal Canaryc465d132017-12-08 10:21:31 -0500116 } kTests[] = {{"images/CMYK.jpg", false, SkJFIFInfo::kGrayscale},
117 {"images/color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
118 {"images/grayscale.jpg", true, SkJFIFInfo::kGrayscale},
119 {"images/mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
120 {"images/randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
halcanary96287f72015-05-07 11:46:59 -0700121 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
bungeman38d909e2016-08-02 14:40:46 -0700122 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
halcanary96287f72015-05-07 11:46:59 -0700123 if (!data) {
124 continue;
125 }
126 SkJFIFInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700127 bool isJfif = SkIsJFIF(data.get(), &info);
halcanary96287f72015-05-07 11:46:59 -0700128 if (isJfif != kTests[i].isJfif) {
129 ERRORF(r, "%s failed isJfif test", kTests[i].path);
130 continue;
131 }
132 if (!isJfif) {
133 continue; // not applicable
134 }
135 if (kTests[i].type != info.fType) {
136 ERRORF(r, "%s failed jfif type test", kTests[i].path);
137 continue;
138 }
halcanary7d571242016-02-24 17:59:16 -0800139 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
140 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700141 }
halcanary57f744e2016-09-09 11:41:59 -0700142
143 // Test several malformed jpegs.
144 SkJFIFInfo info;
145 {
146 static const char goodJpeg[] =
Hal Canary83e0f1b2018-04-05 16:58:41 -0400147 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\20\13\14"
148 "\16\14\n\20\16\r\16\22\21\20\23\30(\32\30\26\26\0301#%\35(:3=<9387"
149 "@H\\N@DWE78PmQW_bghg>Mqypdx\\egc\377\333\0C\1\21\22\22\30\25\30/\32"
150 "\32/cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc\377\300"
151 "\0\21\10\0\10\0\10\3\1\"\0\2\21\1\3\21\1\377\304\0\37\0\0\1\5\1\1\1"
152 "\1\1\1\0\0\0\0\0\0\0\0\1\2\3\4\5\6\7\10\t\n\13\377\304\0\265\20\0\2"
153 "\1\3\3\2\4\3\5\5\4\4\0\0\1}\1\2\3\0\4\21\5\22!1A\6\23Qa\7\"q\0242\201"
154 "\221\241\10#B\261\301\25R\321\360$3br\202\t\n\26\27\30\31\32%&'()*"
155 "456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\203\204\205\206\207\210\211"
156 "\212\222\223\224\225\226\227\230\231\232\242\243\244\245\246\247\250"
157 "\251\252\262\263\264\265\266\267\270\271\272\302\303\304\305\306\307"
158 "\310\311\312\322\323\324\325\326\327\330\331\332\341\342\343\344\345"
159 "\346\347\350\351\352\361\362\363\364\365\366\367\370\371\372\377\304"
160 "\0\37\1\0\3\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\1\2\3\4\5\6\7\10\t\n\13\377"
161 "\304\0\265\21\0\2\1\2\4\4\3\4\7\5\4\4\0\1\2w\0\1\2\3\21\4\5!1\6\22"
162 "AQ\7aq\23\"2\201\10\24B\221\241\261\301\t#3R\360\25br\321\n\26$4\341"
163 "%\361\27\30\31\32&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\202\203"
164 "\204\205\206\207\210\211\212\222\223\224\225\226\227\230\231\232\242"
165 "\243\244\245\246\247\250\251\252\262\263\264\265\266\267\270\271\272"
166 "\302\303\304\305\306\307\310\311\312\322\323\324\325\326\327\330\331"
167 "\332\342\343\344\345\346\347\350\351\352\362\363\364\365\366\367\370"
168 "\371\372\377\332\0\14\3\1\0\2\21\3\21\0?\0\216M\352\214\25\271\224"
169 "\262\310\253\363tl\22209\35O~\237\\\24QZ\306Mh\216\252i\364ml\177\377"
170 "\331";
171 size_t goodJpegLength = 659;
halcanary57f744e2016-09-09 11:41:59 -0700172 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
173 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
Hal Canary83e0f1b2018-04-05 16:58:41 -0400174 REPORTER_ASSERT(r, info.fSize == (SkISize{8, 8}));
halcanary57f744e2016-09-09 11:41:59 -0700175 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
176
177 // Not long enough to read first (SOI) segment marker.
178 data = SkData::MakeWithoutCopy(goodJpeg, 1);
179 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
180
181 // Not long enough to read second segment (APP0) marker.
182 data = SkData::MakeWithoutCopy(goodJpeg, 3);
183 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
184
185 // Not long enough to read second segment's length.
186 data = SkData::MakeWithoutCopy(goodJpeg, 5);
187 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
188
189 // APP0 segment is truncated.
190 data = SkData::MakeWithoutCopy(goodJpeg, 7);
191 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
192
193 // Missing SOF segment.
194 data = SkData::MakeWithoutCopy(goodJpeg, 89);
195 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
196 }
197 {
198 // JFIF tag missing.
199 static const char jpeg[] =
200 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
201 "\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"
202 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
203 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
204 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
205 size_t jpegLength = 177;
206 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
207 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
208 }
209 {
210 // APP0 segment short (byte 6 changed).
211 static const char jpeg[] =
212 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
213 "\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"
214 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
215 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
216 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
217 size_t jpegLength = 177;
218 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
219 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
220 }
221 {
222 // SOF segment short. ('\21' replaced with '\5')
223 static const char jpeg[] =
224 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
225 "\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"
226 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
227 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
228 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
229 size_t jpegLength = 177;
230 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
231 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
232 }
233 {
234 // Unsupported 12-bit components. ('\10' replaced with '\14')
235 static const char jpeg[] =
236 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
237 "\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"
238 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
239 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
240 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
241 size_t jpegLength = 177;
242 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
243 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
244 }
245 {
246 // Two color channels. ('\3' replaced with '\2')
247 static const char jpeg[] =
248 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
249 "\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"
250 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
251 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
252 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
253 size_t jpegLength = 177;
254 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
255 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
256 }
halcanary96287f72015-05-07 11:46:59 -0700257}
Hal Canary43fb7a02016-12-30 13:09:03 -0500258#endif