update
[kdegraphics.git] / libs / libkdcraw / libraw / samples / half_mt.c
blob8de47c076fce3ab64b8336592e8f5ec2104ec80f
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 <pthread.h>
29 #include "libraw/libraw.h"
31 #define HANDLE_ERRORS(ret) do { \
32 if(ret) \
33 { \
34 fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
35 if(LIBRAW_FATAL_ERROR(ret)) \
36 { \
37 libraw_close(iprc); \
38 return -1; \
39 } \
40 } \
41 }while(0)
44 // global settings
45 int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0;
47 // global file queue
48 pthread_mutex_t qm;
49 char **queue=NULL;
50 size_t qsize=0,qptr=0;
52 char *get_next_file()
54 char *ret;
55 if(!queue) return NULL;
56 if(qptr>=qsize) return NULL;
57 pthread_mutex_lock(&qm);
58 ret = queue[qptr++];
59 pthread_mutex_unlock(&qm);
60 return ret;
64 // thread routine
65 int process_files(void *q)
67 int ret;
68 int count=0;
69 char outfn[1024], *fn;
70 libraw_data_t *iprc = libraw_init(0);
72 if(!iprc)
74 fprintf(stderr,"Cannot create libraw handle\n");
75 return -1;
78 while((fn = get_next_file()))
81 iprc->params.half_size = 1; /* dcraw -h */
82 iprc->params.use_camera_wb = use_camera_wb;
83 iprc->params.use_auto_wb = use_auto_wb;
84 iprc->params.output_tiff = tiff_mode;
86 ret = libraw_open_file(iprc,fn);
87 if(verbose) fprintf(stderr,"%s: %s/%s\n",fn,iprc->idata.make,iprc->idata.model);
88 HANDLE_ERRORS(ret);
90 ret = libraw_unpack(iprc);
91 HANDLE_ERRORS(ret);
93 ret = libraw_dcraw_process(iprc);
94 HANDLE_ERRORS(ret);
96 snprintf(outfn,1023,"%s.ppm",fn);
98 if(verbose) fprintf(stderr,"Writing file %s\n",outfn);
99 ret = libraw_dcraw_ppm_tiff_writer(iprc,outfn);
100 HANDLE_ERRORS(ret);
101 count++;
103 libraw_close(iprc);
104 return count;
107 void usage(const char*p)
109 printf("%s: Multi-threaded LibRaw sample app. Emulates dcraw -h [-w] [-a]\n",p);
110 printf(
111 "Options:\n"
112 "-J n - set parrallel job coun (default 2)\n"
113 "-v - verbose\n"
114 "-w - use camera white balance\n"
115 "-a - average image for white balance\n");
116 exit(1);
119 int show_files(void *q)
121 char *p;
122 int cnt = 0;
123 while(p = get_next_file())
125 printf("%s\n",p);
126 cnt++;
128 return cnt;
132 int main(int ac, char *av[])
134 int i, thread_count,max_threads = 2;
135 pthread_t *threads;
136 if(ac<2)
137 usage(av[0]);
139 queue = calloc(ac-1,sizeof(queue[0]));
141 for (i=1;i<ac;i++)
143 if(av[i][0]=='-')
145 if(av[i][1]=='w') use_camera_wb = 1;
146 if(av[i][1]=='a') use_auto_wb = 1;
147 if(av[i][1]=='v') verbose = 1;
148 if(av[i][1]=='T') tiff_mode = 1;
149 if(av[i][1]=='J')
151 max_threads=atoi(av[++i]);
152 if(max_threads<1)
154 fprintf(stderr,"Job count should be at least 1\n");
155 exit(1);
159 else
160 queue[qsize++] = av[i];
162 pthread_mutex_init(&qm,NULL);
163 threads = calloc(max_threads,sizeof(threads[0]));
164 for(i=0;i<max_threads;i++)
165 pthread_create(&threads[i],NULL,process_files,NULL);
166 for(i=0;i<max_threads;i++)
168 int *iptr;
169 if(threads[i])
171 pthread_join(threads[i],&iptr);
172 if(iptr)
173 printf("Thread %d : %d files\n",i,(int)iptr);
177 return 0;