update internal LibRaw to official 0.7.0 release...
[kdegraphics.git] / libs / libkdcraw / libraw / samples / 4channels.cpp
blobd266c53ca82c5c4c4b9ae1ce393bcf0ca8b0fd45
1 /* -*- C++ -*-
2 * File: 4channels.cpp
3 * Copyright 2009 Alex Tutubalin <lexa@lexa.ru>
4 * Created: Mon Feb 09, 2009
6 * LibRaw sample
7 * Generates 4 TIFF file from RAW data, one file per channel
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
24 #include <stdio.h>
25 #include <string.h>
26 #include <math.h>
27 #ifndef WIN32
28 #include <netinet/in.h>
29 #else
30 #include <winsock2.h>
31 #endif
33 #include "libraw/libraw.h"
35 #ifdef WIN32
36 #define snprintf _snprintf
37 #endif
39 int main(int ac, char *av[])
41 int i, ret;
42 int autoscale=0,filtering_mode=LIBRAW_FILTERING_DEFAULT,black_subtraction=1;
43 char outfn[1024],thumbfn[1024];
45 LibRaw RawProcessor;
46 if(ac<2)
48 usage:
49 printf(
50 "4channeld - LibRaw %s sample. %d cameras supported\n"
51 "Usage: %s [-s N] [-g] [-A] [-B] [-N] raw-files....\n"
52 "\t-s N - select Nth image in file (default=0)\n"
53 "\t-g - use gamma correction with gamma 2.2 (not precise,use for visual inspection only)\n"
54 "\t-A - autoscaling (by integer factor)\n"
55 "\t-B - no black subtraction\n"
56 "\t-N - no raw curve\n"
57 ,LibRaw::version(),
58 LibRaw::cameraCount(),
59 av[0]);
60 return 0;
63 #define P1 RawProcessor.imgdata.idata
64 #define S RawProcessor.imgdata.sizes
65 #define C RawProcessor.imgdata.color
66 #define T RawProcessor.imgdata.thumbnail
67 #define P2 RawProcessor.imgdata.other
68 #define OUT RawProcessor.imgdata.params
70 OUT.document_mode=2;
71 OUT.output_bps=16;
72 OUT.output_tiff=1;
73 OUT.user_flip=0;
74 OUT.no_auto_bright = 1;
75 OUT.half_size=1;
76 OUT.filtering_mode= LIBRAW_FILTERING_AUTOMATIC;
78 for (i=1;i<ac;i++)
80 if(av[i][0]=='-')
82 if(av[i][1]=='s' && av[i][2]==0)
84 i++;
85 OUT.shot_select=atoi(av[i]);
87 else if(av[i][1]=='g' && av[i][2]==0)
88 OUT.gamma_16bit=1;
89 else if(av[i][1]=='A' && av[i][2]==0)
90 autoscale=1;
91 else if(av[i][1]=='B' && av[i][2]==0)
93 filtering_mode |= (LIBRAW_FILTERING_NOZEROES | LIBRAW_FILTERING_NOBLACKS);
94 black_subtraction=0;
96 else if(av[i][1]=='N' && av[i][2]==0)
97 filtering_mode |= LIBRAW_FILTERING_NORAWCURVE;
98 else
99 goto usage;
100 continue;
102 if(filtering_mode)
103 OUT.filtering_mode = (LibRaw_filtering) filtering_mode;
104 int r,c;
105 printf("Processing file %s\n",av[i]);
106 if( (ret = RawProcessor.open_file(av[i])) != LIBRAW_SUCCESS)
108 fprintf(stderr,"Cannot open %s: %s\n",av[i],libraw_strerror(ret));
109 continue; // no recycle b/c open file will recycle itself
111 if(P1.is_foveon)
113 printf("Cannot process foveon image %s\n",av[i]);
114 continue ;
116 if( (ret = RawProcessor.unpack() ) != LIBRAW_SUCCESS)
118 fprintf(stderr,"Cannot unpack %s: %s\n",av[i],libraw_strerror(ret));
119 continue;
121 if(black_subtraction && C.black>0)
123 for(int j=0; j<S.iheight*S.iwidth; j++)
124 for(int c = 0; c< 4; c++)
125 if(RawProcessor.imgdata.image[j][c]>C.black)
126 RawProcessor.imgdata.image[j][c]-=C.black;
127 else
128 RawProcessor.imgdata.image[j][c]=0;
131 if(autoscale)
133 unsigned max=0,scale;
134 for(int j=0; j<S.iheight*S.iwidth; j++)
135 for(int c = 0; c< 4; c++)
136 if(max < RawProcessor.imgdata.image[j][c])
137 max = RawProcessor.imgdata.image[j][c];
138 if (max >0 && max< 1<<15)
140 scale = (1<<16)/max;
141 printf("Scaling with multiplier=%d (max=%d)\n",scale,max);
142 for(int j=0; j<S.iheight*S.iwidth; j++)
143 for(c=0;c<4;c++)
144 RawProcessor.imgdata.image[j][c] *= scale;
146 printf("Black level (scaled)=%d\n",C.black*scale);
148 else
149 printf("Black level (unscaled)=%d\n",C.black);
152 // hack to make dcraw tiff writer happy
153 int isrgb=(P1.colors==4?0:1);
154 P1.colors = 1;
155 S.width = S.iwidth;
156 S.height = S.iheight;
158 for (int layer=0;layer<4;layer++)
160 if(layer>0)
162 for (int rc = 0; rc < S.iheight*S.iwidth; rc++)
163 RawProcessor.imgdata.image[rc][0] = RawProcessor.imgdata.image[rc][layer];
165 char lname[8];
166 if(isrgb)
168 snprintf(lname,7,"%c",(char*)("RGBG")[layer]);
169 if(layer==3)
170 strcat(lname,"2");
172 else
173 snprintf(lname,7,"%c",(char*)("GCMY")[layer]);
175 if(OUT.shot_select)
176 snprintf(outfn,sizeof(outfn),"%s-%d.%s.tiff",av[i],OUT.shot_select,lname);
177 else
178 snprintf(outfn,sizeof(outfn),"%s.%s.tiff",av[i],lname);
180 printf("Writing file %s\n",outfn);
181 if( LIBRAW_SUCCESS != (ret = RawProcessor.dcraw_ppm_tiff_writer(outfn)))
182 fprintf(stderr,"Cannot write %s: %s\n",outfn,libraw_strerror(ret));
186 return 0;