blob: 93c204401d1d7aae1b8435ff150e2dfcebd8ca27 [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
18// Returned bitmap is lazy. Only lazy bitmaps hold onto the original data.
19static SkBitmap bitmap_from_data(SkData* data) {
20 SkASSERT(data);
21 SkBitmap bm;
reedd1146452015-09-25 06:56:57 -070022 SkDEPRECATED_InstallDiscardablePixelRef(data, &bm);
halcanarydaefa5b2014-08-27 13:00:54 -070023 return bm;
24}
25
26static bool is_subset_of(SkData* smaller, SkData* larger) {
27 SkASSERT(smaller && larger);
28 if (smaller->size() > larger->size()) {
29 return false;
30 }
31 size_t size = smaller->size();
32 size_t size_diff = larger->size() - size;
33 for (size_t i = 0; i <= size_diff; ++i) {
34 if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
35 return true;
36 }
37 }
38 return false;
39}
40
41
bungeman38d909e2016-08-02 14:40:46 -070042static sk_sp<SkData> load_resource(
halcanarydaefa5b2014-08-27 13:00:54 -070043 skiatest::Reporter* r, const char* test, const char* filename) {
44 SkString path(GetResourcePath(filename));
bungeman38d909e2016-08-02 14:40:46 -070045 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
halcanary7d571242016-02-24 17:59:16 -080046 if (!data) {
47 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
48 test, filename);
halcanarydaefa5b2014-08-27 13:00:54 -070049 }
halcanary96fcdcc2015-08-27 07:41:13 -070050 return data; // May return nullptr.
halcanarydaefa5b2014-08-27 13:00:54 -070051}
52
53/**
54 * Test that for Jpeg files that use the JFIF colorspace, they are
55 * directly embedded into the PDF (without re-encoding) when that
56 * makes sense.
57 */
halcanary57f744e2016-09-09 11:41:59 -070058DEF_TEST(SkPDF_JpegEmbedTest, r) {
59 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
60 const char test[] = "SkPDF_JpegEmbedTest";
reed9ce9d672016-03-17 10:51:11 -070061 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
62 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
halcanarydaefa5b2014-08-27 13:00:54 -070063 if (!mandrillData || !cmykData) {
64 return;
65 }
halcanary7a14b312015-10-01 07:28:13 -070066 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070067 SkDynamicMemoryWStream pdf;
halcanary4b656662016-04-27 07:45:18 -070068 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
halcanarydaefa5b2014-08-27 13:00:54 -070069 SkCanvas* canvas = document->beginPage(642, 1028);
70
71 canvas->clear(SK_ColorLTGRAY);
72
reed9ce9d672016-03-17 10:51:11 -070073 SkBitmap bm1(bitmap_from_data(mandrillData.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070074 canvas->drawBitmap(bm1, 65.0, 0.0, nullptr);
reed9ce9d672016-03-17 10:51:11 -070075 SkBitmap bm2(bitmap_from_data(cmykData.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070076 canvas->drawBitmap(bm2, 0.0, 512.0, nullptr);
halcanarydaefa5b2014-08-27 13:00:54 -070077
78 canvas->flush();
79 document->endPage();
80 document->close();
bungemanffae30d2016-08-03 13:32:32 -070081 sk_sp<SkData> pdfData(pdf.copyToData());
halcanarydaefa5b2014-08-27 13:00:54 -070082 SkASSERT(pdfData);
83 pdf.reset();
84
reed9ce9d672016-03-17 10:51:11 -070085 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -070086
87 // This JPEG uses a nonstandard colorspace - it can not be
88 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -070089 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -070090 ////////////////////////////////////////////////////////////////////////////
91 pdf.reset();
halcanary4b656662016-04-27 07:45:18 -070092 document = SkDocument::MakePDF(&pdf);
halcanary7a14b312015-10-01 07:28:13 -070093 canvas = document->beginPage(642, 1028);
94
95 canvas->clear(SK_ColorLTGRAY);
96
reed9ce9d672016-03-17 10:51:11 -070097 sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
98 canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
99 sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
100 canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);
halcanary7a14b312015-10-01 07:28:13 -0700101
102 canvas->flush();
103 document->endPage();
104 document->close();
105 pdfData.reset(pdf.copyToData());
106 SkASSERT(pdfData);
107 pdf.reset();
108
reed9ce9d672016-03-17 10:51:11 -0700109 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -0700110
111 // This JPEG uses a nonstandard colorspace - it can not be
112 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -0700113 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -0700114}
halcanary96287f72015-05-07 11:46:59 -0700115
halcanary57f744e2016-09-09 11:41:59 -0700116DEF_TEST(SkPDF_JpegIdentification, r) {
halcanary96287f72015-05-07 11:46:59 -0700117 static struct {
118 const char* path;
119 bool isJfif;
120 SkJFIFInfo::Type type;
121 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
122 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
123 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
124 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
125 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
126 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
bungeman38d909e2016-08-02 14:40:46 -0700127 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
halcanary96287f72015-05-07 11:46:59 -0700128 if (!data) {
129 continue;
130 }
131 SkJFIFInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700132 bool isJfif = SkIsJFIF(data.get(), &info);
halcanary96287f72015-05-07 11:46:59 -0700133 if (isJfif != kTests[i].isJfif) {
134 ERRORF(r, "%s failed isJfif test", kTests[i].path);
135 continue;
136 }
137 if (!isJfif) {
138 continue; // not applicable
139 }
140 if (kTests[i].type != info.fType) {
141 ERRORF(r, "%s failed jfif type test", kTests[i].path);
142 continue;
143 }
halcanary7d571242016-02-24 17:59:16 -0800144 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
145 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700146 }
halcanary57f744e2016-09-09 11:41:59 -0700147
148 // Test several malformed jpegs.
149 SkJFIFInfo info;
150 {
151 static const char goodJpeg[] =
152 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
153 "\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"
154 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
155 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
156 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
157 size_t goodJpegLength = 177;
158 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
159 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
160 REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512));
161 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
162
163 // Not long enough to read first (SOI) segment marker.
164 data = SkData::MakeWithoutCopy(goodJpeg, 1);
165 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
166
167 // Not long enough to read second segment (APP0) marker.
168 data = SkData::MakeWithoutCopy(goodJpeg, 3);
169 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
170
171 // Not long enough to read second segment's length.
172 data = SkData::MakeWithoutCopy(goodJpeg, 5);
173 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
174
175 // APP0 segment is truncated.
176 data = SkData::MakeWithoutCopy(goodJpeg, 7);
177 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
178
179 // Missing SOF segment.
180 data = SkData::MakeWithoutCopy(goodJpeg, 89);
181 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
182 }
183 {
184 // JFIF tag missing.
185 static const char jpeg[] =
186 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
187 "\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"
188 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
189 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
190 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
191 size_t jpegLength = 177;
192 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
193 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
194 }
195 {
196 // APP0 segment short (byte 6 changed).
197 static const char jpeg[] =
198 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
199 "\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"
200 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
201 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
202 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
203 size_t jpegLength = 177;
204 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
205 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
206 }
207 {
208 // SOF segment short. ('\21' replaced with '\5')
209 static const char jpeg[] =
210 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
211 "\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"
212 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
213 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
214 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
215 size_t jpegLength = 177;
216 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
217 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
218 }
219 {
220 // Unsupported 12-bit components. ('\10' replaced with '\14')
221 static const char jpeg[] =
222 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
223 "\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"
224 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
225 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
226 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
227 size_t jpegLength = 177;
228 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
229 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
230 }
231 {
232 // Two color channels. ('\3' replaced with '\2')
233 static const char jpeg[] =
234 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
235 "\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"
236 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
237 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
238 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
239 size_t jpegLength = 177;
240 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
241 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
242 }
halcanary96287f72015-05-07 11:46:59 -0700243}