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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* -*- C++ -*-
* File: dcraw_emu.cpp
* Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru>
* Created: Sun Mar 23, 2008
*
* LibRaw simple C++ API (almost complete dcraw emulator)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#ifndef WIN32
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif
#include "libraw/libraw.h"
#ifdef WIN32
#define snprintf _snprintf
#endif
void usage(const char *prog)
{
printf("dcraw_emu: almost complete dcraw emulator\n");
printf("Usage: %s [OPTION]... [FILE]...\n", prog);
printf(
"-v Verbose: print progress messages (repeated -v will add verbosity)\n"
"-w Use camera white balance, if possible\n"
"-a Average the whole image for white balance\n"
"-A <x y w h> Average a grey box for white balance\n"
"-r <r g b g> Set custom white balance\n"
"+M/-M Use/don't use an embedded color matrix\n"
"-C <r b> Correct chromatic aberration\n"
"-P <file> Fix the dead pixels listed in this file\n"
"-K <file> Subtract dark frame (16-bit raw PGM)\n"
"-k <num> Set the darkness level\n"
"-S <num> Set the saturation level\n"
"-n <num> Set threshold for wavelet denoising\n"
"-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)\n"
"-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)\n"
"-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)\n"
#ifndef NO_LCMS
"-o file Output ICC profile\n"
"-p file Camera input profile (use \'embed\' for embedded profile)\n"
#endif
"-j Don't stretch or rotate raw pixels\n"
"-W Don't automatically brighten the image\n"
"-b <num> Adjust brightness (default = 1.0)\n"
"-q [0-3] Set the interpolation quality\n"
"-h Half-size color image (twice as fast as \"-q 0\")\n"
"-f Interpolate RGGB as four colors\n"
"-m <num> Apply a 3x3 median filter to R-G and B-G\n"
"-s [0..N-1] Select one raw image from input file\n"
"-4 Write 16-bit linear instead of 8-bit with gamma\n"
"-g pow ts Set gamma curve to gamma pow and toe slope ts (default = 2.222 4.5)\n"
"-T Write TIFF instead of PPM\n"
#ifndef WIN32
"-B Use mmap()-ed buffer instead of plain FILE I/O\n"
#endif
);
exit(1);
}
static int verbosity=0;
int cnt=0;
int my_progress_callback(void *d,enum LibRaw_progress p,int iteration, int expected)
{
char *passed = (char*)(d?d:"default string"); // data passed to callback at set_callback stage
if(verbosity>2) // verbosity set by repeat -v switches
{
printf("CB: %s pass %d of %d (data passed=%s)\n",libraw_strprogress(p),iteration,expected,passed);
}
else if (iteration == 0) // 1st iteration of each step
printf("Starting %s (expecting %d iterations)\n", libraw_strprogress(p),expected);
else if (iteration == expected-1)
printf("%s finished\n",libraw_strprogress(p));
/// if(++cnt>10) return 1; // emulate user termination on 10-th callback call
return 0; // always return 0 to continue processing
}
int main(int argc, char *argv[])
{
if(argc==1) usage(argv[0]);
LibRaw RawProcessor;
int i,arg,c,ret;
char opm,opt,*cp,*sp;
int use_mmap=0, msize;
void *iobuffer;
#define OUT RawProcessor.imgdata.params
argv[argc] = "";
for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; )
{
opt = argv[arg++][1];
if ((cp = strchr (sp="nbrkStqmHACgU", opt)))
for (i=0; i < "11411111142"[cp-sp]-'0'; i++)
if (!isdigit(argv[arg+i][0]))
{
fprintf (stderr,"Non-numeric argument to \"-%c\"\n", opt);
return 1;
}
switch (opt)
{
case 'v': verbosity++; break;
case 'U': OUT.auto_bright_thr = atof(argv[arg++]); break;
case 'n': OUT.threshold = atof(argv[arg++]); break;
case 'b': OUT.bright = atof(argv[arg++]); break;
case 'P': OUT.bad_pixels = argv[arg++]; break;
case 'K': OUT.dark_frame = argv[arg++]; break;
case 'r':
for(c=0;c<4;c++)
OUT.user_mul[c] = atof(argv[arg++]);
break;
case 'C':
OUT.aber[0] = 1 / atof(argv[arg++]);
OUT.aber[2] = 1 / atof(argv[arg++]);
break;
case 'g':
OUT.gamm[0] = 1 / atof(argv[arg++]);
OUT.gamm[1] = atof(argv[arg++]);
break;
case 'k': OUT.user_black = atoi(argv[arg++]); break;
case 'S': OUT.user_sat = atoi(argv[arg++]); break;
case 't': OUT.user_flip = atoi(argv[arg++]); break;
case 'q': OUT.user_qual = atoi(argv[arg++]); break;
case 'm': OUT.med_passes = atoi(argv[arg++]); break;
case 'H': OUT.highlight = atoi(argv[arg++]); break;
case 's': OUT.shot_select = abs(atoi(argv[arg++])); break;
case 'o':
if(isdigit(argv[arg+1][0]) && !isdigit(argv[arg+1][1]))
OUT.output_color = atoi(argv[arg++]);
#ifndef NO_LCMS
else
OUT.output_profile = argv[arg++];
break;
case 'p': OUT.camera_profile = argv[arg++];
#endif
break;
case 'h': OUT.half_size = 1;
// no break: "-h" implies "-f"
case 'f':
OUT.four_color_rgb = 1;
break;
case 'A': for(c=0; c<4;c++) OUT.greybox[c] = atoi(argv[arg++]);
case 'a': OUT.use_auto_wb = 1; break;
case 'w': OUT.use_camera_wb = 1; break;
case 'M': OUT.use_camera_matrix = (opm == '+'); break;
case 'j': OUT.use_fuji_rotate = 0; break;
case 'W': OUT.no_auto_bright = 1; break;
case 'T': OUT.output_tiff = 1; break;
case '4': OUT.output_bps = 16; break;
case '1': OUT.gamma_16bit = 1; break;
#ifndef WIN32
case 'B': use_mmap = 1; break;
#endif
default:
fprintf (stderr,"Unknown option \"-%c\".\n", opt);
return 1;
}
}
putenv ((char*)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field
OUT.filtering_mode = LIBRAW_FILTERING_AUTOMATIC;
#define P1 RawProcessor.imgdata.idata
#define S RawProcessor.imgdata.sizes
#define C RawProcessor.imgdata.color
#define T RawProcessor.imgdata.thumbnail
#define P2 RawProcessor.imgdata.other
if(verbosity>1)
RawProcessor.set_progress_handler(my_progress_callback,(void*)"Sample data passed");
#ifdef _OPENMP
if(verbosity)
printf ("Using %d threads\n", omp_get_max_threads());
#endif
for ( ; arg < argc; arg++)
{
char outfn[1024];
if(verbosity) printf("Processing file %s\n",argv[arg]);
#ifndef WIN32
if(use_mmap)
{
int file = open(argv[arg],O_RDONLY);
struct stat st;
if(file<0)
{
fprintf(stderr,"Cannot open %s: %s\n",argv[arg],strerror(errno));
continue;
}
if(fstat(file,&st))
{
fprintf(stderr,"Cannot stat %s: %s\n",argv[arg],strerror(errno));
close(file);
continue;
}
int pgsz = getpagesize();
msize = ((st.st_size+pgsz-1)/pgsz)*pgsz;
iobuffer = mmap(NULL,msize,PROT_READ,MAP_PRIVATE,file,0);
if(!iobuffer)
{
fprintf(stderr,"Cannot mmap %s: %s\n",argv[arg],strerror(errno));
close(file);
continue;
}
close(file);
if( (ret = RawProcessor.open_buffer(iobuffer,st.st_size) != LIBRAW_SUCCESS))
{
fprintf(stderr,"Cannot open_buffer %s: %s\n",argv[arg],libraw_strerror(ret));
continue; // no recycle b/c open file will recycle itself
}
}
else
#endif
{
if( (ret = RawProcessor.open_file(argv[arg])) != LIBRAW_SUCCESS)
{
fprintf(stderr,"Cannot open %s: %s\n",argv[arg],libraw_strerror(ret));
continue; // no recycle b/c open_file will recycle itself
}
}
if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
{
fprintf(stderr,"Cannot unpack %s: %s\n",argv[arg],libraw_strerror(ret));
continue;
}
if (LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_process()))
{
fprintf(stderr,"Cannot do postpocessing on %s: %s\n",argv[arg],libraw_strerror(ret));
if(LIBRAW_FATAL_ERROR(ret))
continue;
}
snprintf(outfn,sizeof(outfn),
"%s.%s",
argv[arg], OUT.output_tiff ? "tiff" : (P1.colors>1?"ppm":"pgm"));
if(verbosity) printf("Writing file %s\n",outfn);
if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
fprintf(stderr,"Cannot write %s: %s\n",outfn,libraw_strerror(ret));
#ifndef WIN32
if(use_mmap && iobuffer)
{
munmap(iobuffer,msize);
iobuffer=0;
}
#endif
RawProcessor.recycle(); // just for show this call
}
return 0;
}
|