Path: blob/master/thirdparty/libwebp/src/enc/picture_rescale_enc.c
21694 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// WebPPicture tools: copy, crop, rescaling and view.10//11// Author: Skal ([email protected])1213#include "src/webp/encode.h"1415#include <assert.h>16#include <stdlib.h>1718#include "src/webp/types.h"19#include "src/dsp/dsp.h"20#include "src/enc/vp8i_enc.h"2122#if !defined(WEBP_REDUCE_SIZE)23#include "src/utils/rescaler_utils.h"24#include "src/utils/utils.h"25#endif // !defined(WEBP_REDUCE_SIZE)2627#define HALVE(x) (((x) + 1) >> 1)2829// Grab the 'specs' (writer, *opaque, width, height...) from 'src' and copy them30// into 'dst'. Mark 'dst' as not owning any memory.31static void PictureGrabSpecs(const WebPPicture* const src,32WebPPicture* const dst) {33assert(src != NULL && dst != NULL);34*dst = *src;35WebPPictureResetBuffers(dst);36}3738//------------------------------------------------------------------------------3940// Adjust top-left corner to chroma sample position.41static void SnapTopLeftPosition(const WebPPicture* const pic,42int* const left, int* const top) {43if (!pic->use_argb) {44*left &= ~1;45*top &= ~1;46}47}4849// Adjust top-left corner and verify that the sub-rectangle is valid.50static int AdjustAndCheckRectangle(const WebPPicture* const pic,51int* const left, int* const top,52int width, int height) {53SnapTopLeftPosition(pic, left, top);54if ((*left) < 0 || (*top) < 0) return 0;55if (width <= 0 || height <= 0) return 0;56if ((*left) + width > pic->width) return 0;57if ((*top) + height > pic->height) return 0;58return 1;59}6061#if !defined(WEBP_REDUCE_SIZE)62int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {63if (src == NULL || dst == NULL) return 0;64if (src == dst) return 1;6566PictureGrabSpecs(src, dst);67if (!WebPPictureAlloc(dst)) return 0;6869if (!src->use_argb) {70WebPCopyPlane(src->y, src->y_stride,71dst->y, dst->y_stride, dst->width, dst->height);72WebPCopyPlane(src->u, src->uv_stride, dst->u, dst->uv_stride,73HALVE(dst->width), HALVE(dst->height));74WebPCopyPlane(src->v, src->uv_stride, dst->v, dst->uv_stride,75HALVE(dst->width), HALVE(dst->height));76if (dst->a != NULL) {77WebPCopyPlane(src->a, src->a_stride,78dst->a, dst->a_stride, dst->width, dst->height);79}80} else {81WebPCopyPlane((const uint8_t*)src->argb, 4 * src->argb_stride,82(uint8_t*)dst->argb, 4 * dst->argb_stride,834 * dst->width, dst->height);84}85return 1;86}87#endif // !defined(WEBP_REDUCE_SIZE)8889int WebPPictureIsView(const WebPPicture* picture) {90if (picture == NULL) return 0;91if (picture->use_argb) {92return (picture->memory_argb_ == NULL);93}94return (picture->memory_ == NULL);95}9697int WebPPictureView(const WebPPicture* src,98int left, int top, int width, int height,99WebPPicture* dst) {100if (src == NULL || dst == NULL) return 0;101102// verify rectangle position.103if (!AdjustAndCheckRectangle(src, &left, &top, width, height)) return 0;104105if (src != dst) { // beware of aliasing! We don't want to leak 'memory_'.106PictureGrabSpecs(src, dst);107}108dst->width = width;109dst->height = height;110if (!src->use_argb) {111dst->y = src->y + top * src->y_stride + left;112dst->u = src->u + (top >> 1) * src->uv_stride + (left >> 1);113dst->v = src->v + (top >> 1) * src->uv_stride + (left >> 1);114dst->y_stride = src->y_stride;115dst->uv_stride = src->uv_stride;116if (src->a != NULL) {117dst->a = src->a + top * src->a_stride + left;118dst->a_stride = src->a_stride;119}120} else {121dst->argb = src->argb + top * src->argb_stride + left;122dst->argb_stride = src->argb_stride;123}124return 1;125}126127#if !defined(WEBP_REDUCE_SIZE)128//------------------------------------------------------------------------------129// Picture cropping130131int WebPPictureCrop(WebPPicture* pic,132int left, int top, int width, int height) {133WebPPicture tmp;134135if (pic == NULL) return 0;136if (!AdjustAndCheckRectangle(pic, &left, &top, width, height)) return 0;137138PictureGrabSpecs(pic, &tmp);139tmp.width = width;140tmp.height = height;141if (!WebPPictureAlloc(&tmp)) {142return WebPEncodingSetError(pic, tmp.error_code);143}144145if (!pic->use_argb) {146const int y_offset = top * pic->y_stride + left;147const int uv_offset = (top / 2) * pic->uv_stride + left / 2;148WebPCopyPlane(pic->y + y_offset, pic->y_stride,149tmp.y, tmp.y_stride, width, height);150WebPCopyPlane(pic->u + uv_offset, pic->uv_stride,151tmp.u, tmp.uv_stride, HALVE(width), HALVE(height));152WebPCopyPlane(pic->v + uv_offset, pic->uv_stride,153tmp.v, tmp.uv_stride, HALVE(width), HALVE(height));154155if (tmp.a != NULL) {156const int a_offset = top * pic->a_stride + left;157WebPCopyPlane(pic->a + a_offset, pic->a_stride,158tmp.a, tmp.a_stride, width, height);159}160} else {161const uint8_t* const src =162(const uint8_t*)(pic->argb + top * pic->argb_stride + left);163WebPCopyPlane(src, pic->argb_stride * 4, (uint8_t*)tmp.argb,164tmp.argb_stride * 4, width * 4, height);165}166WebPPictureFree(pic);167*pic = tmp;168return 1;169}170171//------------------------------------------------------------------------------172// Simple picture rescaler173174static int RescalePlane(const uint8_t* src,175int src_width, int src_height, int src_stride,176uint8_t* dst,177int dst_width, int dst_height, int dst_stride,178rescaler_t* const work,179int num_channels) {180WebPRescaler rescaler;181int y = 0;182if (!WebPRescalerInit(&rescaler, src_width, src_height,183dst, dst_width, dst_height, dst_stride,184num_channels, work)) {185return 0;186}187while (y < src_height) {188y += WebPRescalerImport(&rescaler, src_height - y,189src + y * src_stride, src_stride);190WebPRescalerExport(&rescaler);191}192return 1;193}194195static void AlphaMultiplyARGB(WebPPicture* const pic, int inverse) {196assert(pic->argb != NULL);197WebPMultARGBRows((uint8_t*)pic->argb, pic->argb_stride * sizeof(*pic->argb),198pic->width, pic->height, inverse);199}200201static void AlphaMultiplyY(WebPPicture* const pic, int inverse) {202if (pic->a != NULL) {203WebPMultRows(pic->y, pic->y_stride, pic->a, pic->a_stride,204pic->width, pic->height, inverse);205}206}207208int WebPPictureRescale(WebPPicture* picture, int width, int height) {209WebPPicture tmp;210int prev_width, prev_height;211rescaler_t* work;212213if (picture == NULL) return 0;214prev_width = picture->width;215prev_height = picture->height;216if (!WebPRescalerGetScaledDimensions(217prev_width, prev_height, &width, &height)) {218return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);219}220221PictureGrabSpecs(picture, &tmp);222tmp.width = width;223tmp.height = height;224if (!WebPPictureAlloc(&tmp)) {225return WebPEncodingSetError(picture, tmp.error_code);226}227228if (!picture->use_argb) {229work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work));230if (work == NULL) {231WebPPictureFree(&tmp);232return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);233}234// If present, we need to rescale alpha first (for AlphaMultiplyY).235if (picture->a != NULL) {236WebPInitAlphaProcessing();237if (!RescalePlane(picture->a, prev_width, prev_height, picture->a_stride,238tmp.a, width, height, tmp.a_stride, work, 1)) {239return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);240}241}242243// We take transparency into account on the luma plane only. That's not244// totally exact blending, but still is a good approximation.245AlphaMultiplyY(picture, 0);246if (!RescalePlane(picture->y, prev_width, prev_height, picture->y_stride,247tmp.y, width, height, tmp.y_stride, work, 1) ||248!RescalePlane(picture->u, HALVE(prev_width), HALVE(prev_height),249picture->uv_stride, tmp.u, HALVE(width), HALVE(height),250tmp.uv_stride, work, 1) ||251!RescalePlane(picture->v, HALVE(prev_width), HALVE(prev_height),252picture->uv_stride, tmp.v, HALVE(width), HALVE(height),253tmp.uv_stride, work, 1)) {254return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);255}256AlphaMultiplyY(&tmp, 1);257} else {258work = (rescaler_t*)WebPSafeMalloc(2ULL * width * 4, sizeof(*work));259if (work == NULL) {260WebPPictureFree(&tmp);261return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);262}263// In order to correctly interpolate colors, we need to apply the alpha264// weighting first (black-matting), scale the RGB values, and remove265// the premultiplication afterward (while preserving the alpha channel).266WebPInitAlphaProcessing();267AlphaMultiplyARGB(picture, 0);268if (!RescalePlane((const uint8_t*)picture->argb, prev_width, prev_height,269picture->argb_stride * 4, (uint8_t*)tmp.argb, width,270height, tmp.argb_stride * 4, work, 4)) {271return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);272}273AlphaMultiplyARGB(&tmp, 1);274}275WebPPictureFree(picture);276WebPSafeFree(work);277*picture = tmp;278return 1;279}280281#else // defined(WEBP_REDUCE_SIZE)282283int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {284(void)src;285(void)dst;286return 0;287}288289int WebPPictureCrop(WebPPicture* pic,290int left, int top, int width, int height) {291(void)pic;292(void)left;293(void)top;294(void)width;295(void)height;296return 0;297}298299int WebPPictureRescale(WebPPicture* pic, int width, int height) {300(void)pic;301(void)width;302(void)height;303return 0;304}305#endif // !defined(WEBP_REDUCE_SIZE)306307308