File: half_mt_win32.c

package info (click to toggle)
libraw 0.17.2-6%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,636 kB
  • ctags: 4,409
  • sloc: cpp: 28,688; ansic: 16,372; perl: 147; makefile: 91; sh: 77
file content (211 lines) | stat: -rw-r--r-- 5,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- C++ -*-
 * File: halt_mt_win32.c
 * Copyright 2008-2015 LibRaw LLC (info@libraw.org)
 * Created: Sat Mar  8, 2008
 *
 * LibRaw  C API mutithreaded sample:  emulates call to "dcraw  -h [-w] [-a] [-v]"
 * Win32 version

LibRaw is free software; you can redistribute it and/or modify
it under the terms of the one of three licenses as you choose:

1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).

2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).

3. LibRaw Software License 27032010
   (See file LICENSE.LibRaw.pdf provided in LibRaw distribution archive for details).


 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include "libraw/libraw.h"

#ifdef WIN32
#define snprintf _snprintf
#endif


#define HANDLE_ERRORS(ret) do {                                 \
    if(ret)                                                     \
        {                                                       \
            fprintf(stderr,"%s: %s\n",fn,libraw_strerror(ret)); \
            if(LIBRAW_FATAL_ERROR(ret))                         \
                {                                               \
                    libraw_close(iprc);                         \
                    return -1;                                   \
                }                                               \
        }                                                       \
    }while(0)


// global settings
int verbose=0,use_camera_wb=0,use_auto_wb=0,tiff_mode=0;

// global file queue
HANDLE qmutex;
char **queue=NULL;
size_t qsize=0,qptr=0;

char *get_next_file()
{
    char *ret;
	DWORD dwWaitResult;
    if(!queue) return NULL;
    if(qptr>=qsize) return NULL;

	dwWaitResult = WaitForSingleObject( 
            qmutex,    // handle to mutex
            INFINITE);  // no time-out interval
	switch (dwWaitResult) 
        {
            // The thread got ownership of the mutex
            case WAIT_OBJECT_0:     
				ret = queue[qptr++];
				ReleaseMutex(qmutex);
				break;
			case WAIT_ABANDONED: 
                return NULL; // cannot obtain the lock
	};
    return ret;
}


// thread routine
int process_files(void *q)
{
    int ret;
    int count=0;
    char outfn[1024], *fn;
    libraw_data_t *iprc = libraw_init(0);

    if(!iprc)
        {
            fprintf(stderr,"Cannot create libraw handle\n");
            return -1;
        }

    while((fn = get_next_file()))
        {

            iprc->params.half_size = 1; /* dcraw -h */
            iprc->params.use_camera_wb = use_camera_wb;
            iprc->params.use_auto_wb = use_auto_wb;
            iprc->params.output_tiff = tiff_mode;
            
            ret = libraw_open_file(iprc,fn);
            if(verbose) fprintf(stderr,"%s: %s/%s\n",fn,iprc->idata.make,iprc->idata.model);
            HANDLE_ERRORS(ret);

            ret = libraw_unpack(iprc);
            HANDLE_ERRORS(ret);
            
            ret = libraw_dcraw_process(iprc);
            HANDLE_ERRORS(ret);
            
            snprintf(outfn,1023,"%s.%s",fn,tiff_mode?"tif":"ppm");

            if(verbose) fprintf(stderr,"Writing file %s\n",outfn);
            ret = libraw_dcraw_ppm_tiff_writer(iprc,outfn);
            HANDLE_ERRORS(ret);
            count++;
        }
    libraw_close(iprc);
	printf("Processed %d files\n",count);
    return 0;
}

void usage(const char*p)
{
    printf(
        "Options:\n"
        "-J n  - set parrallel job coun (default 2)\n"
        "-v    - verbose\n"
        "-w    - use camera white balance\n"
        "-T    - output TIFF instead of PPM\n"
        "-a    - average image for white balance\n");
    exit(1);
}

int show_files(void *q)
{
    char *p;
    int cnt = 0;
    while(p = get_next_file())
        {
            printf("%s\n",p);
            cnt++;
        }
    return cnt;

}

int main(int ac, char *av[])
{
    int i,max_threads = 2;
    HANDLE *threads;
	DWORD   ThreadID;

    if(ac<2)
        usage(av[0]);

    queue = calloc(ac-1,sizeof(queue[0]));

    for (i=1;i<ac;i++)
        {
            if(av[i][0]=='-')
                {
                    if(av[i][1]=='w') use_camera_wb = 1;
                    if(av[i][1]=='a') use_auto_wb = 1;
                    if(av[i][1]=='v') verbose = 1;
                    if(av[i][1]=='T') tiff_mode = 1;
                    if(av[i][1]=='J')
                        {
                            max_threads=atoi(av[++i]);
                            if(max_threads<1)
                                {
                                    fprintf(stderr,"Job count should be at least 1\n");
                                    exit(1);
                                }
                        }
                }
            else
                queue[qsize++] = av[i];
        }
    qmutex = CreateMutex(NULL,FALSE,NULL);	
    threads = calloc(max_threads,sizeof(threads[0]));
    for(i=0;i<max_threads;i++)
	{

		if (NULL == (threads[i] = CreateThread( 
                     NULL,       // default security attributes
                     0,          // default stack size
                     (LPTHREAD_START_ROUTINE) process_files, 
                     NULL,       // no thread function arguments
                     0,          // default creation flags
                     &ThreadID) // receive thread identifier
					 )
			)
        {
            printf("CreateThread error: %d\n", GetLastError());
            return 1;
        }
	}
        
	WaitForMultipleObjects(max_threads, threads, TRUE, INFINITE);

    // Close thread and mutex handles

    for( i=0; i < max_threads; i++ )
        CloseHandle(threads[i]);

    CloseHandle(qmutex);
           
    return 0;
}