1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // Rescaling functions
12 // Author: Skal (pascal.massimino@gmail.com)
16 #include "./rescaler.h"
18 //------------------------------------------------------------------------------
20 #if defined(__cplusplus) || defined(c_plusplus)
25 #define MULT_FIX(x, y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX)
27 void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
28 uint8_t* const dst, int dst_width, int dst_height,
29 int dst_stride, int num_channels, int x_add, int x_sub,
30 int y_add, int y_sub, int32_t* const work) {
31 wrk->x_expand = (src_width < dst_width);
32 wrk->src_width = src_width;
33 wrk->src_height = src_height;
34 wrk->dst_width = dst_width;
35 wrk->dst_height = dst_height;
37 wrk->dst_stride = dst_stride;
38 wrk->num_channels = num_channels;
39 // for 'x_expand', we use bilinear interpolation
40 wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add - x_sub;
41 wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;
45 wrk->fx_scale = (1 << RFIX) / x_sub;
46 wrk->fy_scale = (1 << RFIX) / y_sub;
47 wrk->fxy_scale = wrk->x_expand ?
48 ((int64_t)dst_height << RFIX) / (x_sub * src_height) :
49 ((int64_t)dst_height << RFIX) / (x_add * src_height);
51 wrk->frow = work + num_channels * dst_width;
54 void WebPRescalerImportRow(WebPRescaler* const wrk,
55 const uint8_t* const src, int channel) {
56 const int x_stride = wrk->num_channels;
57 const int x_out_max = wrk->dst_width * wrk->num_channels;
63 for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
65 for (; accum > 0; accum -= wrk->x_sub) {
69 { // Emit next horizontal pixel.
70 const int32_t base = src[x_in];
71 const int32_t frac = base * (-accum);
73 wrk->frow[x_out] = (sum + base) * wrk->x_sub - frac;
74 // fresh fractional start for next pixel
75 sum = (int)MULT_FIX(frac, wrk->fx_scale);
78 } else { // simple bilinear interpolation
79 int left = src[channel], right = src[channel];
80 for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
87 wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum;
91 // Accumulate the new row's contribution
92 for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
93 wrk->irow[x_out] += wrk->frow[x_out];
97 uint8_t* WebPRescalerExportRow(WebPRescaler* const wrk) {
98 if (wrk->y_accum <= 0) {
100 uint8_t* const dst = wrk->dst;
101 int32_t* const irow = wrk->irow;
102 const int32_t* const frow = wrk->frow;
103 const int yscale = wrk->fy_scale * (-wrk->y_accum);
104 const int x_out_max = wrk->dst_width * wrk->num_channels;
106 for (x_out = 0; x_out < x_out_max; ++x_out) {
107 const int frac = (int)MULT_FIX(frow[x_out], yscale);
108 const int v = (int)MULT_FIX(irow[x_out] - frac, wrk->fxy_scale);
109 dst[x_out] = (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
110 irow[x_out] = frac; // new fractional start
112 wrk->y_accum += wrk->y_add;
113 wrk->dst += wrk->dst_stride;
123 //------------------------------------------------------------------------------
126 int WebPRescalerImport(WebPRescaler* const wrk, int num_lines,
127 const uint8_t* src, int src_stride) {
128 int total_imported = 0;
129 while (total_imported < num_lines && wrk->y_accum > 0) {
131 for (channel = 0; channel < wrk->num_channels; ++channel) {
132 WebPRescalerImportRow(wrk, src, channel);
136 wrk->y_accum -= wrk->y_sub;
138 return total_imported;
141 int WebPRescalerExport(WebPRescaler* const rescaler) {
142 int total_exported = 0;
143 while (WebPRescalerHasPendingOutput(rescaler)) {
144 WebPRescalerExportRow(rescaler);
147 return total_exported;
150 //------------------------------------------------------------------------------
152 #if defined(__cplusplus) || defined(c_plusplus)