-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscale.c
589 lines (526 loc) · 12.3 KB
/
scale.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* rescaling code: from Dale Schumacher's code in
* Graphics Gems III
*/
/*
* Filtered Image Rescaling
*
* by Dale Schumacher
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "tgadefs.h"
#include "tgaproto.h"
#if __MSDOS__
#include <alloc.h>
#define my_calloc(x, y) farcalloc((long)(x), (long)(y))
#define my_malloc(x) farmalloc((long)(x))
#define my_free(x) farfree(x)
#else
#define my_calloc(x, y) calloc(x, y)
#define my_malloc(x) malloc(x)
#define my_free(x) free(x)
#endif
#ifndef M_PI
#define M_PI PI
#endif
#if 0
static char _Program[] = "fzoom";
static char _Version[] = "0.20";
static char _Copyright[] = "Public Domain 1991 by Dale Schumacher";
#endif
#define WHITE_PIXEL (255)
#define BLACK_PIXEL (0)
Image *
new_image(xsize, ysize) /* create a blank image */
int xsize, ysize;
{
Image *image;
if((image = (Image *)my_malloc(sizeof(Image)))
&& (image->data = (Pixel *)my_calloc(ysize*(size_t)xsize, sizeof(Pixel)))) {
image->xsize = xsize;
image->ysize = ysize;
image->span = xsize;
return(image);
}
return 0;
}
void
free_image(image)
Image *image;
{
my_free(image->data);
my_free(image);
}
Pixel
get_pixel(image, x, y)
Image *image;
int x, y;
{
static Image *im = NULL;
static int yy = -1;
static Pixel *p = NULL;
static Pixel dummypix = { 0, 0, 0 };
if((x < 0) || (x >= image->xsize) || (y < 0) || (y >= image->ysize)) {
return dummypix;
}
if((im != image) || (yy != y)) {
im = image;
yy = y;
p = image->data + (y * image->span);
}
return(p[x]);
}
void
get_row(row, image, y)
Pixel *row;
Image *image;
int y;
{
if((y < 0) || (y >= image->ysize)) {
return;
}
memcpy(row,
image->data + (y * image->span),
(sizeof(Pixel) * image->xsize));
}
void
get_column(column, image, x)
Pixel *column;
Image *image;
int x;
{
int i, d;
Pixel *p;
if((x < 0) || (x >= image->xsize)) {
return;
}
d = (int)image->span;
for(i = image->ysize, p = image->data + x; i-- > 0; p += d) {
*column++ = *p;
}
}
void
put_pixel(image, x, y, data)
Image *image;
int x, y;
Pixel data;
{
static Image *im = NULL;
static int yy = -1;
static Pixel *p = NULL;
if((x < 0) || (x >= image->xsize) || (y < 0) || (y >= image->ysize)) {
return;
}
if((im != image) || (yy != y)) {
im = image;
yy = y;
p = image->data + (y * image->span);
}
p[x] = data;
}
static INLINE int
CLAMP(double value, int min, int max)
{
int v = (int)value;
if (v < min) return min;
if (v > max) return max;
return v;
}
/*
* filter function definitions
*/
#define box_support (0.5)
double
box_filter(t)
double t;
{
if((t > -0.5) && (t <= 0.5)) return(1.0);
return(0.0);
}
#define triangle_support (1.0)
double
triangle_filter(t)
double t;
{
if(t < 0.0) t = -t;
if(t < 1.0) return(1.0 - t);
return(0.0);
}
#define bell_support (1.5)
double
bell_filter(t) /* box (*) box (*) box */
double t;
{
if(t < 0) t = -t;
if(t < .5) return(.75 - (t * t));
if(t < 1.5) {
t = (t - 1.5);
return(.5 * (t * t));
}
return(0.0);
}
#define B_spline_support (2.0)
double
B_spline_filter(t) /* box (*) box (*) box (*) box */
double t;
{
double tt;
if(t < 0) t = -t;
if(t < 1) {
tt = t * t;
return((.5 * tt * t) - tt + (2.0 / 3.0));
} else if(t < 2) {
t = 2 - t;
return((1.0 / 6.0) * (t * t * t));
}
return(0.0);
}
double
sinc(x)
double x;
{
x *= M_PI;
if(x != 0) return(sin(x) / x);
return(1.0);
}
#define Lanczos3_support (3.0)
double
Lanczos3_filter(t)
double t;
{
if(t < 0) t = -t;
if(t < 3.0) return(sinc(t) * sinc(t/3.0));
return(0.0);
}
#define Sinc_support (4.0)
double
Sinc_filter(t)
double t;
{
if (t < 0) t = -t;
if (t >= Sinc_support) return 0.0;
return sinc(t);
}
#define Mitchell_support (2.0)
#define B (1.0 / 3.0)
#define C (1.0 / 3.0)
double
Mitchell_filter(t)
double t;
{
double tt;
tt = t * t;
if(t < 0) t = -t;
if(t < 1.0) {
t = (((12.0 - 9.0 * B - 6.0 * C) * (t * tt))
+ ((-18.0 + 12.0 * B + 6.0 * C) * tt)
+ (6.0 - 2 * B));
return(t / 6.0);
} else if(t < 2.0) {
t = (((-1.0 * B - 6.0 * C) * (t * tt))
+ ((6.0 * B + 30.0 * C) * tt)
+ ((-12.0 * B - 48.0 * C) * t)
+ (8.0 * B + 24 * C));
return(t / 6.0);
}
return(0.0);
}
/*
* image rescaling routine
*/
typedef struct {
int pixel;
double weight;
} CONTRIB;
typedef struct {
int n; /* number of contributors */
CONTRIB *p; /* pointer to list of contributions */
} CLIST;
CLIST *contrib; /* array of contribution lists */
void
zoom(dst, src, filterf, fwidth)
Image *dst; /* destination image structure */
Image *src; /* source image structure */
double (*filterf)(); /* filter function */
double fwidth; /* filter width (support) */
{
Image *tmp; /* intermediate image */
double xscale, yscale; /* zoom scale factors */
int i, j, k; /* loop variables */
int n; /* pixel number */
double center, left, right; /* filter calculation variables */
double width, fscale, weight; /* filter calculation variables */
double red, green, blue;
Pixel *raster; /* a row or column of pixels */
Pixel tmppixel;
/* create intermediate image to hold horizontal zoom */
tmp = new_image(dst->xsize, src->ysize);
if (!tmp) {
fprintf(stderr, "Unable to allocate memory for intermediate image\n");
exit(1);
}
xscale = (double) dst->xsize / (double) src->xsize;
yscale = (double) dst->ysize / (double) src->ysize;
/* pre-calculate filter contributions for a row */
contrib = (CLIST *)my_calloc(dst->xsize, sizeof(CLIST));
if(xscale < 1.0) {
width = fwidth / xscale;
fscale = 1.0 / xscale;
for(i = 0; i < dst->xsize; ++i) {
contrib[i].n = 0;
contrib[i].p = (CONTRIB *)my_calloc((int) (width * 2 + 1),
sizeof(CONTRIB));
center = (double) i / xscale;
left = ceil(center - width);
right = floor(center + width);
for(j = left; j <= right; ++j) {
weight = center - (double) j;
weight = (*filterf)(weight / fscale) / fscale;
if(j < 0) {
n = -j;
} else if(j >= src->xsize) {
n = (src->xsize - j) + src->xsize - 1;
} else {
n = j;
}
k = contrib[i].n++;
contrib[i].p[k].pixel = n;
contrib[i].p[k].weight = weight;
}
}
} else {
for(i = 0; i < dst->xsize; ++i) {
contrib[i].n = 0;
contrib[i].p = (CONTRIB *)my_calloc((int) (fwidth * 2 + 1),
sizeof(CONTRIB));
center = (double) i / xscale;
left = ceil(center - fwidth);
right = floor(center + fwidth);
for(j = left; j <= right; ++j) {
weight = center - (double) j;
weight = (*filterf)(weight);
if(j < 0) {
n = -j;
} else if(j >= src->xsize) {
n = (src->xsize - j) + src->xsize - 1;
} else {
n = j;
}
k = contrib[i].n++;
contrib[i].p[k].pixel = n;
contrib[i].p[k].weight = weight;
}
}
}
/* apply filter to zoom horizontally from src to tmp */
raster = (Pixel *)my_calloc(src->xsize, sizeof(Pixel));
for(k = 0; k < tmp->ysize; ++k) {
get_row(raster, src, k);
for(i = 0; i < tmp->xsize; ++i) {
red = green = blue = 0.0;
for(j = 0; j < contrib[i].n; ++j) {
red += raster[contrib[i].p[j].pixel].red
* contrib[i].p[j].weight;
green += raster[contrib[i].p[j].pixel].green
* contrib[i].p[j].weight;
blue += raster[contrib[i].p[j].pixel].blue
* contrib[i].p[j].weight;
}
tmppixel.red = CLAMP(red, BLACK_PIXEL, WHITE_PIXEL);
tmppixel.green = CLAMP(green, BLACK_PIXEL, WHITE_PIXEL);
tmppixel.blue = CLAMP(blue, BLACK_PIXEL, WHITE_PIXEL);
put_pixel(tmp, i, k, tmppixel);
}
}
my_free(raster);
/* free the memory allocated for horizontal filter weights */
for(i = 0; i < tmp->xsize; ++i) {
my_free(contrib[i].p);
}
my_free(contrib);
/* pre-calculate filter contributions for a column */
contrib = (CLIST *)my_calloc(dst->ysize, sizeof(CLIST));
if(yscale < 1.0) {
width = fwidth / yscale;
fscale = 1.0 / yscale;
for(i = 0; i < dst->ysize; ++i) {
contrib[i].n = 0;
contrib[i].p = (CONTRIB *)my_calloc((int) (width * 2 + 1),
sizeof(CONTRIB));
center = (double) i / yscale;
left = ceil(center - width);
right = floor(center + width);
for(j = left; j <= right; ++j) {
weight = center - (double) j;
weight = (*filterf)(weight / fscale) / fscale;
if(j < 0) {
n = -j;
} else if(j >= tmp->ysize) {
n = (tmp->ysize - j) + tmp->ysize - 1;
} else {
n = j;
}
k = contrib[i].n++;
contrib[i].p[k].pixel = n;
contrib[i].p[k].weight = weight;
}
}
} else {
for(i = 0; i < dst->ysize; ++i) {
contrib[i].n = 0;
contrib[i].p = (CONTRIB *)my_calloc((int) (fwidth * 2 + 1),
sizeof(CONTRIB));
center = (double) i / yscale;
left = ceil(center - fwidth);
right = floor(center + fwidth);
for(j = left; j <= right; ++j) {
weight = center - (double) j;
weight = (*filterf)(weight);
if(j < 0) {
n = -j;
} else if(j >= tmp->ysize) {
n = (tmp->ysize - j) + tmp->ysize - 1;
} else {
n = j;
}
k = contrib[i].n++;
contrib[i].p[k].pixel = n;
contrib[i].p[k].weight = weight;
}
}
}
/* apply filter to zoom vertically from tmp to dst */
raster = (Pixel *)my_calloc(tmp->ysize, sizeof(Pixel));
for(k = 0; k < dst->xsize; ++k) {
get_column(raster, tmp, k);
for(i = 0; i < dst->ysize; ++i) {
red = green = blue = 0.0;
for(j = 0; j < contrib[i].n; ++j) {
red += raster[contrib[i].p[j].pixel].red
* contrib[i].p[j].weight;
green += raster[contrib[i].p[j].pixel].green
* contrib[i].p[j].weight;
blue += raster[contrib[i].p[j].pixel].blue
* contrib[i].p[j].weight;
}
tmppixel.red = CLAMP(red, BLACK_PIXEL, WHITE_PIXEL);
tmppixel.green = CLAMP(green, BLACK_PIXEL, WHITE_PIXEL);
tmppixel.blue = CLAMP(blue, BLACK_PIXEL, WHITE_PIXEL);
put_pixel(dst, k, i, tmppixel);
}
}
my_free(raster);
/* free the memory allocated for vertical filter weights */
for(i = 0; i < dst->ysize; ++i) {
my_free(contrib[i].p);
}
my_free(contrib);
free_image(tmp);
}
/*
* interface to tga2cry program
*/
Pixel *
rescale(Pixel *oldpix, unsigned old_w, unsigned old_h, unsigned new_w, unsigned new_h, int filter_type, int aspect)
{
Image oldimage, newimage;
Pixel *newpix;
double delta, fwidth;
unsigned vert_border, horiz_border;
double (*filterf)();
newpix = my_calloc(new_w*(size_t)new_h, sizeof(Pixel));
if (!newpix) return 0;
oldimage.span = (long)old_w;
oldimage.xsize = (int)oldimage.span;
oldimage.ysize = old_h;
oldimage.data = oldpix;
newimage.span = new_w;
newimage.data = newpix;
/*
* figure out the proper new width and height to preserve aspect ratios
* first, we'll try scaling by width (leaving a border at the bottom)
*/
delta = (double)new_w/(double)old_w;
vert_border = delta*old_h;
if (vert_border > new_h) {
/* this is no good! we'll have to scale by height and leave borders
* at the sides
*/
delta = (double)new_h/(double)old_h;
horiz_border = delta*old_w;
vert_border = new_h;
} else {
horiz_border = new_w;
}
if (aspect) {
newimage.xsize = horiz_border;
newimage.ysize = vert_border;
/* center the output */
newimage.data = newpix + ((new_w*(new_h - vert_border)/2) + (new_w - horiz_border)/2);
} else {
newimage.xsize = new_w;
newimage.ysize = new_h;
}
/*
* pick a filter type
*/
switch(filter_type) {
case FILTER_BOX:
filterf = box_filter;
fwidth = box_support;
break;
case FILTER_BELL:
filterf = bell_filter;
fwidth = bell_support;
break;
case FILTER_LANC:
filterf = Lanczos3_filter;
fwidth = Lanczos3_support;
break;
case FILTER_MITCH:
default:
filterf = Mitchell_filter;
fwidth = Mitchell_support;
break;
case FILTER_SINC:
filterf = Sinc_filter;
fwidth = Sinc_support;
break;
case FILTER_TRI:
filterf = triangle_filter;
fwidth = triangle_support;
break;
}
zoom(&newimage, &oldimage, filterf, fwidth);
return newpix;
}
/*
* crop an input image to a specified window
*/
Pixel *
crop(Pixel *oldpix, unsigned image_w, unsigned image_h, unsigned crop_x, unsigned crop_y, unsigned crop_w, unsigned crop_h)
{
unsigned x, y;
Pixel *inptr, *outptr;
Pixel *newpix;
newpix = (Pixel *)my_calloc(crop_w * (size_t)crop_h, sizeof(Pixel));
if (!newpix) {
fprintf(stderr, "ERROR: insufficient memory for cropping picture\n");
exit(1);
}
crop_w += crop_x; /* move to lower left hand corner */
crop_h += crop_y;
inptr = oldpix + crop_y*((long)image_w);
outptr = newpix;
for (y = crop_y; y < crop_h; y++) {
for (x = 0; x < image_w; x++) {
if (x >= crop_x && x < crop_w)
*outptr++ = *inptr;
inptr++;
}
}
return newpix;
}