aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkPDFBitmap.cpp
blob: 5774360a7cce07cf8ab2e118ed0c421bafdebf10 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkColorPriv.h"
#include "SkFlate.h"
#include "SkPDFBitmap.h"
#include "SkPDFCanon.h"
#include "SkPDFCatalog.h"
#include "SkPDFDocument.h"
#include "SkStream.h"
#include "SkUnPreMultiply.h"

////////////////////////////////////////////////////////////////////////////////

static void pdf_stream_begin(SkWStream* stream) {
    static const char streamBegin[] = " stream\n";
    stream->write(streamBegin, strlen(streamBegin));
}

static void pdf_stream_end(SkWStream* stream) {
    static const char streamEnd[] = "\nendstream";
    stream->write(streamEnd, strlen(streamEnd));
}

static size_t pixel_count(const SkBitmap& bm) {
    return SkToSizeT(bm.width()) * SkToSizeT(bm.height());
}

// write a single byte to a stream n times.
static void fill_stream(SkWStream* out, char value, size_t n) {
    char buffer[4096];
    memset(buffer, value, sizeof(buffer));
    while (n) {
        size_t k = SkTMin(n, sizeof(buffer));
        out->write(buffer, k);
        n -= k;
    }
}

static SkPMColor get_pmcolor_neighbor_avg_color(const SkBitmap& bitmap,
                                                int xOrig,
                                                int yOrig) {
    SkASSERT(kN32_SkColorType == bitmap.colorType());
    SkASSERT(bitmap.getPixels());
    uint8_t count = 0;
    unsigned r = 0;
    unsigned g = 0;
    unsigned b = 0;
    for (int y = yOrig - 1; y <= yOrig + 1; ++y) {
        if (y < 0 || y >= bitmap.height()) {
            continue;
        }
        uint32_t* src = bitmap.getAddr32(0, y);
        for (int x = xOrig - 1; x <= xOrig + 1; ++x) {
            if (x < 0 || x >= bitmap.width()) {
                continue;
            }
            SkPMColor pmColor = src[x];
            U8CPU alpha = SkGetPackedA32(pmColor);
            if (alpha != SK_AlphaTRANSPARENT) {
                uint32_t s = SkUnPreMultiply::GetScale(alpha);
                r += SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(pmColor));
                g += SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(pmColor));
                b += SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(pmColor));
                ++count;
            }
        }
    }
    if (count == 0) {
        return SkPackARGB32NoCheck(SK_AlphaOPAQUE, 0, 0, 0);
    } else {
        return SkPackARGB32NoCheck(
                SK_AlphaOPAQUE, r / count, g / count, b / count);
    }
}

static void pmcolor_to_rgb24(const SkBitmap& bm, SkWStream* out) {
    SkASSERT(kN32_SkColorType == bm.colorType());
    if (!bm.getPixels()) {
        fill_stream(out, '\xFF', 3 * pixel_count(bm));
        return;
    }
    size_t scanlineLength = 3 * bm.width();
    SkAutoTMalloc<uint8_t> scanline(scanlineLength);
    for (int y = 0; y < bm.height(); ++y) {
        uint8_t* dst = scanline.get();
        const SkPMColor* src = bm.getAddr32(0, y);
        for (int x = 0; x < bm.width(); ++x) {
            SkPMColor color = *src++;
            U8CPU alpha = SkGetPackedA32(color);
            if (alpha != SK_AlphaTRANSPARENT) {
                uint32_t s = SkUnPreMultiply::GetScale(alpha);
                *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(color));
                *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedG32(color));
                *dst++ = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(color));
            } else {
                /* It is necessary to average the color component of
                   transparent pixels with their surrounding neighbors
                   since the PDF renderer may separately re-sample the
                   alpha and color channels when the image is not
                   displayed at its native resolution. Since an alpha
                   of zero gives no information about the color
                   component, the pathological case is a white image
                   with sharp transparency bounds - the color channel
                   goes to black, and the should-be-transparent pixels
                   are rendered as grey because of the separate soft
                   mask and color resizing. e.g.: gm/bitmappremul.cpp */
                color = get_pmcolor_neighbor_avg_color(bm, x, y);
                *dst++ = SkGetPackedR32(color);
                *dst++ = SkGetPackedG32(color);
                *dst++ = SkGetPackedB32(color);
            }
        }
        out->write(scanline.get(), scanlineLength);
    }
}

static void pmcolor_alpha_to_a8(const SkBitmap& bm, SkWStream* out) {
    SkASSERT(kN32_SkColorType == bm.colorType());
    if (!bm.getPixels()) {
        fill_stream(out, '\xFF', pixel_count(bm));
        return;
    }
    size_t scanlineLength = bm.width();
    SkAutoTMalloc<uint8_t> scanline(scanlineLength);
    for (int y = 0; y < bm.height(); ++y) {
        uint8_t* dst = scanline.get();
        const SkPMColor* src = bm.getAddr32(0, y);
        for (int x = 0; x < bm.width(); ++x) {
            *dst++ = SkGetPackedA32(*src++);
        }
        out->write(scanline.get(), scanlineLength);
    }
}

////////////////////////////////////////////////////////////////////////////////

namespace {
// This SkPDFObject only outputs the alpha layer of the given bitmap.
class PDFAlphaBitmap : public SkPDFObject {
public:
    PDFAlphaBitmap(const SkBitmap& bm) : fBitmap(bm) {}
    ~PDFAlphaBitmap() {}
    void emitObject(SkWStream*, SkPDFCatalog*) SK_OVERRIDE;
    void addResources(SkTSet<SkPDFObject*>*, SkPDFCatalog*) const SK_OVERRIDE {}

private:
    const SkBitmap fBitmap;
    void emitDict(SkWStream*, SkPDFCatalog*, size_t, bool) const;
};

void PDFAlphaBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) {
    SkAutoLockPixels autoLockPixels(fBitmap);

#ifndef SK_NO_FLATE
    // Write to a temporary buffer to get the compressed length.
    SkDynamicMemoryWStream buffer;
    SkDeflateWStream deflateWStream(&buffer);
    pmcolor_alpha_to_a8(fBitmap, &deflateWStream);
    deflateWStream.finalize();  // call before detachAsStream().
    SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());

    this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true);
    pdf_stream_begin(stream);
    stream->writeStream(asset.get(), asset->getLength());
    pdf_stream_end(stream);
#else
    this->emitDict(stream, catalog, pixel_count(fBitmap), /*deflate=*/false);
    pdf_stream_begin(stream);
    pmcolor_alpha_to_a8(fBitmap, stream);
    pdf_stream_end(stream);
#endif  // SK_NO_FLATE
}

void PDFAlphaBitmap::emitDict(SkWStream* stream,
                              SkPDFCatalog* catalog,
                              size_t length,
                              bool deflate) const {
    SkPDFDict pdfDict("XObject");
    pdfDict.insertName("Subtype", "Image");
    pdfDict.insertInt("Width", fBitmap.width());
    pdfDict.insertInt("Height", fBitmap.height());
    pdfDict.insertName("ColorSpace", "DeviceGray");
    pdfDict.insertInt("BitsPerComponent", 8);
    if (deflate) {
        pdfDict.insertName("Filter", "FlateDecode");
    }
    pdfDict.insertInt("Length", length);
    pdfDict.emitObject(stream, catalog);
}
}  // namespace

////////////////////////////////////////////////////////////////////////////////

void SkPDFBitmap::addResources(SkTSet<SkPDFObject*>* resourceSet,
                               SkPDFCatalog* catalog) const {
    if (fSMask.get()) {
        resourceSet->add(fSMask.get());
    }
}

void SkPDFBitmap::emitObject(SkWStream* stream, SkPDFCatalog* catalog) {
    SkAutoLockPixels autoLockPixels(fBitmap);

#ifndef SK_NO_FLATE
    // Write to a temporary buffer to get the compressed length.
    SkDynamicMemoryWStream buffer;
    SkDeflateWStream deflateWStream(&buffer);
    pmcolor_to_rgb24(fBitmap, &deflateWStream);
    deflateWStream.finalize();  // call before detachAsStream().
    SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());

    this->emitDict(stream, catalog, asset->getLength(), /*deflate=*/true);
    pdf_stream_begin(stream);
    stream->writeStream(asset.get(), asset->getLength());
    pdf_stream_end(stream);
#else
    this->emitDict(stream, catalog, 3 * pixel_count(fBitmap), /*deflate=*/false);
    pdf_stream_begin(stream);
    pmcolor_to_rgb24(fBitmap, stream);
    pdf_stream_end(stream);
    return;
#endif  // SK_NO_FLATE
}

void SkPDFBitmap::emitDict(SkWStream* stream,
                           SkPDFCatalog* catalog,
                           size_t length,
                           bool deflate) const {
    SkPDFDict pdfDict("XObject");
    pdfDict.insertName("Subtype", "Image");
    pdfDict.insertInt("Width", fBitmap.width());
    pdfDict.insertInt("Height", fBitmap.height());
    pdfDict.insertName("ColorSpace", "DeviceRGB");
    pdfDict.insertInt("BitsPerComponent", 8);
    if (fSMask) {
        pdfDict.insert("SMask", new SkPDFObjRef(fSMask))->unref();
    }
    if (deflate) {
        pdfDict.insertName("Filter", "FlateDecode");
    }
    pdfDict.insertInt("Length", length);
    pdfDict.emitObject(stream, catalog);
}

SkPDFBitmap::SkPDFBitmap(const SkBitmap& bm,
                         SkPDFObject* smask)
    : fBitmap(bm), fSMask(smask) {}

SkPDFBitmap::~SkPDFBitmap() {}

////////////////////////////////////////////////////////////////////////////////
static bool is_transparent(const SkBitmap& bm) {
    SkAutoLockPixels autoLockPixels(bm);
    if (NULL == bm.getPixels()) {
        return true;
    }
    SkASSERT(kN32_SkColorType == bm.colorType());
    for (int y = 0; y < bm.height(); ++y) {
        U8CPU alpha = 0;
        const SkPMColor* src = bm.getAddr32(0, y);
        for (int x = 0; x < bm.width(); ++x) {
            alpha |= SkGetPackedA32(*src++);
        }
        if (alpha) {
            return false;
        }
    }
    return true;
}

SkPDFBitmap* SkPDFBitmap::Create(SkPDFCanon* canon,
                                 const SkBitmap& bitmap,
                                 const SkIRect& subset) {
    SkASSERT(canon);
    if (kN32_SkColorType != bitmap.colorType()) {
        // TODO(halcanary): support other colortypes.
        return NULL;
    }
    SkBitmap bm;
    // Should extractSubset be done by the SkPDFDevice?
    if (!bitmap.extractSubset(&bm, subset)) {
        return NULL;
    }
    if (bm.drawsNothing()) {
        return NULL;
    }
    if (!bm.isImmutable()) {
        SkBitmap copy;
        if (!bm.copyTo(&copy)) {
            return NULL;
        }
        copy.setImmutable();
        bm = copy;
    }

    SkPDFBitmap* pdfBitmap = canon->findBitmap(bm);
    if (pdfBitmap) {
        return SkRef(pdfBitmap);
    }
    SkPDFObject* smask = NULL;
    if (!bm.isOpaque() && !SkBitmap::ComputeIsOpaque(bm)) {
        if (is_transparent(bm)) {
            return NULL;
        }
        // PDFAlphaBitmaps do not get directly canonicalized (they
        // are refed by the SkPDFBitmap).
        smask = SkNEW_ARGS(PDFAlphaBitmap, (bm));
    }
    pdfBitmap = SkNEW_ARGS(SkPDFBitmap, (bm, smask));
    canon->addBitmap(pdfBitmap);
    return pdfBitmap;
}