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