• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
13 #include "SkDecodingImageGenerator.h"
14 
15 #include "Resources.h"
16 #include "Test.h"
17 
18 // Returned bitmap is lazy.  Only lazy bitmaps hold onto the original data.
bitmap_from_data(SkData * data)19 static SkBitmap bitmap_from_data(SkData* data) {
20     SkASSERT(data);
21     SkBitmap bm;
22     SkInstallDiscardablePixelRef(
23             SkDecodingImageGenerator::Create(
24                     data, SkDecodingImageGenerator::Options()), &bm);
25     return bm;
26 }
27 
is_subset_of(SkData * smaller,SkData * larger)28 static bool is_subset_of(SkData* smaller, SkData* larger) {
29     SkASSERT(smaller && larger);
30     if (smaller->size() > larger->size()) {
31         return false;
32     }
33     size_t size = smaller->size();
34     size_t size_diff = larger->size() - size;
35     for (size_t i = 0; i <= size_diff; ++i) {
36         if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
37             return true;
38         }
39     }
40     return false;
41 }
42 
43 
load_resource(skiatest::Reporter * r,const char * test,const char * filename)44 static SkData* load_resource(
45         skiatest::Reporter* r, const char* test, const char* filename) {
46     SkString path(GetResourcePath(filename));
47     SkData* data = SkData::NewFromFileName(path.c_str());
48     if (!data && r->verbose()) {
49         SkDebugf("\n%s: Resource '%s' can not be found.\n",
50                  test, filename);
51     }
52     return data;  // May return NULL.
53 }
54 
55 /**
56  *  Test that for Jpeg files that use the JFIF colorspace, they are
57  *  directly embedded into the PDF (without re-encoding) when that
58  *  makes sense.
59  */
DEF_TEST(PDFJpegEmbedTest,r)60 DEF_TEST(PDFJpegEmbedTest, r) {
61     const char test[] = "PDFJpegEmbedTest";
62     SkAutoTUnref<SkData> mandrillData(
63             load_resource(r, test, "mandrill_512_q075.jpg"));
64     SkAutoTUnref<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
65     if (!mandrillData || !cmykData) {
66         return;
67     }
68 
69     SkDynamicMemoryWStream pdf;
70     SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdf));
71     SkCanvas* canvas = document->beginPage(642, 1028);
72 
73     canvas->clear(SK_ColorLTGRAY);
74 
75     SkBitmap bm1(bitmap_from_data(mandrillData));
76     canvas->drawBitmap(bm1, 65.0, 0.0, NULL);
77     SkBitmap bm2(bitmap_from_data(cmykData));
78     canvas->drawBitmap(bm2, 0.0, 512.0, NULL);
79 
80     canvas->flush();
81     document->endPage();
82     document->close();
83     SkAutoTUnref<SkData> pdfData(pdf.copyToData());
84     SkASSERT(pdfData);
85     pdf.reset();
86 
87     REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
88 
89     // This JPEG uses a nonstandard colorspace - it can not be
90     // embedded into the PDF directly.
91     REPORTER_ASSERT(r, !is_subset_of(cmykData, pdfData));
92 
93     // The following is for debugging purposes only.
94     const char* outputPath = getenv("SKIA_TESTS_PDF_JPEG_EMBED_OUTPUT_PATH");
95     if (outputPath) {
96         SkFILEWStream output(outputPath);
97         if (output.isValid()) {
98             output.write(pdfData->data(), pdfData->size());
99         }
100     }
101 }
102