libkdcraw from trunk : update internal LibRaw to 0.7.0-alpha4
[kdegraphics.git] / libs / libkdcraw / libraw / samples / unprocessed_raw.cpp
blob718fa07a840e6b4ccd5b7b81e2aef81cfa84f271
1 /* -*- C++ -*-
2 * File: docmode_withmask.cpp
3 * Copyright 2009 Alex Tutubalin <lexa@lexa.ru>
4 * Created: Fri Jan 02, 2009
6 * LibRaw sample
7 * Generates RAW .pgm file with masked pixels included
8 * and all processing turned off
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
25 #include <stdio.h>
26 #include <string.h>
27 #include <math.h>
28 #ifndef WIN32
29 #include <netinet/in.h>
30 #else
31 #include <winsock2.h>
32 #endif
34 #include "libraw/libraw.h"
36 #ifdef WIN32
37 #define snprintf _snprintf
38 #endif
40 int main(int ac, char *av[])
42 int i, ret;
43 int verbose=1,autoscale=0;
44 char outfn[1024],thumbfn[1024];
46 LibRaw RawProcessor;
47 if(ac<2)
49 usage:
50 printf(
51 "unprocessed_raw - LibRaw %s sample. %d cameras supported\n"
52 "Usage: %s [-q] [-A] [-g] [-s N] [-N] raw-files....\n"
53 "\t-q - be quiet\n"
54 "\t-s N - select Nth image in file (default=0)\n"
55 "\t-g - use gamma correction with gamma 2.2 (not precise,use for visual inspection only)\n"
56 "\t-A - autoscaling (by integer factor)\n"
57 "\t-N - no raw curve\n"
58 ,LibRaw::version(),
59 LibRaw::cameraCount(),
60 av[0]);
61 return 0;
64 #define P1 RawProcessor.imgdata.idata
65 #define S RawProcessor.imgdata.sizes
66 #define C RawProcessor.imgdata.color
67 #define T RawProcessor.imgdata.thumbnail
68 #define P2 RawProcessor.imgdata.other
69 #define OUT RawProcessor.imgdata.params
71 OUT.document_mode=2;
72 OUT.output_bps=16;
73 OUT.output_tiff=1;
74 OUT.user_flip=0;
75 OUT.no_auto_bright = 1;
76 OUT.filtering_mode=(LibRaw_filtering)( LIBRAW_FILTERING_NOBLACKS|LIBRAW_FILTERING_NOZEROES);
77 for (i=1;i<ac;i++)
79 if(av[i][0]=='-')
81 if(av[i][1]=='q' && av[i][2]==0)
82 verbose=0;
83 else if(av[i][1]=='A' && av[i][2]==0)
84 autoscale=1;
85 else if(av[i][1]=='g' && av[i][2]==0)
86 OUT.gamma_16bit=1;
87 else if(av[i][1]=='N' && av[i][2]==0)
88 OUT.filtering_mode=LIBRAW_FILTERING_NONE;
89 else if(av[i][1]=='s' && av[i][2]==0)
91 i++;
92 OUT.shot_select=atoi(av[i]);
94 else
95 goto usage;
96 continue;
98 int r,c;
99 if(verbose) printf("Processing file %s\n",av[i]);
100 if( (ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
102 fprintf(stderr,"Cannot open %s: %s\n",av[i],libraw_strerror(ret));
103 continue; // no recycle b/c open file will recycle itself
105 if(verbose)
107 printf("Image size: %dx%d\nRaw size: %dx%d\n",S.width,S.height,S.raw_width,S.raw_height);
108 printf("Margins: top=%d, left=%d, right=%d, bottom=%d\n",
109 S.top_margin,S.left_margin,S.right_margin,S.bottom_margin);
112 if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
114 fprintf(stderr,"Cannot unpack %s: %s\n",av[i],libraw_strerror(ret));
115 continue;
117 if(verbose)
118 printf("Unpacked....\n");
120 if( (ret = RawProcessor.add_masked_borders_to_bitmap() ) != LIBRAW_SUCCESS)
122 fprintf(stderr,"Cannot add mask data to bitmap %s\n",av[i]);
124 for(int r=0;r<S.iheight;r++)
125 for(c=0;c<S.iwidth;c++)
126 RawProcessor.imgdata.image[r*S.iwidth+c][0]
127 = RawProcessor.imgdata.image[r*S.iwidth+c][RawProcessor.FC(r,c)];
129 P1.colors=1;
130 if(autoscale)
132 unsigned max=0,scale;
133 for(int j=0; j<S.iheight*S.iwidth; j++)
134 if(max < RawProcessor.imgdata.image[j][0])
135 max = RawProcessor.imgdata.image[j][0];
136 if (max >0 && max< 1<<15)
138 scale = (1<<16)/max;
139 if(verbose)
140 printf("Scaling with multiplier=%d (max=%d)\n",scale,max);
142 for(int j=0; j<S.iheight*S.iwidth; j++)
143 RawProcessor.imgdata.image[j][0] *= scale;
147 if(OUT.shot_select)
148 snprintf(outfn,sizeof(outfn),"%s-%d.tiff",av[i],OUT.shot_select);
149 else
150 snprintf(outfn,sizeof(outfn),"%s.tiff",av[i]);
152 if(verbose) printf("Writing file %s\n",outfn);
153 if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
154 fprintf(stderr,"Cannot write %s: %s\n",outfn,libraw_strerror(ret));
156 return 0;