1 /*
2    dcraw.h - Dave Coffin's raw photo decoder - header for C++ adaptation
3    Copyright 1997-2016 by Dave Coffin, dcoffin a cybercom o net
4    Copyright 2004-2016 by Udi Fuchs, udifuchs a gmail o com
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 is a adaptation of Dave Coffin's original dcraw.c to C++.
12    It can work as either a command-line tool or called by other programs.
13  */
14 
15 #if !defined(uchar)
16 #define uchar unsigned char
17 #endif
18 #if !defined(ushort)
19 #define ushort unsigned short
20 #endif
21 
22 /*
23  * The following is somewhat ugly because of various requirements:
24  * 1. The stand-alone dcraw binary should not depend on glib
25  * 2. The amount of changes to dcraw source code should be minimal
26  * 3. On win32 fopen needs to be replaced by g_fopen
27  * 4. On other systems g_fopen is defined as a macro
28  * 5. g_fopen only exists since glib 2.6
29  */
30 #if !defined(DCRAW_NOMAIN) && defined(_WIN32)
31 #include <glib.h>
32 extern "C" {
33 #include <glib/gstdio.h>
34 }
35 #define fopen g_fopen
36 #endif
37 
38 class DCRaw
39 {
40 public:
41     /* All dcraw's global variables are members of this class. */
42     FILE *ifp, *ofp;
43     short order, fuji_dr;
44     /*const*/
45     char *ifname, *ifname_display;
46     char *meta_data, xtrans[6][6], xtrans_abs[6][6], cdesc[5], desc[512];
47     char make[64], model[64], model2[64], cm_desc[64], artist[64];
48     float flash_used, canon_ev, iso_speed, shutter, aperture, focal_len;
49     time_t timestamp;
50     off_t strip_offset, data_offset;
51     off_t thumb_offset, meta_offset, profile_offset;
52     unsigned shot_order, kodak_cbpp, exif_cfa, unique_id;
53     unsigned thumb_length, meta_length, profile_length;
54     unsigned thumb_misc, *oprof, fuji_layout, shot_select, multi_out;
55     unsigned tiff_nifds, tiff_samples, tiff_bps, tiff_compress;
56     unsigned black, maximum, mix_green, raw_color, zero_is_bad;
57     unsigned zero_after_ff, is_raw, dng_version, is_foveon, data_error;
58     unsigned tile_width, tile_length, gpsdata[32], load_flags;
59     unsigned flip, tiff_flip, filters, colors;
60     ushort raw_height, raw_width, height, width, top_margin, left_margin;
61     ushort shrink, iheight, iwidth, fuji_width, thumb_width, thumb_height;
62     ushort *raw_image, (*image)[4], cblack[4102];
63     ushort white[8][8], curve[0x10000], cr2_slice[3], sraw_mul[4];
64     double pixel_aspect, aber[4], gamm[6];
65     float bright, user_mul[4], threshold;
66     int mask[8][4];
67     int half_size, four_color_rgb, document_mode, highlight;
68     int verbose, use_auto_wb, use_camera_wb, use_camera_matrix;
69     int output_color, output_bps, output_tiff, med_passes;
70     int no_auto_bright;
71     unsigned greybox[4];
72     float cam_mul[4], pre_mul[4], cmatrix[3][4], rgb_cam[3][4];
73     int histogram[4][0x2000];
74     void (DCRaw::*write_thumb)(), (DCRaw::*write_fun)();
75     void (DCRaw::*load_raw)(), (DCRaw::*thumb_load_raw)();
76     jmp_buf failure;
77 
78     struct decode {
79         struct decode *branch[2];
80         int leaf;
81     } first_decode[2048], *second_decode, *free_decode;
82 
83     struct tiff_ifd {
84         int width, height, bps, comp, phint, offset, flip, samples, bytes;
85         int tile_width, tile_length;
86         float shutter;
87     } tiff_ifd[10];
88 
89     struct ph1 {
90         int format, key_off, tag_21a;
91         int black, split_col, black_col, split_row, black_row;
92         float tag_210;
93     } ph1;
94 
95     int tone_curve_size, tone_curve_offset; /* Nikon Tone Curves UF*/
96     int tone_mode_offset, tone_mode_size; /* Nikon ToneComp UF*/
97 
98     /* Used by dcraw_message() */
99     char *messageBuffer;
100     int lastStatus;
101 
102     unsigned ifpReadCount;
103     unsigned ifpSize;
104     unsigned ifpStepProgress;
105     int eofCount;
106 #define STEPS 50
107     void ifpProgress(unsigned readCount);
108 // Override standard io function for integrity checks and progress report
109     size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
110     size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
111     char *fgets(char *s, int size, FILE *stream);
112     int fgetc(FILE *stream);
113 // dcraw only calls fscanf for single variables
114     int fscanf(FILE *stream, const char *format, void *ptr);
115 // calling with more variables would triger a link error
116 //int fscanf(FILE *stream, const char *format, void *ptr1, void *ptr2, ...);
117 
118     /* Initialization of the variables is done here */
119     DCRaw();
120     ~DCRaw();
121     void dcraw_message(int code, const char *format, ...);
122     /* All dcraw functions with the CLASS prefix are members of this class. */
123     int fcol(int row, int col);
124     void merror(void *ptr, const char *where);
125     void derror();
126     ushort sget2(uchar *s);
127     ushort get2();
128     unsigned sget4(uchar *s);
129     unsigned get4();
130     unsigned getint(int type);
131     float int_to_float(int i);
132     double getreal(int type);
133     void read_shorts(ushort *pixel, unsigned count);
134     void cubic_spline(const int *x_, const int *y_, const int len);
135     void canon_600_fixed_wb(int temp);
136     int canon_600_color(int ratio[2], int mar);
137     void canon_600_auto_wb();
138     void canon_600_coeff();
139     void canon_600_load_raw();
140     void canon_600_correct();
141     int canon_s2is();
142     unsigned getbithuff(int nbits, ushort *huff);
143     ushort * make_decoder_ref(const uchar **source);
144     ushort * make_decoder(const uchar *source);
145     void crw_init_tables(unsigned table, ushort *huff[2]);
146     int canon_has_lowbits();
147     void canon_load_raw();
148     int ljpeg_start(struct jhead *jh, int info_only);
149     void ljpeg_end(struct jhead *jh);
150     int ljpeg_diff(ushort *huff);
151     ushort * ljpeg_row(int jrow, struct jhead *jh);
152     void lossless_jpeg_load_raw();
153     void canon_sraw_load_raw();
154     void adobe_copy_pixel(unsigned row, unsigned col, ushort **rp);
155     void ljpeg_idct(struct jhead *jh);
156     void lossless_dng_load_raw();
157     void packed_dng_load_raw();
158     void pentax_load_raw();
159     void nikon_load_raw();
160     void nikon_yuv_load_raw();
161     int nikon_e995();
162     int nikon_e2100();
163     void nikon_3700();
164     int minolta_z2();
165     void ppm_thumb();
166     void ppm16_thumb();
167     void layer_thumb();
168     void rollei_thumb();
169     void rollei_load_raw();
170     int raw(unsigned row, unsigned col);
171     void phase_one_flat_field(int is_float, int nc);
172     void phase_one_correct();
173     void phase_one_load_raw();
174     unsigned ph1_bithuff(int nbits, ushort *huff);
175     void phase_one_load_raw_c();
176     void hasselblad_load_raw();
177     void leaf_hdr_load_raw();
178     void unpacked_load_raw();
179     void sinar_4shot_load_raw();
180     void imacon_full_load_raw();
181     void packed_load_raw();
182     void nokia_load_raw();
183     void canon_rmf_load_raw();
184     unsigned pana_bits(int nbits);
185     void panasonic_load_raw();
186     void olympus_load_raw();
187     void minolta_rd175_load_raw();
188     void quicktake_100_load_raw();
189     void kodak_radc_load_raw();
190     void kodak_jpeg_load_raw();
191     void lossy_dng_load_raw();
192     void kodak_dc120_load_raw();
193     void eight_bit_load_raw();
194     void kodak_c330_load_raw();
195     void kodak_c603_load_raw();
196     void kodak_262_load_raw();
197     int kodak_65000_decode(short *out, int bsize);
198     void kodak_65000_load_raw();
199     void kodak_ycbcr_load_raw();
200     void kodak_rgb_load_raw();
201     void kodak_thumb_load_raw();
202     void sony_decrypt(unsigned *data, int len, int start, int key);
203     void sony_load_raw();
204     void sony_arw_load_raw();
205     void sony_arw2_load_raw();
206     void samsung_load_raw();
207     void samsung2_load_raw();
208     void samsung3_load_raw();
209     void smal_decode_segment(unsigned seg[2][2], int holes);
210     void smal_v6_load_raw();
211     int median4(int *p);
212     void fill_holes(int holes);
213     void smal_v9_load_raw();
214     void redcine_load_raw();
215     void foveon_decoder(int size, unsigned code);
216     void foveon_thumb();
217     void foveon_sd_load_raw();
218     void foveon_huff(ushort *huff);
219     void foveon_dp_load_raw();
220     void canon_crx_load_raw();
221     void fuji_xtrans_load_raw();
222     void parse_crx (int end);
223     void foveon_load_camf();
224     const char * foveon_camf_param(const char *block, const char *param);
225     void * foveon_camf_matrix(unsigned dim[3], const char *name);
226     int foveon_fixed(void *ptr, int size, const char *name);
227     float foveon_avg(short *pix, int range[2], float cfilt);
228     short * foveon_make_curve(double max, double mul, double filt);
229     void foveon_make_curves
230     (short **curvep, float dq[3], float div[3], float filt);
231     int foveon_apply_curve(short *curve, int i);
232     void sigma_true_ii_interpolate();
233     void foveon_interpolate();
234     void crop_masked_pixels();
235     void remove_zeroes();
236     void bad_pixels(const char *fname);
237     void subtract(const char *fname);
238     void gamma_curve(double pwr, double ts, int mode, int imax);
239     void pseudoinverse(double(*in)[3], double(*out)[3], int size);
240     void cam_xyz_coeff(float rgb_cam[3][4], double cam_xyz[4][3]);
241     void colorcheck();
242     void hat_transform(float *temp, float *base, int st, int size, int sc);
243     void wavelet_denoise();
244     void scale_colors();
245     void pre_interpolate();
246     void border_interpolate(unsigned border);
247     void lin_interpolate();
248     void vng_interpolate();
249     void ppg_interpolate();
250     void cielab(ushort rgb[3], short lab[3]);
251     void xtrans_interpolate(int passes);
252     void ahd_interpolate();
253     void median_filter();
254     void blend_highlights();
255     void recover_highlights();
256     void tiff_get(unsigned base,
257                   unsigned *tag, unsigned *type, unsigned *len, unsigned *save);
258     void parse_thumb_note(int base, unsigned toff, unsigned tlen);
259     void parse_makernote(int base, int uptag);
260     void get_timestamp(int reversed);
261     void parse_exif(int base);
262     void parse_gps(int base);
263     void romm_coeff(float romm_cam[3][3]);
264     void parse_mos(int offset);
265     void linear_table(unsigned len);
266     void parse_kodak_ifd(int base);
267     int parse_tiff_ifd(int base);
268     int parse_tiff(int base);
269     void apply_tiff();
270     void parse_minolta(int base);
271     void parse_external_jpeg();
272     void ciff_block_1030();
273     void parse_ciff(int offset, int length, int depth);
274     void parse_rollei();
275     void parse_sinar_ia();
276     void parse_phase_one(int base);
277     void parse_fuji(int offset);
278     int parse_jpeg(int offset);
279     void parse_riff();
280     void parse_qt(int end);
281     void parse_smal(int offset, unsigned fsize);
282     void parse_cine();
283     void parse_redcine();
284     char * foveon_gets(int offset, char *str, int len);
285     void parse_foveon();
286     void adobe_coeff(const char *make, const char *model);
287     void simple_coeff(int index);
288     short guess_byte_order(int words);
289     float find_green(int bps, int bite, int off0, int off1);
290     void identify();
291 #ifndef NO_LCMS
292     void apply_profile(const char *input, const char *output);
293 #endif
294     void convert_to_rgb();
295     void fuji_rotate();
296     void stretch();
297     int flip_index(int row, int col);
298     void tiff_set(struct tiff_hdr *th, ushort *ntag,
299                   ushort tag, ushort type, int count, int val);
300     void tiff_head(struct tiff_hdr *th, int full);
301     void jpeg_thumb();
302     void write_ppm_tiff();
303     int main(int argc, const char **argv);
304 };
305