libkdcraw from trunk (KDE4.3) : updated internal LibRaw to 0.7.0-a1:
[kdegraphics.git] / libs / libkdcraw / libraw / samples / dcraw_emu.cpp
bloba9b07e4e9f2cfddb32dbbe8f395d08e47059aaa6
1 /* -*- C++ -*-
2 * File: dcraw_emu.cpp
3 * Copyright 2008-2009 Alex Tutubalin <lexa@lexa.ru>
4 * Created: Sun Mar 23, 2008
6 * LibRaw simple C++ API (almost complete dcraw emulator)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <ctype.h>
29 #include "libraw/libraw.h"
30 #ifdef WIN32
31 #define snprintf _snprintf
32 #endif
35 void usage(const char *prog)
37 printf("dcraw_emu: almost complete dcraw emulator\n");
38 printf("Usage: %s [OPTION]... [FILE]...\n", prog);
39 printf(
40 "-v Verbose: print progress messages (repeated -v will add verbosity)\n"
41 "-w Use camera white balance, if possible\n"
42 "-a Average the whole image for white balance\n"
43 "-A <x y w h> Average a grey box for white balance\n"
44 "-r <r g b g> Set custom white balance\n"
45 "+M/-M Use/don't use an embedded color matrix\n"
46 "-C <r b> Correct chromatic aberration\n"
47 "-P <file> Fix the dead pixels listed in this file\n"
48 "-K <file> Subtract dark frame (16-bit raw PGM)\n"
49 "-k <num> Set the darkness level\n"
50 "-S <num> Set the saturation level\n"
51 "-n <num> Set threshold for wavelet denoising\n"
52 "-H [0-9] Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)\n"
53 "-t [0-7] Flip image (0=none, 3=180, 5=90CCW, 6=90CW)\n"
54 "-o [0-5] Output colorspace (raw,sRGB,Adobe,Wide,ProPhoto,XYZ)\n"
55 #ifndef NO_LCMS
56 "-o file Output ICC profile\n"
57 "-p file Camera input profile (use \'embed\' for embedded profile)\n"
58 #endif
59 "-j Don't stretch or rotate raw pixels\n"
60 "-W Don't automatically brighten the image\n"
61 "-b <num> Adjust brightness (default = 1.0)\n"
62 "-q [0-3] Set the interpolation quality\n"
63 "-h Half-size color image (twice as fast as \"-q 0\")\n"
64 "-f Interpolate RGGB as four colors\n"
65 "-m <num> Apply a 3x3 median filter to R-G and B-G\n"
66 "-s [0..N-1] Select one raw image from input file\n"
67 "-4 Write 16-bit linear instead of 8-bit with gamma\n"
68 "-T Write TIFF instead of PPM\n"
70 exit(1);
73 static int verbosity=0;
75 int cnt=0;
76 int my_progress_callback(void *d,enum LibRaw_progress p,int iteration, int expected)
78 char *passed = (char*)(d?d:"default string"); // data passed to callback at set_callback stage
80 if(verbosity>2) // verbosity set by repeat -v switches
82 printf("CB: %s pass %d of %d (data passed=%s)\n",libraw_strprogress(p),iteration,expected,passed);
84 else if (iteration == 0) // 1st iteration of each step
85 printf("Starting %s (expecting %d iterations)\n", libraw_strprogress(p),expected);
86 else if (iteration == expected-1)
87 printf("%s finished\n",libraw_strprogress(p));
89 /// if(++cnt>10) return 1; // emulate user termination on 10-th callback call
91 return 0; // always return 0 to continue processing
95 int main(int argc, char *argv[])
97 if(argc==1) usage(argv[0]);
99 LibRaw RawProcessor;
100 int i,arg,c,ret;
101 char opm,opt,*cp,*sp;
103 #define OUT RawProcessor.imgdata.params
105 argv[argc] = "";
106 for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; )
108 opt = argv[arg++][1];
109 if ((cp = strchr (sp="nbrkStqmHAC", opt)))
110 for (i=0; i < "11411111142"[cp-sp]-'0'; i++)
111 if (!isdigit(argv[arg+i][0]))
113 fprintf (stderr,"Non-numeric argument to \"-%c\"\n", opt);
114 return 1;
116 switch (opt)
118 case 'v': verbosity++; break;
120 case 'n': OUT.threshold = atof(argv[arg++]); break;
121 case 'b': OUT.bright = atof(argv[arg++]); break;
122 case 'P': OUT.bad_pixels = argv[arg++]; break;
123 case 'K': OUT.dark_frame = argv[arg++]; break;
124 case 'r':
125 for(c=0;c<4;c++)
126 OUT.user_mul[c] = atof(argv[arg++]);
127 break;
128 case 'C':
129 OUT.aber[0] = 1 / atof(argv[arg++]);
130 OUT.aber[2] = 1 / atof(argv[arg++]);
131 break;
132 case 'k': OUT.user_black = atoi(argv[arg++]); break;
133 case 'S': OUT.user_sat = atoi(argv[arg++]); break;
134 case 't': OUT.user_flip = atoi(argv[arg++]); break;
135 case 'q': OUT.user_qual = atoi(argv[arg++]); break;
136 case 'm': OUT.med_passes = atoi(argv[arg++]); break;
137 case 'H': OUT.highlight = atoi(argv[arg++]); break;
138 case 's': OUT.shot_select = abs(atoi(argv[arg])); break;
139 case 'o':
140 if(isdigit(argv[arg+1][0]) && !isdigit(argv[arg+1][1]))
141 OUT.output_color = atoi(argv[arg++]);
142 #ifndef NO_LCMS
143 else
144 OUT.output_profile = argv[arg++];
145 break;
146 case 'p': OUT.camera_profile = argv[arg++];
147 #endif
148 break;
149 case 'h': OUT.half_size = 1;
150 // no break: "-h" implies "-f"
151 case 'f':
152 OUT.four_color_rgb = 1;
153 break;
154 case 'A': for(c=0; c<4;c++) OUT.greybox[c] = atoi(argv[arg++]);
155 case 'a': OUT.use_auto_wb = 1; break;
156 case 'w': OUT.use_camera_wb = 1; break;
157 case 'M': OUT.use_camera_matrix = (opm == '+'); break;
158 case 'j': OUT.use_fuji_rotate = 0; break;
159 case 'W': OUT.no_auto_bright = 1; break;
160 case 'T': OUT.output_tiff = 1; break;
161 case '4': OUT.output_bps = 16; break;
162 case '1': OUT.gamma_16bit = 1; break;
163 default:
164 fprintf (stderr,"Unknown option \"-%c\".\n", opt);
165 return 1;
168 putenv ((char*)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field
170 #define P1 RawProcessor.imgdata.idata
171 #define S RawProcessor.imgdata.sizes
172 #define C RawProcessor.imgdata.color
173 #define T RawProcessor.imgdata.thumbnail
174 #define P2 RawProcessor.imgdata.other
176 if(verbosity>1)
177 RawProcessor.set_progress_handler(my_progress_callback,(void*)"Sample data passed");
178 #ifdef _OPENMP
179 if(verbosity)
180 printf ("Using %d threads\n", omp_get_max_threads());
181 #endif
183 for ( ; arg < argc; arg++)
185 char outfn[1024];
187 if(verbosity) printf("Processing file %s\n",argv[arg]);
188 if( (ret = RawProcessor.open_file(argv[arg])) != LIBRAW_SUCCESS)
190 fprintf(stderr,"Cannot open %s: %s\n",argv[arg],libraw_strerror(ret));
191 continue; // no recycle b/c open_file will recycle itself
193 if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
195 fprintf(stderr,"Cannot unpack %s: %s\n",argv[arg],libraw_strerror(ret));
196 continue;
198 if (LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_process()))
200 fprintf(stderr,"Cannot do postpocessing on %s: %s\n",argv[arg],libraw_strerror(ret));
201 if(LIBRAW_FATAL_ERROR(ret))
202 continue;
204 snprintf(outfn,sizeof(outfn),
205 "%s.%s",
206 argv[arg], OUT.output_tiff ? "tiff" : (P1.colors>1?"ppm":"pgm"));
208 if(verbosity) printf("Writing file %s\n",outfn);
210 if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
211 fprintf(stderr,"Cannot write %s: %s\n",outfn,libraw_strerror(ret));
213 RawProcessor.recycle(); // just for show this call
215 return 0;