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