4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
25 #include "bccmodels.h"
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);
42 Mutex FileCR2::dcraw_lock;
44 int FileCR2::dcraw_run(FileCR2 *file, int argc, const char **argv, VFrame *frame)
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);
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();
75 FileCR2::FileCR2(Asset *asset, File *file)
76 : FileList(asset, file, "CR2LIST", ".cr2", FILE_CR2, FILE_CR2_LIST)
79 if(asset->format == FILE_UNKNOWN)
80 asset->format = FILE_CR2;
85 //printf("FileCR2::~FileCR2\n");
94 int FileCR2::check_sig(Asset *asset)
96 char *ptr = strstr(asset->path, ".pcm");
98 //printf("FileCR2::check_sig %d\n", __LINE__);
99 FILE *stream = fopen(asset->path, "rb");
102 (void)fread(test, 10, 1, stream);
104 if( !strncmp("CR2LIST",test,6) ) return 1;
106 char string[BCTEXTLEN];
107 strcpy(string, asset->path);
108 const char *argv[4] = { "dcraw", "-i", string, 0 };
110 int result = dcraw_run(0, argc, argv);
111 //printf("FileCR2::check_sig %d %d\n", __LINE__, result);
115 int FileCR2::read_frame_header(char *path)
118 const char *argv[4] = { "dcraw", "-i", path, 0 };
119 int result = dcraw_run(this, argc, argv);
126 // int FileCR2::close_file()
131 void FileCR2::format_to_asset(const char *info)
133 asset->video_data = 1;
135 sscanf(info, "%d %d", &asset->width, &asset->height);
139 int FileCR2::read_frame(VFrame *frame, char *path)
141 //printf("FileCR2::read_frame\n");
143 if(frame->get_color_model() == BC_RGBA_FLOAT)
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"))
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
165 // printf("FileCR2::read_frame %d interpolate=%d white_balance=%d\n",
166 // __LINE__, file->interpolate_raw, file->white_balance_raw);
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)
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.
184 //printf("FileCR2::read_frame %d %s\n", __LINE__, path);
189 int result = dcraw_run(0, argc, argv, frame);
193 int FileCR2::colormodel_supported(int colormodel)
195 if(colormodel == BC_RGB_FLOAT ||
196 colormodel == BC_RGBA_FLOAT)
202 // Be sure to add a line to File::get_best_colormodel
203 int FileCR2::get_best_colormodel(Asset *asset, int driver)
205 //printf("FileCR2::get_best_colormodel %d\n", __LINE__);
209 // int64_t FileCR2::get_memory_usage()
211 // int64_t result = asset->width * asset->height * sizeof(float) * 3;
212 //printf("FileCR2::get_memory_usage %d " _LD "\n", __LINE__, result);
217 int FileCR2::use_path()