| 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 "src/pdf/SkPDFBitmap.h" |
| 9 |
|
| 10 |
#include "include/codec/SkCodec.h" |
| 11 |
#include "include/codec/SkEncodedOrigin.h" |
| 12 |
#include "include/core/SkAlphaType.h" |
| 13 |
#include "include/core/SkBitmap.h" |
| 14 |
#include "include/core/SkColor.h" |
| 15 |
#include "include/core/SkColorSpace.h" |
| 16 |
#include "include/core/SkColorType.h" |
| 17 |
#include "include/core/SkData.h" |
| 18 |
#include "include/core/SkExecutor.h" |
| 19 |
#include "include/core/SkImage.h" |
| 20 |
#include "include/core/SkImageInfo.h" |
| 21 |
#include "include/core/SkPixmap.h" |
| 22 |
#include "include/core/SkSize.h" |
| 23 |
#include "include/core/SkStream.h" |
| 24 |
#include "include/docs/SkPDFDocument.h" |
| 25 |
#include "include/encode/SkICC.h" |
| 26 |
#include "include/private/SkEncodedInfo.h" |
| 27 |
#include "include/private/base/SkAssert.h" |
| 28 |
#include "include/private/base/SkMutex.h" |
| 29 |
#include "include/private/base/SkTo.h" |
| 30 |
#include "modules/skcms/skcms.h" |
| 31 |
#include "src/codec/SkCodecPriv.h" |
| 32 |
#include "src/core/SkColorPriv.h" |
| 33 |
#include "src/core/SkTHash.h" |
| 34 |
#include "src/pdf/SkDeflate.h" |
| 35 |
#include "src/pdf/SkPDFDocumentPriv.h" |
| 36 |
#include "src/pdf/SkPDFTypes.h" |
| 37 |
#include "src/pdf/SkPDFUnion.h" |
| 38 |
|
| 39 |
#include <algorithm> |
| 40 |
#include <array> |
| 41 |
#include <cstring> |
| 42 |
#include <functional> |
| 43 |
#include <memory> |
| 44 |
#include <optional> |
| 45 |
#include <utility> |
| 46 |
|
| 47 |
namespace { |
| 48 |
|
| 49 |
// write a single byte to a stream n times. |
| 50 |
void fill_stream(SkWStream* out, char value, size_t n) { |
| 51 |
char buffer[4096]; |
| 52 |
memset(buffer, value, sizeof(buffer)); |
| 53 |
for (size_t i = 0; i < n / sizeof(buffer); ++i) { |
| 54 |
out->write(buffer, sizeof(buffer)); |
| 55 |
} |
| 56 |
out->write(buffer, n % sizeof(buffer)); |
| 57 |
} |
| 58 |
|
| 59 |
/* It is necessary to average the color component of transparent |
| 60 |
pixels with their surrounding neighbors since the PDF renderer may |
| 61 |
separately re-sample the alpha and color channels when the image is |
| 62 |
not displayed at its native resolution. Since an alpha of zero |
| 63 |
gives no information about the color component, the pathological |
| 64 |
case is a white image with sharp transparency bounds - the color |
| 65 |
channel goes to black, and the should-be-transparent pixels are |
| 66 |
rendered as grey because of the separate soft mask and color |
| 67 |
resizing. e.g.: gm/bitmappremul.cpp */ |
| 68 |
SkColor get_neighbor_avg_color(const SkPixmap& bm, int xOrig, int yOrig) { |
| 69 |
SkASSERT(kBGRA_8888_SkColorType == bm.colorType()); |
| 70 |
unsigned r = 0, g = 0, b = 0, n = 0; |
| 71 |
// Clamp the range to the edge of the bitmap. |
| 72 |
int ymin = std::max(0, yOrig - 1); |
| 73 |
int ymax = std::min(yOrig + 1, bm.height() - 1); |
| 74 |
int xmin = std::max(0, xOrig - 1); |
| 75 |
int xmax = std::min(xOrig + 1, bm.width() - 1); |
| 76 |
for (int y = ymin; y <= ymax; ++y) { |
| 77 |
const SkColor* scanline = bm.addr32(0, y); |
| 78 |
for (int x = xmin; x <= xmax; ++x) { |
| 79 |
SkColor color = scanline[x]; |
| 80 |
if (color != SK_ColorTRANSPARENT) { |
| 81 |
r += SkColorGetR(color); |
| 82 |
g += SkColorGetG(color); |
| 83 |
b += SkColorGetB(color); |
| 84 |
n++; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return n > 0 ? SkColorSetRGB(SkToU8(r / n), SkToU8(g / n), SkToU8(b / n)) |
| 89 |
: SK_ColorTRANSPARENT; |
| 90 |
} |
| 91 |
|
| 92 |
enum class SkPDFStreamFormat { DCT, Flate, Uncompressed }; |
| 93 |
|
| 94 |
template <typename T> |
| 95 |
void emit_image_stream(SkPDFDocument* doc, |
| 96 |
SkPDFIndirectReference ref, |
| 97 |
T writeStream, |
| 98 |
SkISize size, |
| 99 |
SkPDFUnion&& colorSpace, |
| 100 |
SkPDFIndirectReference sMask, |
| 101 |
size_t length, |
| 102 |
SkPDFStreamFormat format) { |
| 103 |
if (!ref) { |
| 104 |
return; |
| 105 |
} |
| 106 |
SkPDFDict pdfDict("XObject"); |
| 107 |
pdfDict.insertName("Subtype", "Image"); |
| 108 |
pdfDict.insertInt("Width", size.width()); |
| 109 |
pdfDict.insertInt("Height", size.height()); |
| 110 |
pdfDict.insertUnion("ColorSpace", std::move(colorSpace)); |
| 111 |
if (sMask) { |
| 112 |
pdfDict.insertRef("SMask", sMask); |
| 113 |
} |
| 114 |
pdfDict.insertInt("BitsPerComponent", 8); |
| 115 |
switch (format) { |
| 116 |
case SkPDFStreamFormat::DCT: pdfDict.insertName("Filter", "DCTDecode"); break; |
| 117 |
case SkPDFStreamFormat::Flate: pdfDict.insertName("Filter", "FlateDecode"); break; |
| 118 |
case SkPDFStreamFormat::Uncompressed: break; |
| 119 |
} |
| 120 |
if (format == SkPDFStreamFormat::DCT) { |
| 121 |
pdfDict.insertInt("ColorTransform", 0); |
| 122 |
} |
| 123 |
pdfDict.insertInt("Length", length); |
| 124 |
doc->emitStream(pdfDict, std::move(writeStream), ref); |
| 125 |
} |
| 126 |
|
| 127 |
size_t do_deflated_alpha(const SkPixmap& pm, SkPDFDocument* doc, SkPDFIndirectReference ref) { |
| 128 |
SkPDF::Metadata::CompressionLevel compressionLevel = doc->metadata().fCompressionLevel; |
| 129 |
SkPDFStreamFormat format = compressionLevel == SkPDF::Metadata::CompressionLevel::None |
| 130 |
? SkPDFStreamFormat::Uncompressed |
| 131 |
: SkPDFStreamFormat::Flate; |
| 132 |
SkDynamicMemoryWStream buffer; |
| 133 |
SkWStream* stream = &buffer; |
| 134 |
std::optional<SkDeflateWStream> deflateWStream; |
| 135 |
if (format == SkPDFStreamFormat::Flate) { |
| 136 |
deflateWStream.emplace(&buffer, SkToInt(compressionLevel)); |
| 137 |
stream = &*deflateWStream; |
| 138 |
} |
| 139 |
if (kAlpha_8_SkColorType == pm.colorType()) { |
| 140 |
SkASSERT(pm.rowBytes() == (size_t)pm.width()); |
| 141 |
stream->write(pm.addr8(), pm.width() * pm.height()); |
| 142 |
} else { |
| 143 |
SkASSERT(pm.alphaType() == kUnpremul_SkAlphaType); |
| 144 |
SkASSERT(pm.colorType() == kBGRA_8888_SkColorType); |
| 145 |
SkASSERT(pm.rowBytes() == (size_t)pm.width() * 4); |
| 146 |
const uint32_t* ptr = pm.addr32(); |
| 147 |
const uint32_t* stop = ptr + pm.height() * pm.width(); |
| 148 |
|
| 149 |
uint8_t byteBuffer[4092]; |
| 150 |
uint8_t* bufferStop = byteBuffer + std::size(byteBuffer); |
| 151 |
uint8_t* dst = byteBuffer; |
| 152 |
while (ptr != stop) { |
| 153 |
*dst++ = 0xFF & ((*ptr++) >> SK_BGRA_A32_SHIFT); |
| 154 |
if (dst == bufferStop) { |
| 155 |
stream->write(byteBuffer, sizeof(byteBuffer)); |
| 156 |
dst = byteBuffer; |
| 157 |
} |
| 158 |
} |
| 159 |
stream->write(byteBuffer, dst - byteBuffer); |
| 160 |
} |
| 161 |
if (deflateWStream) { |
| 162 |
deflateWStream->finalize(); |
| 163 |
} |
| 164 |
|
| 165 |
size_t length = SkToInt(buffer.bytesWritten()); |
| 166 |
emit_image_stream(doc, ref, [&buffer](SkWStream* stream) { buffer.writeToAndReset(stream); }, |
| 167 |
pm.info().dimensions(), SkPDFUnion::Name("DeviceGray"), |
| 168 |
SkPDFIndirectReference(), length, format); |
| 169 |
return length; |
| 170 |
} |
| 171 |
|
| 172 |
SkPDFUnion write_icc_profile(SkPDFDocument* doc, sk_sp<const SkData>&& icc, int channels) { |
| 173 |
SkPDFIndirectReference iccStreamRef; |
| 174 |
{ |
| 175 |
static SkMutex iccProfileMapMutex; |
| 176 |
SkAutoMutexExclusive lock(iccProfileMapMutex); |
| 177 |
|
| 178 |
SkPDFIndirectReference* ref = doc->fICCProfileMap.find(SkPDFIccProfileKey{icc, channels}); |
| 179 |
if (ref) { |
| 180 |
iccStreamRef = *ref; |
| 181 |
} else { |
| 182 |
std::unique_ptr<SkPDFDict> iccStreamDict = SkPDFMakeDict(); |
| 183 |
iccStreamDict->insertInt("N", channels); |
| 184 |
iccStreamRef = SkPDFStreamOut(std::move(iccStreamDict), SkMemoryStream::Make(icc), doc); |
| 185 |
doc->fICCProfileMap.set(SkPDFIccProfileKey{icc, channels}, iccStreamRef); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
std::unique_ptr<SkPDFArray> iccPDF = SkPDFMakeArray(); |
| 190 |
iccPDF->appendName("ICCBased"); |
| 191 |
iccPDF->appendRef(iccStreamRef); |
| 192 |
return SkPDFUnion::Object(std::move(iccPDF)); |
| 193 |
} |
| 194 |
|
| 195 |
bool icc_channel_mismatch(const skcms_ICCProfile* iccProfile, int expectedChannels) { |
| 196 |
int iccChannels = -1; |
| 197 |
if (iccProfile) { |
| 198 |
iccChannels = skcms_GetInputChannelCount(iccProfile); |
| 199 |
} |
| 200 |
return 0 < iccChannels && expectedChannels != iccChannels; |
| 201 |
} |
| 202 |
|
| 203 |
size_t do_deflated_image(const SkPixmap& pm, |
| 204 |
SkPDFDocument* doc, |
| 205 |
bool isOpaque, |
| 206 |
SkPDFIndirectReference ref) { |
| 207 |
SkPDF::Metadata::CompressionLevel compressionLevel = doc->metadata().fCompressionLevel; |
| 208 |
SkPDFStreamFormat format = compressionLevel == SkPDF::Metadata::CompressionLevel::None |
| 209 |
? SkPDFStreamFormat::Uncompressed |
| 210 |
: SkPDFStreamFormat::Flate; |
| 211 |
SkDynamicMemoryWStream dynamic; |
| 212 |
SkNullWStream writeOnly; |
| 213 |
SkWStream* buffer = ref ? static_cast<SkWStream*>(&dynamic) : &writeOnly; |
| 214 |
SkWStream* stream = buffer; |
| 215 |
std::optional<SkDeflateWStream> deflateWStream; |
| 216 |
if (format == SkPDFStreamFormat::Flate) { |
| 217 |
deflateWStream.emplace(buffer, SkToInt(compressionLevel)); |
| 218 |
stream = &*deflateWStream; |
| 219 |
} |
| 220 |
SkPDFUnion colorSpace = SkPDFUnion::Name("DeviceGray"); |
| 221 |
int channels; |
| 222 |
switch (pm.colorType()) { |
| 223 |
case kAlpha_8_SkColorType: |
| 224 |
channels = 1; |
| 225 |
fill_stream(stream, '\x00', pm.width() * pm.height()); |
| 226 |
break; |
| 227 |
case kGray_8_SkColorType: |
| 228 |
channels = 1; |
| 229 |
SkASSERT(isOpaque); |
| 230 |
SkASSERT(pm.rowBytes() == (size_t)pm.width()); |
| 231 |
stream->write(pm.addr8(), pm.width() * pm.height()); |
| 232 |
break; |
| 233 |
default: |
| 234 |
colorSpace = SkPDFUnion::Name("DeviceRGB"); |
| 235 |
channels = 3; |
| 236 |
SkASSERT(pm.alphaType() == kUnpremul_SkAlphaType); |
| 237 |
SkASSERT(pm.colorType() == kBGRA_8888_SkColorType); |
| 238 |
SkASSERT(pm.rowBytes() == (size_t)pm.width() * 4); |
| 239 |
uint8_t byteBuffer[3072]; |
| 240 |
static_assert(std::size(byteBuffer) % 3 == 0, ""); |
| 241 |
uint8_t* bufferStop = byteBuffer + std::size(byteBuffer); |
| 242 |
uint8_t* dst = byteBuffer; |
| 243 |
for (int y = 0; y < pm.height(); ++y) { |
| 244 |
const SkColor* src = pm.addr32(0, y); |
| 245 |
for (int x = 0; x < pm.width(); ++x) { |
| 246 |
SkColor color = *src++; |
| 247 |
if (SkColorGetA(color) == SK_AlphaTRANSPARENT) { |
| 248 |
color = get_neighbor_avg_color(pm, x, y); |
| 249 |
} |
| 250 |
*dst++ = SkColorGetR(color); |
| 251 |
*dst++ = SkColorGetG(color); |
| 252 |
*dst++ = SkColorGetB(color); |
| 253 |
if (dst == bufferStop) { |
| 254 |
stream->write(byteBuffer, sizeof(byteBuffer)); |
| 255 |
dst = byteBuffer; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
stream->write(byteBuffer, dst - byteBuffer); |
| 260 |
} |
| 261 |
if (deflateWStream) { |
| 262 |
deflateWStream->finalize(); |
| 263 |
} |
| 264 |
|
| 265 |
if (pm.colorSpace()) { |
| 266 |
skcms_ICCProfile iccProfile; |
| 267 |
pm.colorSpace()->toProfile(&iccProfile); |
| 268 |
if (!icc_channel_mismatch(&iccProfile, channels)) { |
| 269 |
sk_sp<SkData> iccData = SkWriteICCProfile(&iccProfile, ""); |
| 270 |
colorSpace = write_icc_profile(doc, std::move(iccData), channels); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
size_t length = buffer->bytesWritten(); |
| 275 |
SkPDFIndirectReference sMask; |
| 276 |
if (!isOpaque && ref) { |
| 277 |
sMask = doc->reserveRef(); |
| 278 |
} |
| 279 |
emit_image_stream(doc, ref, [&dynamic](SkWStream* stream) { dynamic.writeToAndReset(stream); }, |
| 280 |
pm.info().dimensions(), std::move(colorSpace), sMask, length, format); |
| 281 |
if (!isOpaque) { |
| 282 |
length += do_deflated_alpha(pm, doc, sMask); |
| 283 |
} |
| 284 |
return length; |
| 285 |
} |
| 286 |
|
| 287 |
size_t do_jpeg(sk_sp<const SkData> data, |
| 288 |
SkColorSpace* imageColorSpace, |
| 289 |
SkPDFDocument* doc, |
| 290 |
SkISize size, |
| 291 |
SkPDFIndirectReference ref) { |
| 292 |
if (!ref) { |
| 293 |
return data->size(); |
| 294 |
} |
| 295 |
|
| 296 |
SkPDF::DecodeJpegCallback decodeJPEG = doc->metadata().jpegDecoder; |
| 297 |
if (!decodeJPEG) { |
| 298 |
return 0; |
| 299 |
} |
| 300 |
std::unique_ptr<SkCodec> codec = decodeJPEG(data); |
| 301 |
if (!codec) { |
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
SkISize jpegSize = codec->dimensions(); |
| 306 |
const SkEncodedInfo& encodedInfo = SkCodecPriv::GetEncodedInfo(codec.get()); |
| 307 |
SkEncodedInfo::Color jpegColorType = encodedInfo.color(); |
| 308 |
SkEncodedOrigin exifOrientation = codec->getOrigin(); |
| 309 |
|
| 310 |
bool yuv = jpegColorType == SkEncodedInfo::kYUV_Color; |
| 311 |
bool goodColorType = yuv || jpegColorType == SkEncodedInfo::kGray_Color; |
| 312 |
if (jpegSize != size // Safety check. |
| 313 |
|| !goodColorType |
| 314 |
|| kTopLeft_SkEncodedOrigin != exifOrientation) { |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
int channels = yuv ? 3 : 1; |
| 319 |
SkPDFUnion colorSpace = yuv ? SkPDFUnion::Name("DeviceRGB") : SkPDFUnion::Name("DeviceGray"); |
| 320 |
|
| 321 |
if (sk_sp<const SkData> encodedIccProfileData = encodedInfo.profileData(); |
| 322 |
encodedIccProfileData && !icc_channel_mismatch(encodedInfo.profile(), channels)) |
| 323 |
{ |
| 324 |
colorSpace = write_icc_profile(doc, std::move(encodedIccProfileData), channels); |
| 325 |
} else if (const skcms_ICCProfile* codecIccProfile = codec->getICCProfile(); |
| 326 |
codecIccProfile && !icc_channel_mismatch(codecIccProfile, channels)) |
| 327 |
{ |
| 328 |
sk_sp<SkData> codecIccData = SkWriteICCProfile(codecIccProfile, ""); |
| 329 |
colorSpace = write_icc_profile(doc, std::move(codecIccData), channels); |
| 330 |
} else if (imageColorSpace) { |
| 331 |
skcms_ICCProfile imageIccProfile; |
| 332 |
imageColorSpace->toProfile(&imageIccProfile); |
| 333 |
if (!icc_channel_mismatch(&imageIccProfile, channels)) { |
| 334 |
sk_sp<SkData> imageIccData = SkWriteICCProfile(&imageIccProfile, ""); |
| 335 |
colorSpace = write_icc_profile(doc, std::move(imageIccData), channels); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
emit_image_stream(doc, ref, |
| 340 |
[&data](SkWStream* dst) { dst->write(data->data(), data->size()); }, |
| 341 |
jpegSize, std::move(colorSpace), |
| 342 |
SkPDFIndirectReference(), SkToInt(data->size()), SkPDFStreamFormat::DCT); |
| 343 |
return data->size(); |
| 344 |
} |
| 345 |
|
| 346 |
SkBitmap to_pixels(const SkImage* image) { |
| 347 |
SkBitmap bm; |
| 348 |
int w = image->width(), |
| 349 |
h = image->height(); |
| 350 |
switch (image->colorType()) { |
| 351 |
case kAlpha_8_SkColorType: |
| 352 |
bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 353 |
break; |
| 354 |
case kGray_8_SkColorType: |
| 355 |
bm.allocPixels(SkImageInfo::Make(w, h, kGray_8_SkColorType, kOpaque_SkAlphaType)); |
| 356 |
break; |
| 357 |
default: { |
| 358 |
// TODO: makeColorSpace(sRGB) or actually tag the images |
| 359 |
SkAlphaType at = bm.isOpaque() ? kOpaque_SkAlphaType : kUnpremul_SkAlphaType; |
| 360 |
bm.allocPixels( |
| 361 |
SkImageInfo::Make(w, h, kBGRA_8888_SkColorType, at, image->refColorSpace())); |
| 362 |
} |
| 363 |
} |
| 364 |
// TODO: support GPU images in PDFs |
| 365 |
if (!image->readPixels(nullptr, bm.pixmap(), 0, 0)) { |
| 366 |
bm.eraseColor(SkColorSetARGB(0xFF, 0, 0, 0)); |
| 367 |
} |
| 368 |
return bm; |
| 369 |
} |
| 370 |
|
| 371 |
size_t serialize_image(const SkImage* img, |
| 372 |
int encodingQuality, |
| 373 |
SkPDFDocument* doc, |
| 374 |
SkPDFIndirectReference ref) { |
| 375 |
SkASSERT(img); |
| 376 |
SkASSERT(doc); |
| 377 |
SkASSERT(encodingQuality >= 0); |
| 378 |
SkISize dimensions = img->dimensions(); |
| 379 |
|
| 380 |
if (sk_sp<const SkData> data = img->refEncodedData()) { |
| 381 |
if (size_t size = do_jpeg(std::move(data), img->colorSpace(), doc, dimensions, ref)) { |
| 382 |
return size; |
| 383 |
} |
| 384 |
} |
| 385 |
SkBitmap bm = to_pixels(img); |
| 386 |
const SkPixmap& pm = bm.pixmap(); |
| 387 |
SkPDF::EncodeJpegCallback encodeJPEG = doc->metadata().jpegEncoder; |
| 388 |
|
| 389 |
bool isOpaque = pm.isOpaque() || pm.computeIsOpaque(); |
| 390 |
if (encodeJPEG && encodingQuality <= 100 && isOpaque) { |
| 391 |
SkDynamicMemoryWStream stream; |
| 392 |
if (encodeJPEG(&stream, pm, encodingQuality)) { |
| 393 |
if (size_t size = do_jpeg(stream.detachAsData(), pm.colorSpace(), doc, dimensions, ref)) { |
| 394 |
return size; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
return do_deflated_image(pm, doc, isOpaque, ref); |
| 399 |
} |
| 400 |
|
| 401 |
} // namespace |
| 402 |
|
| 403 |
size_t SkPDFSerializeImageSize(const SkImage* img, SkPDFDocument* doc, int encodingQuality) { |
| 404 |
return serialize_image(img, encodingQuality, doc, SkPDFIndirectReference()); |
| 405 |
} |
| 406 |
|
| 407 |
SkPDFIndirectReference SkPDFSerializeImage(const SkImage* img, |
| 408 |
SkPDFDocument* doc, |
| 409 |
int encodingQuality) { |
| 410 |
SkASSERT(img); |
| 411 |
SkASSERT(doc); |
| 412 |
SkPDFIndirectReference ref = doc->reserveRef(); |
| 413 |
if (SkExecutor* executor = doc->executor()) { |
| 414 |
SkRef(img); |
| 415 |
doc->incrementJobCount(); |
| 416 |
executor->add([img, encodingQuality, doc, ref]() { |
| 417 |
serialize_image(img, encodingQuality, doc, ref); |
| 418 |
SkSafeUnref(img); |
| 419 |
doc->signalJobComplete(); |
| 420 |
}); |
| 421 |
return ref; |
| 422 |
} |
| 423 |
serialize_image(img, encodingQuality, doc, ref); |
| 424 |
return ref; |
| 425 |
} |