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 | |
| 18 | // Returned bitmap is lazy. Only lazy bitmaps hold onto the original data. |
| 19 | static SkBitmap bitmap_from_data(SkData* data) { |
| 20 | SkASSERT(data); |
| 21 | SkBitmap bm; |
reed | d114645 | 2015-09-25 06:56:57 -0700 | [diff] [blame] | 22 | SkDEPRECATED_InstallDiscardablePixelRef(data, &bm); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 23 | return bm; |
| 24 | } |
| 25 | |
| 26 | static bool is_subset_of(SkData* smaller, SkData* larger) { |
| 27 | SkASSERT(smaller && larger); |
| 28 | if (smaller->size() > larger->size()) { |
| 29 | return false; |
| 30 | } |
| 31 | size_t size = smaller->size(); |
| 32 | size_t size_diff = larger->size() - size; |
| 33 | for (size_t i = 0; i <= size_diff; ++i) { |
| 34 | if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 42 | static sk_sp<SkData> load_resource( |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 43 | skiatest::Reporter* r, const char* test, const char* filename) { |
| 44 | SkString path(GetResourcePath(filename)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 45 | sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str())); |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 46 | if (!data) { |
| 47 | INFOF(r, "\n%s: Resource '%s' can not be found.\n", |
| 48 | test, filename); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 49 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | return data; // May return nullptr. |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Test that for Jpeg files that use the JFIF colorspace, they are |
| 55 | * directly embedded into the PDF (without re-encoding) when that |
| 56 | * makes sense. |
| 57 | */ |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame^] | 58 | DEF_TEST(SkPDF_JpegEmbedTest, r) { |
| 59 | REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r); |
| 60 | const char test[] = "SkPDF_JpegEmbedTest"; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 61 | sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg")); |
| 62 | sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg")); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 63 | if (!mandrillData || !cmykData) { |
| 64 | return; |
| 65 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 66 | //////////////////////////////////////////////////////////////////////////// |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 67 | SkDynamicMemoryWStream pdf; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 68 | sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf)); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 69 | SkCanvas* canvas = document->beginPage(642, 1028); |
| 70 | |
| 71 | canvas->clear(SK_ColorLTGRAY); |
| 72 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 73 | SkBitmap bm1(bitmap_from_data(mandrillData.get())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | canvas->drawBitmap(bm1, 65.0, 0.0, nullptr); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 75 | SkBitmap bm2(bitmap_from_data(cmykData.get())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | canvas->drawBitmap(bm2, 0.0, 512.0, nullptr); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 77 | |
| 78 | canvas->flush(); |
| 79 | document->endPage(); |
| 80 | document->close(); |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 81 | sk_sp<SkData> pdfData(pdf.copyToData()); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 82 | SkASSERT(pdfData); |
| 83 | pdf.reset(); |
| 84 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 85 | REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 86 | |
| 87 | // This JPEG uses a nonstandard colorspace - it can not be |
| 88 | // embedded into the PDF directly. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 89 | REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 90 | //////////////////////////////////////////////////////////////////////////// |
| 91 | pdf.reset(); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 92 | document = SkDocument::MakePDF(&pdf); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 93 | canvas = document->beginPage(642, 1028); |
| 94 | |
| 95 | canvas->clear(SK_ColorLTGRAY); |
| 96 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 97 | sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData)); |
| 98 | canvas->drawImage(im1.get(), 65.0, 0.0, nullptr); |
| 99 | sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData)); |
| 100 | canvas->drawImage(im2.get(), 0.0, 512.0, nullptr); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 101 | |
| 102 | canvas->flush(); |
| 103 | document->endPage(); |
| 104 | document->close(); |
| 105 | pdfData.reset(pdf.copyToData()); |
| 106 | SkASSERT(pdfData); |
| 107 | pdf.reset(); |
| 108 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 109 | REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get())); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 110 | |
| 111 | // This JPEG uses a nonstandard colorspace - it can not be |
| 112 | // embedded into the PDF directly. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 113 | REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get())); |
halcanary | daefa5b | 2014-08-27 13:00:54 -0700 | [diff] [blame] | 114 | } |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 115 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame^] | 116 | DEF_TEST(SkPDF_JpegIdentification, r) { |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 117 | static struct { |
| 118 | const char* path; |
| 119 | bool isJfif; |
| 120 | SkJFIFInfo::Type type; |
| 121 | } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale}, |
| 122 | {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr}, |
| 123 | {"grayscale.jpg", true, SkJFIFInfo::kGrayscale}, |
| 124 | {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr}, |
| 125 | {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}}; |
| 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 127 | sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path)); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 128 | if (!data) { |
| 129 | continue; |
| 130 | } |
| 131 | SkJFIFInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 132 | bool isJfif = SkIsJFIF(data.get(), &info); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 133 | if (isJfif != kTests[i].isJfif) { |
| 134 | ERRORF(r, "%s failed isJfif test", kTests[i].path); |
| 135 | continue; |
| 136 | } |
| 137 | if (!isJfif) { |
| 138 | continue; // not applicable |
| 139 | } |
| 140 | if (kTests[i].type != info.fType) { |
| 141 | ERRORF(r, "%s failed jfif type test", kTests[i].path); |
| 142 | continue; |
| 143 | } |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 144 | INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path, |
| 145 | info.fSize.width(), info.fSize.height()); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 146 | } |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame^] | 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 | } |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 243 | } |