2 * File: halt_mt_win32.c
3 * Copyright 2008-2009 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]")
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "libraw/libraw.h"
32 #define snprintf _snprintf
36 #define HANDLE_ERRORS(ret) do { \
39 fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
40 if(LIBRAW_FATAL_ERROR(ret)) \
50 int verbose
=0,use_camera_wb
=0,use_auto_wb
=0,tiff_mode
=0;
55 size_t qsize
=0,qptr
=0;
61 if(!queue
) return NULL
;
62 if(qptr
>=qsize
) return NULL
;
64 dwWaitResult
= WaitForSingleObject(
65 qmutex
, // handle to mutex
66 INFINITE
); // no time-out interval
69 // The thread got ownership of the mutex
75 return NULL
; // cannot obtain the lock
82 int process_files(void *q
)
86 char outfn
[1024], *fn
;
87 libraw_data_t
*iprc
= libraw_init(0);
91 fprintf(stderr
,"Cannot create libraw handle\n");
95 while((fn
= get_next_file()))
98 iprc
->params
.half_size
= 1; /* dcraw -h */
99 iprc
->params
.use_camera_wb
= use_camera_wb
;
100 iprc
->params
.use_auto_wb
= use_auto_wb
;
101 iprc
->params
.output_tiff
= tiff_mode
;
103 ret
= libraw_open_file(iprc
,fn
);
104 if(verbose
) fprintf(stderr
,"%s: %s/%s\n",fn
,iprc
->idata
.make
,iprc
->idata
.model
);
107 ret
= libraw_unpack(iprc
);
110 ret
= libraw_dcraw_process(iprc
);
113 snprintf(outfn
,1023,"%s.%s",fn
,tiff_mode
?"tif":"ppm");
115 if(verbose
) fprintf(stderr
,"Writing file %s\n",outfn
);
116 ret
= libraw_dcraw_ppm_tiff_writer(iprc
,outfn
);
121 printf("Processed %d files\n",count
);
125 void usage(const char*p
)
129 "-J n - set parrallel job coun (default 2)\n"
131 "-w - use camera white balance\n"
132 "-T - output TIFF instead of PPM\n"
133 "-a - average image for white balance\n");
137 int show_files(void *q
)
141 while(p
= get_next_file())
150 int main(int ac
, char *av
[])
152 int i
,max_threads
= 2;
159 queue
= calloc(ac
-1,sizeof(queue
[0]));
165 if(av
[i
][1]=='w') use_camera_wb
= 1;
166 if(av
[i
][1]=='a') use_auto_wb
= 1;
167 if(av
[i
][1]=='v') verbose
= 1;
168 if(av
[i
][1]=='T') tiff_mode
= 1;
171 max_threads
=atoi(av
[++i
]);
174 fprintf(stderr
,"Job count should be at least 1\n");
180 queue
[qsize
++] = av
[i
];
182 qmutex
= CreateMutex(NULL
,FALSE
,NULL
);
183 threads
= calloc(max_threads
,sizeof(threads
[0]));
184 for(i
=0;i
<max_threads
;i
++)
187 if (NULL
== (threads
[i
] = CreateThread(
188 NULL
, // default security attributes
189 0, // default stack size
190 (LPTHREAD_START_ROUTINE
) process_files
,
191 NULL
, // no thread function arguments
192 0, // default creation flags
193 &ThreadID
) // receive thread identifier
197 printf("CreateThread error: %d\n", GetLastError());
202 WaitForMultipleObjects(max_threads
, threads
, TRUE
, INFINITE
);
204 // Close thread and mutex handles
206 for( i
=0; i
< max_threads
; i
++ )
207 CloseHandle(threads
[i
]);