summaryrefslogtreecommitdiff
blob: e5516966a5ebbc7194031355cf0f4fddd6810d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
diff -urN old/xv-3.10a/xvsmooth.c xv-3.10a/xvsmooth.c
--- old/xv-3.10a/xvsmooth.c	Thu Dec 22 17:34:42 1994
+++ xv-3.10a/xvsmooth.c	Sun Aug 17 14:07:58 1997
@@ -65,10 +65,13 @@
      returns a dwide*dhigh 24bit image, or NULL on failure (malloc) */
   /* rmap,gmap,bmap should be 'desired' colors */
 
+  /* DANGER:  This code assumes that a right-shift on signed integers is
+     performed arithmetically, i.e. the sign bit is copied to the right.  */
+
   byte *pic24, *pp;
-  int  *cxtab, *pxtab;
-  int   y1Off, cyOff;
-  int   ex, ey, cx, cy, px, py, apx, apy, x1, y1;
+  int  *pxtab;
+  int   ex, ey, px, py, x0, x1, y0, y1;
+  int   y0off, y1off;
   int   cA, cB, cC, cD;
   int   pA, pB, pC, pD;
   int   retval, bperpix;
@@ -98,69 +101,61 @@
   else {
     /* dwide >= swide && dhigh >= shigh */
 
-    /* cx,cy = original pixel in pic824.  px,py = relative position 
-       of pixel ex,ey inside of cx,cy as percentages +-50%, +-50%.  
-       0,0 = middle of pixel */
-
-    /* we can save a lot of time by precomputing cxtab[] and pxtab[], both
-       dwide arrays of ints that contain values for the equations:
-         cx = (ex * swide) / dwide;
-         px = ((ex * swide * 100) / dwide) - (cx * 100) - 50; */
-
-    cxtab = (int *) malloc(dwide * sizeof(int));
-    if (!cxtab) { free(pic24);  return NULL; }
+    /* px, py = location on original image, in units of 1/256 pixel
+       We can save time by precomputing all values of px.  */
 
     pxtab = (int *) malloc(dwide * sizeof(int));
-    if (!pxtab) { free(pic24);  free(cxtab);  return NULL; }
-
-    for (ex=0; ex<dwide; ex++) {
-      cxtab[ex] = (ex * swide) / dwide;
-      pxtab[ex] = (((ex * swide)* 100) / dwide) 
-	           - (cxtab[ex] * 100) - 50;
-    }
+    if (!pxtab) { free(pic24);  return NULL; }
     
-    for (ey=0; ey<dhigh; ey++) {
+    for (ex=0; ex < dwide; ex++)
+      pxtab[ex] = ((ex << 8) + 128) * swide / dwide - 128;
+    
+    for (ey=0; ey < dhigh; ey++) {
       byte *pptr, rA, gA, bA, rB, gB, bB, rC, gC, bC, rD, gD, bD;
 
       ProgressMeter(0, (dhigh)-1, ey, "Smooth");
 
-      cy = (ey * shigh) / dhigh;
-      py = (((ey * shigh) * 100) / dhigh) - (cy * 100) - 50;
-      if (py<0) { y1 = cy-1;  if (y1<0) y1=0; }
-           else { y1 = cy+1;  if (y1>shigh-1) y1=shigh-1; }
+      py = ((ey << 8) + 128) * shigh / dhigh - 128;
+      y0 = py >> 8;			/* Put integer part in y0 */
+      y1 = y0 + 1;
+      py &= 255;			/* Keep fractional part in py */
+
+      if (y0 < 0)	y0 = y1 = 0;
+      if (y1 >= shigh)	y0 = y1 = shigh-1;
 
-      cyOff = cy * swide * bperpix;    /* current line */
-      y1Off = y1 * swide * bperpix;    /* up or down one line, depending */
+      y0off = y0 * swide * bperpix;	/* current line */
+      y1off = y1 * swide * bperpix;	/* one line down */
 
       if ((ey&15) == 0) WaitCursor();
 
-      for (ex=0; ex<dwide; ex++) {
-	rA = rB = rC = rD = gA = gB = gC = gD = bA = bB = bC = bD = 0;
+      for (ex=0; ex < dwide; ex++) {
 
-	cx = cxtab[ex];
 	px = pxtab[ex];
+	x0 = px >> 8;			/* Put integer part in x0 */
+	x1 = x0 + 1;
+	px &= 255;			/* Keep fractional part in px */
 
-	if (px<0) { x1 = cx-1;  if (x1<0) x1=0; }
-	     else { x1 = cx+1;  if (x1>swide-1) x1=swide-1; }
+	if (x0 < 0)	  x0 = x1 = 0;
+	if (x1 >= swide)  x0 = x1 = swide-1;
 
 	if (is24) {
-	  pptr = pic824 + y1Off + x1*bperpix;   /* corner pixel */
+	  pptr = pic824 + y0off + x0*bperpix;   /* upper left pixel */
 	  rA = *pptr++;  gA = *pptr++;  bA = *pptr++;
 
-	  pptr = pic824 + y1Off + cx*bperpix;   /* up/down center pixel */
+	  pptr = pic824 + y0off + x1*bperpix;   /* upper right pixel */
 	  rB = *pptr++;  gB = *pptr++;  bB = *pptr++;
 
-	  pptr = pic824 + cyOff + x1*bperpix;   /* left/right center pixel */
+	  pptr = pic824 + y1off + x0*bperpix;   /* lower left pixel */
 	  rC = *pptr++;  gC = *pptr++;  bC = *pptr++;
 
-	  pptr = pic824 + cyOff + cx*bperpix;   /* center pixel */
+	  pptr = pic824 + y1off + x1*bperpix;   /* lower right pixel */
 	  rD = *pptr++;  gD = *pptr++;  bD = *pptr++;
 	}
 	else {  /* 8-bit picture */
-	  cA = pic824[y1Off + x1];   /* corner pixel */
-	  cB = pic824[y1Off + cx];   /* up/down center pixel */
-	  cC = pic824[cyOff + x1];   /* left/right center pixel */
-	  cD = pic824[cyOff + cx];   /* center pixel */
+	  cA = pic824[y0off + x0];	/* upper left pixel */
+	  cB = pic824[y0off + x1];	/* upper right pixel */
+	  cC = pic824[y1off + x0];	/* lower left pixel */
+	  cD = pic824[y1off + x1];	/* lower right pixel */
 	}
 	 
 	/* quick check */
@@ -170,38 +165,30 @@
 	}
 
 	else {
-	  /* compute weighting factors */
-	  apx = abs(px);  apy = abs(py);
-	  pA = (apx * apy) / 100;
-	  pB = (apy * (100 - apx)) / 100;
-	  pC = (apx * (100 - apy)) / 100;
-	  pD = 100 - (pA + pB + pC);
+	  pA = (256-px)*(256-py);	/* compute weighting factors */
+	  pB = px * (256-py);		/* total weight is exactly 2^16 */
+	  pC = (256-px) * py;
+	  pD = px * py;
 
 	  if (is24) {
-	    *pp++ = ((int) (pA * rA))/100 + ((int) (pB * rB))/100 + 
-	            ((int) (pC * rC))/100 + ((int) (pD * rD))/100;
-
-	    *pp++ = ((int) (pA * gA))/100 + ((int) (pB * gB))/100 + 
-	            ((int) (pC * gC))/100 + ((int) (pD * gD))/100;
-
-	    *pp++ = ((int) (pA * bA))/100 + ((int) (pB * bB))/100 + 
-	            ((int) (pC * bC))/100 + ((int) (pD * bD))/100;
+	    *pp++ = (pA * rA + pB * rB + pC * rC + pD * rD) + 32768 >> 16;
+	    *pp++ = (pA * gA + pB * gB + pC * gC + pD * gD) + 32768 >> 16;
+	    *pp++ = (pA * bA + pB * bB + pC * bC + pD * bD) + 32768 >> 16;
 	  }
 	  else {  /* 8-bit pic */
-	    *pp++ = ((int) (pA * rmap[cA]))/100 + ((int)(pB * rmap[cB]))/100 + 
-	            ((int) (pC * rmap[cC]))/100 + ((int)(pD * rmap[cD]))/100;
+	    *pp++ = (pA * rmap[cA] + pB * rmap[cB] + pC * rmap[cC] +
+		     pD * rmap[cD]) + 32768 >> 16;
 
-	    *pp++ = ((int) (pA * gmap[cA]))/100 + ((int)(pB * gmap[cB]))/100 + 
-	            ((int) (pC * gmap[cC]))/100 + ((int)(pD * gmap[cD]))/100;
+	    *pp++ = (pA * gmap[cA] + pB * gmap[cB] + pC * gmap[cC] +
+		     pD * gmap[cD]) + 32768 >> 16;
 
-	    *pp++ = ((int)(pA * bmap[cA]))/100 + ((int)(pB * bmap[cB]))/100 + 
-  	            ((int)(pC * bmap[cC]))/100 + ((int)(pD * bmap[cD]))/100;
+	    *pp++ = (pA * bmap[cA] + pB * bmap[cB] + pC * bmap[cC] +
+		     pD * bmap[cD]) + 32768 >> 16;
 	  }
 	}
       }
     }
 
-    free(cxtab);  
     free(pxtab);
     retval = 0;    /* okay */
   }