r1026: Videoscope layout tweaks.
[cinelerra_cv/ct.git] / cinelerra / filecr2.C
blob2fb5320b25ab24f9fe6be9eddb08a82a1a2a2fb4
1 #include "asset.h"
2 #include "bchash.h"
3 #include "clip.h"
4 #include "colormodels.h"
5 #include "file.h"
6 #include "filecr2.h"
7 #include "mutex.h"
8 #include <string.h>
9 #include <unistd.h>
11 // Only allow one instance of the decoder to run simultaneously.
12 static Mutex cr2_mutex("cr2_mutex");
14 extern "C"
16 extern char dcraw_info[1024];
17 extern float **dcraw_data;
18 extern int dcraw_alpha;
19 extern float dcraw_matrix[9];
20 int dcraw_main (int argc, char **argv);
24 FileCR2::FileCR2(Asset *asset, File *file)
25  : FileBase(asset, file)
27         reset();
28         if(asset->format == FILE_UNKNOWN)
29                 asset->format = FILE_CR2;
32 FileCR2::~FileCR2()
34         close_file();
38 void FileCR2::reset()
42 int FileCR2::check_sig(Asset *asset)
44         cr2_mutex.lock("FileCR2::check_sig");
45         char string[BCTEXTLEN];
46         int argc = 3;
48         strcpy(string, asset->path);
50         char *argv[4];
51         argv[0] = "dcraw";
52         argv[1] = "-i";
53         argv[2] = string;
54         argv[3] = 0;
56         int result = dcraw_main(argc, argv);
58         cr2_mutex.unlock();
60         return !result;
63 int FileCR2::open_file(int rd, int wr)
65         cr2_mutex.lock("FileCR2::check_sig");
67         int argc = 3;
68         char *argv[3] = 
69         {
70                 "dcraw",
71                 "-i",
72                 asset->path
73         };
75         int result = dcraw_main(argc, argv);
76         if(!result) format_to_asset();
78         cr2_mutex.unlock();
79         return result;
83 int FileCR2::close_file()
85         return 0;
88 void FileCR2::format_to_asset()
90         asset->video_data = 1;
91         asset->layers = 1;
92         sscanf(dcraw_info, "%d %d", &asset->width, &asset->height);
93         if(!asset->frame_rate) asset->frame_rate = 1;
94         asset->video_length = -1;
98 int FileCR2::read_frame(VFrame *frame)
100         cr2_mutex.lock("FileCR2::read_frame");
101         if(frame->get_color_model() == BC_RGBA_FLOAT)
102                 dcraw_alpha = 1;
103         else
104                 dcraw_alpha = 0;
106 // Want to disable interpolation if an interpolation plugin is on, but
107 // this is impractical because of the amount of caching.  The interpolation
108 // could not respond to a change in the plugin settings and it could not
109 // reload the frame after the plugin was added.  Also, since an 8 bit
110 // PBuffer would be required, it could never have enough resolution.
111 //      int interpolate = 0;
112 //      if(!strcmp(frame->get_next_effect(), "Interpolate Pixels"))
113 //              interpolate = 0;
116 // printf("FileCR2::read_frame %d\n", interpolate);
117 // frame->dump_stacks();
118 // output to stdout
119         int argc = 0;
120         char *argv[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
121         argv[argc++] = "dcraw";
122 // write to stdout
123         argv[argc++] = "-c";
124 // Use camera white balance.  
125 // Before 2006, DCraw had no Canon white balance.
126 // In 2006 DCraw seems to support Canon white balance.
127 // Still no gamma support.
128 // Need to toggle this in preferences because it defeats dark frame subtraction.
129         if(file->white_balance_raw)
130                 argv[argc++] = "-w";
131         if(!file->interpolate_raw)
132         {
133 // Trying to do everything but interpolate doesn't work because convert_to_rgb
134 // doesn't work with bayer patterns.
135 // Use document mode and hack dcraw to apply white balance in the write_ function.
136                 argv[argc++] = "-d";
137         }
139         argv[argc++] = asset->path;
141         dcraw_data = (float**)frame->get_rows();
143 //Timer timer;
144         int result = dcraw_main(argc, argv);
146         char string[BCTEXTLEN];
147         sprintf(string, 
148                 "%f %f %f %f %f %f %f %f %f\n",
149                 dcraw_matrix[0],
150                 dcraw_matrix[1],
151                 dcraw_matrix[2],
152                 dcraw_matrix[3],
153                 dcraw_matrix[4],
154                 dcraw_matrix[5],
155                 dcraw_matrix[6],
156                 dcraw_matrix[7],
157                 dcraw_matrix[8]);
160         frame->get_params()->update("DCRAW_MATRIX", string);
162 // printf("FileCR2::read_frame\n");
163 // frame->dump_params();
165         cr2_mutex.unlock();
166         return 0;
169 int FileCR2::colormodel_supported(int colormodel)
171         if(colormodel == BC_RGB_FLOAT ||
172                 colormodel == BC_RGBA_FLOAT)
173                 return colormodel;
174         return BC_RGB_FLOAT;