aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkPDFImage.cpp
blob: b480089b17a8f51892c72422563f49e6c2de69ea (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
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SkPDFImage.h"

#include "SkBitmap.h"
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkPaint.h"
#include "SkPackBits.h"
#include "SkPDFCatalog.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkUnPreMultiply.h"

namespace {

SkMemoryStream* extractImageData(const SkBitmap& bitmap) {
    SkMemoryStream* result;

    switch (bitmap.getConfig()) {
        case SkBitmap::kIndex8_Config:
            result = new SkMemoryStream(bitmap.getPixels(), bitmap.getSize(),
                                        true);
            break;
        case SkBitmap::kRLE_Index8_Config: {
            result = new SkMemoryStream(bitmap.getSize());
            const SkBitmap::RLEPixels* rle =
                (const SkBitmap::RLEPixels*)bitmap.getPixels();
            uint8_t* dst = (uint8_t*)result->getMemoryBase();
            const int width = bitmap.width();
            for (int y = 0; y < bitmap.height(); y++) {
                SkPackBits::Unpack8(rle->packedAtY(y), width, dst);
                dst += width;
            }
            break;
        }
        case SkBitmap::kARGB_4444_Config: {
            const int width = bitmap.width();
            const int rowBytes = (width * 3 + 1) / 2;
            result = new SkMemoryStream(rowBytes * bitmap.height());
            uint8_t* dst = (uint8_t*)result->getMemoryBase();
            for (int y = 0; y < bitmap.height(); y++) {
                uint16_t* src = bitmap.getAddr16(0, y);
                for (int x = 0; x < width; x += 2) {
                    dst[0] = (SkGetPackedR4444(src[0]) << 4) |
                        SkGetPackedG4444(src[0]);
                    dst[1] = (SkGetPackedB4444(src[0]) << 4) |
                        SkGetPackedR4444(src[1]);
                    dst[2] = (SkGetPackedG4444(src[1]) << 4) |
                        SkGetPackedB4444(src[1]);
                    src += 2;
                    dst += 3;
                }
                if (width & 1) {
                    dst[0] = (SkGetPackedR4444(src[0]) << 4) |
                        SkGetPackedG4444(src[0]);
                    dst[1] = (SkGetPackedB4444(src[0]) << 4);
                }
            }
            break;
        }
        case SkBitmap::kRGB_565_Config: {
            const int width = bitmap.width();
            const int rowBytes = width * 3;
            result = new SkMemoryStream(rowBytes * bitmap.height());
            uint8_t* dst = (uint8_t*)result->getMemoryBase();
            for (int y = 0; y < bitmap.height(); y++) {
                uint16_t* src = bitmap.getAddr16(0, y);
                for (int x = 0; x < width; x++) {
                    dst[0] = SkGetPackedR16(src[0]);
                    dst[1] = SkGetPackedG16(src[0]);
                    dst[2] = SkGetPackedB16(src[0]);
                    src++;
                    dst += 3;
                }
            }
            break;
        }
        case SkBitmap::kARGB_8888_Config: {
            const int width = bitmap.width();
            const int rowBytes = width * 3;
            result = new SkMemoryStream(rowBytes * bitmap.height());
            uint8_t* dst = (uint8_t*)result->getMemoryBase();
            for (int y = 0; y < bitmap.height(); y++) {
                uint32_t* src = bitmap.getAddr32(0, y);
                for (int x = 0; x < width; x++) {
                    dst[0] = SkGetPackedR32(src[0]);
                    dst[1] = SkGetPackedG32(src[0]);
                    dst[2] = SkGetPackedB32(src[0]);
                    src++;
                    dst += 3;
                }
            }
            break;
        }
        default:
            SkASSERT(false);
    }
    return result;
}

SkPDFArray* makeIndexedColorSpace(SkColorTable* table) {
    SkPDFArray* result = new SkPDFArray();
    result->reserve(4);
    SkRefPtr<SkPDFName> indexedName = new SkPDFName("Indexed");
    indexedName->unref();  // SkRefPtr and new both took a reference.
    result->append(indexedName.get());

    SkRefPtr<SkPDFName> rgbName = new SkPDFName("DeviceRGB");
    rgbName->unref();  // SkRefPtr and new both took a reference.
    result->append(rgbName.get());

    rgbName->unref();  // SkRefPtr and new both took a reference.
    SkRefPtr<SkPDFInt> countValue = new SkPDFInt(table->count() - 1);
    result->append(countValue.get());

    // Potentially, this could be represented in fewer bytes with a stream.
    // Max size as a string is 1.5k.
    SkString index;
    for (int i = 0; i < table->count(); i++) {
        char buf[3];
        SkColor color = SkUnPreMultiply::PMColorToColor((*table)[i]);
        buf[0] = SkGetPackedR32(color);
        buf[1] = SkGetPackedG32(color);
        buf[2] = SkGetPackedB32(color);
        index.append(buf, 3);
    }
    SkRefPtr<SkPDFString> indexValue = new SkPDFString(index);
    indexValue->unref();  // SkRefPtr and new both took a reference.
    result->append(indexValue.get());
    return result;
}

};  // namespace

SkPDFImage::SkPDFImage(const SkBitmap& bitmap, const SkPaint& paint) {
    SkBitmap::Config config = bitmap.getConfig();

    // TODO(vandebo) Handle alpha and alpha only images correctly.
    SkASSERT(config == SkBitmap::kRGB_565_Config ||
             config == SkBitmap::kARGB_4444_Config ||
             config == SkBitmap::kARGB_8888_Config ||
             config == SkBitmap::kIndex8_Config ||
             config == SkBitmap::kRLE_Index8_Config);

    SkMemoryStream* image_data = extractImageData(bitmap);
    SkAutoUnref image_data_unref(image_data);
    fStream = new SkPDFStream(image_data);
    fStream->unref();  // SkRefPtr and new both took a reference.

    SkRefPtr<SkPDFName> typeValue = new SkPDFName("XObject");
    typeValue->unref();  // SkRefPtr and new both took a reference.
    insert("Type", typeValue.get());

    SkRefPtr<SkPDFName> subTypeValue = new SkPDFName("Image");
    subTypeValue->unref();  // SkRefPtr and new both took a reference.
    insert("Subtype", subTypeValue.get());

    SkRefPtr<SkPDFInt> widthValue = new SkPDFInt(bitmap.width());
    widthValue->unref();  // SkRefPtr and new both took a reference.
    insert("Width", widthValue.get());

    SkRefPtr<SkPDFInt> heightValue = new SkPDFInt(bitmap.height());
    heightValue->unref();  // SkRefPtr and new both took a reference.
    insert("Height", heightValue.get());

    // if (!image mask) {
    SkRefPtr<SkPDFObject> colorSpaceValue;
    if (config == SkBitmap::kIndex8_Config ||
        config == SkBitmap::kRLE_Index8_Config) {
        colorSpaceValue = makeIndexedColorSpace(bitmap.getColorTable());
    } else {
        colorSpaceValue = new SkPDFName("DeviceRGB");
    }
    colorSpaceValue->unref();  // SkRefPtr and new both took a reference.
    insert("ColorSpace", colorSpaceValue.get());
    // }

    int bitsPerComp = bitmap.bytesPerPixel() * 2;
    if (bitsPerComp == 0) {
        SkASSERT(config == SkBitmap::kA1_Config);
        bitsPerComp = 1;
    } else if (bitsPerComp == 2 ||
               (bitsPerComp == 4 && config == SkBitmap::kRGB_565_Config)) {
        bitsPerComp = 8;
    }
    SkRefPtr<SkPDFInt> bitsPerCompValue = new SkPDFInt(bitsPerComp);
    bitsPerCompValue->unref();  // SkRefPtr and new both took a reference.
    insert("BitsPerComponent", bitsPerCompValue.get());

    if (config == SkBitmap::kRGB_565_Config) {
        SkRefPtr<SkPDFInt> zeroVal = new SkPDFInt(0);
        zeroVal->unref();  // SkRefPtr and new both took a reference.
        SkRefPtr<SkPDFScalar> scale5Val = new SkPDFScalar(8.2258);  // 255/2^5-1
        scale5Val->unref();  // SkRefPtr and new both took a reference.
        SkRefPtr<SkPDFScalar> scale6Val = new SkPDFScalar(4.0476);  // 255/2^6-1
        scale6Val->unref();  // SkRefPtr and new both took a reference.
        SkRefPtr<SkPDFArray> decodeValue = new SkPDFArray();
        decodeValue->unref();  // SkRefPtr and new both took a reference.
        decodeValue->reserve(6);
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale5Val.get());
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale6Val.get());
        decodeValue->append(zeroVal.get());
        decodeValue->append(scale5Val.get());
        insert("Decode", decodeValue.get());
    }
}

SkPDFImage::~SkPDFImage() {}

void SkPDFImage::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
                             bool indirect) {
    if (indirect)
        return emitIndirectObject(stream, catalog);

    fStream->emitObject(stream, catalog, indirect);
}

size_t SkPDFImage::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
    if (indirect)
        return getIndirectOutputSize(catalog);

    return fStream->getOutputSize(catalog, indirect);
}

void SkPDFImage::insert(SkPDFName* key, SkPDFObject* value) {
    fStream->insert(key, value);
}

void SkPDFImage::insert(const char key[], SkPDFObject* value) {
    fStream->insert(key, value);
}