halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkColorPriv.h" |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 9 | #include "SkData.h" |
halcanary | d9e5715 | 2015-08-12 11:24:40 -0700 | [diff] [blame] | 10 | #include "SkDeflate.h" |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 11 | #include "SkImage_Base.h" |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 12 | #include "SkJpegInfo.h" |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 13 | #include "SkPDFBitmap.h" |
| 14 | #include "SkPDFCanon.h" |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkUnPreMultiply.h" |
| 17 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 18 | void image_get_ro_pixels(const SkImage* image, SkBitmap* dst) { |
| 19 | if(as_IB(image)->getROPixels(dst) |
| 20 | && dst->dimensions() == image->dimensions()) { |
| 21 | return; |
| 22 | } |
| 23 | // no pixels or wrong size: fill with zeros. |
| 24 | SkAlphaType at = image->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 25 | dst->setInfo(SkImageInfo::MakeN32(image->width(), image->height(), at)); |
| 26 | } |
| 27 | |
| 28 | bool image_compute_is_opaque(const SkImage* image) { |
| 29 | if (image->isOpaque()) { |
| 30 | return true; |
| 31 | } |
| 32 | // keep output PDF small at cost of possible resource use. |
| 33 | SkBitmap bm; |
| 34 | image_get_ro_pixels(image, &bm); |
| 35 | return SkBitmap::ComputeIsOpaque(bm); |
| 36 | } |
| 37 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 38 | //////////////////////////////////////////////////////////////////////////////// |
| 39 | |
| 40 | static void pdf_stream_begin(SkWStream* stream) { |
| 41 | static const char streamBegin[] = " stream\n"; |
| 42 | stream->write(streamBegin, strlen(streamBegin)); |
| 43 | } |
| 44 | |
| 45 | static void pdf_stream_end(SkWStream* stream) { |
| 46 | static const char streamEnd[] = "\nendstream"; |
| 47 | stream->write(streamEnd, strlen(streamEnd)); |
| 48 | } |
| 49 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 50 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 51 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 52 | // write a single byte to a stream n times. |
| 53 | static void fill_stream(SkWStream* out, char value, size_t n) { |
| 54 | char buffer[4096]; |
| 55 | memset(buffer, value, sizeof(buffer)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 56 | for (size_t i = 0; i < n / sizeof(buffer); ++i) { |
| 57 | out->write(buffer, sizeof(buffer)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 58 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 59 | out->write(buffer, n % sizeof(buffer)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 60 | } |
| 61 | |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 62 | // TODO(reed@): Decide if these five functions belong in SkColorPriv.h |
| 63 | static bool SkIsBGRA(SkColorType ct) { |
| 64 | SkASSERT(kBGRA_8888_SkColorType == ct || kRGBA_8888_SkColorType == ct); |
| 65 | return kBGRA_8888_SkColorType == ct; |
| 66 | } |
| 67 | |
| 68 | // Interpret value as the given 4-byte SkColorType (BGRA_8888 or |
| 69 | // RGBA_8888) and return the appropriate component. Each component |
| 70 | // should be interpreted according to the associated SkAlphaType and |
| 71 | // SkColorProfileType. |
| 72 | static U8CPU SkGetA32Component(uint32_t value, SkColorType ct) { |
| 73 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_A32_SHIFT : SK_RGBA_A32_SHIFT)) & 0xFF; |
| 74 | } |
| 75 | static U8CPU SkGetR32Component(uint32_t value, SkColorType ct) { |
| 76 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_R32_SHIFT : SK_RGBA_R32_SHIFT)) & 0xFF; |
| 77 | } |
| 78 | static U8CPU SkGetG32Component(uint32_t value, SkColorType ct) { |
| 79 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_G32_SHIFT : SK_RGBA_G32_SHIFT)) & 0xFF; |
| 80 | } |
| 81 | static U8CPU SkGetB32Component(uint32_t value, SkColorType ct) { |
| 82 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_B32_SHIFT : SK_RGBA_B32_SHIFT)) & 0xFF; |
| 83 | } |
| 84 | |
| 85 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 86 | // unpremultiply and extract R, G, B components. |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 87 | static void pmcolor_to_rgb24(uint32_t color, uint8_t* rgb, SkColorType ct) { |
| 88 | uint32_t s = SkUnPreMultiply::GetScale(SkGetA32Component(color, ct)); |
| 89 | rgb[0] = SkUnPreMultiply::ApplyScale(s, SkGetR32Component(color, ct)); |
| 90 | rgb[1] = SkUnPreMultiply::ApplyScale(s, SkGetG32Component(color, ct)); |
| 91 | rgb[2] = SkUnPreMultiply::ApplyScale(s, SkGetB32Component(color, ct)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* It is necessary to average the color component of transparent |
| 95 | pixels with their surrounding neighbors since the PDF renderer may |
| 96 | separately re-sample the alpha and color channels when the image is |
| 97 | not displayed at its native resolution. Since an alpha of zero |
| 98 | gives no information about the color component, the pathological |
| 99 | case is a white image with sharp transparency bounds - the color |
| 100 | channel goes to black, and the should-be-transparent pixels are |
| 101 | rendered as grey because of the separate soft mask and color |
| 102 | resizing. e.g.: gm/bitmappremul.cpp */ |
| 103 | static void get_neighbor_avg_color(const SkBitmap& bm, |
| 104 | int xOrig, |
| 105 | int yOrig, |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 106 | uint8_t rgb[3], |
| 107 | SkColorType ct) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 108 | unsigned a = 0, r = 0, g = 0, b = 0; |
| 109 | // Clamp the range to the edge of the bitmap. |
| 110 | int ymin = SkTMax(0, yOrig - 1); |
| 111 | int ymax = SkTMin(yOrig + 1, bm.height() - 1); |
| 112 | int xmin = SkTMax(0, xOrig - 1); |
| 113 | int xmax = SkTMin(xOrig + 1, bm.width() - 1); |
| 114 | for (int y = ymin; y <= ymax; ++y) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 115 | uint32_t* scanline = bm.getAddr32(0, y); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 116 | for (int x = xmin; x <= xmax; ++x) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 117 | uint32_t color = scanline[x]; |
| 118 | a += SkGetA32Component(color, ct); |
| 119 | r += SkGetR32Component(color, ct); |
| 120 | g += SkGetG32Component(color, ct); |
| 121 | b += SkGetB32Component(color, ct); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 124 | if (a > 0) { |
| 125 | rgb[0] = SkToU8(255 * r / a); |
| 126 | rgb[1] = SkToU8(255 * g / a); |
| 127 | rgb[2] = SkToU8(255 * b / a); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 128 | } else { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 129 | rgb[0] = rgb[1] = rgb[2] = 0; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 133 | static size_t pixel_count(const SkBitmap& bm) { |
| 134 | return SkToSizeT(bm.width()) * SkToSizeT(bm.height()); |
| 135 | } |
| 136 | |
| 137 | static const SkBitmap& not4444(const SkBitmap& input, SkBitmap* copy) { |
| 138 | if (input.colorType() != kARGB_4444_SkColorType) { |
| 139 | return input; |
| 140 | } |
| 141 | // ARGB_4444 is rarely used, so we can do a wasteful tmp copy. |
| 142 | SkAssertResult(input.copyTo(copy, kN32_SkColorType)); |
| 143 | copy->setImmutable(); |
| 144 | return *copy; |
| 145 | } |
| 146 | |
| 147 | static size_t pdf_color_component_count(SkColorType ct) { |
| 148 | switch (ct) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 149 | case kRGB_565_SkColorType: |
| 150 | case kARGB_4444_SkColorType: |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 151 | case kRGBA_8888_SkColorType: |
| 152 | case kBGRA_8888_SkColorType: |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 153 | return 3; |
| 154 | case kAlpha_8_SkColorType: |
| 155 | case kIndex_8_SkColorType: |
| 156 | case kGray_8_SkColorType: |
| 157 | return 1; |
| 158 | case kUnknown_SkColorType: |
| 159 | default: |
| 160 | SkDEBUGFAIL("unexpected color type"); |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void bitmap_to_pdf_pixels(const SkBitmap& bitmap, SkWStream* out) { |
| 166 | if (!bitmap.getPixels()) { |
| 167 | size_t size = pixel_count(bitmap) * |
| 168 | pdf_color_component_count(bitmap.colorType()); |
| 169 | fill_stream(out, '\x00', size); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 170 | return; |
| 171 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 172 | SkBitmap copy; |
| 173 | const SkBitmap& bm = not4444(bitmap, ©); |
| 174 | SkAutoLockPixels autoLockPixels(bm); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 175 | SkColorType colorType = bm.colorType(); |
| 176 | switch (colorType) { |
| 177 | case kRGBA_8888_SkColorType: |
| 178 | case kBGRA_8888_SkColorType: { |
| 179 | SkASSERT(3 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 180 | SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
| 181 | for (int y = 0; y < bm.height(); ++y) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 182 | const uint32_t* src = bm.getAddr32(0, y); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 183 | uint8_t* dst = scanline.get(); |
| 184 | for (int x = 0; x < bm.width(); ++x) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 185 | uint32_t color = *src++; |
| 186 | U8CPU alpha = SkGetA32Component(color, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 187 | if (alpha != SK_AlphaTRANSPARENT) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 188 | pmcolor_to_rgb24(color, dst, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 189 | } else { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 190 | get_neighbor_avg_color(bm, x, y, dst, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 191 | } |
| 192 | dst += 3; |
| 193 | } |
| 194 | out->write(scanline.get(), 3 * bm.width()); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 195 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 196 | return; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 197 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 198 | case kRGB_565_SkColorType: { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 199 | SkASSERT(3 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 200 | SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
| 201 | for (int y = 0; y < bm.height(); ++y) { |
| 202 | const uint16_t* src = bm.getAddr16(0, y); |
| 203 | uint8_t* dst = scanline.get(); |
| 204 | for (int x = 0; x < bm.width(); ++x) { |
| 205 | U16CPU color565 = *src++; |
| 206 | *dst++ = SkPacked16ToR32(color565); |
| 207 | *dst++ = SkPacked16ToG32(color565); |
| 208 | *dst++ = SkPacked16ToB32(color565); |
| 209 | } |
| 210 | out->write(scanline.get(), 3 * bm.width()); |
| 211 | } |
| 212 | return; |
| 213 | } |
| 214 | case kAlpha_8_SkColorType: |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 215 | SkASSERT(1 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 216 | fill_stream(out, '\x00', pixel_count(bm)); |
| 217 | return; |
| 218 | case kGray_8_SkColorType: |
| 219 | case kIndex_8_SkColorType: |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 220 | SkASSERT(1 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 221 | // these two formats need no transformation to serialize. |
| 222 | for (int y = 0; y < bm.height(); ++y) { |
| 223 | out->write(bm.getAddr8(0, y), bm.width()); |
| 224 | } |
| 225 | return; |
| 226 | case kUnknown_SkColorType: |
| 227 | case kARGB_4444_SkColorType: |
| 228 | default: |
| 229 | SkDEBUGFAIL("unexpected color type"); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 233 | //////////////////////////////////////////////////////////////////////////////// |
| 234 | |
| 235 | static void bitmap_alpha_to_a8(const SkBitmap& bitmap, SkWStream* out) { |
| 236 | if (!bitmap.getPixels()) { |
| 237 | fill_stream(out, '\xFF', pixel_count(bitmap)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 238 | return; |
| 239 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 240 | SkBitmap copy; |
| 241 | const SkBitmap& bm = not4444(bitmap, ©); |
| 242 | SkAutoLockPixels autoLockPixels(bm); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 243 | SkColorType colorType = bm.colorType(); |
| 244 | switch (colorType) { |
| 245 | case kRGBA_8888_SkColorType: |
| 246 | case kBGRA_8888_SkColorType: { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 247 | SkAutoTMalloc<uint8_t> scanline(bm.width()); |
| 248 | for (int y = 0; y < bm.height(); ++y) { |
| 249 | uint8_t* dst = scanline.get(); |
| 250 | const SkPMColor* src = bm.getAddr32(0, y); |
| 251 | for (int x = 0; x < bm.width(); ++x) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 252 | *dst++ = SkGetA32Component(*src++, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 253 | } |
| 254 | out->write(scanline.get(), bm.width()); |
| 255 | } |
| 256 | return; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 257 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 258 | case kAlpha_8_SkColorType: |
| 259 | for (int y = 0; y < bm.height(); ++y) { |
| 260 | out->write(bm.getAddr8(0, y), bm.width()); |
| 261 | } |
| 262 | return; |
| 263 | case kIndex_8_SkColorType: { |
| 264 | SkColorTable* ct = bm.getColorTable(); |
| 265 | SkASSERT(ct); |
| 266 | SkAutoTMalloc<uint8_t> scanline(bm.width()); |
| 267 | for (int y = 0; y < bm.height(); ++y) { |
| 268 | uint8_t* dst = scanline.get(); |
| 269 | const uint8_t* src = bm.getAddr8(0, y); |
| 270 | for (int x = 0; x < bm.width(); ++x) { |
| 271 | *dst++ = SkGetPackedA32((*ct)[*src++]); |
| 272 | } |
| 273 | out->write(scanline.get(), bm.width()); |
| 274 | } |
| 275 | return; |
| 276 | } |
| 277 | case kRGB_565_SkColorType: |
| 278 | case kGray_8_SkColorType: |
| 279 | SkDEBUGFAIL("color type has no alpha"); |
| 280 | return; |
| 281 | case kARGB_4444_SkColorType: |
| 282 | SkDEBUGFAIL("4444 color type should have been converted to N32"); |
| 283 | return; |
| 284 | case kUnknown_SkColorType: |
| 285 | default: |
| 286 | SkDEBUGFAIL("unexpected color type"); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 290 | static SkPDFArray* make_indexed_color_space(const SkColorTable* table) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 291 | SkPDFArray* result = new SkPDFArray; |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 292 | result->reserve(4); |
| 293 | result->appendName("Indexed"); |
| 294 | result->appendName("DeviceRGB"); |
| 295 | SkASSERT(table); |
| 296 | if (table->count() < 1) { |
| 297 | result->appendInt(0); |
| 298 | char shortTableArray[3] = {0, 0, 0}; |
| 299 | SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 300 | result->appendString(tableString); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 301 | return result; |
| 302 | } |
| 303 | result->appendInt(table->count() - 1); // maximum color index. |
| 304 | |
| 305 | // Potentially, this could be represented in fewer bytes with a stream. |
| 306 | // Max size as a string is 1.5k. |
| 307 | char tableArray[256 * 3]; |
| 308 | SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); |
| 309 | uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); |
| 310 | const SkPMColor* colors = table->readColors(); |
| 311 | for (int i = 0; i < table->count(); i++) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 312 | pmcolor_to_rgb24(colors[i], tablePtr, kN32_SkColorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 313 | tablePtr += 3; |
| 314 | } |
| 315 | SkString tableString(tableArray, 3 * table->count()); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 316 | result->appendString(tableString); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 317 | return result; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 318 | } |
| 319 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 320 | static void emit_image_xobject(SkWStream* stream, |
| 321 | const SkImage* image, |
| 322 | bool alpha, |
| 323 | SkPDFObject* smask, |
| 324 | const SkPDFObjNumMap& objNumMap, |
| 325 | const SkPDFSubstituteMap& substitutes) { |
| 326 | SkBitmap bitmap; |
| 327 | image_get_ro_pixels(image, &bitmap); // TODO(halcanary): test |
| 328 | SkAutoLockPixels autoLockPixels(bitmap); // with malformed images. |
| 329 | SkASSERT(bitmap.colorType() != kIndex_8_SkColorType || |
| 330 | bitmap.getColorTable()); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 331 | |
| 332 | // Write to a temporary buffer to get the compressed length. |
| 333 | SkDynamicMemoryWStream buffer; |
| 334 | SkDeflateWStream deflateWStream(&buffer); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 335 | if (alpha) { |
| 336 | bitmap_alpha_to_a8(bitmap, &deflateWStream); |
| 337 | } else { |
| 338 | bitmap_to_pdf_pixels(bitmap, &deflateWStream); |
| 339 | } |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 340 | deflateWStream.finalize(); // call before detachAsStream(). |
| 341 | SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
| 342 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 343 | SkPDFDict pdfDict("XObject"); |
| 344 | pdfDict.insertName("Subtype", "Image"); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 345 | pdfDict.insertInt("Width", bitmap.width()); |
| 346 | pdfDict.insertInt("Height", bitmap.height()); |
| 347 | if (alpha) { |
| 348 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 349 | } else if (bitmap.colorType() == kIndex_8_SkColorType) { |
| 350 | SkASSERT(1 == pdf_color_component_count(bitmap.colorType())); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 351 | pdfDict.insertObject("ColorSpace", |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 352 | make_indexed_color_space(bitmap.getColorTable())); |
| 353 | } else if (1 == pdf_color_component_count(bitmap.colorType())) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 354 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 355 | } else { |
| 356 | pdfDict.insertName("ColorSpace", "DeviceRGB"); |
| 357 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 358 | if (smask) { |
| 359 | pdfDict.insertObjRef("SMask", SkRef(smask)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 360 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 361 | pdfDict.insertInt("BitsPerComponent", 8); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 362 | pdfDict.insertName("Filter", "FlateDecode"); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 363 | pdfDict.insertInt("Length", asset->getLength()); |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 364 | pdfDict.emitObject(stream, objNumMap, substitutes); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 365 | |
| 366 | pdf_stream_begin(stream); |
| 367 | stream->writeStream(asset.get(), asset->getLength()); |
| 368 | pdf_stream_end(stream); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 369 | } |
| 370 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 371 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 372 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 373 | namespace { |
| 374 | // This SkPDFObject only outputs the alpha layer of the given bitmap. |
| 375 | class PDFAlphaBitmap : public SkPDFObject { |
| 376 | public: |
| 377 | PDFAlphaBitmap(const SkImage* image) : fImage(SkRef(image)) {} |
| 378 | ~PDFAlphaBitmap() {} |
| 379 | void emitObject(SkWStream* stream, |
| 380 | const SkPDFObjNumMap& objNumMap, |
| 381 | const SkPDFSubstituteMap& subs) const override { |
| 382 | emit_image_xobject(stream, fImage, true, nullptr, objNumMap, subs); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 383 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 384 | |
| 385 | private: |
| 386 | SkAutoTUnref<const SkImage> fImage; |
| 387 | }; |
| 388 | |
| 389 | } // namespace |
| 390 | |
| 391 | //////////////////////////////////////////////////////////////////////////////// |
| 392 | |
| 393 | namespace { |
| 394 | class PDFDefaultBitmap : public SkPDFObject { |
| 395 | public: |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 396 | void emitObject(SkWStream* stream, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 397 | const SkPDFObjNumMap& objNumMap, |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 398 | const SkPDFSubstituteMap& subs) const override { |
| 399 | emit_image_xobject(stream, fImage, false, fSMask, objNumMap, subs); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 400 | } |
| 401 | void addResources(SkPDFObjNumMap* catalog, |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 402 | const SkPDFSubstituteMap& subs) const override { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 403 | if (fSMask.get()) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 404 | SkPDFObject* obj = subs.getSubstitute(fSMask.get()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 405 | SkASSERT(obj); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 406 | catalog->addObjectRecursively(obj, subs); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | PDFDefaultBitmap(const SkImage* image, SkPDFObject* smask) |
| 410 | : fImage(SkRef(image)), fSMask(smask) {} |
| 411 | |
| 412 | private: |
| 413 | SkAutoTUnref<const SkImage> fImage; |
| 414 | const SkAutoTUnref<SkPDFObject> fSMask; |
| 415 | }; |
| 416 | } // namespace |
| 417 | |
| 418 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 419 | |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 420 | namespace { |
| 421 | /** |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 422 | * This PDFObject assumes that its constructor was handed YUV or |
| 423 | * Grayscale JFIF Jpeg-encoded data that can be directly embedded |
| 424 | * into a PDF. |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 425 | */ |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 426 | class PDFJpegBitmap : public SkPDFObject { |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 427 | public: |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 428 | SkISize fSize; |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 429 | SkAutoTUnref<SkData> fData; |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 430 | bool fIsYUV; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 431 | PDFJpegBitmap(SkISize size, SkData* data, bool isYUV) |
| 432 | : fSize(size), fData(SkRef(data)), fIsYUV(isYUV) {} |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 433 | void emitObject(SkWStream*, |
| 434 | const SkPDFObjNumMap&, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 435 | const SkPDFSubstituteMap&) const override; |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | void PDFJpegBitmap::emitObject(SkWStream* stream, |
| 439 | const SkPDFObjNumMap& objNumMap, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 440 | const SkPDFSubstituteMap& substituteMap) const { |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 441 | SkPDFDict pdfDict("XObject"); |
| 442 | pdfDict.insertName("Subtype", "Image"); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 443 | pdfDict.insertInt("Width", fSize.width()); |
| 444 | pdfDict.insertInt("Height", fSize.height()); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 445 | if (fIsYUV) { |
| 446 | pdfDict.insertName("ColorSpace", "DeviceRGB"); |
| 447 | } else { |
| 448 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 449 | } |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 450 | pdfDict.insertInt("BitsPerComponent", 8); |
| 451 | pdfDict.insertName("Filter", "DCTDecode"); |
| 452 | pdfDict.insertInt("ColorTransform", 0); |
| 453 | pdfDict.insertInt("Length", SkToInt(fData->size())); |
| 454 | pdfDict.emitObject(stream, objNumMap, substituteMap); |
| 455 | pdf_stream_begin(stream); |
| 456 | stream->write(fData->data(), fData->size()); |
| 457 | pdf_stream_end(stream); |
| 458 | } |
| 459 | } // namespace |
| 460 | |
| 461 | //////////////////////////////////////////////////////////////////////////////// |
| 462 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 463 | SkPDFObject* SkPDFCreateBitmapObject(const SkImage* image) { |
| 464 | SkAutoTUnref<SkData> data(image->refEncoded()); |
| 465 | SkJFIFInfo info; |
| 466 | if (data && SkIsJFIF(data, &info)) { |
| 467 | bool yuv = info.fType == SkJFIFInfo::kYCbCr; |
| 468 | if (info.fSize == image->dimensions()) { // Sanity check. |
| 469 | // hold on to data, not image. |
| 470 | #ifdef SK_PDF_IMAGE_STATS |
| 471 | gJpegImageObjects.fetch_add(1); |
| 472 | #endif |
| 473 | return new PDFJpegBitmap(info.fSize, data, yuv); |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 476 | SkPDFObject* smask = |
| 477 | image_compute_is_opaque(image) ? nullptr : new PDFAlphaBitmap(image); |
| 478 | #ifdef SK_PDF_IMAGE_STATS |
| 479 | gRegularImageObjects.fetch_add(1); |
| 480 | #endif |
| 481 | return new PDFDefaultBitmap(image, smask); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 482 | } |