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 "SkDocument.h" |
| 11 | #include "SkImageGenerator.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) { |
| 35 | SkString path(GetResourcePath(filename)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 36 | sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 37 | if (!data) { |
| 38 | INFOF(r, "\n%s: Resource '%s' can not be found.\n", |
| 39 | test, filename); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 40 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 41 | return data; // May return nullptr. |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 42 | } |
| 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 | */ |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 49 | DEF_TEST(SkPDF_JpegEmbedTest, r) { |
| 50 | REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r); |
| 51 | const char test[] = "SkPDF_JpegEmbedTest"; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 52 | sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); |
| 53 | sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 54 | if (!mandrillData || !cmykData) { |
| 55 | return; |
| 56 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 57 | //////////////////////////////////////////////////////////////////////////// |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 58 | SkDynamicMemoryWStream pdf; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 59 | sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf)); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 60 | SkCanvas* canvas = document->beginPage(642, 1028); |
| 61 | |
| 62 | canvas->clear(SK_ColorLTGRAY); |
| 63 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 64 | 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); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 68 | |
| 69 | canvas->flush(); |
| 70 | document->endPage(); |
| 71 | document->close(); |
Mike Reed | 7614794 | 2016-10-25 09:57:13 -0400 | [diff] [blame] | 72 | sk_sp<SkData> pdfData = pdf.detachAsData(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 73 | SkASSERT(pdfData); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 74 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 75 | REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 76 | |
| 77 | // This JPEG uses a nonstandard colorspace - it can not be |
| 78 | // embedded into the PDF directly. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 79 | REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 80 | } |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 81 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 82 | #ifdef SK_SUPPORT_PDF |
| 83 | |
| 84 | #include "SkJpegInfo.h" |
| 85 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 86 | DEF_TEST(SkPDF_JpegIdentification, r) { |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 87 | 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) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 97 | sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path)); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 98 | if (!data) { |
| 99 | continue; |
| 100 | } |
| 101 | SkJFIFInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 102 | bool isJfif = SkIsJFIF(data.get(), &info); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 103 | 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 | } |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 114 | INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, |
| 115 | info.fSize.width(), info.fSize.height()); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 116 | } |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 117 | |
| 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 | } |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 213 | } |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 214 | #endif |