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