-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmbilinearcli.c
227 lines (193 loc) · 6.34 KB
/
dmbilinearcli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* @file dmbilinearcli.c
* @brief Bilinear demosaicing command line program
* @author Pascal Getreuer <getreuer@gmail.com>
*
*
* Copyright (c) 2010-2011, Pascal Getreuer
* All rights reserved.
*
* This program is free software: you can use, modify and/or
* redistribute it under the terms of the simplified BSD License. You
* should have received a copy of this license along this program. If
* not, see <http://www.opensource.org/licenses/bsd-license.html>.
*/
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "imageio.h"
#include "dmbilinear.h"
/** @brief struct of program parameters */
typedef struct
{
/** @brief Input file name */
char *InputFile;
/** @brief Output file name */
char *OutputFile;
/** @brief Quality for saving JPEG images (0 to 100) */
int JpegQuality;
/** @brief CFA pattern upperleftmost red pixel x-coordinate */
int RedX;
/** @brief CFA pattern upperleftmost red pixel y-coordinate */
int RedY;
} programparams;
static int ParseParams(programparams *Param, int argc, char *argv[]);
/** @brief Print program usage help message */
static void PrintHelpMessage()
{
printf("Bilinear demosaicing demo, P. Getreuer 2010-2011\n\n");
printf("Usage: dmbilinear [options] <input file> <output file>\n\n"
"Only " READIMAGE_FORMATS_SUPPORTED " images are supported.\n\n");
printf("Options:\n");
printf(" -p <pattern> CFA pattern, choices for <pattern> are\n");
printf(" RGGB upperleftmost red pixel is at (0,0)\n");
printf(" GRBG upperleftmost red pixel is at (1,0)\n");
printf(" GBRG upperleftmost red pixel is at (0,1)\n");
printf(" BGGR upperleftmost red pixel is at (1,1)\n");
#ifdef LIBJPEG_SUPPORT
printf(" -q <number> Quality for saving JPEG images (0 to 100)\n\n");
#endif
printf("Example: \n"
" dmbilinear -p RGGB frog.bmp frog-dm.bmp\n");
}
int main(int argc, char *argv[])
{
programparams Param;
float *Input = NULL, *Output = NULL;
unsigned long StartTime;
int Width, Height, Status = 1;
if(!ParseParams(&Param, argc, argv))
return 0;
/* Read the input image */
if(!(Input = (float *)ReadImage(&Width, &Height,
Param.InputFile, IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR)))
goto Catch;
if(Width < 2 || Height < 2)
{
ErrorMessage("Image is too small (%dx%d).\n", Width, Height);
goto Catch;
}
if(!(Output = (float *)Malloc(sizeof(float)*3*
((long int)Width)*((long int)Height))))
goto Catch;
/* Start the timer */
StartTime = Clock();
/* Flatten the input to a 2D array */
CfaFlatten(Input, Input, Width, Height, Param.RedX, Param.RedY);
/* Perform demosaicing */
BilinearDemosaic(Output, Input, Width, Height, Param.RedX, Param.RedY);
/* Print the time it took to perform the demosaicking */
printf("CPU Time: %.3f s\n", 0.001f*(Clock() - StartTime));
/* Write the output image */
if(!WriteImage(Output, Width, Height, Param.OutputFile,
IMAGEIO_FLOAT | IMAGEIO_RGB | IMAGEIO_PLANAR, Param.JpegQuality))
goto Catch;
Status = 0; /* Finished successfully, set exit status to zero. */
Catch:
Free(Output);
Free(Input);
return Status;
}
static int ParseParams(programparams *Param, int argc, char *argv[])
{
static char *DefaultOutputFile = (char *)"out.bmp";
char *OptionString;
char OptionChar;
int i;
if(argc < 2)
{
PrintHelpMessage();
return 0;
}
/* Set parameter defaults */
Param->InputFile = 0;
Param->OutputFile = DefaultOutputFile;
Param->JpegQuality = 80;
Param->RedX = 0;
Param->RedY = 0;
for(i = 1; i < argc;)
{
if(argv[i] && argv[i][0] == '-')
{
if((OptionChar = argv[i][1]) == 0)
{
ErrorMessage("Invalid parameter format.\n");
return 0;
}
if(argv[i][2])
OptionString = &argv[i][2];
else if(++i < argc)
OptionString = argv[i];
else
{
ErrorMessage("Invalid parameter format.\n");
return 0;
}
switch(OptionChar)
{
case 'p':
if(!strcmp(OptionString, "RGGB")
|| !strcmp(OptionString, "rggb"))
{
Param->RedX = 0;
Param->RedY = 0;
}
else if(!strcmp(OptionString, "GRBG")
|| !strcmp(OptionString, "grbg"))
{
Param->RedX = 1;
Param->RedY = 0;
}
else if(!strcmp(OptionString, "GBRG")
|| !strcmp(OptionString, "gbrg"))
{
Param->RedX = 0;
Param->RedY = 1;
}
else if(!strcmp(OptionString, "BGGR")
|| !strcmp(OptionString, "bggr"))
{
Param->RedX = 1;
Param->RedY = 1;
}
else
ErrorMessage("CFA pattern must be RGGB, GRBG, GBRG, or BGGR\n");
break;
#ifdef LIBJPEG_SUPPORT
case 'q':
Param->JpegQuality = atoi(OptionString);
if(Param->JpegQuality <= 0 || Param->JpegQuality > 100)
{
ErrorMessage("JPEG quality must be between 0 and 100.\n");
return 0;
}
break;
#endif
case '-':
PrintHelpMessage();
return 0;
default:
if(isprint(OptionChar))
ErrorMessage("Unknown option \"-%c\".\n", OptionChar);
else
ErrorMessage("Unknown option.\n");
return 0;
}
i++;
}
else
{
if(!Param->InputFile)
Param->InputFile = argv[i];
else
Param->OutputFile = argv[i];
i++;
}
}
if(!Param->InputFile)
{
PrintHelpMessage();
return 0;
}
return 1;
}