blob: 133d84a3ff745e9b5177739c59f48c55e43a2975 [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;
reed5965c8a2015-01-07 18:04:45 -080021 SkInstallDiscardablePixelRef(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 }
49 return data; // May return NULL.
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 */
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 }
65
66 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));
73 canvas->drawBitmap(bm1, 65.0, 0.0, NULL);
74 SkBitmap bm2(bitmap_from_data(cmykData));
75 canvas->drawBitmap(bm2, 0.0, 512.0, NULL);
76
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));
halcanarydaefa5b2014-08-27 13:00:54 -070089}