| 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" | 
|  | 12 | #include "SkJpegInfo.h" | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 13 | #include "SkStream.h" | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 14 |  | 
|  | 15 | #include "Resources.h" | 
|  | 16 | #include "Test.h" | 
|  | 17 |  | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 18 | static bool is_subset_of(SkData* smaller, SkData* larger) { | 
|  | 19 | SkASSERT(smaller && larger); | 
|  | 20 | if (smaller->size() > larger->size()) { | 
|  | 21 | return false; | 
|  | 22 | } | 
|  | 23 | size_t size = smaller->size(); | 
|  | 24 | size_t size_diff = larger->size() - size; | 
|  | 25 | for (size_t i = 0; i <= size_diff; ++i) { | 
|  | 26 | if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) { | 
|  | 27 | return true; | 
|  | 28 | } | 
|  | 29 | } | 
|  | 30 | return false; | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 |  | 
| bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 34 | static sk_sp<SkData> load_resource( | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 35 | skiatest::Reporter* r, const char* test, const char* filename) { | 
|  | 36 | SkString path(GetResourcePath(filename)); | 
| bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 37 | sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); | 
| halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 38 | if (!data) { | 
|  | 39 | INFOF(r, "\n%s: Resource '%s' can not be found.\n", | 
|  | 40 | test, filename); | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 41 | } | 
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | return data;  // May return nullptr. | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | /** | 
|  | 46 | *  Test that for Jpeg files that use the JFIF colorspace, they are | 
|  | 47 | *  directly embedded into the PDF (without re-encoding) when that | 
|  | 48 | *  makes sense. | 
|  | 49 | */ | 
| halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 50 | DEF_TEST(SkPDF_JpegEmbedTest, r) { | 
|  | 51 | REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r); | 
|  | 52 | const char test[] = "SkPDF_JpegEmbedTest"; | 
| reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 53 | sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); | 
|  | 54 | sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 55 | if (!mandrillData || !cmykData) { | 
|  | 56 | return; | 
|  | 57 | } | 
| halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 58 | //////////////////////////////////////////////////////////////////////////// | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 59 | SkDynamicMemoryWStream pdf; | 
| halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 60 | sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf)); | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 61 | SkCanvas* canvas = document->beginPage(642, 1028); | 
|  | 62 |  | 
|  | 63 | canvas->clear(SK_ColorLTGRAY); | 
|  | 64 |  | 
| reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 65 | sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData)); | 
|  | 66 | canvas->drawImage(im1.get(), 65.0, 0.0, nullptr); | 
|  | 67 | sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData)); | 
|  | 68 | canvas->drawImage(im2.get(), 0.0, 512.0, nullptr); | 
| halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | canvas->flush(); | 
|  | 71 | document->endPage(); | 
|  | 72 | document->close(); | 
| Mike Reed | 7614794 | 2016-10-25 09:57:13 -0400 | [diff] [blame^] | 73 | sk_sp<SkData> pdfData = pdf.detachAsData(); | 
| halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 74 | SkASSERT(pdfData); | 
| halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 75 |  | 
| reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 76 | REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); | 
| halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // This JPEG uses a nonstandard colorspace - it can not be | 
|  | 79 | // embedded into the PDF directly. | 
| reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 80 | REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); | 
| halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 81 | } | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 82 |  | 
| halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 83 | DEF_TEST(SkPDF_JpegIdentification, r) { | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 84 | static struct { | 
|  | 85 | const char* path; | 
|  | 86 | bool isJfif; | 
|  | 87 | SkJFIFInfo::Type type; | 
|  | 88 | } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale}, | 
|  | 89 | {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr}, | 
|  | 90 | {"grayscale.jpg", true, SkJFIFInfo::kGrayscale}, | 
|  | 91 | {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr}, | 
|  | 92 | {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}}; | 
|  | 93 | for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) { | 
| bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 94 | sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path)); | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 95 | if (!data) { | 
|  | 96 | continue; | 
|  | 97 | } | 
|  | 98 | SkJFIFInfo info; | 
| bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 99 | bool isJfif = SkIsJFIF(data.get(), &info); | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 100 | if (isJfif != kTests[i].isJfif) { | 
|  | 101 | ERRORF(r, "%s failed isJfif test", kTests[i].path); | 
|  | 102 | continue; | 
|  | 103 | } | 
|  | 104 | if (!isJfif) { | 
|  | 105 | continue;  // not applicable | 
|  | 106 | } | 
|  | 107 | if (kTests[i].type != info.fType) { | 
|  | 108 | ERRORF(r, "%s failed jfif type test", kTests[i].path); | 
|  | 109 | continue; | 
|  | 110 | } | 
| halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 111 | INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, | 
|  | 112 | info.fSize.width(), info.fSize.height()); | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 113 | } | 
| halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | // Test several malformed jpegs. | 
|  | 116 | SkJFIFInfo info; | 
|  | 117 | { | 
|  | 118 | static const char goodJpeg[] = | 
|  | 119 | "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 120 | "\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" | 
|  | 121 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 122 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 123 | "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; | 
|  | 124 | size_t goodJpegLength = 177; | 
|  | 125 | auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength); | 
|  | 126 | REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info)); | 
|  | 127 | REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512)); | 
|  | 128 | REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr); | 
|  | 129 |  | 
|  | 130 | // Not long enough to read first (SOI) segment marker. | 
|  | 131 | data = SkData::MakeWithoutCopy(goodJpeg, 1); | 
|  | 132 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 133 |  | 
|  | 134 | // Not long enough to read second segment (APP0) marker. | 
|  | 135 | data = SkData::MakeWithoutCopy(goodJpeg, 3); | 
|  | 136 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 137 |  | 
|  | 138 | // Not long enough to read second segment's length. | 
|  | 139 | data = SkData::MakeWithoutCopy(goodJpeg, 5); | 
|  | 140 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 141 |  | 
|  | 142 | // APP0 segment is truncated. | 
|  | 143 | data = SkData::MakeWithoutCopy(goodJpeg, 7); | 
|  | 144 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 145 |  | 
|  | 146 | // Missing SOF segment. | 
|  | 147 | data = SkData::MakeWithoutCopy(goodJpeg, 89); | 
|  | 148 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 149 | } | 
|  | 150 | { | 
|  | 151 | // JFIF tag missing. | 
|  | 152 | static const char jpeg[] = | 
|  | 153 | "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 154 | "\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" | 
|  | 155 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 156 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 157 | "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; | 
|  | 158 | size_t jpegLength = 177; | 
|  | 159 | auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); | 
|  | 160 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 161 | } | 
|  | 162 | { | 
|  | 163 | // APP0 segment short (byte 6 changed). | 
|  | 164 | static const char jpeg[] = | 
|  | 165 | "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 166 | "\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" | 
|  | 167 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 168 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 169 | "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; | 
|  | 170 | size_t jpegLength = 177; | 
|  | 171 | auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); | 
|  | 172 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 173 | } | 
|  | 174 | { | 
|  | 175 | // SOF segment short. ('\21' replaced with '\5') | 
|  | 176 | static const char jpeg[] = | 
|  | 177 | "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 178 | "\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" | 
|  | 179 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 180 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 181 | "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; | 
|  | 182 | size_t jpegLength = 177; | 
|  | 183 | auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); | 
|  | 184 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 185 | } | 
|  | 186 | { | 
|  | 187 | // Unsupported 12-bit components. ('\10' replaced with '\14') | 
|  | 188 | static const char jpeg[] = | 
|  | 189 | "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 190 | "\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" | 
|  | 191 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 192 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 193 | "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001"; | 
|  | 194 | size_t jpegLength = 177; | 
|  | 195 | auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); | 
|  | 196 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 197 | } | 
|  | 198 | { | 
|  | 199 | // Two color channels.  ('\3' replaced with '\2') | 
|  | 200 | static const char jpeg[] = | 
|  | 201 | "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7" | 
|  | 202 | "\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" | 
|  | 203 | "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t" | 
|  | 204 | "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222" | 
|  | 205 | "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001"; | 
|  | 206 | size_t jpegLength = 177; | 
|  | 207 | auto data = SkData::MakeWithoutCopy(jpeg, jpegLength); | 
|  | 208 | REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info)); | 
|  | 209 | } | 
| halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 210 | } |