mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-27 06:08:21 +01:00
Merge "ReadWebP: decode directly into a pre-allocated buffer"
This commit is contained in:
commit
f153603900
@ -138,46 +138,53 @@ int ReadWebP(const uint8_t* const data, size_t data_size,
|
|||||||
PrintWebPError("input data", status);
|
PrintWebPError("input data", status);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
{
|
|
||||||
const int has_alpha = keep_alpha && bitstream->has_alpha;
|
|
||||||
if (pic->use_argb) {
|
|
||||||
output_buffer->colorspace = has_alpha ? MODE_RGBA : MODE_RGB;
|
|
||||||
} else {
|
|
||||||
output_buffer->colorspace = has_alpha ? MODE_YUVA : MODE_YUV;
|
|
||||||
}
|
|
||||||
|
|
||||||
status = DecodeWebP(data, data_size, &config);
|
do {
|
||||||
if (status == VP8_STATUS_OK) {
|
const int has_alpha = keep_alpha && bitstream->has_alpha;
|
||||||
pic->width = output_buffer->width;
|
pic->width = bitstream->width;
|
||||||
pic->height = output_buffer->height;
|
pic->height = bitstream->height;
|
||||||
if (pic->use_argb) {
|
if (!pic->use_argb) pic->colorspace = has_alpha ? WEBP_YUV420A
|
||||||
const uint8_t* const rgba = output_buffer->u.RGBA.rgba;
|
: WEBP_YUV420;
|
||||||
const int stride = output_buffer->u.RGBA.stride;
|
|
||||||
ok = has_alpha ? WebPPictureImportRGBA(pic, rgba, stride)
|
|
||||||
: WebPPictureImportRGB(pic, rgba, stride);
|
|
||||||
} else {
|
|
||||||
pic->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;
|
|
||||||
ok = WebPPictureAlloc(pic);
|
ok = WebPPictureAlloc(pic);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
status = VP8_STATUS_OUT_OF_MEMORY;
|
status = VP8_STATUS_OUT_OF_MEMORY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (pic->use_argb) {
|
||||||
|
output_buffer->colorspace = MODE_BGRA;
|
||||||
|
output_buffer->u.RGBA.rgba = (uint8_t*)pic->argb;
|
||||||
|
output_buffer->u.RGBA.stride = pic->argb_stride * sizeof(uint32_t);
|
||||||
|
output_buffer->u.RGBA.size = output_buffer->u.RGBA.stride * pic->height;
|
||||||
} else {
|
} else {
|
||||||
const WebPYUVABuffer* const yuva = &output_buffer->u.YUVA;
|
output_buffer->colorspace = has_alpha ? MODE_YUVA : MODE_YUV;
|
||||||
const int uv_width = (pic->width + 1) >> 1;
|
output_buffer->u.YUVA.y = pic->y;
|
||||||
const int uv_height = (pic->height + 1) >> 1;
|
output_buffer->u.YUVA.u = pic->u;
|
||||||
ImgIoUtilCopyPlane(yuva->y, yuva->y_stride,
|
output_buffer->u.YUVA.v = pic->v;
|
||||||
pic->y, pic->y_stride, pic->width, pic->height);
|
output_buffer->u.YUVA.a = has_alpha ? pic->a : NULL;
|
||||||
ImgIoUtilCopyPlane(yuva->u, yuva->u_stride,
|
output_buffer->u.YUVA.y_stride = pic->y_stride;
|
||||||
pic->u, pic->uv_stride, uv_width, uv_height);
|
output_buffer->u.YUVA.u_stride = pic->uv_stride;
|
||||||
ImgIoUtilCopyPlane(yuva->v, yuva->v_stride,
|
output_buffer->u.YUVA.v_stride = pic->uv_stride;
|
||||||
pic->v, pic->uv_stride, uv_width, uv_height);
|
output_buffer->u.YUVA.a_stride = has_alpha ? pic->a_stride : 0;
|
||||||
if (has_alpha) {
|
output_buffer->u.YUVA.y_size = pic->height * pic->y_stride;
|
||||||
ImgIoUtilCopyPlane(yuva->a, yuva->a_stride,
|
output_buffer->u.YUVA.u_size = (pic->height + 1) / 2 * pic->uv_stride;
|
||||||
pic->a, pic->a_stride, pic->width, pic->height);
|
output_buffer->u.YUVA.v_size = (pic->height + 1) / 2 * pic->uv_stride;
|
||||||
}
|
output_buffer->u.YUVA.a_size = pic->height * pic->a_stride;
|
||||||
}
|
}
|
||||||
}
|
output_buffer->is_external_memory = 1;
|
||||||
|
|
||||||
|
status = DecodeWebP(data, data_size, &config);
|
||||||
|
ok = (status == VP8_STATUS_OK);
|
||||||
|
if (!ok) WebPPictureFree(pic);
|
||||||
|
if (ok && !keep_alpha && pic->use_argb) {
|
||||||
|
// Need to wipe out the alpha value, as requested.
|
||||||
|
int x, y;
|
||||||
|
uint32_t* argb = pic->argb;
|
||||||
|
for (y = 0; y < pic->height; ++y) {
|
||||||
|
for (x = 0; x < pic->width; ++x) argb[x] |= 0xff000000u;
|
||||||
|
argb += pic->argb_stride;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} while (0); // <- so we can 'break' out of the loop
|
||||||
|
|
||||||
if (status != VP8_STATUS_OK) {
|
if (status != VP8_STATUS_OK) {
|
||||||
PrintWebPError("input data", status);
|
PrintWebPError("input data", status);
|
||||||
|
Loading…
Reference in New Issue
Block a user