blob: f3f75925ce215636aa65f695d2e6fec5a9392851 [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "SkPDFImage.h"
18
19#include "SkBitmap.h"
20#include "SkColor.h"
21#include "SkColorPriv.h"
22#include "SkPaint.h"
23#include "SkPackBits.h"
24#include "SkPDFCatalog.h"
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000025#include "SkRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000026#include "SkStream.h"
27#include "SkString.h"
28#include "SkUnPreMultiply.h"
29
30namespace {
31
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000032void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
33 SkStream** imageData, SkStream** alphaData) {
34 SkMemoryStream* image = NULL;
35 SkMemoryStream* alpha = NULL;
36 bool hasAlpha = false;
37 bool isTransparent = false;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000038
vandebo@chromium.orgad114952010-10-26 19:43:14 +000039 bitmap.lockPixels();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000040 switch (bitmap.getConfig()) {
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000041 case SkBitmap::kIndex8_Config: {
42 const int rowBytes = srcRect.width();
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000043 image = new SkMemoryStream(rowBytes * srcRect.height());
44 uint8_t* dst = (uint8_t*)image->getMemoryBase();
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000045 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
46 memcpy(dst, bitmap.getAddr8(srcRect.fLeft, y), rowBytes);
47 dst += rowBytes;
48 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000049 break;
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000050 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000051 case SkBitmap::kRLE_Index8_Config: {
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000052 const int rowBytes = srcRect.width();
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000053 image = new SkMemoryStream(rowBytes * srcRect.height());
54 uint8_t* dst = (uint8_t*)image->getMemoryBase();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055 const SkBitmap::RLEPixels* rle =
56 (const SkBitmap::RLEPixels*)bitmap.getPixels();
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000057 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
58 SkPackBits::Unpack8(dst, srcRect.fLeft, rowBytes,
59 rle->packedAtY(y));
60 dst += rowBytes;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000061 }
62 break;
63 }
64 case SkBitmap::kARGB_4444_Config: {
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000065 isTransparent = true;
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000066 const int rowBytes = (srcRect.width() * 3 + 1) / 2;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000067 const int alphaRowBytes = (srcRect.width() + 1) / 2;
68 image = new SkMemoryStream(rowBytes * srcRect.height());
69 alpha = new SkMemoryStream(alphaRowBytes * srcRect.height());
70 uint8_t* dst = (uint8_t*)image->getMemoryBase();
71 uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase();
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000072 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000073 uint16_t* src = bitmap.getAddr16(0, y);
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000074 int x;
75 for (x = srcRect.fLeft; x + 1 < srcRect.fRight; x += 2) {
76 dst[0] = (SkGetPackedR4444(src[x]) << 4) |
77 SkGetPackedG4444(src[x]);
78 dst[1] = (SkGetPackedB4444(src[x]) << 4) |
79 SkGetPackedR4444(src[x + 1]);
80 dst[2] = (SkGetPackedG4444(src[x + 1]) << 4) |
81 SkGetPackedB4444(src[x + 1]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000082 dst += 3;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000083 alphaDst[0] = (SkGetPackedA4444(src[x]) << 4) |
84 SkGetPackedA4444(src[x + 1]);
85 if (alphaDst[0] != 0xFF)
86 hasAlpha = true;
87 if (alphaDst[0])
88 isTransparent = false;
89 alphaDst++;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000090 }
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +000091 if (srcRect.width() & 1) {
92 dst[0] = (SkGetPackedR4444(src[x]) << 4) |
93 SkGetPackedG4444(src[x]);
94 dst[1] = (SkGetPackedB4444(src[x]) << 4);
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +000095 alphaDst[0] = (SkGetPackedA4444(src[x]) << 4);
96 if (alphaDst[0] != 0xF0)
97 hasAlpha = true;
98 if (alphaDst[0] & 0xF0)
99 isTransparent = false;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000100 }
101 }
102 break;
103 }
104 case SkBitmap::kRGB_565_Config: {
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000105 const int rowBytes = srcRect.width() * 3;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000106 image = new SkMemoryStream(rowBytes * srcRect.height());
107 uint8_t* dst = (uint8_t*)image->getMemoryBase();
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000108 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000109 uint16_t* src = bitmap.getAddr16(0, y);
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000110 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
111 dst[0] = SkGetPackedR16(src[x]);
112 dst[1] = SkGetPackedG16(src[x]);
113 dst[2] = SkGetPackedB16(src[x]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000114 dst += 3;
115 }
116 }
117 break;
118 }
119 case SkBitmap::kARGB_8888_Config: {
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000120 isTransparent = true;
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000121 const int rowBytes = srcRect.width() * 3;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000122 image = new SkMemoryStream(rowBytes * srcRect.height());
123 alpha = new SkMemoryStream(srcRect.width() * srcRect.height());
124 uint8_t* dst = (uint8_t*)image->getMemoryBase();
125 uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase();
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000126 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000127 uint32_t* src = bitmap.getAddr32(0, y);
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000128 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
129 dst[0] = SkGetPackedR32(src[x]);
130 dst[1] = SkGetPackedG32(src[x]);
131 dst[2] = SkGetPackedB32(src[x]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000132 dst += 3;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000133 alphaDst[0] = SkGetPackedA32(src[x]);
134 if (alphaDst[0] != 0xFF)
135 hasAlpha = true;
136 if (alphaDst[0])
137 isTransparent = false;
138 alphaDst++;
139 }
140 }
141 break;
142 }
143 case SkBitmap::kA1_Config: {
144 isTransparent = true;
145 image = new SkMemoryStream(1);
146 ((uint8_t*)image->getMemoryBase())[0] = 0;
147
148 const int alphaRowBytes = (srcRect.width() + 7) / 8;
149 alpha = new SkMemoryStream(alphaRowBytes * srcRect.height());
150 uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase();
151 int offset1 = srcRect.fLeft % 8;
152 int offset2 = 8 - offset1;
153 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
154 uint8_t* src = bitmap.getAddr1(0, y);
155 // This may read up to one byte after src, but the potentially
156 // invalid bits are never used for computation.
157 for (int x = srcRect.fLeft; x < srcRect.fRight; x += 8) {
158 if (offset1) {
159 alphaDst[0] = src[x / 8] << offset1 |
160 src[x / 8 + 1] >> offset2;
161 } else {
162 alphaDst[0] = src[x / 8];
163 }
164 if (x + 7 < srcRect.fRight && alphaDst[0] != 0xFF)
165 hasAlpha = true;
166 if (x + 7 < srcRect.fRight && alphaDst[0])
167 isTransparent = false;
168 alphaDst++;
169 }
170 // Calculate the mask of bits we're interested in within the
171 // last byte of alphaDst.
172 // width mod 8 == 1 -> 0x80 ... width mod 8 == 7 -> 0xFE
173 uint8_t mask = ~((1 << (8 - (srcRect.width() % 8))) - 1);
174 if (srcRect.width() % 8 && (alphaDst[-1] & mask) != mask)
175 hasAlpha = true;
176 if (srcRect.width() % 8 && (alphaDst[-1] & mask))
177 isTransparent = false;
178 }
179 break;
180 }
181 case SkBitmap::kA8_Config: {
182 isTransparent = true;
183 image = new SkMemoryStream(1);
184 ((uint8_t*)image->getMemoryBase())[0] = 0;
185
186 const int alphaRowBytes = srcRect.width();
187 alpha = new SkMemoryStream(alphaRowBytes * srcRect.height());
188 uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase();
189 for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
190 uint8_t* src = bitmap.getAddr8(0, y);
191 for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
192 alphaDst[0] = src[x];
193 if (alphaDst[0] != 0xFF)
194 hasAlpha = true;
195 if (alphaDst[0])
196 isTransparent = false;
197 alphaDst++;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000198 }
199 }
200 break;
201 }
202 default:
203 SkASSERT(false);
204 }
vandebo@chromium.orgad114952010-10-26 19:43:14 +0000205 bitmap.unlockPixels();
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000206
207 if (isTransparent) {
208 SkSafeUnref(image);
209 } else {
210 *imageData = image;
211 }
212
213 if (isTransparent || !hasAlpha) {
214 SkSafeUnref(alpha);
215 } else {
216 *alphaData = alpha;
217 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000218}
219
220SkPDFArray* makeIndexedColorSpace(SkColorTable* table) {
221 SkPDFArray* result = new SkPDFArray();
222 result->reserve(4);
223 SkRefPtr<SkPDFName> indexedName = new SkPDFName("Indexed");
224 indexedName->unref(); // SkRefPtr and new both took a reference.
225 result->append(indexedName.get());
226
227 SkRefPtr<SkPDFName> rgbName = new SkPDFName("DeviceRGB");
228 rgbName->unref(); // SkRefPtr and new both took a reference.
229 result->append(rgbName.get());
230
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000231 SkRefPtr<SkPDFInt> countValue = new SkPDFInt(table->count() - 1);
vandebo@chromium.orgfb697e72011-02-01 01:04:00 +0000232 countValue->unref(); // SkRefPtr and new both took a reference.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000233 result->append(countValue.get());
234
235 // Potentially, this could be represented in fewer bytes with a stream.
236 // Max size as a string is 1.5k.
237 SkString index;
238 for (int i = 0; i < table->count(); i++) {
239 char buf[3];
240 SkColor color = SkUnPreMultiply::PMColorToColor((*table)[i]);
241 buf[0] = SkGetPackedR32(color);
242 buf[1] = SkGetPackedG32(color);
243 buf[2] = SkGetPackedB32(color);
244 index.append(buf, 3);
245 }
246 SkRefPtr<SkPDFString> indexValue = new SkPDFString(index);
247 indexValue->unref(); // SkRefPtr and new both took a reference.
248 result->append(indexValue.get());
249 return result;
250}
251
252}; // namespace
253
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000254// static
255SkPDFImage* SkPDFImage::CreateImage(const SkBitmap& bitmap,
256 const SkIRect& srcRect,
257 const SkPaint& paint) {
258 if (bitmap.getConfig() == SkBitmap::kNo_Config)
259 return NULL;
260
261 SkStream* imageData = NULL;
262 SkStream* alphaData = NULL;
263 extractImageData(bitmap, srcRect, &imageData, &alphaData);
264 SkAutoUnref unrefImageData(imageData);
265 SkAutoUnref unrefAlphaData(alphaData);
266 if (!imageData) {
267 SkASSERT(!alphaData);
268 return NULL;
269 }
270
271 SkPDFImage* image =
272 new SkPDFImage(imageData, bitmap, srcRect, false, paint);
273
274 if (alphaData != NULL) {
275 SkRefPtr<SkPDFImage> alphaImage =
276 new SkPDFImage(alphaData, bitmap, srcRect, true, paint);
277 alphaImage->unref(); // SkRefPtr and new both took a reference.
278 image->addSMask(alphaImage.get());
279 }
280 return image;
281}
282
283SkPDFImage::~SkPDFImage() {
284 fResources.unrefAll();
285}
286
287void SkPDFImage::addSMask(SkPDFImage* mask) {
288 fResources.push(mask);
289 mask->ref();
290
291 SkRefPtr<SkPDFObjRef> maskRef = new SkPDFObjRef(mask);
292 maskRef->unref(); // SkRefPtr and new both took a reference.
293 insert("SMask", maskRef.get());
294}
295
296void SkPDFImage::emitObject(SkWStream* stream, SkPDFCatalog* catalog,
297 bool indirect) {
298 if (indirect)
299 return emitIndirectObject(stream, catalog);
300
301 fStream->emitObject(stream, catalog, indirect);
302}
303
304size_t SkPDFImage::getOutputSize(SkPDFCatalog* catalog, bool indirect) {
305 if (indirect)
306 return getIndirectOutputSize(catalog);
307
308 return fStream->getOutputSize(catalog, indirect);
309}
310
311void SkPDFImage::getResources(SkTDArray<SkPDFObject*>* resourceList) {
312 if (fResources.count()) {
313 resourceList->setReserve(resourceList->count() + fResources.count());
314 for (int i = 0; i < fResources.count(); i++) {
315 resourceList->push(fResources[i]);
316 fResources[i]->ref();
317 fResources[i]->getResources(resourceList);
318 }
319 }
320}
321
322SkPDFImage::SkPDFImage(SkStream* imageData, const SkBitmap& bitmap,
323 const SkIRect& srcRect, bool doingAlpha,
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000324 const SkPaint& paint) {
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000325 fStream = new SkPDFStream(imageData);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000326 fStream->unref(); // SkRefPtr and new both took a reference.
327
328 SkRefPtr<SkPDFName> typeValue = new SkPDFName("XObject");
329 typeValue->unref(); // SkRefPtr and new both took a reference.
330 insert("Type", typeValue.get());
331
332 SkRefPtr<SkPDFName> subTypeValue = new SkPDFName("Image");
333 subTypeValue->unref(); // SkRefPtr and new both took a reference.
334 insert("Subtype", subTypeValue.get());
335
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000336 SkBitmap::Config config = bitmap.getConfig();
337 bool alphaOnly = (config == SkBitmap::kA1_Config ||
338 config == SkBitmap::kA8_Config);
339
340 SkRefPtr<SkPDFInt> widthValue;
341 SkRefPtr<SkPDFInt> heightValue;
342 if (!doingAlpha && alphaOnly) {
343 // For alpha only images, we stretch a single pixel of black for
344 // the color/shape part.
345 widthValue = new SkPDFInt(1);
346 heightValue = widthValue.get();
347 } else {
348 widthValue = new SkPDFInt(srcRect.width());
349 heightValue = new SkPDFInt(srcRect.height());
350 heightValue->unref(); // SkRefPtr and new both took a reference.
351 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000352 widthValue->unref(); // SkRefPtr and new both took a reference.
353 insert("Width", widthValue.get());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000354 insert("Height", heightValue.get());
355
356 // if (!image mask) {
357 SkRefPtr<SkPDFObject> colorSpaceValue;
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000358 if (doingAlpha || alphaOnly) {
359 colorSpaceValue = new SkPDFName("DeviceGray");
360 } else if (config == SkBitmap::kIndex8_Config ||
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000361 config == SkBitmap::kRLE_Index8_Config) {
362 colorSpaceValue = makeIndexedColorSpace(bitmap.getColorTable());
363 } else {
364 colorSpaceValue = new SkPDFName("DeviceRGB");
365 }
366 colorSpaceValue->unref(); // SkRefPtr and new both took a reference.
367 insert("ColorSpace", colorSpaceValue.get());
368 // }
369
vandebo@chromium.org1cfa2c42011-01-31 19:35:43 +0000370 int bitsPerComp = 8;
371 if (config == SkBitmap::kARGB_4444_Config)
372 bitsPerComp = 4;
373 else if (doingAlpha && config == SkBitmap::kA1_Config)
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000374 bitsPerComp = 1;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000375 SkRefPtr<SkPDFInt> bitsPerCompValue = new SkPDFInt(bitsPerComp);
376 bitsPerCompValue->unref(); // SkRefPtr and new both took a reference.
377 insert("BitsPerComponent", bitsPerCompValue.get());
378
379 if (config == SkBitmap::kRGB_565_Config) {
380 SkRefPtr<SkPDFInt> zeroVal = new SkPDFInt(0);
381 zeroVal->unref(); // SkRefPtr and new both took a reference.
382 SkRefPtr<SkPDFScalar> scale5Val = new SkPDFScalar(8.2258); // 255/2^5-1
383 scale5Val->unref(); // SkRefPtr and new both took a reference.
384 SkRefPtr<SkPDFScalar> scale6Val = new SkPDFScalar(4.0476); // 255/2^6-1
385 scale6Val->unref(); // SkRefPtr and new both took a reference.
386 SkRefPtr<SkPDFArray> decodeValue = new SkPDFArray();
387 decodeValue->unref(); // SkRefPtr and new both took a reference.
388 decodeValue->reserve(6);
389 decodeValue->append(zeroVal.get());
390 decodeValue->append(scale5Val.get());
391 decodeValue->append(zeroVal.get());
392 decodeValue->append(scale6Val.get());
393 decodeValue->append(zeroVal.get());
394 decodeValue->append(scale5Val.get());
395 insert("Decode", decodeValue.get());
396 }
397}
398
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000399void SkPDFImage::insert(SkPDFName* key, SkPDFObject* value) {
400 fStream->insert(key, value);
401}
402
403void SkPDFImage::insert(const char key[], SkPDFObject* value) {
404 fStream->insert(key, value);
405}