MCPcopy Create free account
hub / github.com/Tencent/tgfx / Encode

Method Encode

src/core/codecs/webp/WebpCodec.cpp:148–213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

146}
147
148std::shared_ptr<Data> WebpCodec::Encode(const Pixmap& pixmap, int quality) {
149 const uint8_t* srcPixels = static_cast<uint8_t*>(const_cast<void*>(pixmap.pixels()));
150 auto srcInfo = pixmap.info();
151 Buffer tempBuffer = {};
152 if (pixmap.alphaType() == AlphaType::Premultiplied ||
153 (pixmap.colorType() != ColorType::RGBA_8888 && pixmap.colorType() != ColorType::BGRA_8888)) {
154 auto alphaType =
155 pixmap.alphaType() == AlphaType::Opaque ? AlphaType::Opaque : AlphaType::Unpremultiplied;
156 srcInfo = ImageInfo::Make(pixmap.width(), pixmap.height(), ColorType::RGBA_8888, alphaType);
157 tempBuffer.alloc(srcInfo.byteSize());
158 srcPixels = tempBuffer.bytes();
159 if (!pixmap.readPixels(srcInfo, tempBuffer.data())) {
160 return nullptr;
161 }
162 }
163 WebPConfig webp_config;
164 bool isLossless = false;
165 if (quality == 100) {
166 quality = 75;
167 isLossless = true;
168 }
169 if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, static_cast<float>(quality))) {
170 return nullptr;
171 }
172 WebPPicture pic;
173 WebPPictureInit(&pic);
174 pic.width = srcInfo.width();
175 pic.height = srcInfo.height();
176 pic.writer = webp_reader_write_data;
177 if (isLossless) {
178 webp_config.lossless = 1;
179 webp_config.method = 1;
180 pic.use_argb = 1;
181 } else {
182 webp_config.lossless = 0;
183 webp_config.method = 3;
184 pic.use_argb = 0;
185 }
186
187 WebpWriter webpWriter;
188 pic.custom_ptr = &webpWriter;
189 auto importProc = WebPPictureImportRGBX;
190 if (ColorType::RGBA_8888 == srcInfo.colorType()) {
191 if (AlphaType::Opaque == srcInfo.alphaType()) {
192 importProc = WebPPictureImportRGBX;
193 } else {
194 importProc = WebPPictureImportRGBA;
195 }
196 } else if (ColorType::BGRA_8888 == srcInfo.colorType()) {
197 if (AlphaType::Opaque == srcInfo.alphaType()) {
198 importProc = WebPPictureImportBGRX;
199 } else {
200 importProc = WebPPictureImportBGRA;
201 }
202 }
203 auto rowBytes = static_cast<int>(srcInfo.rowBytes());
204 if (!importProc(&pic, srcPixels, rowBytes) || !WebPEncode(&webp_config, &pic)) {
205 WebPPictureFree(&pic);

Callers

nothing calls this directly

Calls 12

MakeFunction · 0.85
pixelsMethod · 0.80
allocMethod · 0.80
alphaTypeMethod · 0.45
colorTypeMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45
byteSizeMethod · 0.45
bytesMethod · 0.45
readPixelsMethod · 0.45
dataMethod · 0.45
rowBytesMethod · 0.45

Tested by

no test coverage detected