1 /*
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
8 #include "SkCanvas.h"
9 #include "SkData.h"
10 #include "SkImageGenerator.h"
11 #include "SkPDFDocument.h"
12 #include "SkStream.h"
13
14 #include "Resources.h"
15 #include "Test.h"
16
is_subset_of(SkData * smaller,SkData * larger)17 static 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
load_resource(skiatest::Reporter * r,const char * test,const char * filename)33 static sk_sp<SkData> load_resource(
34 skiatest::Reporter* r, const char* test, const char* filename) {
35 sk_sp<SkData> data = GetResourceAsData(filename);
36 if (!data) {
37 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
38 test, filename);
39 }
40 return data; // May return nullptr.
41 }
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 */
DEF_TEST(SkPDF_JpegEmbedTest,r)48 DEF_TEST(SkPDF_JpegEmbedTest, r) {
49 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
50 const char test[] = "SkPDF_JpegEmbedTest";
51 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"));
53 if (!mandrillData || !cmykData) {
54 return;
55 }
56 ////////////////////////////////////////////////////////////////////////////
57 SkDynamicMemoryWStream pdf;
58 auto document = SkPDF::MakeDocument(&pdf);
59 SkCanvas* canvas = document->beginPage(642, 1028);
60
61 canvas->clear(SK_ColorLTGRAY);
62
63 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);
67
68 canvas->flush();
69 document->endPage();
70 document->close();
71 sk_sp<SkData> pdfData = pdf.detachAsData();
72 SkASSERT(pdfData);
73
74 #ifndef SK_PDF_BASE85_BINARY
75 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
76 #endif
77
78 // This JPEG uses a nonstandard colorspace - it can not be
79 // embedded into the PDF directly.
80 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
81 }
82
83 #ifdef SK_SUPPORT_PDF
84
85 #include "SkJpegInfo.h"
86
87 struct SkJFIFInfo {
88 SkISize fSize;
89 enum Type {
90 kGrayscale,
91 kYCbCr,
92 } fType;
93 };
SkIsJFIF(const SkData * data,SkJFIFInfo * info)94 bool SkIsJFIF(const SkData* data, SkJFIFInfo* info) {
95 SkISize jpegSize;
96 SkEncodedInfo::Color jpegColorType;
97 SkEncodedOrigin exifOrientation;
98 if (data && SkGetJpegInfo(data->data(), data->size(), &jpegSize,
99 &jpegColorType, &exifOrientation)) {
100 bool yuv = jpegColorType == SkEncodedInfo::kYUV_Color;
101 bool goodColorType = yuv || jpegColorType == SkEncodedInfo::kGray_Color;
102 if (goodColorType && kTopLeft_SkEncodedOrigin == exifOrientation) {
103 if (info) {
104 *info = {jpegSize, yuv ? SkJFIFInfo::kYCbCr : SkJFIFInfo::kGrayscale};
105 }
106 return true;
107 }
108 }
109 return false;
110 }
111
DEF_TEST(SkPDF_JpegIdentification,r)112 DEF_TEST(SkPDF_JpegIdentification, r) {
113 static struct {
114 const char* path;
115 bool isJfif;
116 SkJFIFInfo::Type type;
117 } kTests[] = {{"images/CMYK.jpg", false, SkJFIFInfo::kGrayscale},
118 {"images/color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
119 {"images/grayscale.jpg", true, SkJFIFInfo::kGrayscale},
120 {"images/mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
121 {"images/randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
122 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
123 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
124 if (!data) {
125 continue;
126 }
127 SkJFIFInfo info;
128 bool isJfif = SkIsJFIF(data.get(), &info);
129 if (isJfif != kTests[i].isJfif) {
130 ERRORF(r, "%s failed isJfif test", kTests[i].path);
131 continue;
132 }
133 if (!isJfif) {
134 continue; // not applicable
135 }
136 if (kTests[i].type != info.fType) {
137 ERRORF(r, "%s failed jfif type test", kTests[i].path);
138 continue;
139 }
140 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
141 info.fSize.width(), info.fSize.height());
142 }
143
144 // Test several malformed jpegs.
145 SkJFIFInfo info;
146 {
147 static const char goodJpeg[] =
148 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\20\13\14"
149 "\16\14\n\20\16\r\16\22\21\20\23\30(\32\30\26\26\0301#%\35(:3=<9387"
150 "@H\\N@DWE78PmQW_bghg>Mqypdx\\egc\377\333\0C\1\21\22\22\30\25\30/\32"
151 "\32/cB8Bcccccccccccccccccccccccccccccccccccccccccccccccccc\377\300"
152 "\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"
153 "\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"
154 "\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"
155 "\221\241\10#B\261\301\25R\321\360$3br\202\t\n\26\27\30\31\32%&'()*"
156 "456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\203\204\205\206\207\210\211"
157 "\212\222\223\224\225\226\227\230\231\232\242\243\244\245\246\247\250"
158 "\251\252\262\263\264\265\266\267\270\271\272\302\303\304\305\306\307"
159 "\310\311\312\322\323\324\325\326\327\330\331\332\341\342\343\344\345"
160 "\346\347\350\351\352\361\362\363\364\365\366\367\370\371\372\377\304"
161 "\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"
162 "\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"
163 "AQ\7aq\23\"2\201\10\24B\221\241\261\301\t#3R\360\25br\321\n\26$4\341"
164 "%\361\27\30\31\32&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\202\203"
165 "\204\205\206\207\210\211\212\222\223\224\225\226\227\230\231\232\242"
166 "\243\244\245\246\247\250\251\252\262\263\264\265\266\267\270\271\272"
167 "\302\303\304\305\306\307\310\311\312\322\323\324\325\326\327\330\331"
168 "\332\342\343\344\345\346\347\350\351\352\362\363\364\365\366\367\370"
169 "\371\372\377\332\0\14\3\1\0\2\21\3\21\0?\0\216M\352\214\25\271\224"
170 "\262\310\253\363tl\22209\35O~\237\\\24QZ\306Mh\216\252i\364ml\177\377"
171 "\331";
172 size_t goodJpegLength = 659;
173 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
174 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
175 REPORTER_ASSERT(r, info.fSize == (SkISize{8, 8}));
176 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
177
178 // Not long enough to read first (SOI) segment marker.
179 data = SkData::MakeWithoutCopy(goodJpeg, 1);
180 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
181
182 // Not long enough to read second segment (APP0) marker.
183 data = SkData::MakeWithoutCopy(goodJpeg, 3);
184 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
185
186 // Not long enough to read second segment's length.
187 data = SkData::MakeWithoutCopy(goodJpeg, 5);
188 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
189
190 // APP0 segment is truncated.
191 data = SkData::MakeWithoutCopy(goodJpeg, 7);
192 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
193
194 // Missing SOF segment.
195 data = SkData::MakeWithoutCopy(goodJpeg, 89);
196 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
197 }
198 {
199 // JFIF tag missing.
200 static const char jpeg[] =
201 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
202 "\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"
203 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
204 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
205 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
206 size_t jpegLength = 177;
207 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
208 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
209 }
210 {
211 // APP0 segment short (byte 6 changed).
212 static const char jpeg[] =
213 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
214 "\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"
215 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
216 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
217 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
218 size_t jpegLength = 177;
219 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
220 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
221 }
222 {
223 // SOF segment short. ('\21' replaced with '\5')
224 static const char jpeg[] =
225 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
226 "\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"
227 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
228 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
229 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
230 size_t jpegLength = 177;
231 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
232 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
233 }
234 {
235 // Unsupported 12-bit components. ('\10' replaced with '\14')
236 static const char jpeg[] =
237 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
238 "\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"
239 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
240 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
241 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
242 size_t jpegLength = 177;
243 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
244 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
245 }
246 {
247 // Two color channels. ('\3' replaced with '\2')
248 static const char jpeg[] =
249 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
250 "\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"
251 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
252 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
253 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
254 size_t jpegLength = 177;
255 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
256 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
257 }
258 }
259 #endif
260