From 68ae5b671f947d170bd29e362ba2efda2ec99e37 Mon Sep 17 00:00:00 2001 From: hui su Date: Mon, 3 Oct 2016 14:40:51 -0700 Subject: [PATCH] Add libwebp/src/mux/animi.h Change-Id: I80ca2070d419acf6e8355a295ee965d2df5a4d8f --- makefile.unix | 1 + src/mux/Makefile.am | 1 + src/mux/anim_encode.c | 34 ++++++++++++++++++++++++++++++++++ src/mux/animi.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/mux/animi.h diff --git a/makefile.unix b/makefile.unix index 2a17f571..382ffb28 100644 --- a/makefile.unix +++ b/makefile.unix @@ -298,6 +298,7 @@ HDRS = \ src/enc/histogram.h \ src/enc/vp8enci.h \ src/enc/vp8li.h \ + src/mux/animi.h \ src/mux/muxi.h \ src/utils/bit_reader.h \ src/utils/bit_reader_inl.h \ diff --git a/src/mux/Makefile.am b/src/mux/Makefile.am index 85882c89..565565bb 100644 --- a/src/mux/Makefile.am +++ b/src/mux/Makefile.am @@ -2,6 +2,7 @@ lib_LTLIBRARIES = libwebpmux.la libwebpmux_la_SOURCES = libwebpmux_la_SOURCES += anim_encode.c +libwebpmux_la_SOURCES += animi.h libwebpmux_la_SOURCES += muxedit.c libwebpmux_la_SOURCES += muxi.h libwebpmux_la_SOURCES += muxinternal.c diff --git a/src/mux/anim_encode.c b/src/mux/anim_encode.c index 8b65a952..f2a6c05a 100644 --- a/src/mux/anim_encode.c +++ b/src/mux/anim_encode.c @@ -16,6 +16,7 @@ #include #include // for abs() +#include "../mux/animi.h" #include "../utils/utils.h" #include "../webp/decode.h" #include "../webp/encode.h" @@ -586,6 +587,39 @@ static int GetSubRects(const WebPPicture* const prev_canvas, ¶ms->rect_lossy_, ¶ms->sub_frame_lossy_); } +static WEBP_INLINE int clip(int v, int min_v, int max_v) { + return (v < min_v) ? min_v : (v > max_v) ? max_v : v; +} + +int WebPAnimEncoderRefineRect( + const WebPPicture* const prev_canvas, const WebPPicture* const curr_canvas, + int is_lossless, float quality, int* const x_offset, int* const y_offset, + int* const width, int* const height) { + FrameRect rect; + const int right = clip(*x_offset + *width, 0, curr_canvas->width); + const int left = clip(*x_offset, 0, curr_canvas->width - 1); + const int bottom = clip(*y_offset + *height, 0, curr_canvas->height); + const int top = clip(*y_offset, 0, curr_canvas->height - 1); + if (prev_canvas == NULL || curr_canvas == NULL || + prev_canvas->width != curr_canvas->width || + prev_canvas->height != curr_canvas->height || + !prev_canvas->use_argb || !curr_canvas->use_argb) { + return 0; + } + rect.x_offset_ = left; + rect.y_offset_ = top; + rect.width_ = clip(right - left, 0, curr_canvas->width - rect.x_offset_); + rect.height_ = clip(bottom - top, 0, curr_canvas->height - rect.y_offset_); + MinimizeChangeRectangle(prev_canvas, curr_canvas, &rect, is_lossless, + quality); + SnapToEvenOffsets(&rect); + *x_offset = rect.x_offset_; + *y_offset = rect.y_offset_; + *width = rect.width_; + *height = rect.height_; + return 1; +} + static void DisposeFrameRectangle(int dispose_method, const FrameRect* const rect, WebPPicture* const curr_canvas) { diff --git a/src/mux/animi.h b/src/mux/animi.h new file mode 100644 index 00000000..cecaf1fe --- /dev/null +++ b/src/mux/animi.h @@ -0,0 +1,43 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +// Internal header for animation related functions. +// +// Author: Hui Su (huisu@google.com) + +#ifndef WEBP_MUX_ANIMI_H_ +#define WEBP_MUX_ANIMI_H_ + +#include "../webp/mux.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Picks the optimal rectangle between two pictures, starting with initial +// values of offsets and dimensions that are passed in. The initial +// values will be clipped, if necessary, to make sure the rectangle is +// within the canvas. "use_argb" must be true for both pictures. +// Parameters: +// prev_canvas, curr_canvas - (in) two input pictures to compare. +// is_lossless, quality - (in) encoding settings. +// x_offset, y_offset, width, height - (in/out) rectangle between the two +// input pictures. +// Returns true on success. +int WebPAnimEncoderRefineRect( + const struct WebPPicture* const prev_canvas, + const struct WebPPicture* const curr_canvas, + int is_lossless, float quality, int* const x_offset, int* const y_offset, + int* const width, int* const height); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* WEBP_MUX_ANIMI_H_ */