epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 10 | #include "SkPDFImage.h" |
| 11 | |
| 12 | #include "SkBitmap.h" |
| 13 | #include "SkColor.h" |
| 14 | #include "SkColorPriv.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 15 | #include "SkPackBits.h" |
| 16 | #include "SkPDFCatalog.h" |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 17 | #include "SkRect.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 18 | #include "SkStream.h" |
| 19 | #include "SkString.h" |
| 20 | #include "SkUnPreMultiply.h" |
| 21 | |
| 22 | namespace { |
| 23 | |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 24 | void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect, |
| 25 | SkStream** imageData, SkStream** alphaData) { |
| 26 | SkMemoryStream* image = NULL; |
| 27 | SkMemoryStream* alpha = NULL; |
| 28 | bool hasAlpha = false; |
| 29 | bool isTransparent = false; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 30 | |
vandebo@chromium.org | ad11495 | 2010-10-26 19:43:14 +0000 | [diff] [blame] | 31 | bitmap.lockPixels(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 32 | switch (bitmap.getConfig()) { |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 33 | case SkBitmap::kIndex8_Config: { |
| 34 | const int rowBytes = srcRect.width(); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 35 | image = new SkMemoryStream(rowBytes * srcRect.height()); |
| 36 | uint8_t* dst = (uint8_t*)image->getMemoryBase(); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 37 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
| 38 | memcpy(dst, bitmap.getAddr8(srcRect.fLeft, y), rowBytes); |
| 39 | dst += rowBytes; |
| 40 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 41 | break; |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 42 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 43 | case SkBitmap::kRLE_Index8_Config: { |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 44 | const int rowBytes = srcRect.width(); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 45 | image = new SkMemoryStream(rowBytes * srcRect.height()); |
| 46 | uint8_t* dst = (uint8_t*)image->getMemoryBase(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 47 | const SkBitmap::RLEPixels* rle = |
| 48 | (const SkBitmap::RLEPixels*)bitmap.getPixels(); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 49 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
| 50 | SkPackBits::Unpack8(dst, srcRect.fLeft, rowBytes, |
| 51 | rle->packedAtY(y)); |
| 52 | dst += rowBytes; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 53 | } |
| 54 | break; |
| 55 | } |
| 56 | case SkBitmap::kARGB_4444_Config: { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 57 | isTransparent = true; |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 58 | const int rowBytes = (srcRect.width() * 3 + 1) / 2; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 59 | const int alphaRowBytes = (srcRect.width() + 1) / 2; |
| 60 | image = new SkMemoryStream(rowBytes * srcRect.height()); |
| 61 | alpha = new SkMemoryStream(alphaRowBytes * srcRect.height()); |
| 62 | uint8_t* dst = (uint8_t*)image->getMemoryBase(); |
| 63 | uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase(); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 64 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 65 | uint16_t* src = bitmap.getAddr16(0, y); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 66 | int x; |
| 67 | for (x = srcRect.fLeft; x + 1 < srcRect.fRight; x += 2) { |
| 68 | dst[0] = (SkGetPackedR4444(src[x]) << 4) | |
| 69 | SkGetPackedG4444(src[x]); |
| 70 | dst[1] = (SkGetPackedB4444(src[x]) << 4) | |
| 71 | SkGetPackedR4444(src[x + 1]); |
| 72 | dst[2] = (SkGetPackedG4444(src[x + 1]) << 4) | |
| 73 | SkGetPackedB4444(src[x + 1]); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 74 | dst += 3; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 75 | alphaDst[0] = (SkGetPackedA4444(src[x]) << 4) | |
| 76 | SkGetPackedA4444(src[x + 1]); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 77 | if (alphaDst[0] != 0xFF) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 78 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 79 | } |
| 80 | if (alphaDst[0]) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 81 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 82 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 83 | alphaDst++; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 84 | } |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 85 | if (srcRect.width() & 1) { |
| 86 | dst[0] = (SkGetPackedR4444(src[x]) << 4) | |
| 87 | SkGetPackedG4444(src[x]); |
| 88 | dst[1] = (SkGetPackedB4444(src[x]) << 4); |
vandebo@chromium.org | 8ab3e0f | 2011-02-24 21:51:13 +0000 | [diff] [blame] | 89 | dst += 2; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 90 | alphaDst[0] = (SkGetPackedA4444(src[x]) << 4); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 91 | if (alphaDst[0] != 0xF0) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 92 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 93 | } |
| 94 | if (alphaDst[0] & 0xF0) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 95 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 96 | } |
vandebo@chromium.org | 8ab3e0f | 2011-02-24 21:51:13 +0000 | [diff] [blame] | 97 | alphaDst++; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | case SkBitmap::kRGB_565_Config: { |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 103 | const int rowBytes = srcRect.width() * 3; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 104 | image = new SkMemoryStream(rowBytes * srcRect.height()); |
| 105 | uint8_t* dst = (uint8_t*)image->getMemoryBase(); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 106 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 107 | uint16_t* src = bitmap.getAddr16(0, y); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 108 | for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { |
| 109 | dst[0] = SkGetPackedR16(src[x]); |
| 110 | dst[1] = SkGetPackedG16(src[x]); |
| 111 | dst[2] = SkGetPackedB16(src[x]); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 112 | dst += 3; |
| 113 | } |
| 114 | } |
| 115 | break; |
| 116 | } |
| 117 | case SkBitmap::kARGB_8888_Config: { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 118 | isTransparent = true; |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 119 | const int rowBytes = srcRect.width() * 3; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 120 | image = new SkMemoryStream(rowBytes * srcRect.height()); |
| 121 | alpha = new SkMemoryStream(srcRect.width() * srcRect.height()); |
| 122 | uint8_t* dst = (uint8_t*)image->getMemoryBase(); |
| 123 | uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase(); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 124 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 125 | uint32_t* src = bitmap.getAddr32(0, y); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 126 | for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { |
| 127 | dst[0] = SkGetPackedR32(src[x]); |
| 128 | dst[1] = SkGetPackedG32(src[x]); |
| 129 | dst[2] = SkGetPackedB32(src[x]); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 130 | dst += 3; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 131 | alphaDst[0] = SkGetPackedA32(src[x]); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 132 | if (alphaDst[0] != 0xFF) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 133 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 134 | } |
| 135 | if (alphaDst[0]) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 136 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 137 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 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); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 155 | // This may read up to one byte after src, but the potentially |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 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 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 164 | if (x + 7 < srcRect.fRight && alphaDst[0] != 0xFF) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 165 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 166 | } |
| 167 | if (x + 7 < srcRect.fRight && alphaDst[0]) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 168 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 169 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 170 | alphaDst++; |
| 171 | } |
| 172 | // Calculate the mask of bits we're interested in within the |
| 173 | // last byte of alphaDst. |
| 174 | // width mod 8 == 1 -> 0x80 ... width mod 8 == 7 -> 0xFE |
| 175 | uint8_t mask = ~((1 << (8 - (srcRect.width() % 8))) - 1); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 176 | if (srcRect.width() % 8 && (alphaDst[-1] & mask) != mask) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 177 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 178 | } |
| 179 | if (srcRect.width() % 8 && (alphaDst[-1] & mask)) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 180 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 181 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 182 | } |
| 183 | break; |
| 184 | } |
| 185 | case SkBitmap::kA8_Config: { |
| 186 | isTransparent = true; |
| 187 | image = new SkMemoryStream(1); |
| 188 | ((uint8_t*)image->getMemoryBase())[0] = 0; |
| 189 | |
| 190 | const int alphaRowBytes = srcRect.width(); |
| 191 | alpha = new SkMemoryStream(alphaRowBytes * srcRect.height()); |
| 192 | uint8_t* alphaDst = (uint8_t*)alpha->getMemoryBase(); |
| 193 | for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { |
| 194 | uint8_t* src = bitmap.getAddr8(0, y); |
| 195 | for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { |
| 196 | alphaDst[0] = src[x]; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 197 | if (alphaDst[0] != 0xFF) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 198 | hasAlpha = true; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 199 | } |
| 200 | if (alphaDst[0]) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 201 | isTransparent = false; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 202 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 203 | alphaDst++; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | default: |
| 209 | SkASSERT(false); |
| 210 | } |
vandebo@chromium.org | ad11495 | 2010-10-26 19:43:14 +0000 | [diff] [blame] | 211 | bitmap.unlockPixels(); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 212 | |
| 213 | if (isTransparent) { |
| 214 | SkSafeUnref(image); |
| 215 | } else { |
| 216 | *imageData = image; |
| 217 | } |
| 218 | |
| 219 | if (isTransparent || !hasAlpha) { |
| 220 | SkSafeUnref(alpha); |
| 221 | } else { |
| 222 | *alphaData = alpha; |
| 223 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | SkPDFArray* makeIndexedColorSpace(SkColorTable* table) { |
| 227 | SkPDFArray* result = new SkPDFArray(); |
| 228 | result->reserve(4); |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 229 | result->appendName("Indexed"); |
| 230 | result->appendName("DeviceRGB"); |
| 231 | result->appendInt(table->count() - 1); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 232 | |
| 233 | // Potentially, this could be represented in fewer bytes with a stream. |
| 234 | // Max size as a string is 1.5k. |
| 235 | SkString index; |
| 236 | for (int i = 0; i < table->count(); i++) { |
| 237 | char buf[3]; |
| 238 | SkColor color = SkUnPreMultiply::PMColorToColor((*table)[i]); |
| 239 | buf[0] = SkGetPackedR32(color); |
| 240 | buf[1] = SkGetPackedG32(color); |
| 241 | buf[2] = SkGetPackedB32(color); |
| 242 | index.append(buf, 3); |
| 243 | } |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 244 | result->append(new SkPDFString(index))->unref(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 245 | return result; |
| 246 | } |
| 247 | |
| 248 | }; // namespace |
| 249 | |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 250 | // static |
| 251 | SkPDFImage* SkPDFImage::CreateImage(const SkBitmap& bitmap, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 252 | const SkIRect& srcRect) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 253 | if (bitmap.getConfig() == SkBitmap::kNo_Config) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 254 | return NULL; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 255 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 256 | |
| 257 | SkStream* imageData = NULL; |
| 258 | SkStream* alphaData = NULL; |
| 259 | extractImageData(bitmap, srcRect, &imageData, &alphaData); |
| 260 | SkAutoUnref unrefImageData(imageData); |
| 261 | SkAutoUnref unrefAlphaData(alphaData); |
| 262 | if (!imageData) { |
| 263 | SkASSERT(!alphaData); |
| 264 | return NULL; |
| 265 | } |
| 266 | |
| 267 | SkPDFImage* image = |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 268 | new SkPDFImage(imageData, bitmap, srcRect, false); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 269 | |
| 270 | if (alphaData != NULL) { |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 271 | image->addSMask(new SkPDFImage(alphaData, bitmap, srcRect, true))->unref(); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 272 | } |
| 273 | return image; |
| 274 | } |
| 275 | |
| 276 | SkPDFImage::~SkPDFImage() { |
| 277 | fResources.unrefAll(); |
| 278 | } |
| 279 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 280 | SkPDFImage* SkPDFImage::addSMask(SkPDFImage* mask) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 281 | fResources.push(mask); |
| 282 | mask->ref(); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 283 | insert("SMask", new SkPDFObjRef(mask))->unref(); |
| 284 | return mask; |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 285 | } |
| 286 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 287 | void SkPDFImage::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, |
| 288 | SkTSet<SkPDFObject*>* newResourceObjects) { |
| 289 | GetResourcesHelper(&fResources, knownResourceObjects, newResourceObjects); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | SkPDFImage::SkPDFImage(SkStream* imageData, const SkBitmap& bitmap, |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 293 | const SkIRect& srcRect, bool doingAlpha) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 294 | this->setData(imageData); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 295 | SkBitmap::Config config = bitmap.getConfig(); |
| 296 | bool alphaOnly = (config == SkBitmap::kA1_Config || |
| 297 | config == SkBitmap::kA8_Config); |
| 298 | |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 299 | insertName("Type", "XObject"); |
| 300 | insertName("Subtype", "Image"); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 301 | |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 302 | if (!doingAlpha && alphaOnly) { |
| 303 | // For alpha only images, we stretch a single pixel of black for |
| 304 | // the color/shape part. |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 305 | SkAutoTUnref<SkPDFInt> one(new SkPDFInt(1)); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 306 | insert("Width", one.get()); |
| 307 | insert("Height", one.get()); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 308 | } else { |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 309 | insertInt("Width", srcRect.width()); |
| 310 | insertInt("Height", srcRect.height()); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 311 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 312 | |
| 313 | // if (!image mask) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 314 | if (doingAlpha || alphaOnly) { |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 315 | insertName("ColorSpace", "DeviceGray"); |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 316 | } else if (config == SkBitmap::kIndex8_Config || |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 317 | config == SkBitmap::kRLE_Index8_Config) { |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 318 | insert("ColorSpace", |
| 319 | makeIndexedColorSpace(bitmap.getColorTable()))->unref(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 320 | } else { |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 321 | insertName("ColorSpace", "DeviceRGB"); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 322 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 323 | // } |
| 324 | |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 325 | int bitsPerComp = 8; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 326 | if (config == SkBitmap::kARGB_4444_Config) { |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 327 | bitsPerComp = 4; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 328 | } else if (doingAlpha && config == SkBitmap::kA1_Config) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 329 | bitsPerComp = 1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 330 | } |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 331 | insertInt("BitsPerComponent", bitsPerComp); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 332 | |
| 333 | if (config == SkBitmap::kRGB_565_Config) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 334 | SkAutoTUnref<SkPDFInt> zeroVal(new SkPDFInt(0)); |
| 335 | SkAutoTUnref<SkPDFScalar> scale5Val( |
| 336 | new SkPDFScalar(SkFloatToScalar(8.2258f))); // 255/2^5-1 |
| 337 | SkAutoTUnref<SkPDFScalar> scale6Val( |
| 338 | new SkPDFScalar(SkFloatToScalar(4.0476f))); // 255/2^6-1 |
| 339 | SkAutoTUnref<SkPDFArray> decodeValue(new SkPDFArray()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 340 | decodeValue->reserve(6); |
| 341 | decodeValue->append(zeroVal.get()); |
| 342 | decodeValue->append(scale5Val.get()); |
| 343 | decodeValue->append(zeroVal.get()); |
| 344 | decodeValue->append(scale6Val.get()); |
| 345 | decodeValue->append(zeroVal.get()); |
| 346 | decodeValue->append(scale5Val.get()); |
| 347 | insert("Decode", decodeValue.get()); |
| 348 | } |
| 349 | } |