compile
[kdegraphics.git] / libs / libkdcraw / libraw / samples / half_mt_win32.c
blob990a800ec64f7745afc298f0b34f30382b760f27
1 /* -*- C++ -*-
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]")
7 * Win32 version
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)
12 * any later version.
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
22 * 02111-1307, USA.
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <windows.h>
29 #include "libraw/libraw.h"
31 #ifdef WIN32
32 #define snprintf _snprintf
33 #endif
36 #define HANDLE_ERRORS(ret) do { \
37 if(ret) \
38 { \
39 fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
40 if(LIBRAW_FATAL_ERROR(ret)) \
41 { \
42 libraw_close(iprc); \
43 return -1; \
44 } \
45 } \
46 }while(0)
49 // global settings
50 int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0;
52 // global file queue
53 HANDLE qmutex;
54 char **queue=NULL;
55 size_t qsize=0,qptr=0;
57 char *get_next_file()
59 char *ret;
60 DWORD dwWaitResult;
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
67 switch (dwWaitResult)
69 // The thread got ownership of the mutex
70 case WAIT_OBJECT_0:
71 ret = queue[qptr++];
72 ReleaseMutex(qmutex);
73 break;
74 case WAIT_ABANDONED:
75 return NULL; // cannot obtain the lock
77 return ret;
81 // thread routine
82 int process_files(void *q)
84 int ret;
85 int count=0;
86 char outfn[1024], *fn;
87 libraw_data_t *iprc = libraw_init(0);
89 if(!iprc)
91 fprintf(stderr,"Cannot create libraw handle\n");
92 return -1;
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);
105 HANDLE_ERRORS(ret);
107 ret = libraw_unpack(iprc);
108 HANDLE_ERRORS(ret);
110 ret = libraw_dcraw_process(iprc);
111 HANDLE_ERRORS(ret);
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);
117 HANDLE_ERRORS(ret);
118 count++;
120 libraw_close(iprc);
121 printf("Processed %d files\n",count);
122 return 0;
125 void usage(const char*p)
127 printf(
128 "Options:\n"
129 "-J n - set parrallel job coun (default 2)\n"
130 "-v - verbose\n"
131 "-w - use camera white balance\n"
132 "-T - output TIFF instead of PPM\n"
133 "-a - average image for white balance\n");
134 exit(1);
137 int show_files(void *q)
139 char *p;
140 int cnt = 0;
141 while(p = get_next_file())
143 printf("%s\n",p);
144 cnt++;
146 return cnt;
150 int main(int ac, char *av[])
152 int i,max_threads = 2;
153 HANDLE *threads;
154 DWORD ThreadID;
156 if(ac<2)
157 usage(av[0]);
159 queue = calloc(ac-1,sizeof(queue[0]));
161 for (i=1;i<ac;i++)
163 if(av[i][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;
169 if(av[i][1]=='J')
171 max_threads=atoi(av[++i]);
172 if(max_threads<1)
174 fprintf(stderr,"Job count should be at least 1\n");
175 exit(1);
179 else
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());
198 return 1;
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]);
209 CloseHandle(qmutex);
211 return 0;