initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / filecr2.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #include "asset.h"
23 #include "bchash.h"
24 #include "clip.h"
25 #include "colormodels.h"
26 #include "format.inc"
27 #include "file.h"
28 #include "filecr2.h"
29 #include "mutex.h"
30 #include <string.h>
31 #include <unistd.h>
32
33
34 extern "C"
35 {
36 extern char dcraw_info[1024];
37 extern float **dcraw_data;
38 extern int dcraw_alpha;
39 extern float dcraw_matrix[9];
40 int dcraw_main (int argc, const char **argv);
41 }
42
43
44 FileCR2::FileCR2(Asset *asset, File *file)
45  : FileList(asset, file, "CR2LIST", ".cr2", FILE_CR2, FILE_CR2_LIST)
46 {
47         reset();
48         if(asset->format == FILE_UNKNOWN)
49                 asset->format = FILE_CR2;
50 }
51
52 FileCR2::~FileCR2()
53 {
54 //printf("FileCR2::~FileCR2\n");
55         close_file();
56 }
57
58
59 void FileCR2::reset()
60 {
61 }
62
63 int FileCR2::check_sig(Asset *asset)
64 {
65         char *ptr = strstr(asset->path, ".pcm");
66         if(ptr) return 0;
67 //printf("FileCR2::check_sig %d\n", __LINE__);
68         FILE *stream = fopen(asset->path, "rb");
69
70         if(stream)
71         {
72                 char test[10];
73                 (void)fread(test, 10, 1, stream);
74                 fclose(stream);
75
76                 if(test[0] == 'C' && test[1] == 'R' && test[2] == '2' && 
77                         test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
78                 {
79 //printf("FileCR2::check_sig %d\n", __LINE__);
80                         return 1;
81                 }
82         }
83
84 //printf("FileCR2::check_sig %d\n", __LINE__);
85
86
87         char string[BCTEXTLEN];
88         int argc = 3;
89
90         strcpy(string, asset->path);
91
92         const char *argv[4] =
93         {
94                 "dcraw",
95                 "-i",
96                 string,
97                 0
98         };
99
100         int result = dcraw_main(argc, argv);
101
102 //printf("FileCR2::check_sig %d %d\n", __LINE__, result);
103
104         return !result;
105 }
106
107 // int FileCR2::open_file(int rd, int wr)
108 // {
109 // 
110 //      int argc = 3;
111 //      const char *argv[4] = 
112 //      {
113 //              "dcraw",
114 //              "-i",
115 //              asset->path,
116 //              0
117 //      };
118 // 
119 //      int result = dcraw_main(argc, argv);
120 //      if(!result) format_to_asset();
121 // 
122 //      return result;
123 // }
124
125 int FileCR2::read_frame_header(char *path)
126 {
127         int argc = 3;
128 printf("FileCR2::read_frame_header %d\n", __LINE__);
129         const char *argv[4] = 
130         {
131                 "dcraw",
132                 "-i",
133                 path,
134                 0
135         };
136
137         int result = dcraw_main(argc, argv);
138         if(!result) format_to_asset();
139
140 printf("FileCR2::read_frame_header %d %d\n", __LINE__, result);
141         return result;
142 }
143
144
145
146
147 // int FileCR2::close_file()
148 // {
149 //      return 0;
150 // }
151 // 
152 void FileCR2::format_to_asset()
153 {
154         asset->video_data = 1;
155         asset->layers = 1;
156         sscanf(dcraw_info, "%d %d", &asset->width, &asset->height);
157 }
158
159
160 int FileCR2::read_frame(VFrame *frame, char *path)
161 {
162 //printf("FileCR2::read_frame\n");
163
164         if(frame->get_color_model() == BC_RGBA_FLOAT)
165                 dcraw_alpha = 1;
166         else
167                 dcraw_alpha = 0;
168
169 // Want to disable interpolation if an interpolation plugin is on, but
170 // this is impractical because of the amount of caching.  The interpolation
171 // could not respond to a change in the plugin settings and it could not
172 // reload the frame after the plugin was added.  Also, since an 8 bit
173 // PBuffer would be required, it could never have enough resolution.
174 //      int interpolate = 0;
175 //      if(!strcmp(frame->get_next_effect(), "Interpolate Pixels"))
176 //              interpolate = 0;
177
178
179 // printf("FileCR2::read_frame %d\n", interpolate);
180 // frame->dump_stacks();
181 // output to stdout
182         int argc = 0;
183         char *argv[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
184         argv[argc++] = (char*)"dcraw";
185 // write to stdout
186         argv[argc++] = (char*)"-c";
187
188 // printf("FileCR2::read_frame %d interpolate=%d white_balance=%d\n", 
189 // __LINE__,
190 // file->interpolate_raw,
191 // file->white_balance_raw);
192
193 // Use camera white balance.  
194 // Before 2006, DCraw had no Canon white balance.
195 // In 2006 DCraw seems to support Canon white balance.
196 // Still no gamma support.
197 // Need to toggle this in preferences because it defeats dark frame subtraction.
198         if(file->white_balance_raw)
199                 argv[argc++] = (char*)"-w";
200
201
202         if(!file->interpolate_raw)
203         {
204 // Trying to do everything but interpolate doesn't work because convert_to_rgb
205 // doesn't work with bayer patterns.
206 // Use document mode and hack dcraw to apply white balance in the write_ function.
207                 argv[argc++] = (char*)"-d";
208         }
209
210 printf("FileCR2::read_frame %d %s\n", __LINE__, path);
211         argv[argc++] = path;
212
213         dcraw_data = (float**)frame->get_rows();
214
215 //Timer timer;
216         int result = dcraw_main(argc, (const char**) argv);
217
218 // This was only used by the bayer interpolate plugin, which itself created
219 // too much complexity to use effectively.
220 // It required bypassing the cache any time a plugin parameter changed 
221 // to store the color matrix from dcraw in the frame stack along with the new
222 // plugin parameters.  The cache couldn't know if a parameter in the stack came
223 // from dcraw or a plugin & replace it.
224         char string[BCTEXTLEN];
225         sprintf(string, 
226                 "%f %f %f %f %f %f %f %f %f\n",
227                 dcraw_matrix[0],
228                 dcraw_matrix[1],
229                 dcraw_matrix[2],
230                 dcraw_matrix[3],
231                 dcraw_matrix[4],
232                 dcraw_matrix[5],
233                 dcraw_matrix[6],
234                 dcraw_matrix[7],
235                 dcraw_matrix[8]);
236
237
238         frame->get_params()->update("DCRAW_MATRIX", string);
239
240 // frame->dump_params();
241
242         return result;
243 }
244
245 int FileCR2::colormodel_supported(int colormodel)
246 {
247         if(colormodel == BC_RGB_FLOAT ||
248                 colormodel == BC_RGBA_FLOAT)
249                 return colormodel;
250         return BC_RGB_FLOAT;
251 }
252
253
254 // Be sure to add a line to File::get_best_colormodel
255 int FileCR2::get_best_colormodel(Asset *asset, int driver)
256 {
257 //printf("FileCR2::get_best_colormodel %d\n", __LINE__);
258         return BC_RGB_FLOAT;
259 }
260
261 // int64_t FileCR2::get_memory_usage()
262 // {
263 //      int64_t result = asset->width * asset->height * sizeof(float) * 3;
264 // //printf("FileCR2::get_memory_usage %d " _LD "\n", __LINE__, result);
265 //      return result;
266 // }
267
268
269 int FileCR2::use_path()
270 {
271         return 1;
272 }
273
274
275
276
277