bump ABI/API for KDE4.3
[kdegraphics.git] / libs / libkdcraw / libraw / samples / half_mt_win32.cpp
blob2a8f756ab3b864e880918cd058e9b97b032600ee
1 /* -*- C++ -*-
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)
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 <windows.h>
28 #include "libraw/libraw.h"
30 #ifdef WIN32
31 #define snprintf _snprintf
32 #endif
35 #define HANDLE_ERRORS(ret) do { \
36 if(ret) \
37 { \
38 fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
39 if(LIBRAW_FATAL_ERROR(ret)) \
40 { \
41 libraw_close(iprc); \
42 return -1; \
43 } \
44 } \
45 }while(0)
48 // global settings
49 int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0;
51 // global file queue
52 HANDLE qmutex;
53 char **queue=NULL;
54 size_t qsize=0,qptr=0;
56 char *get_next_file()
58 char *ret;
59 DWORD dwWaitResult;
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
66 switch (dwWaitResult)
68 // The thread got ownership of the mutex
69 case WAIT_OBJECT_0:
70 ret = queue[qptr++];
71 ReleaseMutex(qmutex);
72 break;
73 case WAIT_ABANDONED:
74 return NULL; // cannot obtain the lock
76 return ret;
80 // thread routine
81 int process_files(void *q)
83 int ret;
84 int count=0;
85 char outfn[1024], *fn;
86 libraw_data_t *iprc = libraw_init(0);
88 if(!iprc)
90 fprintf(stderr,"Cannot create libraw handle\n");
91 return -1;
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);
104 HANDLE_ERRORS(ret);
106 ret = libraw_unpack(iprc);
107 HANDLE_ERRORS(ret);
109 ret = libraw_dcraw_process(iprc);
110 HANDLE_ERRORS(ret);
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);
116 HANDLE_ERRORS(ret);
117 count++;
119 libraw_close(iprc);
120 printf("Processed %d files\n",count);
121 return 0;
124 void usage(const char*p)
126 printf(
127 "Options:\n"
128 "-J n - set parrallel job coun (default 2)\n"
129 "-v - verbose\n"
130 "-w - use camera white balance\n"
131 "-T - output TIFF instead of PPM\n"
132 "-a - average image for white balance\n");
133 exit(1);
136 int show_files(void *q)
138 char *p;
139 int cnt = 0;
140 while(p = get_next_file())
142 printf("%s\n",p);
143 cnt++;
145 return cnt;
149 int main(int ac, char *av[])
151 int i,max_threads = 2;
152 HANDLE *threads;
153 DWORD ThreadID;
155 if(ac<2)
156 usage(av[0]);
158 queue = calloc(ac-1,sizeof(queue[0]));
160 for (i=1;i<ac;i++)
162 if(av[i][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;
168 if(av[i][1]=='J')
170 max_threads=atoi(av[++i]);
171 if(max_threads<1)
173 fprintf(stderr,"Job count should be at least 1\n");
174 exit(1);
178 else
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());
197 return 1;
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]);
208 CloseHandle(qmutex);
210 return 0;