1 /* -*- C++ -*-
2  * File: libraw_internal.h
3  * Copyright 2008-2018 LibRaw LLC (info@libraw.org)
4  * Created: Sat Mar  8 , 2008
5  *
6  * LibRaw internal data structures (not visible outside)
7 
8 LibRaw is free software; you can redistribute it and/or modify
9 it under the terms of the one of two licenses as you choose:
10 
11 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13 
14 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16 
17  */
18 
19 #ifndef _LIBRAW_INTERNAL_TYPES_H
20 #define _LIBRAW_INTERNAL_TYPES_H
21 
22 #include <stdio.h>
23 #ifdef __cplusplus
24 
25 #ifdef LIBRAW_LIBRARY_BUILD
26 #ifndef CLASS
27 #define CLASS LibRaw::
28 #endif
29 #endif
30 
31 #else
32 #ifndef CLASS
33 #define CLASS
34 #endif
35 #endif
36 
37 #ifdef __cplusplus
38 
39 #include "libraw_datastream.h"
40 #include "libraw_types.h"
41 
42 class LibRaw_TLS
43 {
44 public:
45   struct
46   {
47     unsigned bitbuf;
48     int vbits, reset;
49   } getbits;
50   struct
51   {
52     UINT64 bitbuf;
53     int vbits;
54 
55   } ph1_bits;
56   struct
57   {
58     unsigned pad[128], p;
59   } sony_decrypt;
60   struct
61   {
62     uchar buf[0x4002];
63     int vpos, padding;
64   } pana_data;
65   uchar jpeg_buffer[4096];
66   struct
67   {
68     float cbrt[0x10000], xyz_cam[3][4];
69   } ahd_data;
init()70   void init()
71   {
72     getbits.bitbuf = 0;
73     getbits.vbits = getbits.reset = 0;
74     ph1_bits.bitbuf = 0;
75     ph1_bits.vbits = 0;
76     pana_data.vpos = 0;
77     ahd_data.cbrt[0] = -2.0f;
78   }
79 };
80 
81 class LibRaw_constants
82 {
83 public:
84   static const float d65_white[3];
85   static const double xyz_rgb[3][3];
86 };
87 #endif /* __cplusplus */
88 
89 typedef struct
90 {
91 #ifndef __cplusplus
92   struct
93 #endif
94       LibRaw_abstract_datastream *input;
95   FILE *output;
96   int input_internal;
97   char *meta_data;
98   INT64 profile_offset;
99   INT64 toffset;
100   unsigned pana_black[4];
101 
102 } internal_data_t;
103 
104 #define LIBRAW_HISTOGRAM_SIZE 0x2000
105 typedef struct
106 {
107   int (*histogram)[LIBRAW_HISTOGRAM_SIZE];
108   unsigned *oprof;
109 } output_data_t;
110 
111 typedef struct
112 {
113   unsigned olympus_exif_cfa;
114   unsigned unique_id;
115   unsigned long long OlyID;
116   unsigned tiff_nifds;
117   int tiff_flip;
118 } identify_data_t;
119 
120 typedef struct
121 {
122   short order;
123   ushort sraw_mul[4], cr2_slice[3];
124   unsigned kodak_cbpp;
125   INT64 strip_offset, data_offset;
126   INT64 meta_offset;
127   unsigned data_size;
128   unsigned meta_length;
129   unsigned thumb_misc;
130   unsigned fuji_layout;
131   unsigned tiff_samples;
132   unsigned tiff_bps;
133   unsigned tiff_compress;
134   unsigned zero_after_ff;
135   unsigned tile_width, tile_length, load_flags;
136   unsigned data_error;
137   int hasselblad_parser_flag;
138   long long posRAFData;
139   unsigned lenRAFData;
140   int fuji_total_lines, fuji_total_blocks, fuji_block_width, fuji_bits, fuji_raw_type;
141  int pana_encoding, pana_bpp;
142 } unpacker_data_t;
143 
144 typedef struct
145 {
146   internal_data_t internal_data;
147   libraw_internal_output_params_t internal_output_params;
148   output_data_t output_data;
149   identify_data_t identify_data;
150   unpacker_data_t unpacker_data;
151 } libraw_internal_data_t;
152 
153 struct decode
154 {
155   struct decode *branch[2];
156   int leaf;
157 };
158 
159 struct tiff_ifd_t
160 {
161   int t_width, t_height, bps, comp, phint, offset, t_flip, samples, bytes;
162   int t_tile_width, t_tile_length, sample_format, predictor;
163   int rows_per_strip;
164   int *strip_offsets, strip_offsets_count;
165   int *strip_byte_counts, strip_byte_counts_count;
166   float t_shutter;
167   /* Per-IFD DNG fields */
168   INT64 opcode2_offset;
169   INT64 lineartable_offset;
170   int lineartable_len;
171   libraw_dng_color_t dng_color[2];
172   libraw_dng_levels_t dng_levels;
173 };
174 
175 struct jhead
176 {
177   int algo, bits, high, wide, clrs, sraw, psv, restart, vpred[6];
178   ushort quant[64], idct[64], *huff[20], *free[20], *row;
179 };
180 
181 struct libraw_tiff_tag
182 {
183   ushort tag, type;
184   int count;
185   union {
186     char c[4];
187     short s[2];
188     int i;
189   } val;
190 };
191 
192 struct tiff_hdr
193 {
194   ushort t_order, magic;
195   int ifd;
196   ushort pad, ntag;
197   struct libraw_tiff_tag tag[23];
198   int nextifd;
199   ushort pad2, nexif;
200   struct libraw_tiff_tag exif[4];
201   ushort pad3, ngps;
202   struct libraw_tiff_tag gpst[10];
203   short bps[4];
204   int rat[10];
205   unsigned gps[26];
206   char t_desc[512], t_make[64], t_model[64], soft[32], date[20], t_artist[64];
207 };
208 
209 #ifdef DEBUG_STAGE_CHECKS
210 #define CHECK_ORDER_HIGH(expected_stage)                                                                               \
211   do                                                                                                                   \
212   {                                                                                                                    \
213     if ((imgdata.progress_flags & LIBRAW_PROGRESS_THUMB_MASK) >= expected_stage)                                       \
214     {                                                                                                                  \
215       fprintf(stderr, "CHECK_HIGH: check %d >=  %d\n", imgdata.progress_flags &LIBRAW_PROGRESS_THUMB_MASK,             \
216               expected_stage);                                                                                         \
217       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
218     }                                                                                                                  \
219   } while (0)
220 
221 #define CHECK_ORDER_LOW(expected_stage)                                                                                \
222   do                                                                                                                   \
223   {                                                                                                                    \
224     printf("Checking LOW %d/%d : %d\n", imgdata.progress_flags, expected_stage,                                        \
225            imgdata.progress_flags < expected_stage);                                                                   \
226     if ((imgdata.progress_flags & LIBRAW_PROGRESS_THUMB_MASK) < expected_stage)                                        \
227     {                                                                                                                  \
228       printf("failed!\n");                                                                                             \
229       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
230     }                                                                                                                  \
231   } while (0)
232 #define CHECK_ORDER_BIT(expected_stage)                                                                                \
233   do                                                                                                                   \
234   {                                                                                                                    \
235     if (imgdata.progress_flags & expected_stage)                                                                       \
236       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
237   } while (0)
238 
239 #define SET_PROC_FLAG(stage)                                                                                           \
240   do                                                                                                                   \
241   {                                                                                                                    \
242     imgdata.progress_flags |= stage;                                                                                   \
243     fprintf(stderr, "SET_FLAG: %d\n", stage);                                                                          \
244   } while (0)
245 
246 #else
247 
248 #define CHECK_ORDER_HIGH(expected_stage)                                                                               \
249   do                                                                                                                   \
250   {                                                                                                                    \
251     if ((imgdata.progress_flags & LIBRAW_PROGRESS_THUMB_MASK) >= expected_stage)                                       \
252     {                                                                                                                  \
253       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
254     }                                                                                                                  \
255   } while (0)
256 
257 #define CHECK_ORDER_LOW(expected_stage)                                                                                \
258   do                                                                                                                   \
259   {                                                                                                                    \
260     if ((imgdata.progress_flags & LIBRAW_PROGRESS_THUMB_MASK) < expected_stage)                                        \
261       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
262   } while (0)
263 
264 #define CHECK_ORDER_BIT(expected_stage)                                                                                \
265   do                                                                                                                   \
266   {                                                                                                                    \
267     if (imgdata.progress_flags & expected_stage)                                                                       \
268       return LIBRAW_OUT_OF_ORDER_CALL;                                                                                 \
269   } while (0)
270 
271 #define SET_PROC_FLAG(stage)                                                                                           \
272   do                                                                                                                   \
273   {                                                                                                                    \
274     imgdata.progress_flags |= stage;                                                                                   \
275   } while (0)
276 
277 #endif
278 
279 #endif
280