Path: blob/master/thirdparty/libwebp/src/enc/picture_rescale_enc.c
9917 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/enc/vp8i_enc.h"1920#if !defined(WEBP_REDUCE_SIZE)21#include "src/utils/rescaler_utils.h"22#include "src/utils/utils.h"23#endif // !defined(WEBP_REDUCE_SIZE)2425#define HALVE(x) (((x) + 1) >> 1)2627// Grab the 'specs' (writer, *opaque, width, height...) from 'src' and copy them28// into 'dst'. Mark 'dst' as not owning any memory.29static void PictureGrabSpecs(const WebPPicture* const src,30WebPPicture* const dst) {31assert(src != NULL && dst != NULL);32*dst = *src;33WebPPictureResetBuffers(dst);34}3536//------------------------------------------------------------------------------3738// Adjust top-left corner to chroma sample position.39static void SnapTopLeftPosition(const WebPPicture* const pic,40int* const left, int* const top) {41if (!pic->use_argb) {42*left &= ~1;43*top &= ~1;44}45}4647// Adjust top-left corner and verify that the sub-rectangle is valid.48static int AdjustAndCheckRectangle(const WebPPicture* const pic,49int* const left, int* const top,50int width, int height) {51SnapTopLeftPosition(pic, left, top);52if ((*left) < 0 || (*top) < 0) return 0;53if (width <= 0 || height <= 0) return 0;54if ((*left) + width > pic->width) return 0;55if ((*top) + height > pic->height) return 0;56return 1;57}5859#if !defined(WEBP_REDUCE_SIZE)60int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {61if (src == NULL || dst == NULL) return 0;62if (src == dst) return 1;6364PictureGrabSpecs(src, dst);65if (!WebPPictureAlloc(dst)) return 0;6667if (!src->use_argb) {68WebPCopyPlane(src->y, src->y_stride,69dst->y, dst->y_stride, dst->width, dst->height);70WebPCopyPlane(src->u, src->uv_stride, dst->u, dst->uv_stride,71HALVE(dst->width), HALVE(dst->height));72WebPCopyPlane(src->v, src->uv_stride, dst->v, dst->uv_stride,73HALVE(dst->width), HALVE(dst->height));74if (dst->a != NULL) {75WebPCopyPlane(src->a, src->a_stride,76dst->a, dst->a_stride, dst->width, dst->height);77}78} else {79WebPCopyPlane((const uint8_t*)src->argb, 4 * src->argb_stride,80(uint8_t*)dst->argb, 4 * dst->argb_stride,814 * dst->width, dst->height);82}83return 1;84}85#endif // !defined(WEBP_REDUCE_SIZE)8687int WebPPictureIsView(const WebPPicture* picture) {88if (picture == NULL) return 0;89if (picture->use_argb) {90return (picture->memory_argb_ == NULL);91}92return (picture->memory_ == NULL);93}9495int WebPPictureView(const WebPPicture* src,96int left, int top, int width, int height,97WebPPicture* dst) {98if (src == NULL || dst == NULL) return 0;99100// verify rectangle position.101if (!AdjustAndCheckRectangle(src, &left, &top, width, height)) return 0;102103if (src != dst) { // beware of aliasing! We don't want to leak 'memory_'.104PictureGrabSpecs(src, dst);105}106dst->width = width;107dst->height = height;108if (!src->use_argb) {109dst->y = src->y + top * src->y_stride + left;110dst->u = src->u + (top >> 1) * src->uv_stride + (left >> 1);111dst->v = src->v + (top >> 1) * src->uv_stride + (left >> 1);112dst->y_stride = src->y_stride;113dst->uv_stride = src->uv_stride;114if (src->a != NULL) {115dst->a = src->a + top * src->a_stride + left;116dst->a_stride = src->a_stride;117}118} else {119dst->argb = src->argb + top * src->argb_stride + left;120dst->argb_stride = src->argb_stride;121}122return 1;123}124125#if !defined(WEBP_REDUCE_SIZE)126//------------------------------------------------------------------------------127// Picture cropping128129int WebPPictureCrop(WebPPicture* pic,130int left, int top, int width, int height) {131WebPPicture tmp;132133if (pic == NULL) return 0;134if (!AdjustAndCheckRectangle(pic, &left, &top, width, height)) return 0;135136PictureGrabSpecs(pic, &tmp);137tmp.width = width;138tmp.height = height;139if (!WebPPictureAlloc(&tmp)) {140return WebPEncodingSetError(pic, tmp.error_code);141}142143if (!pic->use_argb) {144const int y_offset = top * pic->y_stride + left;145const int uv_offset = (top / 2) * pic->uv_stride + left / 2;146WebPCopyPlane(pic->y + y_offset, pic->y_stride,147tmp.y, tmp.y_stride, width, height);148WebPCopyPlane(pic->u + uv_offset, pic->uv_stride,149tmp.u, tmp.uv_stride, HALVE(width), HALVE(height));150WebPCopyPlane(pic->v + uv_offset, pic->uv_stride,151tmp.v, tmp.uv_stride, HALVE(width), HALVE(height));152153if (tmp.a != NULL) {154const int a_offset = top * pic->a_stride + left;155WebPCopyPlane(pic->a + a_offset, pic->a_stride,156tmp.a, tmp.a_stride, width, height);157}158} else {159const uint8_t* const src =160(const uint8_t*)(pic->argb + top * pic->argb_stride + left);161WebPCopyPlane(src, pic->argb_stride * 4, (uint8_t*)tmp.argb,162tmp.argb_stride * 4, width * 4, height);163}164WebPPictureFree(pic);165*pic = tmp;166return 1;167}168169//------------------------------------------------------------------------------170// Simple picture rescaler171172static int RescalePlane(const uint8_t* src,173int src_width, int src_height, int src_stride,174uint8_t* dst,175int dst_width, int dst_height, int dst_stride,176rescaler_t* const work,177int num_channels) {178WebPRescaler rescaler;179int y = 0;180if (!WebPRescalerInit(&rescaler, src_width, src_height,181dst, dst_width, dst_height, dst_stride,182num_channels, work)) {183return 0;184}185while (y < src_height) {186y += WebPRescalerImport(&rescaler, src_height - y,187src + y * src_stride, src_stride);188WebPRescalerExport(&rescaler);189}190return 1;191}192193static void AlphaMultiplyARGB(WebPPicture* const pic, int inverse) {194assert(pic->argb != NULL);195WebPMultARGBRows((uint8_t*)pic->argb, pic->argb_stride * sizeof(*pic->argb),196pic->width, pic->height, inverse);197}198199static void AlphaMultiplyY(WebPPicture* const pic, int inverse) {200if (pic->a != NULL) {201WebPMultRows(pic->y, pic->y_stride, pic->a, pic->a_stride,202pic->width, pic->height, inverse);203}204}205206int WebPPictureRescale(WebPPicture* picture, int width, int height) {207WebPPicture tmp;208int prev_width, prev_height;209rescaler_t* work;210211if (picture == NULL) return 0;212prev_width = picture->width;213prev_height = picture->height;214if (!WebPRescalerGetScaledDimensions(215prev_width, prev_height, &width, &height)) {216return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);217}218219PictureGrabSpecs(picture, &tmp);220tmp.width = width;221tmp.height = height;222if (!WebPPictureAlloc(&tmp)) {223return WebPEncodingSetError(picture, tmp.error_code);224}225226if (!picture->use_argb) {227work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work));228if (work == NULL) {229WebPPictureFree(&tmp);230return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);231}232// If present, we need to rescale alpha first (for AlphaMultiplyY).233if (picture->a != NULL) {234WebPInitAlphaProcessing();235if (!RescalePlane(picture->a, prev_width, prev_height, picture->a_stride,236tmp.a, width, height, tmp.a_stride, work, 1)) {237return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);238}239}240241// We take transparency into account on the luma plane only. That's not242// totally exact blending, but still is a good approximation.243AlphaMultiplyY(picture, 0);244if (!RescalePlane(picture->y, prev_width, prev_height, picture->y_stride,245tmp.y, width, height, tmp.y_stride, work, 1) ||246!RescalePlane(picture->u, HALVE(prev_width), HALVE(prev_height),247picture->uv_stride, tmp.u, HALVE(width), HALVE(height),248tmp.uv_stride, work, 1) ||249!RescalePlane(picture->v, HALVE(prev_width), HALVE(prev_height),250picture->uv_stride, tmp.v, HALVE(width), HALVE(height),251tmp.uv_stride, work, 1)) {252return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);253}254AlphaMultiplyY(&tmp, 1);255} else {256work = (rescaler_t*)WebPSafeMalloc(2ULL * width * 4, sizeof(*work));257if (work == NULL) {258WebPPictureFree(&tmp);259return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);260}261// In order to correctly interpolate colors, we need to apply the alpha262// weighting first (black-matting), scale the RGB values, and remove263// the premultiplication afterward (while preserving the alpha channel).264WebPInitAlphaProcessing();265AlphaMultiplyARGB(picture, 0);266if (!RescalePlane((const uint8_t*)picture->argb, prev_width, prev_height,267picture->argb_stride * 4, (uint8_t*)tmp.argb, width,268height, tmp.argb_stride * 4, work, 4)) {269return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);270}271AlphaMultiplyARGB(&tmp, 1);272}273WebPPictureFree(picture);274WebPSafeFree(work);275*picture = tmp;276return 1;277}278279#else // defined(WEBP_REDUCE_SIZE)280281int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {282(void)src;283(void)dst;284return 0;285}286287int WebPPictureCrop(WebPPicture* pic,288int left, int top, int width, int height) {289(void)pic;290(void)left;291(void)top;292(void)width;293(void)height;294return 0;295}296297int WebPPictureRescale(WebPPicture* pic, int width, int height) {298(void)pic;299(void)width;300(void)height;301return 0;302}303#endif // !defined(WEBP_REDUCE_SIZE)304305306