Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(601)

Side by Side Diff: tests/PDFJpegEmbedTest.cpp

Issue 2322133003: SkPDF/Tests: imporve test coverage. (Closed)
Patch Set: toms nit Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/PDFInvalidBitmapTest.cpp ('k') | tests/PDFPrimitivesTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkCanvas.h"
9 #include "SkData.h"
8 #include "SkDocument.h" 10 #include "SkDocument.h"
9 #include "SkCanvas.h"
10 #include "SkImageGenerator.h" 11 #include "SkImageGenerator.h"
11 #include "SkData.h" 12 #include "SkJpegInfo.h"
12 #include "SkStream.h" 13 #include "SkStream.h"
13 14
14 #include "Resources.h" 15 #include "Resources.h"
15 #include "Test.h" 16 #include "Test.h"
16 17
17 // Returned bitmap is lazy. Only lazy bitmaps hold onto the original data. 18 // Returned bitmap is lazy. Only lazy bitmaps hold onto the original data.
18 static SkBitmap bitmap_from_data(SkData* data) { 19 static SkBitmap bitmap_from_data(SkData* data) {
19 SkASSERT(data); 20 SkASSERT(data);
20 SkBitmap bm; 21 SkBitmap bm;
21 SkDEPRECATED_InstallDiscardablePixelRef(data, &bm); 22 SkDEPRECATED_InstallDiscardablePixelRef(data, &bm);
(...skipping 25 matching lines...) Expand all
47 test, filename); 48 test, filename);
48 } 49 }
49 return data; // May return nullptr. 50 return data; // May return nullptr.
50 } 51 }
51 52
52 /** 53 /**
53 * Test that for Jpeg files that use the JFIF colorspace, they are 54 * Test that for Jpeg files that use the JFIF colorspace, they are
54 * directly embedded into the PDF (without re-encoding) when that 55 * directly embedded into the PDF (without re-encoding) when that
55 * makes sense. 56 * makes sense.
56 */ 57 */
57 DEF_TEST(PDFJpegEmbedTest, r) { 58 DEF_TEST(SkPDF_JpegEmbedTest, r) {
58 const char test[] = "PDFJpegEmbedTest"; 59 REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
60 const char test[] = "SkPDF_JpegEmbedTest";
59 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); 61 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
60 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); 62 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
61 if (!mandrillData || !cmykData) { 63 if (!mandrillData || !cmykData) {
62 return; 64 return;
63 } 65 }
64 //////////////////////////////////////////////////////////////////////////// 66 ////////////////////////////////////////////////////////////////////////////
65 SkDynamicMemoryWStream pdf; 67 SkDynamicMemoryWStream pdf;
66 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf)); 68 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
67 SkCanvas* canvas = document->beginPage(642, 1028); 69 SkCanvas* canvas = document->beginPage(642, 1028);
68 70
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 SkASSERT(pdfData); 106 SkASSERT(pdfData);
105 pdf.reset(); 107 pdf.reset();
106 108
107 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); 109 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
108 110
109 // This JPEG uses a nonstandard colorspace - it can not be 111 // This JPEG uses a nonstandard colorspace - it can not be
110 // embedded into the PDF directly. 112 // embedded into the PDF directly.
111 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); 113 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
112 } 114 }
113 115
114 #include "SkJpegInfo.h" 116 DEF_TEST(SkPDF_JpegIdentification, r) {
115
116 DEF_TEST(JpegIdentification, r) {
117 static struct { 117 static struct {
118 const char* path; 118 const char* path;
119 bool isJfif; 119 bool isJfif;
120 SkJFIFInfo::Type type; 120 SkJFIFInfo::Type type;
121 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale}, 121 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
122 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr}, 122 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
123 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale}, 123 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
124 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr}, 124 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
125 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}}; 125 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
126 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) { 126 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
(...skipping 10 matching lines...) Expand all
137 if (!isJfif) { 137 if (!isJfif) {
138 continue; // not applicable 138 continue; // not applicable
139 } 139 }
140 if (kTests[i].type != info.fType) { 140 if (kTests[i].type != info.fType) {
141 ERRORF(r, "%s failed jfif type test", kTests[i].path); 141 ERRORF(r, "%s failed jfif type test", kTests[i].path);
142 continue; 142 continue;
143 } 143 }
144 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, 144 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
145 info.fSize.width(), info.fSize.height()); 145 info.fSize.width(), info.fSize.height());
146 } 146 }
147
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 }
147 } 243 }
OLDNEW
« no previous file with comments | « tests/PDFInvalidBitmapTest.cpp ('k') | tests/PDFPrimitivesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698