Path: blob/master/3rdparty/libwebp/src/enc/picture_rescale_enc.c
16344 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#if !defined(WEBP_REDUCE_SIZE)1617#include <assert.h>18#include <stdlib.h>1920#include "src/enc/vp8i_enc.h"21#include "src/utils/rescaler_utils.h"22#include "src/utils/utils.h"2324#define HALVE(x) (((x) + 1) >> 1)2526// Grab the 'specs' (writer, *opaque, width, height...) from 'src' and copy them27// into 'dst'. Mark 'dst' as not owning any memory.28static void PictureGrabSpecs(const WebPPicture* const src,29WebPPicture* const dst) {30assert(src != NULL && dst != NULL);31*dst = *src;32WebPPictureResetBuffers(dst);33}3435//------------------------------------------------------------------------------3637// Adjust top-left corner to chroma sample position.38static void SnapTopLeftPosition(const WebPPicture* const pic,39int* const left, int* const top) {40if (!pic->use_argb) {41*left &= ~1;42*top &= ~1;43}44}4546// Adjust top-left corner and verify that the sub-rectangle is valid.47static int AdjustAndCheckRectangle(const WebPPicture* const pic,48int* const left, int* const top,49int width, int height) {50SnapTopLeftPosition(pic, left, top);51if ((*left) < 0 || (*top) < 0) return 0;52if (width <= 0 || height <= 0) return 0;53if ((*left) + width > pic->width) return 0;54if ((*top) + height > pic->height) return 0;55return 1;56}5758int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {59if (src == NULL || dst == NULL) return 0;60if (src == dst) return 1;6162PictureGrabSpecs(src, dst);63if (!WebPPictureAlloc(dst)) return 0;6465if (!src->use_argb) {66WebPCopyPlane(src->y, src->y_stride,67dst->y, dst->y_stride, dst->width, dst->height);68WebPCopyPlane(src->u, src->uv_stride, dst->u, dst->uv_stride,69HALVE(dst->width), HALVE(dst->height));70WebPCopyPlane(src->v, src->uv_stride, dst->v, dst->uv_stride,71HALVE(dst->width), HALVE(dst->height));72if (dst->a != NULL) {73WebPCopyPlane(src->a, src->a_stride,74dst->a, dst->a_stride, dst->width, dst->height);75}76} else {77WebPCopyPlane((const uint8_t*)src->argb, 4 * src->argb_stride,78(uint8_t*)dst->argb, 4 * dst->argb_stride,794 * dst->width, dst->height);80}81return 1;82}8384int WebPPictureIsView(const WebPPicture* picture) {85if (picture == NULL) return 0;86if (picture->use_argb) {87return (picture->memory_argb_ == NULL);88}89return (picture->memory_ == NULL);90}9192int WebPPictureView(const WebPPicture* src,93int left, int top, int width, int height,94WebPPicture* dst) {95if (src == NULL || dst == NULL) return 0;9697// verify rectangle position.98if (!AdjustAndCheckRectangle(src, &left, &top, width, height)) return 0;99100if (src != dst) { // beware of aliasing! We don't want to leak 'memory_'.101PictureGrabSpecs(src, dst);102}103dst->width = width;104dst->height = height;105if (!src->use_argb) {106dst->y = src->y + top * src->y_stride + left;107dst->u = src->u + (top >> 1) * src->uv_stride + (left >> 1);108dst->v = src->v + (top >> 1) * src->uv_stride + (left >> 1);109dst->y_stride = src->y_stride;110dst->uv_stride = src->uv_stride;111if (src->a != NULL) {112dst->a = src->a + top * src->a_stride + left;113dst->a_stride = src->a_stride;114}115} else {116dst->argb = src->argb + top * src->argb_stride + left;117dst->argb_stride = src->argb_stride;118}119return 1;120}121122//------------------------------------------------------------------------------123// Picture cropping124125int WebPPictureCrop(WebPPicture* pic,126int left, int top, int width, int height) {127WebPPicture tmp;128129if (pic == NULL) return 0;130if (!AdjustAndCheckRectangle(pic, &left, &top, width, height)) return 0;131132PictureGrabSpecs(pic, &tmp);133tmp.width = width;134tmp.height = height;135if (!WebPPictureAlloc(&tmp)) return 0;136137if (!pic->use_argb) {138const int y_offset = top * pic->y_stride + left;139const int uv_offset = (top / 2) * pic->uv_stride + left / 2;140WebPCopyPlane(pic->y + y_offset, pic->y_stride,141tmp.y, tmp.y_stride, width, height);142WebPCopyPlane(pic->u + uv_offset, pic->uv_stride,143tmp.u, tmp.uv_stride, HALVE(width), HALVE(height));144WebPCopyPlane(pic->v + uv_offset, pic->uv_stride,145tmp.v, tmp.uv_stride, HALVE(width), HALVE(height));146147if (tmp.a != NULL) {148const int a_offset = top * pic->a_stride + left;149WebPCopyPlane(pic->a + a_offset, pic->a_stride,150tmp.a, tmp.a_stride, width, height);151}152} else {153const uint8_t* const src =154(const uint8_t*)(pic->argb + top * pic->argb_stride + left);155WebPCopyPlane(src, pic->argb_stride * 4, (uint8_t*)tmp.argb,156tmp.argb_stride * 4, width * 4, height);157}158WebPPictureFree(pic);159*pic = tmp;160return 1;161}162163//------------------------------------------------------------------------------164// Simple picture rescaler165166static void RescalePlane(const uint8_t* src,167int src_width, int src_height, int src_stride,168uint8_t* dst,169int dst_width, int dst_height, int dst_stride,170rescaler_t* const work,171int num_channels) {172WebPRescaler rescaler;173int y = 0;174WebPRescalerInit(&rescaler, src_width, src_height,175dst, dst_width, dst_height, dst_stride,176num_channels, work);177while (y < src_height) {178y += WebPRescalerImport(&rescaler, src_height - y,179src + y * src_stride, src_stride);180WebPRescalerExport(&rescaler);181}182}183184static void AlphaMultiplyARGB(WebPPicture* const pic, int inverse) {185assert(pic->argb != NULL);186WebPMultARGBRows((uint8_t*)pic->argb, pic->argb_stride * sizeof(*pic->argb),187pic->width, pic->height, inverse);188}189190static void AlphaMultiplyY(WebPPicture* const pic, int inverse) {191if (pic->a != NULL) {192WebPMultRows(pic->y, pic->y_stride, pic->a, pic->a_stride,193pic->width, pic->height, inverse);194}195}196197int WebPPictureRescale(WebPPicture* pic, int width, int height) {198WebPPicture tmp;199int prev_width, prev_height;200rescaler_t* work;201202if (pic == NULL) return 0;203prev_width = pic->width;204prev_height = pic->height;205if (!WebPRescalerGetScaledDimensions(206prev_width, prev_height, &width, &height)) {207return 0;208}209210PictureGrabSpecs(pic, &tmp);211tmp.width = width;212tmp.height = height;213if (!WebPPictureAlloc(&tmp)) return 0;214215if (!pic->use_argb) {216work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work));217if (work == NULL) {218WebPPictureFree(&tmp);219return 0;220}221// If present, we need to rescale alpha first (for AlphaMultiplyY).222if (pic->a != NULL) {223WebPInitAlphaProcessing();224RescalePlane(pic->a, prev_width, prev_height, pic->a_stride,225tmp.a, width, height, tmp.a_stride, work, 1);226}227228// We take transparency into account on the luma plane only. That's not229// totally exact blending, but still is a good approximation.230AlphaMultiplyY(pic, 0);231RescalePlane(pic->y, prev_width, prev_height, pic->y_stride,232tmp.y, width, height, tmp.y_stride, work, 1);233AlphaMultiplyY(&tmp, 1);234235RescalePlane(pic->u,236HALVE(prev_width), HALVE(prev_height), pic->uv_stride,237tmp.u,238HALVE(width), HALVE(height), tmp.uv_stride, work, 1);239RescalePlane(pic->v,240HALVE(prev_width), HALVE(prev_height), pic->uv_stride,241tmp.v,242HALVE(width), HALVE(height), tmp.uv_stride, work, 1);243} else {244work = (rescaler_t*)WebPSafeMalloc(2ULL * width * 4, sizeof(*work));245if (work == NULL) {246WebPPictureFree(&tmp);247return 0;248}249// In order to correctly interpolate colors, we need to apply the alpha250// weighting first (black-matting), scale the RGB values, and remove251// the premultiplication afterward (while preserving the alpha channel).252WebPInitAlphaProcessing();253AlphaMultiplyARGB(pic, 0);254RescalePlane((const uint8_t*)pic->argb, prev_width, prev_height,255pic->argb_stride * 4,256(uint8_t*)tmp.argb, width, height,257tmp.argb_stride * 4,258work, 4);259AlphaMultiplyARGB(&tmp, 1);260}261WebPPictureFree(pic);262WebPSafeFree(work);263*pic = tmp;264return 1;265}266267#else // defined(WEBP_REDUCE_SIZE)268269int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {270(void)src;271(void)dst;272return 0;273}274275int WebPPictureIsView(const WebPPicture* picture) {276(void)picture;277return 0;278}279280int WebPPictureView(const WebPPicture* src,281int left, int top, int width, int height,282WebPPicture* dst) {283(void)src;284(void)left;285(void)top;286(void)width;287(void)height;288(void)dst;289return 0;290}291292int WebPPictureCrop(WebPPicture* pic,293int left, int top, int width, int height) {294(void)pic;295(void)left;296(void)top;297(void)width;298(void)height;299return 0;300}301302int WebPPictureRescale(WebPPicture* pic, int width, int height) {303(void)pic;304(void)width;305(void)height;306return 0;307}308#endif // !defined(WEBP_REDUCE_SIZE)309310311