blob: 17dbac8cfa7ea1c58dfb2a45717bc4159bbff425 [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"
12#include "SkJpegInfo.h"
halcanarydaefa5b2014-08-27 13:00:54 -070013#include "SkStream.h"
halcanarydaefa5b2014-08-27 13:00:54 -070014
15#include "Resources.h"
16#include "Test.h"
17
halcanarydaefa5b2014-08-27 13:00:54 -070018static bool is_subset_of(SkData* smaller, SkData* larger) {
19 SkASSERT(smaller && larger);
20 if (smaller->size() > larger->size()) {
21 return false;
22 }
23 size_t size = smaller->size();
24 size_t size_diff = larger->size() - size;
25 for (size_t i = 0; i <= size_diff; ++i) {
26 if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
27 return true;
28 }
29 }
30 return false;
31}
32
33
bungeman38d909e2016-08-02 14:40:46 -070034static sk_sp<SkData> load_resource(
halcanarydaefa5b2014-08-27 13:00:54 -070035 skiatest::Reporter* r, const char* test, const char* filename) {
36 SkString path(GetResourcePath(filename));
bungeman38d909e2016-08-02 14:40:46 -070037 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
halcanary7d571242016-02-24 17:59:16 -080038 if (!data) {
39 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
40 test, filename);
halcanarydaefa5b2014-08-27 13:00:54 -070041 }
halcanary96fcdcc2015-08-27 07:41:13 -070042 return data; // May return nullptr.
halcanarydaefa5b2014-08-27 13:00:54 -070043}
44
45/**
46 * Test that for Jpeg files that use the JFIF colorspace, they are
47 * directly embedded into the PDF (without re-encoding) when that
48 * makes sense.
49 */
halcanary57f744e2016-09-09 11:41:59 -070050DEF_TEST(SkPDF_JpegEmbedTest, r) {
51 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
52 const char test[] = "SkPDF_JpegEmbedTest";
reed9ce9d672016-03-17 10:51:11 -070053 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
54 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
halcanarydaefa5b2014-08-27 13:00:54 -070055 if (!mandrillData || !cmykData) {
56 return;
57 }
halcanary7a14b312015-10-01 07:28:13 -070058 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070059 SkDynamicMemoryWStream pdf;
halcanary4b656662016-04-27 07:45:18 -070060 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
halcanarydaefa5b2014-08-27 13:00:54 -070061 SkCanvas* canvas = document->beginPage(642, 1028);
62
63 canvas->clear(SK_ColorLTGRAY);
64
reed9ce9d672016-03-17 10:51:11 -070065 sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
66 canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
67 sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
68 canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);
halcanary7a14b312015-10-01 07:28:13 -070069
70 canvas->flush();
71 document->endPage();
72 document->close();
Mike Reed76147942016-10-25 09:57:13 -040073 sk_sp<SkData> pdfData = pdf.detachAsData();
halcanary7a14b312015-10-01 07:28:13 -070074 SkASSERT(pdfData);
halcanary7a14b312015-10-01 07:28:13 -070075
reed9ce9d672016-03-17 10:51:11 -070076 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -070077
78 // This JPEG uses a nonstandard colorspace - it can not be
79 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -070080 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -070081}
halcanary96287f72015-05-07 11:46:59 -070082
halcanary57f744e2016-09-09 11:41:59 -070083DEF_TEST(SkPDF_JpegIdentification, r) {
halcanary96287f72015-05-07 11:46:59 -070084 static struct {
85 const char* path;
86 bool isJfif;
87 SkJFIFInfo::Type type;
88 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
89 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
90 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
91 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
92 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
93 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
bungeman38d909e2016-08-02 14:40:46 -070094 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
halcanary96287f72015-05-07 11:46:59 -070095 if (!data) {
96 continue;
97 }
98 SkJFIFInfo info;
bungeman38d909e2016-08-02 14:40:46 -070099 bool isJfif = SkIsJFIF(data.get(), &info);
halcanary96287f72015-05-07 11:46:59 -0700100 if (isJfif != kTests[i].isJfif) {
101 ERRORF(r, "%s failed isJfif test", kTests[i].path);
102 continue;
103 }
104 if (!isJfif) {
105 continue; // not applicable
106 }
107 if (kTests[i].type != info.fType) {
108 ERRORF(r, "%s failed jfif type test", kTests[i].path);
109 continue;
110 }
halcanary7d571242016-02-24 17:59:16 -0800111 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
112 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700113 }
halcanary57f744e2016-09-09 11:41:59 -0700114
115 // Test several malformed jpegs.
116 SkJFIFInfo info;
117 {
118 static const char goodJpeg[] =
119 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
120 "\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"
121 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
122 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
123 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
124 size_t goodJpegLength = 177;
125 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
126 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
127 REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512));
128 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
129
130 // Not long enough to read first (SOI) segment marker.
131 data = SkData::MakeWithoutCopy(goodJpeg, 1);
132 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
133
134 // Not long enough to read second segment (APP0) marker.
135 data = SkData::MakeWithoutCopy(goodJpeg, 3);
136 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
137
138 // Not long enough to read second segment's length.
139 data = SkData::MakeWithoutCopy(goodJpeg, 5);
140 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
141
142 // APP0 segment is truncated.
143 data = SkData::MakeWithoutCopy(goodJpeg, 7);
144 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
145
146 // Missing SOF segment.
147 data = SkData::MakeWithoutCopy(goodJpeg, 89);
148 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
149 }
150 {
151 // JFIF tag missing.
152 static const char jpeg[] =
153 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
154 "\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"
155 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
156 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
157 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
158 size_t jpegLength = 177;
159 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
160 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
161 }
162 {
163 // APP0 segment short (byte 6 changed).
164 static const char jpeg[] =
165 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
166 "\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"
167 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
168 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
169 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
170 size_t jpegLength = 177;
171 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
172 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
173 }
174 {
175 // SOF segment short. ('\21' replaced with '\5')
176 static const char jpeg[] =
177 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
178 "\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"
179 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
180 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
181 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
182 size_t jpegLength = 177;
183 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
184 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
185 }
186 {
187 // Unsupported 12-bit components. ('\10' replaced with '\14')
188 static const char jpeg[] =
189 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
190 "\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"
191 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
192 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
193 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
194 size_t jpegLength = 177;
195 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
196 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
197 }
198 {
199 // Two color channels. ('\3' replaced with '\2')
200 static const char jpeg[] =
201 "\377\330\377\340\0\20JFIF\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\2\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 }
halcanary96287f72015-05-07 11:46:59 -0700210}