File: postprocessing_benchmark.cpp

package info (click to toggle)
libraw 0.14.6-2%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,728 kB
  • sloc: cpp: 14,132; sh: 10,692; ansic: 10,229; makefile: 87
file content (212 lines) | stat: -rw-r--r-- 7,507 bytes parent folder | download | duplicates (3)
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
/* -*- C++ -*-
 * File: postprocessing_benchmark.cpp
 * Copyright 2008-2010 LibRaw LLC (info@libraw.org)
 * Created: Jul 13, 2011
 *
 * LibRaw simple C++ API:  creates 8 different renderings from 1 source file. The 1st and 4th one should be identical

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>
#else
#include <winsock2.h>
#endif

#include "libraw/libraw.h"

void timerstart(void);
float timerend(void);


int main(int argc, char *argv[])
{
    int  i, ret,rep=1;
    LibRaw RawProcessor;
#define OUT RawProcessor.imgdata.params
#define S RawProcessor.imgdata.sizes

    if(argc<2) 
        {
            printf(
                "postprocessing benchmark: LibRaw %s sample, %d cameras supported\n"
                "Measures postprocessing speed with different options\n"
                "Usage: %s [-a] [-H N] [-q N] [-h] [-m N] [-n N] [-s N] [-B x y w h] [-R N]\n"
                "-a             average image for white balance\n"
                "-H <num>       Highlight mode (0=clip, 1=unclip, 2=blend, 3+=rebuild)\n"
                "-q <num>       Set the interpolation quality\n"
                "-h             Half-size color image\n"
                "-m <num>       Apply a num-passes 3x3 median filter to R-G and B-G\n"
                "-n <num>       Set threshold for wavelet denoising\n"
                "-s <num>       Select one raw image from input file\n"
                "-B <x y w h>   Crop output image\n"
                "-R <num>       Number of repetitions\n"
                ,LibRaw::version(), LibRaw::cameraCount(),
                argv[0]);
            return 0;
        }
    char opm,opt,*cp,*sp;
    int arg,c;
    int shrink = 0;

    argv[argc] = (char*)"";
    for (arg=1; (((opm = argv[arg][0]) - 2) | 2) == '+'; ) 
        {
            char *optstr = argv[arg];
            opt = argv[arg++][1];
            if ((cp = strchr (sp=(char*)"HqmnsBR", opt))!=0)
                for (i=0; i < "1111141"[cp-sp]-'0'; i++)
                    if (!isdigit(argv[arg+i][0]) && !optstr[2]) 
                        {
                            fprintf (stderr,"Non-numeric argument to \"-%c\"\n", opt);
                            return 1;
                        }
            switch (opt) 
                {
                case 'a': 
                    OUT.use_auto_wb = 1;  
                    break;
                case 'H':
                    OUT.highlight   = atoi(argv[arg++]);  
                    break;
                case 'q':
                    OUT.user_qual   = atoi(argv[arg++]);  
                    break;
                case 'h':  
                    OUT.half_size = 1;		
                    OUT.four_color_rgb    = 1;  
                    shrink = 1;
                    break;
                case 'm':
                    OUT.med_passes  = atoi(argv[arg++]);  
                    break;
                case 'n':  
                    OUT.threshold   = (float)atof(argv[arg++]);  
                    break;
                case 's':  
                    OUT.shot_select = abs(atoi(argv[arg++])); 
                    break;
                case 'B':  
                    for(c=0; c<4;c++) OUT.cropbox[c]  = atoi(argv[arg++]); 
                    break;
                case 'R':  
                    rep = abs(atoi(argv[arg++])); 
                    if(rep<1) rep = 1;
                    break;
                default:
                    fprintf (stderr,"Unknown option \"-%c\".\n", opt);
                    return 1;
                }
        }
    for ( ; arg < argc; arg++)
        {
            printf("Processing file %s\n",argv[arg]);
            if( (ret = RawProcessor.open_file(argv[arg])) != LIBRAW_SUCCESS)
                {
                    fprintf(stderr,"Cannot open_file %s: %s\n",argv[arg],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",argv[arg],libraw_strerror(ret));
                    continue;
                }
            float mpix,rmpix;
            timerstart();
            for(c=0; c < rep; c++)
                {
                    if( (ret = RawProcessor.dcraw_process() ) != LIBRAW_SUCCESS)
                        {
                            fprintf(stderr,"Cannot postprocess %s: %s\n",argv[arg],libraw_strerror(ret));
                            break;
                        }
                    libraw_processed_image_t *p = RawProcessor.dcraw_make_mem_image();
                    if(p)
                        RawProcessor.dcraw_clear_mem(p);
                    RawProcessor.free_image();
                }
            float msec = timerend()/(float)rep;

            if( (ret = RawProcessor.adjust_sizes_info_only() ) != LIBRAW_SUCCESS)
                {
                    fprintf(stderr,"Cannot adjust sizes for %s: %s\n",argv[arg],libraw_strerror(ret));
                    break;
                }
            rmpix = (S.iwidth*S.iheight)/1000000.0f;

            if(c==rep) // no failure
                {
                    unsigned int crop[4];
                    for(int i=0;i<4;i++) crop[i] = (OUT.cropbox[i])>>shrink;
                    if(crop[0]+crop[2]>S.iwidth) crop[2] = S.iwidth-crop[0];
                    if(crop[1]+crop[3]>S.iheight) crop[3] = S.iheight-crop[1];

                    mpix = float(crop[2]*crop[3])/1000000.0f;
                    float mpixsec = mpix*1000.0f/msec;

                    printf(
                        "Performance: %.2f Mpix/sec\n"
                        "File: %s, Frame: %d %.1f total Mpix, %.1f msec\n"
                        "Params:      WB=%s Highlight=%d Qual=%d HalfSize=%s Median=%d Wavelet=%.0f\n"
                        "Crop:        %u-%u:%ux%u, active Mpix: %.2f, %.1f frames/sec\n",
                        mpixsec
                        ,argv[arg],OUT.shot_select,rmpix,msec,
                        OUT.use_auto_wb?"auto":"default",OUT.highlight,OUT.user_qual,OUT.half_size?"YES":"No",
                        OUT.med_passes,OUT.threshold,
                        crop[0],crop[1],crop[2],crop[3],mpix,1000.0f/msec);
                }
        }
    
    return 0;
}


#ifndef WIN32
static struct timeval start,end;
void timerstart(void)
{
    gettimeofday(&start,NULL);
}
float timerend(void)
{
    gettimeofday(&end,NULL);
    float msec = (end.tv_sec - start.tv_sec)*1000.0f + (end.tv_usec - start.tv_usec)/1000.0f;
    return msec;
}
#else
LARGE_INTEGER start;
void timerstart(void)
{
	QueryPerformanceCounter(&start);
}
float timerend()
{
	LARGE_INTEGER unit,end;
	QueryPerformanceCounter(&end);
	QueryPerformanceFrequency(&unit);
	float msec = (float)(end.QuadPart - start.QuadPart);
	msec /= (float)unit.QuadPart/1000.0f;
        return msec;
}

#endif