Zhang-Wu Directional LMMSE Image Demosaicking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
dmbilinearcli.c
Go to the documentation of this file.
1 
16 #include <math.h>
17 #include <string.h>
18 #include <ctype.h>
19 
20 #include "imageio.h"
21 #include "dmbilinear.h"
22 
23 
25 typedef struct
26 {
28  char *InputFile;
30  char *OutputFile;
34  int RedX;
36  int RedY;
38 
39 
40 static int ParseParams(programparams *Param, int argc, char *argv[]);
41 
42 
44 static void PrintHelpMessage()
45 {
46  puts("Bilinear demosaicing demo, P. Getreuer 2010-2011\n");
47  puts("Usage: dmbilinear [options] <input file> <output file>\n"
48  "Only " READIMAGE_FORMATS_SUPPORTED " images are supported.\n");
49  puts("Options:");
50  puts(" -p <pattern> CFA pattern, choices for <pattern> are");
51  puts(" RGGB upperleftmost red pixel is at (0,0)");
52  puts(" GRBG upperleftmost red pixel is at (1,0)");
53  puts(" GBRG upperleftmost red pixel is at (0,1)");
54  puts(" BGGR upperleftmost red pixel is at (1,1)");
55 #ifdef USE_LIBJPEG
56  puts(" -q <number> Quality for saving JPEG images (0 to 100)\n");
57 #endif
58  puts("Example: \n"
59  " dmbilinear -p RGGB frog.bmp frog-dm.bmp");
60 }
61 
62 
63 int main(int argc, char *argv[])
64 {
65  programparams Param;
66  float *Input = NULL, *Output = NULL;
67  int Width, Height, Status = 1;
68 
69 
70  if(!ParseParams(&Param, argc, argv))
71  return 0;
72 
73  /* Read the input image */
74  if(!(Input = (float *)ReadImage(&Width, &Height,
76  goto Catch;
77 
78  if(Width < 2 || Height < 2)
79  {
80  ErrorMessage("Image is too small (%dx%d).\n", Width, Height);
81  goto Catch;
82  }
83 
84  if(!(Output = (float *)Malloc(sizeof(float)*3*
85  ((long int)Width)*((long int)Height))))
86  goto Catch;
87 
88  /* Flatten the input to a 2D array */
89  CfaFlatten(Input, Input, Width, Height, Param.RedX, Param.RedY);
90  /* Perform demosaicing */
91  BilinearDemosaic(Output, Input, Width, Height, Param.RedX, Param.RedY);
92 
93  /* Write the output image */
94  if(!WriteImage(Output, Width, Height, Param.OutputFile,
96  goto Catch;
97 
98  Status = 0; /* Finished successfully, set exit status to zero. */
99 Catch:
100  Free(Output);
101  Free(Input);
102  return Status;
103 }
104 
105 
106 static int ParseParams(programparams *Param, int argc, char *argv[])
107 {
108  static char *DefaultOutputFile = (char *)"out.bmp";
109  char *OptionString;
110  char OptionChar;
111  int i;
112 
113 
114  if(argc < 2)
115  {
117  return 0;
118  }
119 
120  /* Set parameter defaults */
121  Param->InputFile = 0;
122  Param->OutputFile = DefaultOutputFile;
123  Param->JpegQuality = 80;
124  Param->RedX = 0;
125  Param->RedY = 0;
126 
127  for(i = 1; i < argc;)
128  {
129  if(argv[i] && argv[i][0] == '-')
130  {
131  if((OptionChar = argv[i][1]) == 0)
132  {
133  ErrorMessage("Invalid parameter format.\n");
134  return 0;
135  }
136 
137  if(argv[i][2])
138  OptionString = &argv[i][2];
139  else if(++i < argc)
140  OptionString = argv[i];
141  else
142  {
143  ErrorMessage("Invalid parameter format.\n");
144  return 0;
145  }
146 
147  switch(OptionChar)
148  {
149  case 'p':
150  if(!strcmp(OptionString, "RGGB")
151  || !strcmp(OptionString, "rggb"))
152  {
153  Param->RedX = 0;
154  Param->RedY = 0;
155  }
156  else if(!strcmp(OptionString, "GRBG")
157  || !strcmp(OptionString, "grbg"))
158  {
159  Param->RedX = 1;
160  Param->RedY = 0;
161  }
162  else if(!strcmp(OptionString, "GBRG")
163  || !strcmp(OptionString, "gbrg"))
164  {
165  Param->RedX = 0;
166  Param->RedY = 1;
167  }
168  else if(!strcmp(OptionString, "BGGR")
169  || !strcmp(OptionString, "bggr"))
170  {
171  Param->RedX = 1;
172  Param->RedY = 1;
173  }
174  else
175  ErrorMessage("CFA pattern must be RGGB, GRBG, GBRG, or BGGR\n");
176  break;
177 #ifdef USE_LIBJPEG
178  case 'q':
179  Param->JpegQuality = atoi(OptionString);
180 
181  if(Param->JpegQuality <= 0 || Param->JpegQuality > 100)
182  {
183  ErrorMessage("JPEG quality must be between 0 and 100.\n");
184  return 0;
185  }
186  break;
187 #endif
188  case '-':
190  return 0;
191  default:
192  if(isprint(OptionChar))
193  ErrorMessage("Unknown option \"-%c\".\n", OptionChar);
194  else
195  ErrorMessage("Unknown option.\n");
196 
197  return 0;
198  }
199 
200  i++;
201  }
202  else
203  {
204  if(!Param->InputFile)
205  Param->InputFile = argv[i];
206  else
207  Param->OutputFile = argv[i];
208 
209  i++;
210  }
211  }
212 
213  if(!Param->InputFile)
214  {
216  return 0;
217  }
218 
219  return 1;
220 }