File: simple_dcraw.cpp

package info (click to toggle)
libraw 0.9.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 1,840 kB
  • ctags: 1,995
  • sloc: cpp: 12,197; ansic: 8,897; makefile: 68
file content (213 lines) | stat: -rw-r--r-- 7,824 bytes parent folder | download | duplicates (2)
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
212
213
/* -*- C++ -*-
 * File: simple_dcraw.cpp
 * Copyright 2008-2010 LibRaw LLC (info@libraw.org)
 * Created: Sat Mar  8 , 2008
 *
 * LibRaw simple C++ API:  emulates call to "dcraw  [-D]  [-T] [-v] [-e] [-4]"

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 <math.h>

#ifndef WIN32
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#endif

#include "libraw/libraw.h"

#ifdef WIN32
#define snprintf _snprintf
#endif

int my_progress_callback(void *unused_data,enum LibRaw_progress state,int iter, int expected)
{
    if(iter==0)
        printf("CB: state=%x, expected %d iterations\n",state,expected);
    return 0;
}


int main(int ac, char *av[])
{
    int  i, ret, verbose=0, output_thumbs=0,use_mmap=0,msize;
    void *file_buffer;

    // don't use fixed size buffers in real apps!
    char outfn[1024],thumbfn[1024]; 

    LibRaw RawProcessor;
    if(ac<2) 
        {
            printf(
                "simple_dcraw - LibRaw %s sample. Emulates dcraw [-D] [-T] [-v] [-e] [-B]\n"
                " %d cameras supported\n"
                "Usage: %s [-D] [-T] [-v] [-e] raw-files....\n"
                "\t-D - document mode emulation\n"
                "\t-4 - 16-bit mode\n"
                "\t-v - verbose output\n"
                "\t-T - output TIFF files instead of .pgm/ppm\n"
#ifndef WIN32
                "\t-B - use mmap()-ed I/O (Unix only)\n"
#endif
                "\t-e - extract thumbnails (same as dcraw -e in separate run)\n",LibRaw::version(),
                LibRaw::cameraCount(),
                av[0]);
            return 0;
        }
    
    putenv ((char*)"TZ=UTC"); // dcraw compatibility, affects TIFF datestamp field

#define P1 RawProcessor.imgdata.idata
#define S RawProcessor.imgdata.sizes
#define C RawProcessor.imgdata.color
#define T RawProcessor.imgdata.thumbnail
#define P2 RawProcessor.imgdata.other
#define OUT RawProcessor.imgdata.params


    for (i=1;i<ac;i++)
        {
            if(av[i][0]=='-')
                {
                    if(av[i][1]=='T' && av[i][2]==0)
                        OUT.output_tiff=1;
                    if(av[i][1]=='v' && av[i][2]==0)
                        verbose++;
                    if(av[i][1]=='e' && av[i][2]==0)
                        output_thumbs++;
                    if(av[i][1]=='D' && av[i][2]==0)
                        OUT.document_mode=2;
                    if(av[i][1]=='B' && av[i][2]==0)
                        use_mmap=1;
                    if(av[i][1]=='4' && av[i][2]==0)
                        OUT.output_bps=16;
                    if(av[i][1]=='C' && av[i][2]==0)
                        RawProcessor.set_progress_handler(my_progress_callback,NULL);
                    continue;
                }

            if(verbose) printf("Processing file %s\n",av[i]);

#ifndef WIN32
            if(use_mmap)
                {
                    int file = open(av[i],O_RDONLY);
                    struct stat st;
                    if(file<0)
                        {
                            fprintf(stderr,"Cannot open %s: %s\n",av[i],strerror(errno));
                            continue;
                        }
                    if(fstat(file,&st))
                        {
                            fprintf(stderr,"Cannot stat %s: %s\n",av[i],strerror(errno));
                            close(file);
                            continue;
                        }
                    int pgsz = getpagesize();
                    msize = ((st.st_size+pgsz-1)/pgsz)*pgsz;
                    file_buffer = mmap(NULL,msize,PROT_READ,MAP_PRIVATE,file,0);
                    if(!file_buffer)
                        {
                            fprintf(stderr,"Cannot mmap %s: %s\n",av[i],strerror(errno));
                            close(file);
                            continue;
                        }
                    close(file);
                    if( (ret = RawProcessor.open_buffer(file_buffer,st.st_size) != LIBRAW_SUCCESS))
                        {
                            fprintf(stderr,"Cannot open_buffer %s: %s\n",av[i],libraw_strerror(ret));
                            continue; // no recycle b/c open file will recycle itself
                        }
                }
            else
#endif
                {
                    if( (ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
                        {
                            fprintf(stderr,"Cannot open_file %s: %s\n",av[i],libraw_strerror(ret));
                            continue; // no recycle b/c open file will recycle itself
                        }
                }

            if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
                {
                    fprintf(stderr,"Cannot unpack %s: %s\n",av[i],libraw_strerror(ret));
                    continue;
                }

            // thumbnail unpacking and output in the middle of main
            // image processing - for test purposes!
            if(output_thumbs)
                {
                    if( (ret = RawProcessor.unpack_thumb() ) != LIBRAW_SUCCESS)
                        {
                            fprintf(stderr,"Cannot unpack_thumb %s: %s\n",av[i],libraw_strerror(ret));
                            if(LIBRAW_FATAL_ERROR(ret))
                                continue; // skip to next file
                        }
                    else
                        {
                            snprintf(thumbfn,sizeof(thumbfn),"%s.%s",
                                     av[i],T.tformat == LIBRAW_THUMBNAIL_JPEG ? "thumb.jpg" : "thumb.ppm");

                            if(verbose) printf("Writing thumbnail file %s\n",thumbfn);
                            if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_thumb_writer(thumbfn)))
                                {
                                    fprintf(stderr,"Cannot write %s: %s\n",thumbfn,libraw_strerror(ret));
                                    if(LIBRAW_FATAL_ERROR(ret))
                                        continue; 
                                }
                        }
                }
            
            if(OUT.document_mode)
                ret = RawProcessor.dcraw_document_mode_processing();
            else
                ret = RawProcessor.dcraw_process();
                
            if(LIBRAW_SUCCESS !=ret)
                {
                    fprintf(stderr,"Cannot do postpocessing on %s: %s\n",
                            av[i],libraw_strerror(ret));
                    if(LIBRAW_FATAL_ERROR(ret))
                        continue; 
                }
            snprintf(outfn,sizeof(outfn),
                     "%s.%s",
                     av[i], OUT.output_tiff ? "tiff" : (P1.colors>1?"ppm":"pgm"));

            if(verbose) printf("Writing file %s\n",outfn);

            if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
                fprintf(stderr,"Cannot write %s: %s\n",outfn,libraw_strerror(ret));

#ifndef WIN32            
            if(use_mmap && file_buffer)
                {
                    munmap(file_buffer,msize);
                    file_buffer=0;
                }
#endif
            RawProcessor.recycle(); // just for show this call
        }
    return 0;
}