blob: 2dcbdd05d8469680a625eda0666f5ac735e8ad3b [file] [log] [blame]
halcanarydaefa5b2014-08-27 13:00:54 -07001/*
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"
halcanarydaefa5b2014-08-27 13:00:54 -070013
14#include "Resources.h"
15#include "Test.h"
16
17// Returned bitmap is lazy. Only lazy bitmaps hold onto the original data.
18static SkBitmap bitmap_from_data(SkData* data) {
19 SkASSERT(data);
20 SkBitmap bm;
reedd1146452015-09-25 06:56:57 -070021 SkDEPRECATED_InstallDiscardablePixelRef(data, &bm);
halcanarydaefa5b2014-08-27 13:00:54 -070022 return bm;
23}
24
25static 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
41static 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());
halcanary7d571242016-02-24 17:59:16 -080045 if (!data) {
46 INFOF(r, "\n%s: Resource '%s' can not be found.\n",
47 test, filename);
halcanarydaefa5b2014-08-27 13:00:54 -070048 }
halcanary96fcdcc2015-08-27 07:41:13 -070049 return data; // May return nullptr.
halcanarydaefa5b2014-08-27 13:00:54 -070050}
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 */
57DEF_TEST(PDFJpegEmbedTest, r) {
58 const char test[] = "PDFJpegEmbedTest";
reed9ce9d672016-03-17 10:51:11 -070059 sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
60 sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
halcanarydaefa5b2014-08-27 13:00:54 -070061 if (!mandrillData || !cmykData) {
62 return;
63 }
halcanary7a14b312015-10-01 07:28:13 -070064 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070065 SkDynamicMemoryWStream pdf;
66 SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdf));
67 SkCanvas* canvas = document->beginPage(642, 1028);
68
69 canvas->clear(SK_ColorLTGRAY);
70
reed9ce9d672016-03-17 10:51:11 -070071 SkBitmap bm1(bitmap_from_data(mandrillData.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070072 canvas->drawBitmap(bm1, 65.0, 0.0, nullptr);
reed9ce9d672016-03-17 10:51:11 -070073 SkBitmap bm2(bitmap_from_data(cmykData.get()));
halcanary96fcdcc2015-08-27 07:41:13 -070074 canvas->drawBitmap(bm2, 0.0, 512.0, nullptr);
halcanarydaefa5b2014-08-27 13:00:54 -070075
76 canvas->flush();
77 document->endPage();
78 document->close();
79 SkAutoTUnref<SkData> pdfData(pdf.copyToData());
80 SkASSERT(pdfData);
81 pdf.reset();
82
reed9ce9d672016-03-17 10:51:11 -070083 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -070084
85 // This JPEG uses a nonstandard colorspace - it can not be
86 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -070087 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -070088 ////////////////////////////////////////////////////////////////////////////
89 pdf.reset();
90 document.reset(SkDocument::CreatePDF(&pdf));
91 canvas = document->beginPage(642, 1028);
92
93 canvas->clear(SK_ColorLTGRAY);
94
reed9ce9d672016-03-17 10:51:11 -070095 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);
halcanary7a14b312015-10-01 07:28:13 -070099
100 canvas->flush();
101 document->endPage();
102 document->close();
103 pdfData.reset(pdf.copyToData());
104 SkASSERT(pdfData);
105 pdf.reset();
106
reed9ce9d672016-03-17 10:51:11 -0700107 REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));
halcanary7a14b312015-10-01 07:28:13 -0700108
109 // This JPEG uses a nonstandard colorspace - it can not be
110 // embedded into the PDF directly.
reed9ce9d672016-03-17 10:51:11 -0700111 REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
halcanarydaefa5b2014-08-27 13:00:54 -0700112}
halcanary96287f72015-05-07 11:46:59 -0700113
114#include "SkJpegInfo.h"
115
116DEF_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 }
halcanary7d571242016-02-24 17:59:16 -0800145 INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
146 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700147 }
148}