blewett@RESEARCH.ATT.COM (C. Douglas Blewett) (09/05/89)
The following is a fun routine. It can be used to sample an X image. One can use this to scale up or down, or stretch images. It does not include any fancy signal processing, but it is portable and fairly fast. This fits into the magnifying widget that popped around many months ago. /* * SampleImage: integer interpolation; * new image may be larger or smaller than original; * */ #define OnePixel() \ x = (x_fctr * i) >> 9; \ if (x == lastx) \ { \ XPutPixel (out, i, j, pixel); \ } \ else \ { \ pixel = XGetPixel (in, x, y); \ XPutPixel (out, i, j, pixel); \ } \ lastx = x; \ i++; \ #define OneLine() \ y = (y_fctr * j) >> 9; \ if (y == lasty) \ { \ char *ptr; \ \ ptr = out -> data + out -> bytes_per_line * (j - 1); \ bcopy (ptr, ptr + out -> bytes_per_line, \ out -> bytes_per_line); \ } \ else \ { \ register int x, lastx; \ register int i; \ register Pixel pixel; \ \ lastx = -1; \ i = 0; \ switch (out -> width & 7) \ { \ do \ { \ OnePixel (); \ case 7: OnePixel (); \ case 6: OnePixel (); \ case 5: OnePixel (); \ case 4: OnePixel (); \ case 3: OnePixel (); \ case 2: OnePixel (); \ case 1: OnePixel (); \ case 0: ; \ } while (i < out -> width); \ } \ } \ lasty = y; \ j++; \ static void SampleImage (in, out) register XImage *in; register XImage *out; { register int y, lasty; register int j; register long x_fctr, y_fctr; XtWarning ("Sampling Image."); x_fctr = (in -> width << 9) / (out -> width); y_fctr = (in -> height << 9) / (out -> height); lasty = -1; j = 0; switch (out -> height & 7) { do { OneLine (); case 7: OneLine (); case 6: OneLine (); case 5: OneLine (); case 4: OneLine (); case 3: OneLine (); case 2: OneLine (); case 1: OneLine (); case 0: ; } while (j < out -> height); } XtWarning ("Sampling Image Completed."); } Doug Blewett (201) 582-6496 AT&T Bell Laboratories research!blewett office 2c470 blewett%research.att.com@relay.cs.net 600 Mountain Avenue blewett@research.att.com Murray Hill, NJ 07974