1 /*
2  * Image Scaling Functions (4 tap)
3  * Copyright (c) 2005 David A. Schleef <ds@schleef.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "vs_image.h"
29 #include "vs_scanline.h"
30 
31 #include <liboil/liboil.h>
32 #include <math.h>
33 
34 #define SHIFT 10
35 
36 int16_t vs_4tap_taps[256][4];
37 
vs_4tap_func(double x)38 double vs_4tap_func (double x)
39 {
40 #if 0
41   if (x<-1) return 0;
42   if (x>1) return 0;
43   if (x<0) return 1 + x;
44   return 1 - x;
45 #endif
46 #if 0
47   if (x==0) return 1;
48   return sin(M_PI*x)/(M_PI*x) * (1 - 0.25*x*x);
49 #endif
50 #if 1
51   if (x==0) return 1;
52   return sin(M_PI*x)/(M_PI*x);
53 #endif
54 }
55 
56 void
vs_4tap_init(void)57 vs_4tap_init (void)
58 {
59   int i;
60   double a, b, c, d;
61   double sum;
62 
63   for(i=0;i<256;i++){
64     a = vs_4tap_func(-1 - i/256.0);
65     b = vs_4tap_func(0 - i/256.0);
66     c = vs_4tap_func(1 - i/256.0);
67     d = vs_4tap_func(2 - i/256.0);
68     sum = a + b + c + d;
69 
70     vs_4tap_taps[i][0] = rint((1<<SHIFT)*(a/sum));
71     vs_4tap_taps[i][1] = rint((1<<SHIFT)*(b/sum));
72     vs_4tap_taps[i][2] = rint((1<<SHIFT)*(c/sum));
73     vs_4tap_taps[i][3] = rint((1<<SHIFT)*(d/sum));
74   }
75 }
76 
77 
78 void
vs_scanline_resample_4tap_Y(uint8_t * dest,uint8_t * src,int n,int * xacc,int increment)79 vs_scanline_resample_4tap_Y (uint8_t *dest, uint8_t *src,
80     int n, int *xacc, int increment)
81 {
82   int i;
83   int j;
84   int acc;
85   int x;
86   int y;
87 
88   acc = *xacc;
89   for(i=0;i<n;i++){
90     j = acc>>16;
91     x = (acc&0xff00)>>8;
92     y = (vs_4tap_taps[x][0] * src[MAX(j-1,0)] +
93         vs_4tap_taps[x][1] * src[j] +
94         vs_4tap_taps[x][2] * src[j+1] +
95         vs_4tap_taps[x][3] * src[j+2] + (1<<(SHIFT-1))) >> SHIFT;
96     if (y<0) y = 0;
97     if (y>255) y = 255;
98     dest[i] = y;
99     acc += increment;
100   }
101   *xacc = acc;
102 }
103 
104 void
vs_scanline_merge_4tap_Y(uint8_t * dest,uint8_t * src1,uint8_t * src2,uint8_t * src3,uint8_t * src4,int n,int acc)105 vs_scanline_merge_4tap_Y (uint8_t *dest, uint8_t *src1, uint8_t *src2,
106     uint8_t *src3, uint8_t *src4, int n, int acc)
107 {
108   int i;
109   int y;
110 
111   acc = (acc>>8) & 0xff;
112   for(i=0;i<n;i++){
113     y = (vs_4tap_taps[acc][0] * src1[i] +
114         vs_4tap_taps[acc][1] * src2[i] +
115         vs_4tap_taps[acc][2] * src3[i] +
116         vs_4tap_taps[acc][3] * src4[i] + (1<<(SHIFT-1))) >> SHIFT;
117     if (y<0) y = 0;
118     if (y>255) y = 255;
119     dest[i] = y;
120   }
121 
122 }
123 
124 
125 void
vs_image_scale_4tap_Y(const VSImage * dest,const VSImage * src,uint8_t * tmpbuf)126 vs_image_scale_4tap_Y (const VSImage * dest, const VSImage * src,
127     uint8_t * tmpbuf)
128 {
129   int yacc;
130   int y_increment;
131   int x_increment;
132   int i;
133   int j;
134   int xacc;
135   int k;
136 
137   y_increment = ((src->height - 1) << 16) / (dest->height - 1);
138   x_increment = ((src->width - 1) << 16) / (dest->width - 1);
139 
140   k = 0;
141   for(i=0;i<4;i++){
142     xacc = 0;
143     vs_scanline_resample_4tap_Y (tmpbuf + i * dest->width,
144         src->pixels + i * src->stride,
145         dest->width, &xacc, x_increment);
146   }
147 
148   yacc = 0;
149   for (i = 0; i < dest->height; i++) {
150     uint8_t *t0, *t1, *t2, *t3;
151 
152     j = yacc >> 16;
153 
154     if (j > k) {
155       k++;
156       if (k + 3 < src->height) {
157         xacc = 0;
158         vs_scanline_resample_4tap_Y (tmpbuf + ((k+3)&3) * dest->width,
159             src->pixels + (k+3) * src->stride,
160             dest->width, &xacc, x_increment);
161       }
162     }
163 
164     t0 = tmpbuf + (CLAMP(j - 1, 0, src->height - 1)&3) * dest->width;
165     t1 = tmpbuf + (CLAMP(j    , 0, src->height - 1)&3) * dest->width;
166     t2 = tmpbuf + (CLAMP(j + 1, 0, src->height - 1)&3) * dest->width;
167     t3 = tmpbuf + (CLAMP(j + 2, 0, src->height - 1)&3) * dest->width;
168     vs_scanline_merge_4tap_Y (dest->pixels + i*dest->stride,
169         t0, t1, t2, t3, dest->width, yacc & 0xffff);
170 
171     yacc += y_increment;
172   }
173 }
174 
175 
176