Roussos-Maragos Tensor-Driven Diffusion Interpolation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
nninterpcli.c
Go to the documentation of this file.
1 
16 #include <math.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include "nninterp.h"
20 
21 #define VERBOSE 0
22 
23 
25 typedef struct
26 {
28  uint32_t *Data;
30  int Width;
32  int Height;
33 } image;
34 
35 
37 typedef struct
38 {
40  char *InputFile;
42  char *OutputFile;
44  int JpegQuality;
46  int CenteredGrid;
48  float ScaleFactor;
50 
51 
52 int ParseParams(programparams *Param, int argc, char *argv[]);
53 
54 
57 {
58  puts("Nearest neighbor interpolation utility, P. Getreuer 2011\n");
59  puts("Usage: nninterp [options] <input file> <output file>\n"
60  "Only " READIMAGE_FORMATS_SUPPORTED " images are supported.\n");
61  puts("Options:\n");
62  puts(" -x <number> the interpolation factor\n");
63  puts(
64  " -g <grid> grid to use for resampling, choices for <grid> are\n"
65  " centered grid with centered alignment (default)\n"
66  " topleft the top-left anchored grid\n");
67 #ifdef USE_LIBJPEG
68  puts(" -q <number> quality for saving JPEG images (0 to 100)\n");
69 #endif
70  puts("Example: interpolate by factor 2.5\n"
71  " nninterp -x 2.5 frog.bmp coarse.bmp");
72 }
73 
74 
75 int main(int argc, char *argv[])
76 {
77  programparams Param;
78  image u = {NULL, 0, 0}, v = {NULL, 0, 0};
79  int Status = 1;
80 
81 
82  if(!ParseParams(&Param, argc, argv))
83  return 0;
84 
85  /* Read the input image */
86  if(!(v.Data = (uint32_t *)ReadImage(&v.Width, &v.Height, Param.InputFile,
88  goto Catch;
89 
90  /* Allocate the output image */
91  u.Width = (int)ceil(v.Width * Param.ScaleFactor);
92  u.Height = (int)ceil(v.Height * Param.ScaleFactor);
93 #if VERBOSE > 0
94  printf("%dx%d input --> %dx%d output\n", v.Width, v.Height, u.Width, u.Height);
95 #endif
96 
97  if(!(u.Data = (uint32_t *)Malloc(sizeof(uint32_t)*
98  ((long int)u.Width)*((long int)u.Height))))
99  {
100  fprintf(stderr, "Memory allocation failed.\n");
101  goto Catch;
102  }
103 
104  NearestInterp(u.Data, u.Width, u.Height,
105  v.Data, v.Width, v.Height,
106  Param.ScaleFactor, Param.CenteredGrid);
107 
108  /* Write the output image */
109  if(!WriteImage(u.Data, u.Width, u.Height, Param.OutputFile,
111  goto Catch;
112 #if VERBOSE > 0
113  else
114  printf("Output written to \"%s\".\n", Param.OutputFile);
115 #endif
116 
117  Status = 0; /* Finished successfully, set exit status to zero. */
118 Catch:
119  Free(u.Data);
120  Free(v.Data);
121  return Status;
122 }
123 
124 
125 int ParseParams(programparams *Param, int argc, char *argv[])
126 {
127  static char *DefaultOutputFile = (char *)"out.bmp";
128  char *OptionString;
129  char OptionChar;
130  int i;
131 
132 
133  if(argc < 2)
134  {
136  return 0;
137  }
138 
139  /* Set parameter defaults */
140  Param->InputFile = 0;
141  Param->OutputFile = DefaultOutputFile;
142  Param->JpegQuality = 99;
143  Param->ScaleFactor = 1;
144  Param->CenteredGrid = 1;
145 
146  for(i = 1; i < argc;)
147  {
148  if(argv[i] && argv[i][0] == '-')
149  {
150  if((OptionChar = argv[i][1]) == 0)
151  {
152  ErrorMessage("Invalid parameter format.\n");
153  return 0;
154  }
155 
156  if(argv[i][2])
157  OptionString = &argv[i][2];
158  else if(++i < argc)
159  OptionString = argv[i];
160  else
161  {
162  ErrorMessage("Invalid parameter format.\n");
163  return 0;
164  }
165 
166  switch(OptionChar)
167  {
168  case 'x':
169  Param->ScaleFactor = (float)atof(OptionString);
170 
171  if(Param->ScaleFactor < 0)
172  {
173  ErrorMessage("Invalid scale factor.\n");
174  return 0;
175  }
176  break;
177  case 'g':
178  if(!strcmp(OptionString, "centered")
179  || !strcmp(OptionString, "center"))
180  Param->CenteredGrid = 1;
181  else if(!strcmp(OptionString, "topleft")
182  || !strcmp(OptionString, "top-left"))
183  Param->CenteredGrid = 0;
184  else
185  {
186  ErrorMessage("Grid must be either \"centered\" or \"topleft\".\n");
187  return 0;
188  }
189  break;
190 
191 #ifdef USE_LIBJPEG
192  case 'q':
193  Param->JpegQuality = atoi(OptionString);
194 
195  if(Param->JpegQuality <= 0 || Param->JpegQuality > 100)
196  {
197  ErrorMessage("JPEG quality must be between 0 and 100.\n");
198  return 0;
199  }
200  break;
201 #endif
202  case '-':
204  return 0;
205  default:
206  if(isprint(OptionChar))
207  ErrorMessage("Unknown option \"-%c\".\n", OptionChar);
208  else
209  ErrorMessage("Unknown option.\n");
210 
211  return 0;
212  }
213 
214  i++;
215  }
216  else
217  {
218  if(!Param->InputFile)
219  Param->InputFile = argv[i];
220  else
221  Param->OutputFile = argv[i];
222 
223  i++;
224  }
225  }
226 
227  if(!Param->InputFile)
228  {
230  return 0;
231  }
232 
233  return 1;
234 }