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