aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/PDFJpegEmbedTest.cpp
blob: 100c9b3f11881231296b23aca9245b33a1946746 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkCanvas.h"
#include "SkData.h"
#include "SkDocument.h"
#include "SkImageGenerator.h"
#include "SkStream.h"

#include "Resources.h"
#include "Test.h"

static bool is_subset_of(SkData* smaller, SkData* larger) {
    SkASSERT(smaller && larger);
    if (smaller->size() > larger->size()) {
        return false;
    }
    size_t size = smaller->size();
    size_t size_diff = larger->size() - size;
    for (size_t i = 0; i <= size_diff; ++i) {
        if (0 == memcmp(larger->bytes() + i, smaller->bytes(), size)) {
            return true;
        }
    }
    return false;
}


static sk_sp<SkData> load_resource(
        skiatest::Reporter* r, const char* test, const char* filename) {
    SkString path(GetResourcePath(filename));
    sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
    if (!data) {
        INFOF(r, "\n%s: Resource '%s' can not be found.\n",
              test, filename);
    }
    return data;  // May return nullptr.
}

/**
 *  Test that for Jpeg files that use the JFIF colorspace, they are
 *  directly embedded into the PDF (without re-encoding) when that
 *  makes sense.
 */
DEF_TEST(SkPDF_JpegEmbedTest, r) {
    REQUIRE_PDF_DOCUMENT(SkPDF_JpegEmbedTest, r);
    const char test[] = "SkPDF_JpegEmbedTest";
    sk_sp<SkData> mandrillData(load_resource(r, test, "mandrill_512_q075.jpg"));
    sk_sp<SkData> cmykData(load_resource(r, test, "CMYK.jpg"));
    if (!mandrillData || !cmykData) {
        return;
    }
    ////////////////////////////////////////////////////////////////////////////
    SkDynamicMemoryWStream pdf;
    sk_sp<SkDocument> document(SkDocument::MakePDF(&pdf));
    SkCanvas* canvas = document->beginPage(642, 1028);

    canvas->clear(SK_ColorLTGRAY);

    sk_sp<SkImage> im1(SkImage::MakeFromEncoded(mandrillData));
    canvas->drawImage(im1.get(), 65.0, 0.0, nullptr);
    sk_sp<SkImage> im2(SkImage::MakeFromEncoded(cmykData));
    canvas->drawImage(im2.get(), 0.0, 512.0, nullptr);

    canvas->flush();
    document->endPage();
    document->close();
    sk_sp<SkData> pdfData = pdf.detachAsData();
    SkASSERT(pdfData);

    REPORTER_ASSERT(r, is_subset_of(mandrillData.get(), pdfData.get()));

    // This JPEG uses a nonstandard colorspace - it can not be
    // embedded into the PDF directly.
    REPORTER_ASSERT(r, !is_subset_of(cmykData.get(), pdfData.get()));
}

#ifdef SK_SUPPORT_PDF

#include "SkJpegInfo.h"

DEF_TEST(SkPDF_JpegIdentification, r) {
    static struct {
        const char* path;
        bool isJfif;
        SkJFIFInfo::Type type;
    } kTests[] = {{"CMYK.jpg", false, SkJFIFInfo::kGrayscale},
                  {"color_wheel.jpg", true, SkJFIFInfo::kYCbCr},
                  {"grayscale.jpg", true, SkJFIFInfo::kGrayscale},
                  {"mandrill_512_q075.jpg", true, SkJFIFInfo::kYCbCr},
                  {"randPixels.jpg", true, SkJFIFInfo::kYCbCr}};
    for (size_t i = 0; i < SK_ARRAY_COUNT(kTests); ++i) {
        sk_sp<SkData> data(load_resource(r, "JpegIdentification", kTests[i].path));
        if (!data) {
            continue;
        }
        SkJFIFInfo info;
        bool isJfif = SkIsJFIF(data.get(), &info);
        if (isJfif != kTests[i].isJfif) {
            ERRORF(r, "%s failed isJfif test", kTests[i].path);
            continue;
        }
        if (!isJfif) {
            continue;  // not applicable
        }
        if (kTests[i].type != info.fType) {
            ERRORF(r, "%s failed jfif type test", kTests[i].path);
            continue;
        }
        INFOF(r, "\nJpegIdentification: %s [%d x %d]\n", kTests[i].path,
              info.fSize.width(), info.fSize.height());
    }

    // Test several malformed jpegs.
    SkJFIFInfo info;
    {
        static const char goodJpeg[] =
            "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
        size_t goodJpegLength = 177;
        auto data = SkData::MakeWithoutCopy(goodJpeg, goodJpegLength);
        REPORTER_ASSERT(r, SkIsJFIF(data.get(), &info));
        REPORTER_ASSERT(r, info.fSize == SkISize::Make(512, 512));
        REPORTER_ASSERT(r, info.fType == SkJFIFInfo::kYCbCr);

        // Not long enough to read first (SOI) segment marker.
        data = SkData::MakeWithoutCopy(goodJpeg, 1);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));

        // Not long enough to read second segment (APP0) marker.
        data = SkData::MakeWithoutCopy(goodJpeg, 3);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));

        // Not long enough to read second segment's length.
        data = SkData::MakeWithoutCopy(goodJpeg, 5);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));

        // APP0 segment is truncated.
        data = SkData::MakeWithoutCopy(goodJpeg, 7);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));

        // Missing SOF segment.
        data = SkData::MakeWithoutCopy(goodJpeg, 89);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
    {
        // JFIF tag missing.
        static const char jpeg[] =
            "\377\330\377\340\0\20JFIX\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
        size_t jpegLength = 177;
        auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
    {
        // APP0 segment short (byte 6 changed).
        static const char jpeg[] =
            "\377\330\377\340\0\5JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\21\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
        size_t jpegLength = 177;
        auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
    {
        // SOF segment short. ('\21' replaced with '\5')
        static const char jpeg[] =
            "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\5\10\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
        size_t jpegLength = 177;
        auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
    {
        // Unsupported 12-bit components. ('\10' replaced with '\14')
        static const char jpeg[] =
            "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\21\14\2\0\2\0\3\1\"\0\2\21\1\3\21\001";
        size_t jpegLength = 177;
        auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
    {
        // Two color channels.  ('\3' replaced with '\2')
        static const char jpeg[] =
            "\377\330\377\340\0\20JFIF\0\1\1\0\0\1\0\1\0\0\377\333\0C\0\10\6\6\7"
            "\6\5\10\7\7\7\t\t\10\n\14\24\r\14\13\13\14\31\22\23\17\24\35\32\37"
            "\36\35\32\34\34 $.' \",#\34\34(7),01444\37'9=82<.342\377\333\0C\1\t"
            "\t\t\14\13\14\30\r\r\0302!\34!222222222222222222222222222222222222"
            "22222222222222\377\300\0\21\10\2\0\2\0\2\1\"\0\2\21\1\3\21\001";
        size_t jpegLength = 177;
        auto data = SkData::MakeWithoutCopy(jpeg, jpegLength);
        REPORTER_ASSERT(r, !SkIsJFIF(data.get(), &info));
    }
}
#endif