MCPcopy Create free account
hub / github.com/Snapchat/Valdi / encodeImage

Function encodeImage

snap_drawing/src/snap_drawing/cpp/Utils/Image.cpp:75–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73Valdi::Result<Valdi::BytesView> Image::toPNG() const {
74 return encode(EncodedImageFormatPNG, 1.0);
75}
76
77static bool encodeImage(SkWStream* dst, const SkPixmap& src, EncodedImageFormat format, int quality) {
78 switch (format) {
79 case EncodedImageFormatJPG: {
80 SkJpegEncoder::Options opts;
81 opts.fQuality = quality;
82 return SkJpegEncoder::Encode(dst, src, opts);
83 }
84 case EncodedImageFormatPNG: {
85 SkPngEncoder::Options opts;
86 return SkPngEncoder::Encode(dst, src, opts);
87 }
88 case EncodedImageFormatWebP: {
89 SkWebpEncoder::Options opts;
90 if (quality == 100) {
91 opts.fCompression = SkWebpEncoder::Compression::kLossless;
92 // Note: SkEncodeImage treats 0 quality as the lowest quality
93 // (greatest compression) and 100 as the highest quality (least
94 // compression). For kLossy, this matches libwebp's
95 // interpretation, so it is passed directly to libwebp. But
96 // with kLossless, libwebp always creates the highest quality
97 // image. In this case, fQuality is reinterpreted as how much
98 // effort (time) to put into making a smaller file. This API
99 // does not provide a way to specify this value (though it can
100 // be specified by using SkWebpEncoder::Encode) so we have to
101 // pick one arbitrarily. This value matches that chosen by
102 // blink::ImageEncoder::ComputeWebpOptions as well
103 // WebPConfigInit.
104 opts.fQuality = 75;
105 } else {
106 opts.fCompression = SkWebpEncoder::Compression::kLossy;
107 opts.fQuality = quality;
108 }
109 return SkWebpEncoder::Encode(dst, src, opts);
110 }
111 }
112}
113

Callers 1

encodeSKImageToSKDataMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected