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" |
martina.kollarova | b8d6af1 | 2016-06-29 05:12:31 -0700 | [diff] [blame] | 15 | #include "SkPDFTypes.h" |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkUnPreMultiply.h" |
| 18 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 19 | void image_get_ro_pixels(const SkImage* image, SkBitmap* dst) { |
| 20 | if(as_IB(image)->getROPixels(dst) |
| 21 | && dst->dimensions() == image->dimensions()) { |
halcanary | 80ec3db | 2015-11-10 09:48:40 -0800 | [diff] [blame] | 22 | if (dst->colorType() != kIndex_8_SkColorType) { |
| 23 | return; |
| 24 | } |
| 25 | // We must check to see if the bitmap has a color table. |
| 26 | SkAutoLockPixels autoLockPixels(*dst); |
| 27 | if (!dst->getColorTable()) { |
| 28 | // We can't use an indexed bitmap with no colortable. |
| 29 | dst->reset(); |
| 30 | } else { |
| 31 | return; |
| 32 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 33 | } |
| 34 | // no pixels or wrong size: fill with zeros. |
| 35 | SkAlphaType at = image->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 36 | dst->setInfo(SkImageInfo::MakeN32(image->width(), image->height(), at)); |
| 37 | } |
| 38 | |
| 39 | bool image_compute_is_opaque(const SkImage* image) { |
| 40 | if (image->isOpaque()) { |
| 41 | return true; |
| 42 | } |
| 43 | // keep output PDF small at cost of possible resource use. |
| 44 | SkBitmap bm; |
| 45 | image_get_ro_pixels(image, &bm); |
| 46 | return SkBitmap::ComputeIsOpaque(bm); |
| 47 | } |
| 48 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 49 | //////////////////////////////////////////////////////////////////////////////// |
| 50 | |
| 51 | static void pdf_stream_begin(SkWStream* stream) { |
| 52 | static const char streamBegin[] = " stream\n"; |
| 53 | stream->write(streamBegin, strlen(streamBegin)); |
| 54 | } |
| 55 | |
| 56 | static void pdf_stream_end(SkWStream* stream) { |
| 57 | static const char streamEnd[] = "\nendstream"; |
| 58 | stream->write(streamEnd, strlen(streamEnd)); |
| 59 | } |
| 60 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 61 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 62 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 63 | // write a single byte to a stream n times. |
| 64 | static void fill_stream(SkWStream* out, char value, size_t n) { |
| 65 | char buffer[4096]; |
| 66 | memset(buffer, value, sizeof(buffer)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 67 | for (size_t i = 0; i < n / sizeof(buffer); ++i) { |
| 68 | out->write(buffer, sizeof(buffer)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 69 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 70 | out->write(buffer, n % sizeof(buffer)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 73 | // TODO(reed@): Decide if these five functions belong in SkColorPriv.h |
| 74 | static bool SkIsBGRA(SkColorType ct) { |
| 75 | SkASSERT(kBGRA_8888_SkColorType == ct || kRGBA_8888_SkColorType == ct); |
| 76 | return kBGRA_8888_SkColorType == ct; |
| 77 | } |
| 78 | |
| 79 | // Interpret value as the given 4-byte SkColorType (BGRA_8888 or |
| 80 | // RGBA_8888) and return the appropriate component. Each component |
| 81 | // should be interpreted according to the associated SkAlphaType and |
| 82 | // SkColorProfileType. |
| 83 | static U8CPU SkGetA32Component(uint32_t value, SkColorType ct) { |
| 84 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_A32_SHIFT : SK_RGBA_A32_SHIFT)) & 0xFF; |
| 85 | } |
| 86 | static U8CPU SkGetR32Component(uint32_t value, SkColorType ct) { |
| 87 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_R32_SHIFT : SK_RGBA_R32_SHIFT)) & 0xFF; |
| 88 | } |
| 89 | static U8CPU SkGetG32Component(uint32_t value, SkColorType ct) { |
| 90 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_G32_SHIFT : SK_RGBA_G32_SHIFT)) & 0xFF; |
| 91 | } |
| 92 | static U8CPU SkGetB32Component(uint32_t value, SkColorType ct) { |
| 93 | return (value >> (SkIsBGRA(ct) ? SK_BGRA_B32_SHIFT : SK_RGBA_B32_SHIFT)) & 0xFF; |
| 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(); |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 186 | SkAlphaType alphaType = bm.alphaType(); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 187 | switch (colorType) { |
| 188 | case kRGBA_8888_SkColorType: |
| 189 | case kBGRA_8888_SkColorType: { |
| 190 | SkASSERT(3 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 191 | SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
| 192 | for (int y = 0; y < bm.height(); ++y) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 193 | const uint32_t* src = bm.getAddr32(0, y); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 194 | uint8_t* dst = scanline.get(); |
| 195 | for (int x = 0; x < bm.width(); ++x) { |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 196 | if (alphaType == kPremul_SkAlphaType) { |
| 197 | uint32_t color = *src++; |
| 198 | U8CPU alpha = SkGetA32Component(color, colorType); |
| 199 | if (alpha != SK_AlphaTRANSPARENT) { |
| 200 | pmcolor_to_rgb24(color, dst, colorType); |
| 201 | } else { |
| 202 | get_neighbor_avg_color(bm, x, y, dst, colorType); |
| 203 | } |
| 204 | dst += 3; |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 205 | } else { |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 206 | uint32_t color = *src++; |
| 207 | *dst++ = SkGetR32Component(color, colorType); |
| 208 | *dst++ = SkGetG32Component(color, colorType); |
| 209 | *dst++ = SkGetB32Component(color, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 210 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 211 | } |
| 212 | out->write(scanline.get(), 3 * bm.width()); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 213 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 214 | return; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 215 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 216 | case kRGB_565_SkColorType: { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 217 | SkASSERT(3 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 218 | SkAutoTMalloc<uint8_t> scanline(3 * bm.width()); |
| 219 | for (int y = 0; y < bm.height(); ++y) { |
| 220 | const uint16_t* src = bm.getAddr16(0, y); |
| 221 | uint8_t* dst = scanline.get(); |
| 222 | for (int x = 0; x < bm.width(); ++x) { |
| 223 | U16CPU color565 = *src++; |
| 224 | *dst++ = SkPacked16ToR32(color565); |
| 225 | *dst++ = SkPacked16ToG32(color565); |
| 226 | *dst++ = SkPacked16ToB32(color565); |
| 227 | } |
| 228 | out->write(scanline.get(), 3 * bm.width()); |
| 229 | } |
| 230 | return; |
| 231 | } |
| 232 | case kAlpha_8_SkColorType: |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 233 | SkASSERT(1 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 234 | fill_stream(out, '\x00', pixel_count(bm)); |
| 235 | return; |
| 236 | case kGray_8_SkColorType: |
| 237 | case kIndex_8_SkColorType: |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 238 | SkASSERT(1 == pdf_color_component_count(colorType)); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 239 | // these two formats need no transformation to serialize. |
| 240 | for (int y = 0; y < bm.height(); ++y) { |
| 241 | out->write(bm.getAddr8(0, y), bm.width()); |
| 242 | } |
| 243 | return; |
| 244 | case kUnknown_SkColorType: |
| 245 | case kARGB_4444_SkColorType: |
| 246 | default: |
| 247 | SkDEBUGFAIL("unexpected color type"); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 251 | //////////////////////////////////////////////////////////////////////////////// |
| 252 | |
| 253 | static void bitmap_alpha_to_a8(const SkBitmap& bitmap, SkWStream* out) { |
| 254 | if (!bitmap.getPixels()) { |
| 255 | fill_stream(out, '\xFF', pixel_count(bitmap)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 256 | return; |
| 257 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 258 | SkBitmap copy; |
| 259 | const SkBitmap& bm = not4444(bitmap, ©); |
| 260 | SkAutoLockPixels autoLockPixels(bm); |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 261 | SkColorType colorType = bm.colorType(); |
| 262 | switch (colorType) { |
| 263 | case kRGBA_8888_SkColorType: |
| 264 | case kBGRA_8888_SkColorType: { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 265 | SkAutoTMalloc<uint8_t> scanline(bm.width()); |
| 266 | for (int y = 0; y < bm.height(); ++y) { |
| 267 | uint8_t* dst = scanline.get(); |
| 268 | const SkPMColor* src = bm.getAddr32(0, y); |
| 269 | for (int x = 0; x < bm.width(); ++x) { |
halcanary | aa4ba90 | 2015-11-06 07:27:23 -0800 | [diff] [blame] | 270 | *dst++ = SkGetA32Component(*src++, colorType); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 271 | } |
| 272 | out->write(scanline.get(), bm.width()); |
| 273 | } |
| 274 | return; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 275 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 276 | case kAlpha_8_SkColorType: |
| 277 | for (int y = 0; y < bm.height(); ++y) { |
| 278 | out->write(bm.getAddr8(0, y), bm.width()); |
| 279 | } |
| 280 | return; |
| 281 | case kIndex_8_SkColorType: { |
| 282 | SkColorTable* ct = bm.getColorTable(); |
| 283 | SkASSERT(ct); |
| 284 | SkAutoTMalloc<uint8_t> scanline(bm.width()); |
| 285 | for (int y = 0; y < bm.height(); ++y) { |
| 286 | uint8_t* dst = scanline.get(); |
| 287 | const uint8_t* src = bm.getAddr8(0, y); |
| 288 | for (int x = 0; x < bm.width(); ++x) { |
| 289 | *dst++ = SkGetPackedA32((*ct)[*src++]); |
| 290 | } |
| 291 | out->write(scanline.get(), bm.width()); |
| 292 | } |
| 293 | return; |
| 294 | } |
| 295 | case kRGB_565_SkColorType: |
| 296 | case kGray_8_SkColorType: |
| 297 | SkDEBUGFAIL("color type has no alpha"); |
| 298 | return; |
| 299 | case kARGB_4444_SkColorType: |
| 300 | SkDEBUGFAIL("4444 color type should have been converted to N32"); |
| 301 | return; |
| 302 | case kUnknown_SkColorType: |
| 303 | default: |
| 304 | SkDEBUGFAIL("unexpected color type"); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 308 | static sk_sp<SkPDFArray> make_indexed_color_space( |
| 309 | const SkColorTable* table, |
| 310 | SkAlphaType alphaType) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 311 | auto result = sk_make_sp<SkPDFArray>(); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 312 | result->reserve(4); |
| 313 | result->appendName("Indexed"); |
| 314 | result->appendName("DeviceRGB"); |
| 315 | SkASSERT(table); |
| 316 | if (table->count() < 1) { |
| 317 | result->appendInt(0); |
| 318 | char shortTableArray[3] = {0, 0, 0}; |
| 319 | SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 320 | result->appendString(tableString); |
halcanary | 51d04d3 | 2016-03-08 13:03:55 -0800 | [diff] [blame] | 321 | return result; |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 322 | } |
| 323 | result->appendInt(table->count() - 1); // maximum color index. |
| 324 | |
| 325 | // Potentially, this could be represented in fewer bytes with a stream. |
| 326 | // Max size as a string is 1.5k. |
| 327 | char tableArray[256 * 3]; |
| 328 | SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); |
| 329 | uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); |
| 330 | const SkPMColor* colors = table->readColors(); |
| 331 | for (int i = 0; i < table->count(); i++) { |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 332 | if (alphaType == kPremul_SkAlphaType) { |
| 333 | pmcolor_to_rgb24(colors[i], tablePtr, kN32_SkColorType); |
| 334 | tablePtr += 3; |
| 335 | } else { |
| 336 | *tablePtr++ = SkGetR32Component(colors[i], kN32_SkColorType); |
| 337 | *tablePtr++ = SkGetG32Component(colors[i], kN32_SkColorType); |
| 338 | *tablePtr++ = SkGetB32Component(colors[i], kN32_SkColorType); |
| 339 | } |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 340 | } |
| 341 | SkString tableString(tableArray, 3 * table->count()); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 342 | result->appendString(tableString); |
halcanary | 51d04d3 | 2016-03-08 13:03:55 -0800 | [diff] [blame] | 343 | return result; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 344 | } |
| 345 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 346 | static void emit_image_xobject(SkWStream* stream, |
| 347 | const SkImage* image, |
| 348 | bool alpha, |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 349 | const sk_sp<SkPDFObject>& smask, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 350 | const SkPDFObjNumMap& objNumMap, |
| 351 | const SkPDFSubstituteMap& substitutes) { |
| 352 | SkBitmap bitmap; |
| 353 | image_get_ro_pixels(image, &bitmap); // TODO(halcanary): test |
| 354 | SkAutoLockPixels autoLockPixels(bitmap); // with malformed images. |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 355 | |
| 356 | // Write to a temporary buffer to get the compressed length. |
| 357 | SkDynamicMemoryWStream buffer; |
| 358 | SkDeflateWStream deflateWStream(&buffer); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 359 | if (alpha) { |
| 360 | bitmap_alpha_to_a8(bitmap, &deflateWStream); |
| 361 | } else { |
| 362 | bitmap_to_pdf_pixels(bitmap, &deflateWStream); |
| 363 | } |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 364 | deflateWStream.finalize(); // call before detachAsStream(). |
halcanary | b8fb993 | 2016-03-28 07:58:30 -0700 | [diff] [blame] | 365 | std::unique_ptr<SkStreamAsset> asset(buffer.detachAsStream()); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 366 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 367 | SkPDFDict pdfDict("XObject"); |
| 368 | pdfDict.insertName("Subtype", "Image"); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 369 | pdfDict.insertInt("Width", bitmap.width()); |
| 370 | pdfDict.insertInt("Height", bitmap.height()); |
| 371 | if (alpha) { |
| 372 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 373 | } else if (bitmap.colorType() == kIndex_8_SkColorType) { |
| 374 | SkASSERT(1 == pdf_color_component_count(bitmap.colorType())); |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 375 | pdfDict.insertObject("ColorSpace", |
halcanary | 7b9eabb | 2016-06-03 08:57:03 -0700 | [diff] [blame] | 376 | make_indexed_color_space(bitmap.getColorTable(), |
| 377 | bitmap.alphaType())); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 378 | } else if (1 == pdf_color_component_count(bitmap.colorType())) { |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 379 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 380 | } else { |
| 381 | pdfDict.insertName("ColorSpace", "DeviceRGB"); |
| 382 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 383 | if (smask) { |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 384 | pdfDict.insertObjRef("SMask", smask); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 385 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 386 | pdfDict.insertInt("BitsPerComponent", 8); |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 387 | pdfDict.insertName("Filter", "FlateDecode"); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 388 | pdfDict.insertInt("Length", asset->getLength()); |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 389 | pdfDict.emitObject(stream, objNumMap, substitutes); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 390 | |
| 391 | pdf_stream_begin(stream); |
| 392 | stream->writeStream(asset.get(), asset->getLength()); |
| 393 | pdf_stream_end(stream); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 394 | } |
| 395 | |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 396 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 397 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 398 | namespace { |
| 399 | // This SkPDFObject only outputs the alpha layer of the given bitmap. |
halcanary | 70d1554 | 2015-11-22 12:55:04 -0800 | [diff] [blame] | 400 | class PDFAlphaBitmap final : public SkPDFObject { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 401 | public: |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 402 | PDFAlphaBitmap(sk_sp<SkImage> image) : fImage(std::move(image)) { SkASSERT(fImage); } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 403 | void emitObject(SkWStream* stream, |
| 404 | const SkPDFObjNumMap& objNumMap, |
| 405 | const SkPDFSubstituteMap& subs) const override { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 406 | SkASSERT(fImage); |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 407 | emit_image_xobject(stream, fImage.get(), true, nullptr, objNumMap, subs); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 408 | } |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 409 | void drop() override { fImage = nullptr; } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 410 | |
| 411 | private: |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 412 | sk_sp<SkImage> fImage; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | } // namespace |
| 416 | |
| 417 | //////////////////////////////////////////////////////////////////////////////// |
| 418 | |
| 419 | namespace { |
halcanary | 70d1554 | 2015-11-22 12:55:04 -0800 | [diff] [blame] | 420 | class PDFDefaultBitmap final : public SkPDFObject { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 421 | public: |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 422 | void emitObject(SkWStream* stream, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 423 | const SkPDFObjNumMap& objNumMap, |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 424 | const SkPDFSubstituteMap& subs) const override { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 425 | SkASSERT(fImage); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 426 | emit_image_xobject(stream, fImage.get(), false, fSMask, objNumMap, subs); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 427 | } |
| 428 | void addResources(SkPDFObjNumMap* catalog, |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 429 | const SkPDFSubstituteMap& subs) const override { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 430 | SkASSERT(fImage); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 431 | if (fSMask.get()) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 432 | SkPDFObject* obj = subs.getSubstitute(fSMask.get()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 433 | SkASSERT(obj); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 434 | catalog->addObjectRecursively(obj, subs); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 437 | void drop() override { fImage = nullptr; fSMask = nullptr; } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 438 | PDFDefaultBitmap(sk_sp<SkImage> image, sk_sp<SkPDFObject> smask) |
| 439 | : fImage(std::move(image)), fSMask(std::move(smask)) { SkASSERT(fImage); } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 440 | |
| 441 | private: |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 442 | sk_sp<SkImage> fImage; |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 443 | sk_sp<SkPDFObject> fSMask; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 444 | }; |
| 445 | } // namespace |
| 446 | |
| 447 | //////////////////////////////////////////////////////////////////////////////// |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 448 | |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 449 | namespace { |
| 450 | /** |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 451 | * This PDFObject assumes that its constructor was handed YUV or |
| 452 | * Grayscale JFIF Jpeg-encoded data that can be directly embedded |
| 453 | * into a PDF. |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 454 | */ |
halcanary | 70d1554 | 2015-11-22 12:55:04 -0800 | [diff] [blame] | 455 | class PDFJpegBitmap final : public SkPDFObject { |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 456 | public: |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 457 | SkISize fSize; |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 458 | sk_sp<SkData> fData; |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 459 | bool fIsYUV; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 460 | PDFJpegBitmap(SkISize size, SkData* data, bool isYUV) |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 461 | : fSize(size), fData(SkRef(data)), fIsYUV(isYUV) { SkASSERT(data); } |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 462 | void emitObject(SkWStream*, |
| 463 | const SkPDFObjNumMap&, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 464 | const SkPDFSubstituteMap&) const override; |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 465 | void drop() override { fData = nullptr; } |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | void PDFJpegBitmap::emitObject(SkWStream* stream, |
| 469 | const SkPDFObjNumMap& objNumMap, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 470 | const SkPDFSubstituteMap& substituteMap) const { |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 471 | SkASSERT(fData); |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 472 | SkPDFDict pdfDict("XObject"); |
| 473 | pdfDict.insertName("Subtype", "Image"); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 474 | pdfDict.insertInt("Width", fSize.width()); |
| 475 | pdfDict.insertInt("Height", fSize.height()); |
halcanary | 96287f7 | 2015-05-07 11:46:59 -0700 | [diff] [blame] | 476 | if (fIsYUV) { |
| 477 | pdfDict.insertName("ColorSpace", "DeviceRGB"); |
| 478 | } else { |
| 479 | pdfDict.insertName("ColorSpace", "DeviceGray"); |
| 480 | } |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 481 | pdfDict.insertInt("BitsPerComponent", 8); |
| 482 | pdfDict.insertName("Filter", "DCTDecode"); |
| 483 | pdfDict.insertInt("ColorTransform", 0); |
| 484 | pdfDict.insertInt("Length", SkToInt(fData->size())); |
| 485 | pdfDict.emitObject(stream, objNumMap, substituteMap); |
| 486 | pdf_stream_begin(stream); |
| 487 | stream->write(fData->data(), fData->size()); |
| 488 | pdf_stream_end(stream); |
| 489 | } |
| 490 | } // namespace |
| 491 | |
| 492 | //////////////////////////////////////////////////////////////////////////////// |
| 493 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 494 | sk_sp<SkPDFObject> SkPDFCreateBitmapObject(sk_sp<SkImage> image, |
| 495 | SkPixelSerializer* pixelSerializer) { |
| 496 | SkASSERT(image); |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 497 | sk_sp<SkData> data(image->refEncoded()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 498 | SkJFIFInfo info; |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 499 | if (data && SkIsJFIF(data.get(), &info) && |
halcanary | 7363e13 | 2016-02-25 17:21:40 -0800 | [diff] [blame] | 500 | (!pixelSerializer || |
| 501 | pixelSerializer->useEncodedData(data->data(), data->size()))) { |
| 502 | // If there is a SkPixelSerializer, give it a chance to |
| 503 | // re-encode the JPEG with more compression by returning false |
| 504 | // from useEncodedData. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 505 | bool yuv = info.fType == SkJFIFInfo::kYCbCr; |
| 506 | if (info.fSize == image->dimensions()) { // Sanity check. |
| 507 | // hold on to data, not image. |
| 508 | #ifdef SK_PDF_IMAGE_STATS |
| 509 | gJpegImageObjects.fetch_add(1); |
| 510 | #endif |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 511 | return sk_make_sp<PDFJpegBitmap>(info.fSize, data.get(), yuv); |
halcanary | a8448bc | 2015-04-17 13:27:24 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 514 | |
| 515 | if (pixelSerializer) { |
| 516 | SkBitmap bm; |
| 517 | SkAutoPixmapUnlock apu; |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 518 | if (as_IB(image.get())->getROPixels(&bm) && bm.requestLock(&apu)) { |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 519 | data.reset(pixelSerializer->encode(apu.pixmap())); |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 520 | if (data && SkIsJFIF(data.get(), &info)) { |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 521 | bool yuv = info.fType == SkJFIFInfo::kYCbCr; |
| 522 | if (info.fSize == image->dimensions()) { // Sanity check. |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 523 | return sk_make_sp<PDFJpegBitmap>(info.fSize, data.get(), yuv); |
halcanary | 712fdf7 | 2015-12-10 08:59:43 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 529 | sk_sp<SkPDFObject> smask; |
| 530 | if (!image_compute_is_opaque(image.get())) { |
| 531 | smask = sk_make_sp<PDFAlphaBitmap>(image); |
| 532 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 533 | #ifdef SK_PDF_IMAGE_STATS |
| 534 | gRegularImageObjects.fetch_add(1); |
| 535 | #endif |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 536 | return sk_make_sp<PDFDefaultBitmap>(std::move(image), std::move(smask)); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 537 | } |