2 dcraw.c -- Dave Coffin's raw photo decoder
3 Copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net
5 This is a command-line ANSI C program to convert raw photos from
6 any digital camera on any computer running any operating system.
8 No license is required to download and use dcraw.c. However,
9 to lawfully redistribute dcraw, you must either (a) offer, at
10 no extra charge, full source code* for all executable files
11 containing RESTRICTED functions, (b) distribute this code under
12 the GPL Version 2 or later, (c) remove all RESTRICTED functions,
13 re-implement them, or copy them from an earlier, unrestricted
14 Revision of dcraw.c, or (d) purchase a license from the author.
16 The functions that process Foveon images have been RESTRICTED
17 since Revision 1.237. All other code remains free for all uses.
19 *If you have not modified dcraw.c in any way, a link to my
20 homepage qualifies as "full source code".
23 $Date: 2018/06/01 20:36:25 $
36 #define uchar unsigned char
39 #define ushort unsigned short
47 // ZEROd by DCRaw reset
52 char *meta_data, xtrans[6][6], xtrans_abs[6][6];
53 char cdesc[5], desc[512], make[64], model[64], model2[64], artist[64];
54 float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
56 off_t strip_offset, data_offset;
57 off_t thumb_offset, meta_offset, profile_offset;
58 unsigned shot_order, kodak_cbpp, exif_cfa, unique_id;
59 unsigned thumb_length, meta_length, profile_length;
60 unsigned thumb_misc, *oprof, fuji_layout;
61 unsigned tiff_nifds, tiff_samples, tiff_bps, tiff_compress;
62 unsigned black, maximum, mix_green, raw_color, zero_is_bad;
63 unsigned zero_after_ff, is_raw, dng_version, is_foveon, data_error;
64 unsigned tile_width, tile_length, gpsdata[32], load_flags;
65 unsigned flip, tiff_flip, filters, colors;
66 ushort raw_height, raw_width, height, width, top_margin, left_margin;
67 ushort shrink, iheight, iwidth, fuji_width, thumb_width, thumb_height;
68 ushort *raw_image, (*image)[4], cblack[4102];
69 ushort white[8][8], curve[0x10000], cr2_slice[3], sraw_mul[4];
71 unsigned shot_select, multi_out;
72 double pixel_aspect, aber[4], gamm[6];
73 float bright, user_mul[4], threshold;
75 int half_size, four_color_rgb, document_mode, highlight;
76 int verbose, use_auto_wb, use_camera_wb, use_camera_matrix;
77 int output_color, output_bps, output_tiff, med_passes;
80 float cam_mul[4], pre_mul[4], cmatrix[3][4], rgb_cam[3][4];
81 int histogram[4][0x2000];
82 void (CLASS *write_thumb)(), (CLASS *write_fun)();
83 void (CLASS *load_raw)(), (CLASS *thumb_load_raw)();
87 struct decode *branch[2];
89 } first_decode[2048], /* *second_decode, CINELERRA */ *free_decode;
92 int width, height, bps, comp, phint, offset, flip, samples, bytes;
93 int tile_width, tile_length;
98 int format, key_off, tag_21a;
99 int black, split_col, black_col, split_row, black_row;
105 int gbh_vbits, gbh_reset;
109 unsigned sony_pad[128], sony_p;
110 unsigned fov_huff[1024];
111 float clb_cbrt[0x10000], clb_xyz_cam[3][4];
112 uchar pana_buf[0x4000]; int pana_vbits;
114 static const double xyz_rgb[3][3];
115 static const float d65_white[3];
119 class DCRaw : public DCRaw_data {
121 int fcol(int row,int col);
123 char *my_memmem(char *haystack,size_t haystacklen,char *needle,size_t needlelen);
124 char *my_strcasestr(char *haystack,const char *needle);
126 void merror(void *ptr,const char *where);
128 ushort sget2(uchar *s);
130 unsigned sget4(uchar *s);
132 unsigned getint(int type);
133 float int_to_float(int i);
134 double getreal(int type);
135 void read_shorts(ushort *pixel,int count);
136 void cubic_spline(const int *x_,const int *y_,const int len);
137 void canon_600_fixed_wb(int temp);
138 int canon_600_color(int ratio[2],int mar);
139 void canon_600_auto_wb(void);
140 void canon_600_coeff(void);
141 void canon_600_load_raw(void);
142 void canon_600_correct(void);
143 int canon_s2is(void);
144 unsigned getbithuff(int nbits,ushort *huff);
145 ushort *make_decoder_ref(const uchar **source);
146 ushort *make_decoder(const uchar *source);
147 void crw_init_tables(unsigned table,ushort *huff[2]);
148 int canon_has_lowbits(void);
149 void canon_load_raw(void);
150 int ljpeg_start(struct jhead *jh,int info_only);
151 void ljpeg_end(struct jhead *jh);
152 int ljpeg_diff(ushort *huff);
153 ushort *ljpeg_row(int jrow,struct jhead *jh);
154 void lossless_jpeg_load_raw(void);
155 void canon_sraw_load_raw(void);
156 void adobe_copy_pixel(unsigned row,unsigned col,ushort **rp);
157 void ljpeg_idct(struct jhead *jh);
158 void lossless_dng_load_raw(void);
159 void packed_dng_load_raw(void);
160 void pentax_load_raw(void);
161 void nikon_load_raw(void);
162 void nikon_yuv_load_raw(void);
163 int nikon_e995(void);
164 int nikon_e2100(void);
165 void nikon_3700(void);
166 int minolta_z2(void);
167 void ppm_thumb(void);
168 void ppm16_thumb(void);
169 void layer_thumb(void);
170 void rollei_thumb(void);
171 void rollei_load_raw(void);
172 int raw(unsigned row,unsigned col);
173 void phase_one_flat_field(int is_float,int nc);
174 void phase_one_correct(void);
175 void phase_one_load_raw(void);
176 unsigned ph1_bithuff(int nbits,ushort *huff);
177 void phase_one_load_raw_c(void);
178 void hasselblad_load_raw(void);
179 void leaf_hdr_load_raw(void);
180 void unpacked_load_raw(void);
181 void sinar_4shot_load_raw(void);
182 void imacon_full_load_raw(void);
183 void packed_load_raw(void);
184 void nokia_load_raw(void);
185 void canon_rmf_load_raw(void);
186 unsigned pana_bits(int nbits);
187 void panasonic_load_raw(void);
188 void olympus_load_raw(void);
189 void canon_crx_load_raw();
190 void fuji_xtrans_load_raw();
191 void minolta_rd175_load_raw(void);
192 void quicktake_100_load_raw(void);
193 void kodak_radc_load_raw(void);
194 void kodak_jpeg_load_raw(void);
195 void lossy_dng_load_raw(void);
197 boolean fill_input_buffer(j_decompress_ptr cinfo);
199 void kodak_dc120_load_raw(void);
200 void eight_bit_load_raw(void);
201 void kodak_c330_load_raw(void);
202 void kodak_c603_load_raw(void);
203 void kodak_262_load_raw(void);
204 int kodak_65000_decode(short *out,int bsize);
205 void kodak_65000_load_raw(void);
206 void kodak_ycbcr_load_raw(void);
207 void kodak_rgb_load_raw(void);
208 void kodak_thumb_load_raw(void);
209 void sony_decrypt(unsigned *data,int len,int start,int key);
210 void sony_load_raw(void);
211 void sony_arw_load_raw(void);
212 void sony_arw2_load_raw(void);
213 void samsung_load_raw(void);
214 void samsung2_load_raw(void);
215 void samsung3_load_raw(void);
216 void smal_decode_segment(unsigned seg[2][2],int holes);
217 void smal_v6_load_raw(void);
219 void fill_holes(int holes);
220 void smal_v9_load_raw(void);
221 void redcine_load_raw(void);
222 void foveon_decoder(unsigned size,unsigned code);
223 void foveon_thumb(void);
224 void foveon_sd_load_raw(void);
225 void foveon_huff(ushort *huff);
226 void foveon_dp_load_raw(void);
227 void foveon_load_camf(void);
228 const char *foveon_camf_param(const char *block,const char *param);
229 void *foveon_camf_matrix(unsigned dim[3],const char *name);
230 int foveon_fixed(void *ptr,int size,const char *name);
231 float foveon_avg(short *pix,int range[2],float cfilt);
232 short *foveon_make_curve(double max,double mul,double filt);
233 void foveon_make_curves(short **curvep,float dq[3],float div[3],float filt);
234 int foveon_apply_curve(short *curve,int i);
235 void foveon_interpolate(void);
236 void crop_masked_pixels(void);
237 void remove_zeroes(void);
238 void bad_pixels(const char *cfname);
239 void subtract(const char *fname);
240 void gamma_curve(double pwr,double ts,int mode,int imax);
241 void pseudoinverse (double (*in)[3],double (*out)[3],int size);
242 void cam_xyz_coeff(float rgb_cam[3][4],double cam_xyz[4][3]);
244 void colorcheck(void);
246 void hat_transform(float *temp,float *base,int st,int size,int sc);
247 void wavelet_denoise(void);
248 void scale_colors(void);
249 void pre_interpolate(void);
250 void border_interpolate(int border);
251 void lin_interpolate(void);
252 void vng_interpolate(void);
253 void ppg_interpolate(void);
254 void cielab(ushort rgb[3],short lab[3]);
255 void xtrans_interpolate(int passes);
256 void ahd_interpolate(void);
257 void median_filter(void);
258 void blend_highlights(void);
259 void recover_highlights(void);
260 void tiff_get(unsigned base,unsigned *tag,unsigned *type,unsigned *len,unsigned *save);
261 void parse_thumb_note(int base,unsigned toff,unsigned tlen);
262 void parse_makernote(int base,int uptag);
263 void get_timestamp(int reversed);
264 void parse_exif(int base);
265 void parse_gps(int base);
266 void romm_coeff(float romm_cam[3][3]);
267 void parse_mos(int offset);
268 void linear_table(unsigned len);
269 void parse_kodak_ifd(int base);
270 int parse_tiff_ifd(int base);
271 int parse_tiff(int base);
272 void apply_tiff(void);
273 void parse_minolta(int base);
274 void parse_external_jpeg(void);
275 void ciff_block_1030(void);
276 void parse_ciff(int offset,int length,int depth);
277 void parse_rollei(void);
278 void parse_sinar_ia(void);
279 void parse_phase_one(int base);
280 void parse_fuji(int offset);
281 int parse_jpeg(int offset);
282 void parse_riff(void);
283 void parse_crx(int end);
284 void parse_qt(int end);
285 void parse_smal(int offset,int fsize);
286 void parse_cine(void);
287 void parse_redcine(void);
288 char *foveon_gets(int offset,char *str,int len);
289 void parse_foveon(void);
290 void adobe_coeff(const char *make,const char *model);
291 void simple_coeff(int index);
292 short guess_byte_order(int words);
293 float find_green(int bps,int bite,int off0,int off1);
296 void apply_profile(const char *input,const char *output);
298 void convert_to_rgb(void);
299 void fuji_rotate(void);
301 int flip_index(int row, int col);
302 void tiff_set(struct tiff_hdr *th,ushort *ntag,ushort tag,ushort type,int count,int val);
303 void tiff_head(struct tiff_hdr *th,int full);
304 void jpeg_thumb(void);
305 void write_ppm_tiff(void);
306 void write_cinelerra(void);
316 int main(int argc, const char **argv);