blob: 5185eb7bd4169efa97fe1772d2dd87c0a081856b [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());
45 if (!data && r->verbose()) {
46 SkDebugf("\n%s: Resource '%s' can not be found.\n",
47 test, filename);
48 }
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";
59 SkAutoTUnref<SkData> mandrillData(
60 load_resource(r, test, "mandrill_512_q075.jpg"));
61 SkAutoTUnref<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
62 if (!mandrillData || !cmykData) {
63 return;
64 }
halcanary7a14b312015-10-01 07:28:13 -070065 ////////////////////////////////////////////////////////////////////////////
halcanarydaefa5b2014-08-27 13:00:54 -070066 SkDynamicMemoryWStream pdf;
67 SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdf));
68 SkCanvas* canvas = document->beginPage(642, 1028);
69
70 canvas->clear(SK_ColorLTGRAY);
71
72 SkBitmap bm1(bitmap_from_data(mandrillData));
halcanary96fcdcc2015-08-27 07:41:13 -070073 canvas->drawBitmap(bm1, 65.0, 0.0, nullptr);
halcanarydaefa5b2014-08-27 13:00:54 -070074 SkBitmap bm2(bitmap_from_data(cmykData));
halcanary96fcdcc2015-08-27 07:41:13 -070075 canvas->drawBitmap(bm2, 0.0, 512.0, nullptr);
halcanarydaefa5b2014-08-27 13:00:54 -070076
77 canvas->flush();
78 document->endPage();
79 document->close();
80 SkAutoTUnref<SkData> pdfData(pdf.copyToData());
81 SkASSERT(pdfData);
82 pdf.reset();
83
halcanarya8448bc2015-04-17 13:27:24 -070084 REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
halcanarydaefa5b2014-08-27 13:00:54 -070085
86 // This JPEG uses a nonstandard colorspace - it can not be
87 // embedded into the PDF directly.
88 REPORTER_ASSERT(r, !is_subset_of(cmykData, pdfData));
halcanary7a14b312015-10-01 07:28:13 -070089 ////////////////////////////////////////////////////////////////////////////
90 pdf.reset();
91 document.reset(SkDocument::CreatePDF(&pdf));
92 canvas = document->beginPage(642, 1028);
93
94 canvas->clear(SK_ColorLTGRAY);
95
96 SkAutoTUnref<SkImage> im1(SkImage::NewFromEncoded(mandrillData));
97 canvas->drawImage(im1, 65.0, 0.0, nullptr);
98 SkAutoTUnref<SkImage> im2(SkImage::NewFromEncoded(cmykData));
99 canvas->drawImage(im2, 0.0, 512.0, nullptr);
100
101 canvas->flush();
102 document->endPage();
103 document->close();
104 pdfData.reset(pdf.copyToData());
105 SkASSERT(pdfData);
106 pdf.reset();
107
108 REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
109
110 // This JPEG uses a nonstandard colorspace - it can not be
111 // embedded into the PDF directly.
112 REPORTER_ASSERT(r, !is_subset_of(cmykData, pdfData));
halcanarydaefa5b2014-08-27 13:00:54 -0700113}
halcanary96287f72015-05-07 11:46:59 -0700114
115#include "SkJpegInfo.h"
116
117DEF_TEST(JpegIdentification, r) {
118 static struct {
119 const char* path;
120 bool isJfif;
121 SkJFIFInfo::Type type;
122 } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
123 {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
124 {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
125 {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
126 {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
127 for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
128 SkAutoTUnref<SkData> data(
129 load_resource(r, "JpegIdentification", kTests[i].path));
130 if (!data) {
131 continue;
132 }
133 SkJFIFInfo info;
134 bool isJfif = SkIsJFIF(data, &info);
135 if (isJfif != kTests[i].isJfif) {
136 ERRORF(r, "%s failed isJfif test", kTests[i].path);
137 continue;
138 }
139 if (!isJfif) {
140 continue; // not applicable
141 }
142 if (kTests[i].type != info.fType) {
143 ERRORF(r, "%s failed jfif type test", kTests[i].path);
144 continue;
145 }
146 if (r->verbose()) {
147 SkDebugf("\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
halcanary7a14b312015-10-01 07:28:13 -0700148 info.fSize.width(), info.fSize.height());
halcanary96287f72015-05-07 11:46:59 -0700149 }
150 }
151}