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