37void fill_stream(
SkWStream* out,
char value,
size_t n) {
40 for (
size_t i = 0; i < n /
sizeof(
buffer); ++i) {
55SkColor get_neighbor_avg_color(
const SkPixmap& bm,
int xOrig,
int yOrig) {
57 unsigned r = 0, g = 0,
b = 0, n = 0;
59 int ymin = std::max(0, yOrig - 1);
60 int ymax = std::min(yOrig + 1, bm.
height() - 1);
61 int xmin = std::max(0, xOrig - 1);
62 int xmax = std::min(xOrig + 1, bm.
width() - 1);
63 for (
int y = ymin;
y <= ymax; ++
y) {
65 for (
int x = xmin;
x <= xmax; ++
x) {
79enum class SkPDFStreamFormat { DCT, Flate, Uncompressed };
89 SkPDFStreamFormat
format) {
91 pdfDict.insertName(
"Subtype",
"Image");
92 pdfDict.insertInt(
"Width",
size.width());
93 pdfDict.insertInt(
"Height",
size.height());
94 pdfDict.insertUnion(
"ColorSpace", std::move(colorSpace));
96 pdfDict.insertRef(
"SMask", sMask);
98 pdfDict.insertInt(
"BitsPerComponent", 8);
99 #ifdef SK_PDF_BASE85_BINARY
101 filters->appendName(
"ASCII85Decode");
103 case SkPDFStreamFormat::DCT: filters->appendName(
"DCTDecode");
break;
104 case SkPDFStreamFormat::Flate: filters->appendName(
"FlateDecode");
break;
105 case SkPDFStreamFormat::Uncompressed:
break;
107 pdfDict.insertObject(
"Filter", std::move(filters));
110 case SkPDFStreamFormat::DCT: pdfDict.insertName(
"Filter",
"DCTDecode");
break;
111 case SkPDFStreamFormat::Flate: pdfDict.insertName(
"Filter",
"FlateDecode");
break;
112 case SkPDFStreamFormat::Uncompressed:
break;
115 if (
format == SkPDFStreamFormat::DCT) {
116 pdfDict.insertInt(
"ColorTransform", 0);
118 pdfDict.insertInt(
"Length",
length);
119 doc->
emitStream(pdfDict, std::move(writeStream), ref);
125 ? SkPDFStreamFormat::Uncompressed
126 : SkPDFStreamFormat::Flate;
129 std::optional<SkDeflateWStream> deflateWStream;
130 if (
format == SkPDFStreamFormat::Flate) {
132 stream = &*deflateWStream;
141 const uint32_t* ptr = pm.
addr32();
142 const uint32_t* stop = ptr + pm.
height() * pm.
width();
144 uint8_t byteBuffer[4092];
145 uint8_t* bufferStop = byteBuffer + std::size(byteBuffer);
146 uint8_t*
dst = byteBuffer;
147 while (ptr != stop) {
149 if (dst == bufferStop) {
150 stream->write(byteBuffer,
sizeof(byteBuffer));
154 stream->write(byteBuffer, dst - byteBuffer);
156 if (deflateWStream) {
157 deflateWStream->finalize();
160 #ifdef SK_PDF_BASE85_BINARY
161 SkPDFUtils::Base85Encode(
buffer.detachAsStream(), &
buffer);
172 static SkMutex iccProfileMapMutex;
180 iccStreamDict->insertInt(
"N", channels);
187 iccPDF->appendName(
"ICCBased");
188 iccPDF->appendRef(iccStreamRef);
192void do_deflated_image(
const SkPixmap& pm,
202 ? SkPDFStreamFormat::Uncompressed
203 : SkPDFStreamFormat::Flate;
206 std::optional<SkDeflateWStream> deflateWStream;
207 if (
format == SkPDFStreamFormat::Flate) {
209 stream = &*deflateWStream;
216 fill_stream(stream,
'\x00', pm.
width() * pm.
height());
230 uint8_t byteBuffer[3072];
231 static_assert(std::size(byteBuffer) % 3 == 0,
"");
232 uint8_t* bufferStop = byteBuffer + std::size(byteBuffer);
233 uint8_t*
dst = byteBuffer;
236 for (
int x = 0;
x < pm.
width(); ++
x) {
239 color = get_neighbor_avg_color(pm,
x,
y);
244 if (dst == bufferStop) {
245 stream->write(byteBuffer,
sizeof(byteBuffer));
250 stream->write(byteBuffer, dst - byteBuffer);
252 if (deflateWStream) {
253 deflateWStream->finalize();
260 colorSpace = write_icc_profile(doc, std::move(iccData), channels);
263 #ifdef SK_PDF_BASE85_BINARY
264 SkPDFUtils::Base85Encode(
buffer.detachAsStream(), &
buffer);
270 do_deflated_alpha(pm, doc, sMask);
276 static constexpr const SkCodecs::Decoder decoders[] = {
284 SkISize jpegSize = codec->dimensions();
296 #ifdef SK_PDF_BASE85_BINARY
302 int channels = yuv ? 3 : 1;
305 colorSpace = write_icc_profile(doc, std::move(encodedIccProfileData), channels);
306 }
else if (
const skcms_ICCProfile* codecIccProfile = codec->getICCProfile()) {
308 colorSpace = write_icc_profile(doc, std::move(codecIccData), channels);
309 }
else if (imageColorSpace && channels != 1) {
311 imageColorSpace->
toProfile(&imageIccProfile);
313 colorSpace = write_icc_profile(doc, std::move(imageIccData), channels);
316 emit_image_stream(doc, ref,
318 jpegSize, std::move(colorSpace),
358 if (do_jpeg(std::move(data), img->
colorSpace(), doc, dimensions, ref)) {
365 if (encodingQuality <= 100 && isOpaque) {
370 if (do_jpeg(
stream.detachAsData(), pm.
colorSpace(), doc, dimensions, ref)) {
375 do_deflated_image(pm, doc, isOpaque, ref);
382 int encodingQuality) {
389 executor->add([img, encodingQuality, doc, ref]() {
@ kOpaque_SkAlphaType
pixel is opaque
#define SK_BGRA_A32_SHIFT
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
#define SkColorGetR(color)
#define SkColorGetG(color)
#define SkColorSetRGB(r, g, b)
constexpr SkColor SK_ColorTRANSPARENT
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
#define SkColorGetA(color)
#define SkColorGetB(color)
constexpr SkAlpha SK_AlphaTRANSPARENT
@ kTopLeft_SkEncodedOrigin
SK_API sk_sp< SkData > SkWriteICCProfile(const skcms_TransferFunction &, const skcms_Matrix3x3 &toXYZD50)
SkPDFIndirectReference SkPDFSerializeImage(const SkImage *img, SkPDFDocument *doc, int encodingQuality)
SkPDFIndirectReference SkPDFStreamOut(std::unique_ptr< SkPDFDict > dict, std::unique_ptr< SkStreamAsset > content, SkPDFDocument *doc, SkPDFSteamCompressionEnabled compress)
static std::unique_ptr< SkPDFDict > SkPDFMakeDict(const char *type=nullptr)
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
static void SkSafeUnref(T *obj)
constexpr int SkToInt(S x)
constexpr uint8_t SkToU8(S x)
static sk_sp< SkData > serialize_image(const SkImage *image, SkSerialProcs procs)
void allocPixels(const SkImageInfo &info, size_t rowBytes)
const SkPixmap & pixmap() const
void eraseColor(SkColor4f) const
static std::unique_ptr< SkCodec > MakeFromData(sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
const SkEncodedInfo & getEncodedInfo() const
void toProfile(skcms_ICCProfile *) const
SkColorSpace * colorSpace() const
SkISize dimensions() const
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
SkColorType colorType() const
sk_sp< SkData > refEncodedData() const
sk_sp< SkColorSpace > refColorSpace() const
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
static std::unique_ptr< SkMemoryStream > MakeDirect(const void *data, size_t length)
static const SkEncodedInfo & GetEncodedInfo(SkCodec &)
SkExecutor * executor() const
const SkPDF::Metadata & metadata() const
skia_private::THashMap< SkPDFIccProfileKey, SkPDFIndirectReference, SkPDFIccProfileKey::Hash > fICCProfileMap
void emitStream(const SkPDFDict &dict, T writeStream, SkPDFIndirectReference ref)
SkPDFIndirectReference reserveRef()
static SkPDFUnion Object(std::unique_ptr< SkPDFObject >)
static SkPDFUnion Name(const char *)
const uint8_t * addr8() const
bool computeIsOpaque() const
const uint32_t * addr32() const
SkColorType colorType() const
SkColorSpace * colorSpace() const
const SkImageInfo & info() const
SkAlphaType alphaType() const
V * find(const K &key) const
static const uint8_t buffer[]
uint32_t uint32_t * format
constexpr SkCodecs::Decoder Decoder()
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
sk_sp< SkData > profileData() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkImageInfo MakeA8(int width, int height)