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 
14 #include "Resources.h"
15 #include "Test.h"
16 
17 // Returned bitmap is lazy.  Only lazy bitmaps hold onto the original data.
bitmap_from_data(SkData * data)18 static SkBitmap bitmap_from_data(SkData* data) {
19     SkASSERT(data);
20     SkBitmap bm;
21     SkInstallDiscardablePixelRef(data, &bm);
22     return bm;
23 }
24 
is_subset_of(SkData * smaller,SkData * larger)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 
load_resource(skiatest::Reporter * r,const char * test,const char * filename)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());
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  */
DEF_TEST(PDFJpegEmbedTest,r)57 DEF_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 
84     REPORTER_ASSERT(r, is_subset_of(mandrillData, pdfData));
85 
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));
89 }
90 
91 #include "SkJpegInfo.h"
92 
DEF_TEST(JpegIdentification,r)93 DEF_TEST(JpegIdentification, r) {
94     static struct {
95         const char* path;
96         bool isJfif;
97         SkJFIFInfo::Type type;
98     } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
99                   {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
100                   {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
101                   {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
102                   {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
103     for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
104         SkAutoTUnref<SkData> data(
105                 load_resource(r, "JpegIdentification", kTests[i].path));
106         if (!data) {
107             continue;
108         }
109         SkJFIFInfo info;
110         bool isJfif = SkIsJFIF(data, &info);
111         if (isJfif != kTests[i].isJfif) {
112             ERRORF(r, "%s failed isJfif test", kTests[i].path);
113             continue;
114         }
115         if (!isJfif) {
116             continue;  // not applicable
117         }
118         if (kTests[i].type != info.fType) {
119             ERRORF(r, "%s failed jfif type test", kTests[i].path);
120             continue;
121         }
122         if (r->verbose()) {
123             SkDebugf("\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
124                      info.fWidth, info.fHeight);
125         }
126     }
127 }
128