78 static int ParseScaling(
programparams *Param,
int InputWidth,
int InputHeight);
82 printf(
"Linear interpolation demo, P. Getreuer 2010-2011\n\n");
83 printf(
"Usage: linterp [options] <input file> <output file>\n\n"
86 printf(
" -m <method> interpolation method to apply, choices for <method> are\n");
87 printf(
" nearest nearest neighbor (pixel duplication)\n");
88 printf(
" bilinear standard bilinear interpolation\n");
89 printf(
" bicubic Keys bicubic with parameter -0.5\n");
90 printf(
" lanczosN Lanczos radius-N sinc approximation,\n");
91 printf(
" N = 2, 3, 4\n");
92 printf(
" bsplineN B-spline of degree N,\n");
93 printf(
" N = 2, 3, 5, 7, 9, 11\n");
94 printf(
" omomsN o-Moms of degree N,\n");
95 printf(
" N = 3, 5, 7\n");
96 printf(
" fourier Fourier zero-padding (sinc)\n\n");
97 printf(
" -x <scale> the scale factor (may be non-integer)\n");
98 printf(
" -x <x-scale>,<y-scale> set horizontal and vertical scale factors\n");
99 printf(
" -x <width>x<height> set maximum interpolated size in pixels, \n");
100 printf(
" preserves aspect ratio\n");
101 printf(
" -x <width>x<height>^ set minimum interpolated size in pixels, \n");
102 printf(
" preserves aspect ratio\n");
103 printf(
" -x <width>x<height>! set actual interpolated size in pixels, \n");
104 printf(
" ignores aspect ratio\n\n");
105 printf(
" -r <number> rotation, counter clockwise in degrees\n");
106 printf(
" (if specified, preserves aspect ratio regardless of -x)\n");
107 printf(
" -p <number> sigma_h, the blur size of the point spread function\n");
108 printf(
" -b <ext> extension to use for boundary handling, choices for <ext> are\n");
109 printf(
" const constant extension\n");
110 printf(
" hsym half-sample symmetric\n");
111 printf(
" wsym whole-sample symmetric\n");
112 printf(
" -g <grid> grid to use for resampling, choices for <grid> are\n"
113 " centered grid with centered alignment (default)\n"
114 " topleft the top-left anchored grid\n\n");
116 printf(
" -q <number> quality for saving JPEG images (0 to 100)\n\n");
118 printf(
"Example: 4.5x cubic B-spline scaling, sigma_h = 0.35\n"
119 " linterp -m bspline3 -x 4.5 -p 0.35 frog.bmp interpolation.bmp\n");
125 float Sigma = *((
float *)Param);
126 return exp(-(x*x)/(2*Sigma*Sigma)) / (sqrt(
M_2PI)*Sigma);
135 float *Coeff = NULL, *X = NULL, *Y = NULL;
136 float XStart, XStep, YStart, YStep, Theta;
137 int NumCoeffs, Channel;
142 fprintf(stderr,
"Unknown interpolation method.\n");
155 "PSF prefiltering only supported for half- and whole-sample symmetric\n"
156 "boundary extension.\n");
162 if(!(Coeff = (
float *)
Malloc(
sizeof(
float)*NumCoeffs)))
164 fprintf(stderr,
"Memory allocation failed.\n");
169 printf(
"PSF prefiltering\n");
180 printf(
"Prefiltering\n");
192 printf(
"Scaling %dx%d -> %dx%d\n",
204 XStart = (XStep - 1.0)/2;
205 YStart = (YStep - 1.0)/2;
228 printf(
"Scaling and rotating %dx%d -> %dx%d\n",
235 for(Channel = 0; Channel < 3; Channel++)
259 int main(
int argc,
char *argv[])
262 imagef v = {NULL, 0, 0}, u = {NULL, 0, 0};
263 unsigned long StartTime;
264 float XStart, YStart;
276 fprintf(stderr,
"Image is too small (%dx%d).\n", v.
Width, v.
Height);
285 if(!strcmp(Param.
Method,
"fourier") || !strcmp(Param.
Method,
"sinc"))
289 fprintf(stderr,
"Rotation is not supported with Fourier interpolation.\n");
296 "Fourier interpolation is only supported for half- and whole-sample\n"
297 "symmetric boundary extension.\n");
305 printf(
"Fourier scaling %dx%d -> %dx%d\n",
309 if(!(u.Data = (
float *)
Malloc(
sizeof(
float)*3*u.Width*u.Height)))
314 XStart = v.
Width/(2.0f*u.Width) - 0.5f;
315 YStart = v.
Height/(2.0f*u.Height) - 0.5f;
329 fprintf(stderr,
"Error in computation.\n");
333 printf(
"CPU Time: %.3f\n", (
Clock() - StartTime)*0.001f);
338 fprintf(stderr,
"Error writing output file.\n");
349 static char *DefaultOutputFile = (
char *)
"out.bmp";
368 Param->
Method = (
char *)
"bspline3";
372 for(i = 1; i < argc;)
374 if(argv[i] && argv[i][0] ==
'-')
376 if((OptionChar = argv[i][1]) == 0)
378 fprintf(stderr,
"Invalid parameter format.\n");
383 OptionString = &argv[i][2];
385 OptionString = argv[i];
388 fprintf(stderr,
"Invalid parameter format.\n");
395 if(!strcmp(OptionString,
"centered")
396 || !strcmp(OptionString,
"center"))
398 else if(!strcmp(OptionString,
"topleft")
399 || !strcmp(OptionString,
"top-left"))
403 fprintf(stderr,
"Grid must be either \"centered\" or \"topleft\".\n");
408 if(!strcmp(OptionString,
"const"))
410 else if(!strcmp(OptionString,
"hsym"))
412 else if(!strcmp(OptionString,
"wsym"))
416 fprintf(stderr,
"Invalid boundary extension.\n");
424 Param->
Rotation = atof(OptionString);
427 Param->
Method = OptionString;
430 Param->
PsfSigma = atof(OptionString);
434 fprintf(stderr,
"Point spread blur size must be nonnegative.\n");
444 fprintf(stderr,
"JPEG quality must be between 0 and 100.\n");
453 if(isprint(OptionChar))
454 fprintf(stderr,
"Unknown option \"-%c\".\n", OptionChar);
456 fprintf(stderr,
"Unknown option.\n");
481 printf(
"Interpolation with %s\n", Param->
Method);
496 static int ParseScaling(
programparams *Param,
int InputWidth,
int InputHeight)
498 const char *StrPtr = Param->
ScaleStr;
510 printf(
"Scaling by %g (%dx%d to %dx%d)\n", Param->
ScaleX,
511 InputWidth, InputHeight,
515 else if(*StrPtr ==
',')
527 printf(
"Scaling by %g,%g (%dx%d to %dx%d)\n",
529 InputWidth, InputHeight,
533 else if(*StrPtr ==
'x' || *StrPtr ==
'X')
539 || !(*StrPtr ==
'x' || *StrPtr ==
'X'))
561 (int)floor(Param->
ScaleY*InputHeight + 0.5);
563 printf(
"Scaling %dx%d to %dx(%d) (preserving aspect ratio)\n",
564 InputWidth, InputHeight,
572 (int)floor(Param->
ScaleX*InputWidth + 0.5);
574 printf(
"Scaling %dx%d to (%d)x%d (preserving aspect ratio)\n",
575 InputWidth, InputHeight,
586 (int)floor(Param->
ScaleY*InputHeight + 0.5);
588 printf(
"Scaling %dx%d to %dx(%d)^ (preserving aspect ratio)\n",
589 InputWidth, InputHeight,
597 (int)floor(Param->
ScaleX*InputWidth + 0.5);
599 printf(
"Scaling %dx%d to (%d)x%d^ (preserving aspect ratio)\n",
600 InputWidth, InputHeight,
607 printf(
"Scaling %dx%d to %dx%d!\n",
608 InputWidth, InputHeight,