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