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