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