File: lg3zoom.c

package info (click to toggle)
xslideshow 3.1-7
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 792 kB
  • ctags: 1,375
  • sloc: ansic: 14,006; makefile: 897; sh: 1
file content (284 lines) | stat: -rw-r--r-- 7,934 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
	lg3zoom.c - zoom the image using Lagrange polynominals algorithm

	Author:   Susumu Shiohara (shiohara@tpp.epson.co.jp)

		Copyright 1993-1997 by Susumu Shiohara

			All Rights Reserved

	------------------------------------------------------------------

	This enlargement and reduction method was proposed by

			Mr. Shinhwan KIM
			Mr. Shigeo KATO
			Mr. Yasuhiko YASUDA

			Institute of Industrial Science, University of Tokyo

			18th Image Technology Conference, 1987
*/

#include "xslideshow.h"

extern void goodbyekiss();
extern void myevent();
/*
	LG3 (Lagrange polynominals algorithm)

	M: Magnify rate
		Enlarge   > 1.0
		Reduce    < 1.0

	S0       S1       S2      S3
    .        .<- dk ->.       .
	0        1        2       3

	The pixel value which has been interpolated is: 
	Yk = LG3(dk)		(k=1,2,,,,,,n*M)


	3 degree lagrange polynominals is:
	LG3(dk) = (A * (dk)^3 + B * (dk)^2 + C * (dk)) + D

	A = ( -1*S0 +  3*S1 -  3*S2 + 1*S3)/6
	B = (  6*S0 - 15*S1 + 12*S2 - 3*S3)/6
	C = (-11*S0 + 18*S1 -  9*S2 + 2*S3)/6
	D = S0

	dk = 1 + (k-1)/M - (INT)((k-1)/M) + a	(0<= a < 1)

*/

#if defined(__STDC__) || defined(__cplusplus)
static void readLine(byte *srcptr, byte *dstptr, dword xwidth, double rzoom)
#else
static void readLine(srcptr, dstptr, xwidth, rzoom)
byte *srcptr;
byte *dstptr;
dword xwidth;
double rzoom;
#endif
{
dword x;
int iposi;
double dposi;
double dk1,dk2,dk3;
double A,B,C,D;
double r,g,b;
dword r0,r1,r2,r3;
dword g0,g1,g2,g3;
dword b0,b1,b2,b3;

	/* Calcurate LG3 */
	for (x = 1; x < (xwidth-2); x++) {

		iposi = (int)((x-1)/rzoom);
		dposi = (double)((x-1)/rzoom);

		/* Set the interpolation position */
		dk1 = 1.0 + dposi - iposi + 0.0;
		dk2 = dk1*dk1;
		dk3 = dk1*dk2;

		/* Get the pixel value */
		if(gim.subImageList->bits_per_pixel == 8){
			/* Red */
			r0 = gim.subImageList->red[*(srcptr+iposi-1)];
			r1 = gim.subImageList->red[*(srcptr+iposi+0)];
			r2 = gim.subImageList->red[*(srcptr+iposi+1)];
			r3 = gim.subImageList->red[*(srcptr+iposi+2)];

			/* Green */
			g0 = gim.subImageList->green[*(srcptr+iposi-1)];
			g1 = gim.subImageList->green[*(srcptr+iposi+0)];
			g2 = gim.subImageList->green[*(srcptr+iposi+1)];
			g3 = gim.subImageList->green[*(srcptr+iposi+2)];

			/* Blue */
			b0 = gim.subImageList->blue[*(srcptr+iposi-1)];
			b1 = gim.subImageList->blue[*(srcptr+iposi+0)];
			b2 = gim.subImageList->blue[*(srcptr+iposi+1)];
			b3 = gim.subImageList->blue[*(srcptr+iposi+2)];
		}
		else {
			/* Red */
			r0 = *(srcptr+(iposi-1)*3+0);
			r1 = *(srcptr+(iposi+0)*3+0);
			r2 = *(srcptr+(iposi+1)*3+0);
			r3 = *(srcptr+(iposi+2)*3+0);

			/* Green */
			g0 = *(srcptr+(iposi-1)*3+1);
			g1 = *(srcptr+(iposi+0)*3+1);
			g2 = *(srcptr+(iposi+1)*3+1);
			g3 = *(srcptr+(iposi+2)*3+1);

			/* Blue */
			b0 = *(srcptr+(iposi-1)*3+2);
			b1 = *(srcptr+(iposi+0)*3+2);
			b2 = *(srcptr+(iposi+1)*3+2);
			b3 = *(srcptr+(iposi+2)*3+2);
		}

		A = ( 1 * (double)r3 -  3 * (double)r2 +  3 * (double)r1 -  1 * (double)r0)/6;
		B = (-3 * (double)r3 + 12 * (double)r2 - 15 * (double)r1 +  6 * (double)r0)/6;
		C = ( 2 * (double)r3 -  9 * (double)r2 + 18 * (double)r1 - 11 * (double)r0)/6;
		D = (double)r0;
		r = A*dk3 + B*dk2 + C*dk1 + D;

		A = ( 1 * (double)g3 -  3 * (double)g2 +  3 * (double)g1 -  1 * (double)g0)/6;
		B = (-3 * (double)g3 + 12 * (double)g2 - 15 * (double)g1 +  6 * (double)g0)/6;
		C = ( 2 * (double)g3 -  9 * (double)g2 + 18 * (double)g1 - 11 * (double)g0)/6;
		D = (double)g0;
		g = A*dk3 + B*dk2 + C*dk1 + D;

		A = ( 1 * (double)b3 -  3 * (double)b2 +  3 * (double)b1 -  1 * (double)b0)/6;
		B = (-3 * (double)b3 + 12 * (double)b2 - 15 * (double)b1 +  6 * (double)b0)/6;
		C = ( 2 * (double)b3 -  9 * (double)b2 + 18 * (double)b1 - 11 * (double)b0)/6;
		D = (double)b0;
		b = A*dk3 + B*dk2 + C*dk1 + D;

		if(r > 255.0) r = 255.0; else if(r < 0.0) r = 0.0;
		if(g > 255.0) g = 255.0; else if(g < 0.0) g = 0.0;
		if(b > 255.0) b = 255.0; else if(b < 0.0) b = 0.0;

		*dstptr++ = (byte)r;
		*dstptr++ = (byte)g;
		*dstptr++ = (byte)b;
	}
}

#if defined(__STDC__) || defined(__cplusplus)
void lg3zoom(dword xzoom, dword yzoom)
#else
void lg3zoom(xzoom, yzoom)
dword xzoom, yzoom;
#endif
{ 
int i;
dword x, y;
dword xwidth, ywidth;
double rzoom;
dword srclinelen;
byte *srcptr, *srcdata, *dstptr, *dstdata;
int iposi;
double dposi;
double dk1,dk2,dk3;
double A,B,C,D;
double r,g,b;
dword r0,r1,r2,r3;
dword g0,g1,g2,g3;
dword b0,b1,b2,b3;
byte *lineBuf[5];

	rzoom= (double)xzoom / 100.0;
	xwidth= rzoom * gim.subImageList->width;
	ywidth= rzoom * gim.subImageList->height;

	if(app_data.verbose){
		fprintf(stderr,"xslideshow: lg3zoom() %d%% (%d x %d) -> (%d x %d)\n",
				(int)xzoom, (int)gim.subImageList->width,(int)gim.subImageList->height,(int)xwidth,(int)ywidth);
	}

	dstdata = (byte *)XtCalloc(xwidth * ywidth * 3, sizeof(byte));
	if(dstdata ==(byte *)NULL){
		(void) fprintf(stderr,"xslideshow: lg3zoom() memory allocation error.\n");
		goodbyekiss();
	}

	for(i=0; i<4; i++){
		lineBuf[i] = (byte *)XtCalloc(xwidth * 3, sizeof(byte));
		if(lineBuf[i] ==(byte *)NULL){
			(void) fprintf(stderr,"xslideshow: lg3zoom() memory allocation error.\n");
			goodbyekiss();
		}
	}

	/* read and write from the second pixel */
	if(gim.subImageList->bits_per_pixel == 8) {
		srcdata = gim.subImageList->image + 1;
		srclinelen = gim.subImageList->width;
	}
	else {
		srcdata = gim.subImageList->image + 3;
		srclinelen = gim.subImageList->width * 3;
	}
	dstptr = dstdata + 3;

	/* Read 3 line */
	for(y=1; y < 4; y++) {
		iposi = (int)((y-1)/rzoom);
		srcptr = srcdata + iposi * srclinelen;
		if(y < ywidth)
			readLine(srcptr, lineBuf[y-1], xwidth, rzoom);
	}

	for(y=1; y < (ywidth-2); y++) {
		iposi = (int)((y-1)/rzoom);
		dposi = (double)((y-1)/rzoom);

		/* Set the interpolation line position  (0.0 <= a < 1.0) */
		dk1 = 1.0 + dposi - iposi + 0.0;
		dk2 = dk1*dk1;
		dk3 = dk1*dk2;

		/* Read the 4th line */
		srcptr = srcdata + (int)((y-1+3)/rzoom) * srclinelen;
		readLine(srcptr, lineBuf[3], xwidth, rzoom);

		for(x=0; x < xwidth; x++) {
			r0 = *(lineBuf[0]+x*3+0); r1 = *(lineBuf[1]+x*3+0);
			r2 = *(lineBuf[2]+x*3+0); r3 = *(lineBuf[3]+x*3+0);

			g0 = *(lineBuf[0]+x*3+1); g1 = *(lineBuf[1]+x*3+1);
			g2 = *(lineBuf[2]+x*3+1); g3 = *(lineBuf[3]+x*3+1);

			b0 = *(lineBuf[0]+x*3+2); b1 = *(lineBuf[1]+x*3+2);
			b2 = *(lineBuf[2]+x*3+2); b3 = *(lineBuf[3]+x*3+2);

			A = ( 1 * (double)r3 -  3 * (double)r2 +  3 * (double)r1 -  1 * (double)r0)/6;
			B = (-3 * (double)r3 + 12 * (double)r2 - 15 * (double)r1 +  6 * (double)r0)/6;
			C = ( 2 * (double)r3 -  9 * (double)r2 + 18 * (double)r1 - 11 * (double)r0)/6;
			D = (double)r0;
			r = A*dk3 + B*dk2 + C*dk1 + D;

			A = ( 1 * (double)g3 -  3 * (double)g2 +  3 * (double)g1 -  1 * (double)g0)/6;
			B = (-3 * (double)g3 + 12 * (double)g2 - 15 * (double)g1 +  6 * (double)g0)/6;
			C = ( 2 * (double)g3 -  9 * (double)g2 + 18 * (double)g1 - 11 * (double)g0)/6;
			D = (double)g0;
			g = A*dk3 + B*dk2 + C*dk1 + D;

			A = ( 1 * (double)b3 -  3 * (double)b2 +  3 * (double)b1 -  1 * (double)b0)/6;
			B = (-3 * (double)b3 + 12 * (double)b2 - 15 * (double)b1 +  6 * (double)b0)/6;
			C = ( 2 * (double)b3 -  9 * (double)b2 + 18 * (double)b1 - 11 * (double)b0)/6;
			D = (double)b0;
			b = A*dk3 + B*dk2 + C*dk1 + D;

			if(r > 255.0) r = 255.0; else if(r < 0.0) r = 0.0;
			if(g > 255.0) g = 255.0; else if(g < 0.0) g = 0.0;
			if(b > 255.0) b = 255.0; else if(b < 0.0) b = 0.0;

			*dstptr++ = (byte)r;
			*dstptr++ = (byte)g;
			*dstptr++ = (byte)b;

			myevent();
		}

		/* Set next 3 line data */
		lineBuf[4] = lineBuf[0];
		lineBuf[0] = lineBuf[1]; lineBuf[1] = lineBuf[2];
		lineBuf[2] = lineBuf[3]; lineBuf[3] = lineBuf[4];
	}

	for(i=0; i<4; i++)
		XtFree((char *)(lineBuf[i]));
	XtFree((char *)(gim.subImageList->image));
	gim.subImageList->bits_per_pixel = 24;
	gim.subImageList->image  = dstdata;
	gim.subImageList->width  = xwidth;
	gim.subImageList->height = ywidth;
}