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