Malvar-He-Cutler Linear Image Demosaicking
|
00001 00016 #include <math.h> 00017 #include <string.h> 00018 #include <ctype.h> 00019 00020 #include "imageio.h" 00021 #include "dmbilinear.h" 00022 00023 00025 typedef struct 00026 { 00028 char *InputFile; 00030 char *OutputFile; 00032 int JpegQuality; 00034 int RedX; 00036 int RedY; 00037 } programparams; 00038 00039 00040 static int ParseParams(programparams *Param, int argc, char *argv[]); 00041 00042 00044 static void PrintHelpMessage() 00045 { 00046 printf("Bilinear demosaicing demo, P. Getreuer 2010-2011\n\n"); 00047 printf("Usage: dmbilinear [options] <input file> <output file>\n\n" 00048 "Only " READIMAGE_FORMATS_SUPPORTED " images are supported.\n\n"); 00049 printf("Options:\n"); 00050 printf(" -p <pattern> CFA pattern, choices for <pattern> are\n"); 00051 printf(" RGGB upperleftmost red pixel is at (0,0)\n"); 00052 printf(" GRBG upperleftmost red pixel is at (1,0)\n"); 00053 printf(" GBRG upperleftmost red pixel is at (0,1)\n"); 00054 printf(" BGGR upperleftmost red pixel is at (1,1)\n"); 00055 #ifdef LIBJPEG_SUPPORT 00056 printf(" -q <number> Quality for saving JPEG images (0 to 100)\n\n"); 00057 #endif 00058 printf("Example: \n" 00059 " dmbilinear -p RGGB frog.bmp frog-dm.bmp\n"); 00060 } 00061 00062 00063 int main(int argc, char *argv[]) 00064 { 00065 programparams Param; 00066 float *Input = NULL, *Output = NULL; 00067 int Width, Height, Status = 1; 00068 00069 00070 if(!ParseParams(&Param, argc, argv)) 00071 return 0; 00072 00073 /* Read the input image */ 00074 if(!(Input = (float *)ReadImage(&Width, &Height, 00075 Param.InputFile, IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR))) 00076 goto Catch; 00077 00078 if(Width < 2 || Height < 2) 00079 { 00080 ErrorMessage("Image is too small (%dx%d).\n", Width, Height); 00081 goto Catch; 00082 } 00083 00084 if(!(Output = (float *)Malloc(sizeof(float)*3* 00085 ((long int)Width)*((long int)Height)))) 00086 goto Catch; 00087 00088 /* Flatten the input to a 2D array */ 00089 CfaFlatten(Input, Input, Width, Height, Param.RedX, Param.RedY); 00090 /* Perform demosaicing */ 00091 BilinearDemosaic(Output, Input, Width, Height, Param.RedX, Param.RedY); 00092 00093 /* Write the output image */ 00094 if(!WriteImage(Output, Width, Height, Param.OutputFile, 00095 IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR, Param.JpegQuality)) 00096 goto Catch; 00097 00098 Status = 0; /* Finished successfully, set exit status to zero. */ 00099 Catch: 00100 Free(Output); 00101 Free(Input); 00102 return Status; 00103 } 00104 00105 00106 static int ParseParams(programparams *Param, int argc, char *argv[]) 00107 { 00108 static char *DefaultOutputFile = (char *)"out.bmp"; 00109 char *OptionString; 00110 char OptionChar; 00111 int i; 00112 00113 00114 if(argc < 2) 00115 { 00116 PrintHelpMessage(); 00117 return 0; 00118 } 00119 00120 /* Set parameter defaults */ 00121 Param->InputFile = 0; 00122 Param->OutputFile = DefaultOutputFile; 00123 Param->JpegQuality = 80; 00124 Param->RedX = 0; 00125 Param->RedY = 0; 00126 00127 for(i = 1; i < argc;) 00128 { 00129 if(argv[i] && argv[i][0] == '-') 00130 { 00131 if((OptionChar = argv[i][1]) == 0) 00132 { 00133 ErrorMessage("Invalid parameter format.\n"); 00134 return 0; 00135 } 00136 00137 if(argv[i][2]) 00138 OptionString = &argv[i][2]; 00139 else if(++i < argc) 00140 OptionString = argv[i]; 00141 else 00142 { 00143 ErrorMessage("Invalid parameter format.\n"); 00144 return 0; 00145 } 00146 00147 switch(OptionChar) 00148 { 00149 case 'p': 00150 if(!strcmp(OptionString, "RGGB") 00151 || !strcmp(OptionString, "rggb")) 00152 { 00153 Param->RedX = 0; 00154 Param->RedY = 0; 00155 } 00156 else if(!strcmp(OptionString, "GRBG") 00157 || !strcmp(OptionString, "grbg")) 00158 { 00159 Param->RedX = 1; 00160 Param->RedY = 0; 00161 } 00162 else if(!strcmp(OptionString, "GBRG") 00163 || !strcmp(OptionString, "gbrg")) 00164 { 00165 Param->RedX = 0; 00166 Param->RedY = 1; 00167 } 00168 else if(!strcmp(OptionString, "BGGR") 00169 || !strcmp(OptionString, "bggr")) 00170 { 00171 Param->RedX = 1; 00172 Param->RedY = 1; 00173 } 00174 else 00175 ErrorMessage("CFA pattern must be RGGB, GRBG, GBRG, or BGGR\n"); 00176 break; 00177 #ifdef LIBJPEG_SUPPORT 00178 case 'q': 00179 Param->JpegQuality = atoi(OptionString); 00180 00181 if(Param->JpegQuality <= 0 || Param->JpegQuality > 100) 00182 { 00183 ErrorMessage("JPEG quality must be between 0 and 100.\n"); 00184 return 0; 00185 } 00186 break; 00187 #endif 00188 case '-': 00189 PrintHelpMessage(); 00190 return 0; 00191 default: 00192 if(isprint(OptionChar)) 00193 ErrorMessage("Unknown option \"-%c\".\n", OptionChar); 00194 else 00195 ErrorMessage("Unknown option.\n"); 00196 00197 return 0; 00198 } 00199 00200 i++; 00201 } 00202 else 00203 { 00204 if(!Param->InputFile) 00205 Param->InputFile = argv[i]; 00206 else 00207 Param->OutputFile = argv[i]; 00208 00209 i++; 00210 } 00211 } 00212 00213 if(!Param->InputFile) 00214 { 00215 PrintHelpMessage(); 00216 return 0; 00217 } 00218 00219 return 1; 00220 }