1 /* -*- C++ -*-
2  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3  *
4 
5  LibRaw is free software; you can redistribute it and/or modify
6  it under the terms of the one of two licenses as you choose:
7 
8 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
9    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
10 
11 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
12    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
13  */
14 
15 /* Library for accessing X3F Files
16 ----------------------------------------------------------------
17 BSD-style License
18 ----------------------------------------------------------------
19 
20 * Copyright (c) 2010, Roland Karlsson (roland@proxel.se)
21 * All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions are met:
25 *     * Redistributions of source code must retain the above copyright
26 *       notice, this list of conditions and the following disclaimer.
27 *     * Redistributions in binary form must reproduce the above copyright
28 *       notice, this list of conditions and the following disclaimer in the
29 *       documentation and/or other materials provided with the distribution.
30 *     * Neither the name of the organization nor the
31 *       names of its contributors may be used to endorse or promote products
32 *       derived from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY ROLAND KARLSSON ''AS IS'' AND ANY
35 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL ROLAND KARLSSON BE LIABLE FOR ANY
38 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 
45 */
46 
47 #ifndef X3F_TOOLS_H
48 #define X3F_TOOLS_H
49 
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <math.h>
54 #include <stdio.h>
55 #include "../libraw/libraw_datastream.h"
56 
57 /* From X3F_IO.H */
58 
59 #define SIZE_UNIQUE_IDENTIFIER 16
60 #define SIZE_WHITE_BALANCE 32
61 #define SIZE_COLOR_MODE 32
62 #define NUM_EXT_DATA_2_1 32
63 #define NUM_EXT_DATA_3_0 64
64 #define NUM_EXT_DATA NUM_EXT_DATA_3_0
65 
66 #define X3F_VERSION(MAJ, MIN) (uint32_t)(((MAJ) << 16) + MIN)
67 #define X3F_VERSION_2_0 X3F_VERSION(2, 0)
68 #define X3F_VERSION_2_1 X3F_VERSION(2, 1)
69 #define X3F_VERSION_2_2 X3F_VERSION(2, 2)
70 #define X3F_VERSION_2_3 X3F_VERSION(2, 3)
71 #define X3F_VERSION_3_0 X3F_VERSION(3, 0)
72 #define X3F_VERSION_4_0 X3F_VERSION(4, 0)
73 
74 /* Main file identifier */
75 #define X3F_FOVb (uint32_t)(0x62564f46)
76 /* Directory identifier */
77 #define X3F_SECd (uint32_t)(0x64434553)
78 /* Property section identifiers */
79 #define X3F_PROP (uint32_t)(0x504f5250)
80 #define X3F_SECp (uint32_t)(0x70434553)
81 /* Image section identifiers */
82 #define X3F_IMAG (uint32_t)(0x46414d49)
83 #define X3F_IMA2 (uint32_t)(0x32414d49)
84 #define X3F_SECi (uint32_t)(0x69434553)
85 /* CAMF section identifiers */
86 #define X3F_CAMF (uint32_t)(0x464d4143)
87 #define X3F_SECc (uint32_t)(0x63434553)
88 /* CAMF entry identifiers */
89 #define X3F_CMbP (uint32_t)(0x50624d43)
90 #define X3F_CMbT (uint32_t)(0x54624d43)
91 #define X3F_CMbM (uint32_t)(0x4d624d43)
92 #define X3F_CMb (uint32_t)(0x00624d43)
93 /* SDQ section identifiers ? - TODO */
94 #define X3F_SPPA (uint32_t)(0x41505053)
95 #define X3F_SECs (uint32_t)(0x73434553)
96 
97 #define X3F_IMAGE_THUMB_PLAIN (uint32_t)(0x00020003)
98 #define X3F_IMAGE_THUMB_HUFFMAN (uint32_t)(0x0002000b)
99 #define X3F_IMAGE_THUMB_JPEG (uint32_t)(0x00020012)
100 #define X3F_IMAGE_THUMB_SDQ (uint32_t)(0x00020019) /* SDQ ? - TODO */
101 
102 #define X3F_IMAGE_RAW_HUFFMAN_X530 (uint32_t)(0x00030005)
103 #define X3F_IMAGE_RAW_HUFFMAN_10BIT (uint32_t)(0x00030006)
104 #define X3F_IMAGE_RAW_TRUE (uint32_t)(0x0003001e)
105 #define X3F_IMAGE_RAW_MERRILL (uint32_t)(0x0001001e)
106 #define X3F_IMAGE_RAW_QUATTRO (uint32_t)(0x00010023)
107 #define X3F_IMAGE_RAW_SDQ (uint32_t)(0x00010025)
108 #define X3F_IMAGE_RAW_SDQH (uint32_t)(0x00010027)
109 #define X3F_IMAGE_RAW_SDQH2 (uint32_t)(0x00010029)
110 
111 #define X3F_IMAGE_HEADER_SIZE 28
112 #define X3F_CAMF_HEADER_SIZE 28
113 #define X3F_PROPERTY_LIST_HEADER_SIZE 24
114 
115 typedef uint16_t utf16_t;
116 
117 typedef int bool_t;
118 
119 typedef enum x3f_extended_types_e
120 {
121   X3F_EXT_TYPE_NONE = 0,
122   X3F_EXT_TYPE_EXPOSURE_ADJUST = 1,
123   X3F_EXT_TYPE_CONTRAST_ADJUST = 2,
124   X3F_EXT_TYPE_SHADOW_ADJUST = 3,
125   X3F_EXT_TYPE_HIGHLIGHT_ADJUST = 4,
126   X3F_EXT_TYPE_SATURATION_ADJUST = 5,
127   X3F_EXT_TYPE_SHARPNESS_ADJUST = 6,
128   X3F_EXT_TYPE_RED_ADJUST = 7,
129   X3F_EXT_TYPE_GREEN_ADJUST = 8,
130   X3F_EXT_TYPE_BLUE_ADJUST = 9,
131   X3F_EXT_TYPE_FILL_LIGHT_ADJUST = 10
132 } x3f_extended_types_t;
133 
134 typedef struct x3f_property_s
135 {
136   /* Read from file */
137   uint32_t name_offset;
138   uint32_t value_offset;
139 
140   /* Computed */
141   utf16_t *name;  /* 0x0000 terminated UTF 16 */
142   utf16_t *value; /* 0x0000 terminated UTF 16 */
143 } x3f_property_t;
144 
145 typedef struct x3f_property_table_s
146 {
147   uint32_t size;
148   x3f_property_t *element;
149 } x3f_property_table_t;
150 
151 typedef struct x3f_property_list_s
152 {
153   /* 2.0 Fields */
154   uint32_t num_properties;
155   uint32_t character_format;
156   uint32_t reserved;
157   uint32_t total_length;
158 
159   x3f_property_table_t property_table;
160 
161   void *data;
162 
163   uint32_t data_size;
164 
165 } x3f_property_list_t;
166 
167 typedef struct x3f_table8_s
168 {
169   uint32_t size;
170   uint8_t *element;
171 } x3f_table8_t;
172 
173 typedef struct x3f_table16_s
174 {
175   uint32_t size;
176   uint16_t *element;
177 } x3f_table16_t;
178 
179 typedef struct x3f_table32_s
180 {
181   uint32_t size;
182   uint32_t *element;
183 } x3f_table32_t;
184 
185 typedef struct
186 {
187   uint8_t *data; /* Pointer to actual image data */
188   void *buf;     /* Pointer to allocated buffer for free() */
189   uint32_t rows;
190   uint32_t columns;
191   uint32_t channels;
192   uint32_t row_stride;
193 } x3f_area8_t;
194 
195 typedef struct
196 {
197   uint16_t *data; /* Pointer to actual image data */
198   void *buf;      /* Pointer to allocated buffer for free() */
199   uint32_t rows;
200   uint32_t columns;
201   uint32_t channels;
202   uint32_t row_stride;
203 } x3f_area16_t;
204 
205 #define UNDEFINED_LEAF 0xffffffff
206 
207 typedef struct x3f_huffnode_s
208 {
209   struct x3f_huffnode_s *branch[2];
210   uint32_t leaf;
211 } x3f_huffnode_t;
212 
213 typedef struct x3f_hufftree_s
214 {
215   uint32_t free_node_index; /* Free node index in huffman tree array */
216   uint32_t total_node_index;
217   x3f_huffnode_t *nodes;    /* Coding tree */
218 } x3f_hufftree_t;
219 
220 typedef struct x3f_true_huffman_element_s
221 {
222   uint8_t code_size;
223   uint8_t code;
224 } x3f_true_huffman_element_t;
225 
226 typedef struct x3f_true_huffman_s
227 {
228   uint32_t size;
229   x3f_true_huffman_element_t *element;
230 } x3f_true_huffman_t;
231 
232 /* 0=bottom, 1=middle, 2=top */
233 #define TRUE_PLANES 3
234 
235 typedef struct x3f_true_s
236 {
237   uint16_t seed[TRUE_PLANES]; /* Always 512,512,512 */
238   uint16_t unknown;           /* Always 0 */
239   x3f_true_huffman_t table;   /* Huffman table - zero
240                      terminated. size is the number of
241                      leaves plus 1.*/
242 
243   x3f_table32_t plane_size;            /* Size of the 3 planes */
244   uint8_t *plane_address[TRUE_PLANES]; /* computed offset to the planes */
245   x3f_hufftree_t tree;                 /* Coding tree */
246   x3f_area16_t x3rgb16;                /* 3x16 bit X3-RGB data */
247 } x3f_true_t;
248 
249 typedef struct x3f_quattro_s
250 {
251   struct
252   {
253     uint16_t columns;
254     uint16_t rows;
255   } plane[TRUE_PLANES];
256   uint32_t unknown;
257 
258   bool_t quattro_layout;
259   x3f_area16_t top16; /* Container for the bigger top layer */
260 } x3f_quattro_t;
261 
262 typedef struct x3f_huffman_s
263 {
264   x3f_table16_t mapping;     /* Value Mapping = X3F lossy compression */
265   x3f_table32_t table;       /* Coding Table */
266   x3f_hufftree_t tree;       /* Coding tree */
267   x3f_table32_t row_offsets; /* Row offsets */
268   x3f_area8_t rgb8;          /* 3x8 bit RGB data */
269   x3f_area16_t x3rgb16;      /* 3x16 bit X3-RGB data */
270 } x3f_huffman_t;
271 
272 typedef struct x3f_image_data_s
273 {
274   /* 2.0 Fields */
275   /* ------------------------------------------------------------------ */
276   /* Known combinations of type and format are:
277      1-6, 2-3, 2-11, 2-18, 3-6 */
278   uint32_t type;        /* 1 = RAW X3 (SD1)
279                            2 = thumbnail or maybe just RGB
280                            3 = RAW X3 */
281   uint32_t format;      /* 3 = 3x8 bit pixmap
282                            6 = 3x10 bit huffman with map table
283                            11 = 3x8 bit huffman
284                            18 = JPEG */
285   uint32_t type_format; /* type<<16 + format */
286   /* ------------------------------------------------------------------ */
287 
288   uint32_t columns;    /* width / row size in pixels */
289   uint32_t rows;       /* height */
290   uint32_t row_stride; /* row size in bytes */
291 
292   /* NULL if not used */
293   x3f_huffman_t *huffman; /* Huffman help data */
294   x3f_true_t *tru;        /* TRUE help data */
295   x3f_quattro_t *quattro; /* Quattro help data */
296 
297   void *data; /* Take from file if NULL. Otherwise,
298                  this is the actual data bytes in
299                  the file. */
300   uint32_t data_size;
301 
302 } x3f_image_data_t;
303 
304 typedef struct camf_dim_entry_s
305 {
306   uint32_t size;
307   uint32_t name_offset;
308   uint32_t n; /* 0,1,2,3... */
309   char *name;
310 } camf_dim_entry_t;
311 
312 typedef enum
313 {
314   M_FLOAT,
315   M_INT,
316   M_UINT
317 } matrix_type_t;
318 
319 typedef struct camf_entry_s
320 {
321   /* pointer into decoded data */
322   void *entry;
323 
324   /* entry header */
325   uint32_t id;
326   uint32_t version;
327   uint32_t entry_size;
328   uint32_t name_offset;
329   uint32_t value_offset;
330 
331   /* computed values */
332   char *name_address;
333   void *value_address;
334   uint32_t name_size;
335   uint32_t value_size;
336 
337   /* extracted values for explicit CAMF entry types*/
338   uint32_t text_size;
339   char *text;
340 
341   uint32_t property_num;
342   char **property_name;
343   uint8_t **property_value;
344 
345   uint32_t matrix_dim;
346   camf_dim_entry_t *matrix_dim_entry;
347 
348   /* Offset, pointer and size and type of raw data */
349   uint32_t matrix_type;
350   uint32_t matrix_data_off;
351   void *matrix_data;
352   uint32_t matrix_element_size;
353 
354   /* Pointer and type of copied data */
355   matrix_type_t matrix_decoded_type;
356   void *matrix_decoded;
357 
358   /* Help data to try to estimate element size */
359   uint32_t matrix_elements;
360   uint32_t matrix_used_space;
361   double matrix_estimated_element_size;
362 
363 } camf_entry_t;
364 
365 typedef struct camf_entry_table_s
366 {
367   uint32_t size;
368   camf_entry_t *element;
369 } camf_entry_table_t;
370 
371 typedef struct x3f_camf_typeN_s
372 {
373   uint32_t val0;
374   uint32_t val1;
375   uint32_t val2;
376   uint32_t val3;
377 } x3f_camf_typeN_t;
378 
379 typedef struct x3f_camf_type2_s
380 {
381   uint32_t reserved;
382   uint32_t infotype;
383   uint32_t infotype_version;
384   uint32_t crypt_key;
385 } x3f_camf_type2_t;
386 
387 typedef struct x3f_camf_type4_s
388 {
389   uint32_t decoded_data_size;
390   uint32_t decode_bias;
391   uint32_t block_size;
392   uint32_t block_count;
393 } x3f_camf_type4_t;
394 
395 typedef struct x3f_camf_type5_s
396 {
397   uint32_t decoded_data_size;
398   uint32_t decode_bias;
399   uint32_t unknown2;
400   uint32_t unknown3;
401 } x3f_camf_type5_t;
402 
403 typedef struct x3f_camf_s
404 {
405 
406   /* Header info */
407   uint32_t type;
408   union {
409     x3f_camf_typeN_t tN;
410     x3f_camf_type2_t t2;
411     x3f_camf_type4_t t4;
412     x3f_camf_type5_t t5;
413   };
414 
415   /* The encrypted raw data */
416   void *data;
417   uint32_t data_size;
418 
419   /* Help data for type 4 Huffman compression */
420   x3f_true_huffman_t table;
421   x3f_hufftree_t tree;
422   uint8_t *decoding_start;
423   uint32_t decoding_size;
424 
425   /* The decrypted data */
426   void *decoded_data;
427   uint32_t decoded_data_size;
428 
429   /* Pointers into the decrypted data */
430   camf_entry_table_t entry_table;
431 } x3f_camf_t;
432 
433 typedef struct x3f_directory_entry_header_s
434 {
435   uint32_t identifier; /* Should be ´SECp´, "SECi", ... */
436   uint32_t version;    /* 0x00020001 is version 2.1  */
437   union {
438     x3f_property_list_t property_list;
439     x3f_image_data_t image_data;
440     x3f_camf_t camf;
441   } data_subsection;
442 } x3f_directory_entry_header_t;
443 
444 typedef struct x3f_directory_entry_s
445 {
446   struct
447   {
448     uint32_t offset;
449     uint32_t size;
450   } input, output;
451 
452   uint32_t type;
453 
454   x3f_directory_entry_header_t header;
455 } x3f_directory_entry_t;
456 
457 typedef struct x3f_directory_section_s
458 {
459   uint32_t identifier; /* Should be ´SECd´ */
460   uint32_t version;    /* 0x00020001 is version 2.1  */
461 
462   /* 2.0 Fields */
463   uint32_t num_directory_entries;
464   x3f_directory_entry_t *directory_entry;
465 } x3f_directory_section_t;
466 
467 typedef struct x3f_header_s
468 {
469   /* 2.0 Fields */
470   uint32_t identifier; /* Should be ´FOVb´ */
471   uint32_t version;    /* 0x00020001 means 2.1 */
472   uint8_t unique_identifier[SIZE_UNIQUE_IDENTIFIER];
473   uint32_t mark_bits;
474   uint32_t columns;  /* Columns and rows ... */
475   uint32_t rows;     /* ... before rotation */
476   uint32_t rotation; /* 0, 90, 180, 270 */
477 
478   char white_balance[SIZE_WHITE_BALANCE]; /* Introduced in 2.1 */
479   char color_mode[SIZE_COLOR_MODE];       /* Introduced in 2.3 */
480 
481   /* Introduced in 2.1 and extended from 32 to 64 in 3.0 */
482   uint8_t extended_types[NUM_EXT_DATA]; /* x3f_extended_types_t */
483   float extended_data[NUM_EXT_DATA];    /* 32 bits, but do type differ? */
484 } x3f_header_t;
485 
486 typedef struct x3f_info_s
487 {
488   char *error;
489   struct
490   {
491     LibRaw_abstract_datastream *file; /* Use if more data is needed */
492   } input, output;
493 } x3f_info_t;
494 
495 typedef struct x3f_s
496 {
497   x3f_info_t info;
498   x3f_header_t header;
499   x3f_directory_section_t directory_section;
500 } x3f_t;
501 
502 typedef enum x3f_return_e
503 {
504   X3F_OK = 0,
505   X3F_ARGUMENT_ERROR = 1,
506   X3F_INFILE_ERROR = 2,
507   X3F_OUTFILE_ERROR = 3,
508   X3F_INTERNAL_ERROR = 4
509 } x3f_return_t;
510 
511 x3f_return_t x3f_delete(x3f_t *x3f);
512 
513 /* Hacky external flags                                                 */
514 /* --------------------------------------------------------------------- */
515 
516 extern int legacy_offset;
517 extern bool_t auto_legacy_offset;
518 
519 /* --------------------------------------------------------------------- */
520 /* Huffman Decode Macros                                                 */
521 /* --------------------------------------------------------------------- */
522 
523 #define HUF_TREE_MAX_LENGTH 27
524 #define HUF_TREE_MAX_NODES(_leaves) ((HUF_TREE_MAX_LENGTH + 1) * (_leaves))
525 #define HUF_TREE_GET_LENGTH(_v) (((_v) >> 27) & 0x1f)
526 #define HUF_TREE_GET_CODE(_v) ((_v)&0x07ffffff)
527 
528 x3f_t *x3f_new_from_file(LibRaw_abstract_datastream *infile);
529 x3f_return_t x3f_delete(x3f_t *x3f);
530 x3f_directory_entry_t *x3f_get_raw(x3f_t *x3f);
531 x3f_directory_entry_t *x3f_get_thumb_plain(x3f_t *x3f);
532 x3f_return_t x3f_load_data(x3f_t *x3f, x3f_directory_entry_t *DE);
533 x3f_directory_entry_t *x3f_get_thumb_huffman(x3f_t *x3f);
534 x3f_directory_entry_t *x3f_get_thumb_jpeg(x3f_t *x3f);
535 x3f_directory_entry_t *x3f_get_camf(x3f_t *x3f);
536 x3f_directory_entry_t *x3f_get_prop(x3f_t *x3f);
537 /* extern */ int64_t x3f_load_data_size(x3f_t *x3f, x3f_directory_entry_t *DE);
538 
539 #endif
540