• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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@gmail.com)
13 
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include "../dsp/dsp.h"
18 #include "./rescaler.h"
19 
20 //------------------------------------------------------------------------------
21 
WebPRescalerInit(WebPRescaler * const wrk,int src_width,int src_height,uint8_t * const dst,int dst_width,int dst_height,int dst_stride,int num_channels,rescaler_t * const work)22 void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
23                       uint8_t* const dst,
24                       int dst_width, int dst_height, int dst_stride,
25                       int num_channels, rescaler_t* const work) {
26   const int x_add = src_width, x_sub = dst_width;
27   const int y_add = src_height, y_sub = dst_height;
28   wrk->x_expand = (src_width < dst_width);
29   wrk->y_expand = (src_height < dst_height);
30   wrk->src_width = src_width;
31   wrk->src_height = src_height;
32   wrk->dst_width = dst_width;
33   wrk->dst_height = dst_height;
34   wrk->src_y = 0;
35   wrk->dst_y = 0;
36   wrk->dst = dst;
37   wrk->dst_stride = dst_stride;
38   wrk->num_channels = num_channels;
39 
40   // for 'x_expand', we use bilinear interpolation
41   wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add;
42   wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;
43   if (!wrk->x_expand) {  // fx_scale is not used otherwise
44     wrk->fx_scale = WEBP_RESCALER_FRAC(1, wrk->x_sub);
45   }
46   // vertical scaling parameters
47   wrk->y_add = wrk->y_expand ? y_add - 1 : y_add;
48   wrk->y_sub = wrk->y_expand ? y_sub - 1 : y_sub;
49   wrk->y_accum = wrk->y_expand ? wrk->y_sub : wrk->y_add;
50   if (!wrk->y_expand) {
51     // this is WEBP_RESCALER_FRAC(dst_height, x_add * y_add) without the cast.
52     const uint64_t ratio =
53         (uint64_t)dst_height * WEBP_RESCALER_ONE / (wrk->x_add * wrk->y_add);
54     if (ratio != (uint32_t)ratio) {
55       // We can't represent the ratio with the current fixed-point precision.
56       // => We special-case fxy_scale = 0, in WebPRescalerExportRow().
57       wrk->fxy_scale = 0;
58     } else {
59       wrk->fxy_scale = (uint32_t)ratio;
60     }
61     wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->y_sub);
62   } else {
63     wrk->fy_scale = WEBP_RESCALER_FRAC(1, wrk->x_add);
64     // wrk->fxy_scale is unused here.
65   }
66   wrk->irow = work;
67   wrk->frow = work + num_channels * dst_width;
68   memset(work, 0, 2 * dst_width * num_channels * sizeof(*work));
69 
70   WebPRescalerDspInit();
71 }
72 
WebPRescalerGetScaledDimensions(int src_width,int src_height,int * const scaled_width,int * const scaled_height)73 int WebPRescalerGetScaledDimensions(int src_width, int src_height,
74                                     int* const scaled_width,
75                                     int* const scaled_height) {
76   assert(scaled_width != NULL);
77   assert(scaled_height != NULL);
78   {
79     int width = *scaled_width;
80     int height = *scaled_height;
81 
82     // if width is unspecified, scale original proportionally to height ratio.
83     if (width == 0) {
84       width = (src_width * height + src_height / 2) / src_height;
85     }
86     // if height is unspecified, scale original proportionally to width ratio.
87     if (height == 0) {
88       height = (src_height * width + src_width / 2) / src_width;
89     }
90     // Check if the overall dimensions still make sense.
91     if (width <= 0 || height <= 0) {
92       return 0;
93     }
94 
95     *scaled_width = width;
96     *scaled_height = height;
97     return 1;
98   }
99 }
100 
101 //------------------------------------------------------------------------------
102 // all-in-one calls
103 
WebPRescaleNeededLines(const WebPRescaler * const wrk,int max_num_lines)104 int WebPRescaleNeededLines(const WebPRescaler* const wrk, int max_num_lines) {
105   const int num_lines = (wrk->y_accum + wrk->y_sub - 1) / wrk->y_sub;
106   return (num_lines > max_num_lines) ? max_num_lines : num_lines;
107 }
108 
WebPRescalerImport(WebPRescaler * const wrk,int num_lines,const uint8_t * src,int src_stride)109 int WebPRescalerImport(WebPRescaler* const wrk, int num_lines,
110                        const uint8_t* src, int src_stride) {
111   int total_imported = 0;
112   while (total_imported < num_lines && !WebPRescalerHasPendingOutput(wrk)) {
113     if (wrk->y_expand) {
114       rescaler_t* const tmp = wrk->irow;
115       wrk->irow = wrk->frow;
116       wrk->frow = tmp;
117     }
118     WebPRescalerImportRow(wrk, src);
119     if (!wrk->y_expand) {     // Accumulate the contribution of the new row.
120       int x;
121       for (x = 0; x < wrk->num_channels * wrk->dst_width; ++x) {
122         wrk->irow[x] += wrk->frow[x];
123       }
124     }
125     ++wrk->src_y;
126     src += src_stride;
127     ++total_imported;
128     wrk->y_accum -= wrk->y_sub;
129   }
130   return total_imported;
131 }
132 
WebPRescalerExport(WebPRescaler * const rescaler)133 int WebPRescalerExport(WebPRescaler* const rescaler) {
134   int total_exported = 0;
135   while (WebPRescalerHasPendingOutput(rescaler)) {
136     WebPRescalerExportRow(rescaler);
137     ++total_exported;
138   }
139   return total_exported;
140 }
141 
142 //------------------------------------------------------------------------------
143