1 // Copyright 2012 Google Inc. All Rights Reserved. 2 // 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 // ----------------------------------------------------------------------------- 9 // 10 // Rescaling functions 11 // 12 // Author: Skal (pascal.massimino (at) gmail.com) 13 14 #include <assert.h> 15 #include <stdlib.h> 16 #include "./rescaler.h" 17 18 //------------------------------------------------------------------------------ 19 20 #if defined(__cplusplus) || defined(c_plusplus) 21 extern "C" { 22 #endif 23 24 #define RFIX 30 25 #define MULT_FIX(x, y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX) 26 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; 36 wrk->dst = dst; 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; 42 wrk->y_accum = y_add; 43 wrk->y_add = y_add; 44 wrk->y_sub = y_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); 50 wrk->irow = work; 51 wrk->frow = work + num_channels * dst_width; 52 } 53 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; 58 int x_in = channel; 59 int x_out; 60 int accum = 0; 61 if (!wrk->x_expand) { 62 int sum = 0; 63 for (x_out = channel; x_out < x_out_max; x_out += x_stride) { 64 accum += wrk->x_add; 65 for (; accum > 0; accum -= wrk->x_sub) { 66 sum += src[x_in]; 67 x_in += x_stride; 68 } 69 { // Emit next horizontal pixel. 70 const int32_t base = src[x_in]; 71 const int32_t frac = base * (-accum); 72 x_in += x_stride; 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); 76 } 77 } 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) { 81 if (accum < 0) { 82 left = right; 83 x_in += x_stride; 84 right = src[x_in]; 85 accum += wrk->x_add; 86 } 87 wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum; 88 accum -= wrk->x_sub; 89 } 90 } 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]; 94 } 95 } 96 97 uint8_t* WebPRescalerExportRow(WebPRescaler* const wrk) { 98 if (wrk->y_accum <= 0) { 99 int x_out; 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; 105 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 111 } 112 wrk->y_accum += wrk->y_add; 113 wrk->dst += wrk->dst_stride; 114 return dst; 115 } else { 116 return NULL; 117 } 118 } 119 120 #undef MULT_FIX 121 #undef RFIX 122 123 //------------------------------------------------------------------------------ 124 // all-in-one calls 125 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) { 130 int channel; 131 for (channel = 0; channel < wrk->num_channels; ++channel) { 132 WebPRescalerImportRow(wrk, src, channel); 133 } 134 src += src_stride; 135 ++total_imported; 136 wrk->y_accum -= wrk->y_sub; 137 } 138 return total_imported; 139 } 140 141 int WebPRescalerExport(WebPRescaler* const rescaler) { 142 int total_exported = 0; 143 while (WebPRescalerHasPendingOutput(rescaler)) { 144 WebPRescalerExportRow(rescaler); 145 ++total_exported; 146 } 147 return total_exported; 148 } 149 150 //------------------------------------------------------------------------------ 151 152 #if defined(__cplusplus) || defined(c_plusplus) 153 } // extern "C" 154 #endif 155