blob: 100c9b3f11881231296b23aca9245b33a1946746 [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"
halcanarydaefa5b2014-08-27 13:00:54 -070012#include "SkStream.h"
halcanarydaefa5b2014-08-27 13:00:54 -070013
14#include "Resources.h"
15#include "Test.h"
16
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) {
35 SkString path(GetResourcePath(filename));
bungeman38d909e2016-08-02 14:40:46 -070036 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
halcanary7d571242016-02-24 17:59:16 -080037 if (!data) {
38 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
39 test, filename);
halcanarydaefa5b2014-08-27 13:00:54 -070040 }
halcanary96fcdcc2015-08-27 07:41:13 -070041 return data; // May return nullptr.
halcanarydaefa5b2014-08-27 13:00:54 -070042}
43
44/**
45 * Test that for Jpeg files that use the JFIF colorspace, they are
46 * directly embedded into the PDF (without re-encoding) when that
47 * makes sense.
48 */
halcanary57f744e2016-09-09 11:41:59 -070049DEF_TEST(SkPDF_JpegEmbedTest, r) {
50 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
51 const char test[] = "SkPDF_JpegEmbedTest";
reed9ce9d672016-03-17 10:51:11 -070052 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
53 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
halcanarydaefa5b2014-08-27 13:00:54 -070054 if (!mandrillData || !cmykData) {
55 return;
56 }
halcanary7a14b312015-10-01 07:28:13 -070057 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070058 SkDynamicMemoryWStream pdf;
halcanary4b656662016-04-27 07:45:18 -070059 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
halcanarydaefa5b2014-08-27 13:00:54 -070060 SkCanvas* canvas = document->beginPage(642, 1028);
61
62 canvas->clear(SK_ColorLTGRAY);
63
reed9ce9d672016-03-17 10:51:11 -070064 sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
65 canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
66 sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
67 canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);
halcanary7a14b312015-10-01 07:28:13 -070068
69 canvas->flush();
70 document->endPage();
71 document->close();
Mike Reed76147942016-10-25 09:57:13 -040072 sk_sp<SkData> pdfData = pdf.detachAsData();
halcanary7a14b312015-10-01 07:28:13 -070073 SkASSERT(pdfData);
halcanary7a14b312015-10-01 07:28:13 -070074
reed9ce9d672016-03-17 10:51:11 -070075 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
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
84#include "SkJpegInfo.h"
85
halcanary57f744e2016-09-09 11:41:59 -070086DEF_TEST(SkPDF_JpegIdentification, r) {
halcanary96287f72015-05-07 11:46:59 -070087 static struct {
88 const char* path;
89 bool isJfif;
90 SkJFIFInfo::Type type;
91 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
92 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
93 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
94 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
95 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
96 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
bungeman38d909e2016-08-02 14:40:46 -070097 sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
halcanary96287f72015-05-07 11:46:59 -070098 if (!data) {
99 continue;
100 }
101 SkJFIFInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700102 bool isJfif = SkIsJFIF(data.get(), &info);
halcanary96287f72015-05-07 11:46:59 -0700103 if (isJfif != kTests[i].isJfif) {
104 ERRORF(r, "%s failed isJfif test", kTests[i].path);
105 continue;
106 }
107 if (!isJfif) {
108 continue; // not applicable
109 }
110 if (kTests[i].type != info.fType) {
111 ERRORF(r, "%s failed jfif type test", kTests[i].path);
112 continue;
113 }
halcanary7d571242016-02-24 17:59:16 -0800114 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
115 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700116 }
halcanary57f744e2016-09-09 11:41:59 -0700117
118 // Test several malformed jpegs.
119 SkJFIFInfo info;
120 {
121 static const char goodJpeg[] =
122 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
123 "\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"
124 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
125 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
126 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
127 size_t goodJpegLength = 177;
128 auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
129 REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
130 REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512));
131 REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);
132
133 // Not long enough to read first (SOI) segment marker.
134 data = SkData::MakeWithoutCopy(goodJpeg, 1);
135 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
136
137 // Not long enough to read second segment (APP0) marker.
138 data = SkData::MakeWithoutCopy(goodJpeg, 3);
139 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
140
141 // Not long enough to read second segment's length.
142 data = SkData::MakeWithoutCopy(goodJpeg, 5);
143 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
144
145 // APP0 segment is truncated.
146 data = SkData::MakeWithoutCopy(goodJpeg, 7);
147 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
148
149 // Missing SOF segment.
150 data = SkData::MakeWithoutCopy(goodJpeg, 89);
151 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
152 }
153 {
154 // JFIF tag missing.
155 static const char jpeg[] =
156 "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
157 "\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"
158 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
159 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
160 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
161 size_t jpegLength = 177;
162 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
163 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
164 }
165 {
166 // APP0 segment short (byte 6 changed).
167 static const char jpeg[] =
168 "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
169 "\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"
170 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
171 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
172 "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
173 size_t jpegLength = 177;
174 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
175 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
176 }
177 {
178 // SOF segment short. ('\21' replaced with '\5')
179 static const char jpeg[] =
180 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
181 "\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"
182 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
183 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
184 "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
185 size_t jpegLength = 177;
186 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
187 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
188 }
189 {
190 // Unsupported 12-bit components. ('\10' replaced with '\14')
191 static const char jpeg[] =
192 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
193 "\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"
194 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
195 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
196 "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
197 size_t jpegLength = 177;
198 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
199 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
200 }
201 {
202 // Two color channels. ('\3' replaced with '\2')
203 static const char jpeg[] =
204 "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
205 "\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"
206 "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
207 "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
208 "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
209 size_t jpegLength = 177;
210 auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
211 REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
212 }
halcanary96287f72015-05-07 11:46:59 -0700213}
Hal Canary43fb7a02016-12-30 13:09:03 -0500214#endif