2 * File: simple_dcraw_c.c
3 * Copyright 2008 Alex Tutubalin <lexa@lexa.ru>
4 * Created: Sat Mar 8 , 2008
6 * LibRaw C API mutithreaded sample (emulates call to "dcraw -h [-w] [-a] [-v]")
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)
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
28 #include "libraw/libraw.h"
31 #define snprintf _snprintf
35 #define HANDLE_ERRORS(ret) do { \
38 fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
39 if(LIBRAW_FATAL_ERROR(ret)) \
49 int verbose
=0,use_camera_wb
=0,use_auto_wb
=0,tiff_mode
=0;
54 size_t qsize
=0,qptr
=0;
60 if(!queue
) return NULL
;
61 if(qptr
>=qsize
) return NULL
;
63 dwWaitResult
= WaitForSingleObject(
64 qmutex
, // handle to mutex
65 INFINITE
); // no time-out interval
68 // The thread got ownership of the mutex
74 return NULL
; // cannot obtain the lock
81 int process_files(void *q
)
85 char outfn
[1024], *fn
;
86 libraw_data_t
*iprc
= libraw_init(0);
90 fprintf(stderr
,"Cannot create libraw handle\n");
94 while((fn
= get_next_file()))
97 iprc
->params
.half_size
= 1; /* dcraw -h */
98 iprc
->params
.use_camera_wb
= use_camera_wb
;
99 iprc
->params
.use_auto_wb
= use_auto_wb
;
100 iprc
->params
.output_tiff
= tiff_mode
;
102 ret
= libraw_open_file(iprc
,fn
);
103 if(verbose
) fprintf(stderr
,"%s: %s/%s\n",fn
,iprc
->idata
.make
,iprc
->idata
.model
);
106 ret
= libraw_unpack(iprc
);
109 ret
= libraw_dcraw_process(iprc
);
112 snprintf(outfn
,1023,"%s.%s",fn
,tiff_mode
?"tif":"ppm");
114 if(verbose
) fprintf(stderr
,"Writing file %s\n",outfn
);
115 ret
= libraw_dcraw_ppm_tiff_writer(iprc
,outfn
);
120 printf("Processed %d files\n",count
);
124 void usage(const char*p
)
128 "-J n - set parrallel job coun (default 2)\n"
130 "-w - use camera white balance\n"
131 "-T - output TIFF instead of PPM\n"
132 "-a - average image for white balance\n");
136 int show_files(void *q
)
140 while(p
= get_next_file())
149 int main(int ac
, char *av
[])
151 int i
,max_threads
= 2;
158 queue
= calloc(ac
-1,sizeof(queue
[0]));
164 if(av
[i
][1]=='w') use_camera_wb
= 1;
165 if(av
[i
][1]=='a') use_auto_wb
= 1;
166 if(av
[i
][1]=='v') verbose
= 1;
167 if(av
[i
][1]=='T') tiff_mode
= 1;
170 max_threads
=atoi(av
[++i
]);
173 fprintf(stderr
,"Job count should be at least 1\n");
179 queue
[qsize
++] = av
[i
];
181 qmutex
= CreateMutex(NULL
,FALSE
,NULL
);
182 threads
= calloc(max_threads
,sizeof(threads
[0]));
183 for(i
=0;i
<max_threads
;i
++)
186 if (NULL
== (threads
[i
] = CreateThread(
187 NULL
, // default security attributes
188 0, // default stack size
189 (LPTHREAD_START_ROUTINE
) process_files
,
190 NULL
, // no thread function arguments
191 0, // default creation flags
192 &ThreadID
) // receive thread identifier
196 printf("CreateThread error: %d\n", GetLastError());
201 WaitForMultipleObjects(max_threads
, threads
, TRUE
, INFINITE
);
203 // Close thread and mutex handles
205 for( i
=0; i
< max_threads
; i
++ )
206 CloseHandle(threads
[i
]);