File: x3f_tools.h

package info (click to toggle)
libraw 0.21.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,496 kB
  • sloc: cpp: 53,067; ansic: 2,430; makefile: 133; sh: 82; perl: 58
file content (539 lines) | stat: -rw-r--r-- 14,883 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* -*- C++ -*-
 * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
 *

 LibRaw is free software; you can redistribute it and/or modify
 it under the terms of the one of two licenses as you choose:

1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).

2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
 */

/* Library for accessing X3F Files
----------------------------------------------------------------
BSD-style License
----------------------------------------------------------------

* Copyright (c) 2010, Roland Karlsson (roland@proxel.se)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the organization nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROLAND KARLSSON ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROLAND KARLSSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef X3F_TOOLS_H
#define X3F_TOOLS_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include "../libraw/libraw_datastream.h"

/* From X3F_IO.H */

#define SIZE_UNIQUE_IDENTIFIER 16
#define SIZE_WHITE_BALANCE 32
#define SIZE_COLOR_MODE 32
#define NUM_EXT_DATA_2_1 32
#define NUM_EXT_DATA_3_0 64
#define NUM_EXT_DATA NUM_EXT_DATA_3_0

#define X3F_VERSION(MAJ, MIN) (uint32_t)(((MAJ) << 16) + MIN)
#define X3F_VERSION_2_0 X3F_VERSION(2, 0)
#define X3F_VERSION_2_1 X3F_VERSION(2, 1)
#define X3F_VERSION_2_2 X3F_VERSION(2, 2)
#define X3F_VERSION_2_3 X3F_VERSION(2, 3)
#define X3F_VERSION_3_0 X3F_VERSION(3, 0)
#define X3F_VERSION_4_0 X3F_VERSION(4, 0)

/* Main file identifier */
#define X3F_FOVb (uint32_t)(0x62564f46)
/* Directory identifier */
#define X3F_SECd (uint32_t)(0x64434553)
/* Property section identifiers */
#define X3F_PROP (uint32_t)(0x504f5250)
#define X3F_SECp (uint32_t)(0x70434553)
/* Image section identifiers */
#define X3F_IMAG (uint32_t)(0x46414d49)
#define X3F_IMA2 (uint32_t)(0x32414d49)
#define X3F_SECi (uint32_t)(0x69434553)
/* CAMF section identifiers */
#define X3F_CAMF (uint32_t)(0x464d4143)
#define X3F_SECc (uint32_t)(0x63434553)
/* CAMF entry identifiers */
#define X3F_CMbP (uint32_t)(0x50624d43)
#define X3F_CMbT (uint32_t)(0x54624d43)
#define X3F_CMbM (uint32_t)(0x4d624d43)
#define X3F_CMb (uint32_t)(0x00624d43)
/* SDQ section identifiers ? - TODO */
#define X3F_SPPA (uint32_t)(0x41505053)
#define X3F_SECs (uint32_t)(0x73434553)

#define X3F_IMAGE_THUMB_PLAIN (uint32_t)(0x00020003)
#define X3F_IMAGE_THUMB_HUFFMAN (uint32_t)(0x0002000b)
#define X3F_IMAGE_THUMB_JPEG (uint32_t)(0x00020012)
#define X3F_IMAGE_THUMB_SDQ (uint32_t)(0x00020019) /* SDQ ? - TODO */

#define X3F_IMAGE_RAW_HUFFMAN_X530 (uint32_t)(0x00030005)
#define X3F_IMAGE_RAW_HUFFMAN_10BIT (uint32_t)(0x00030006)
#define X3F_IMAGE_RAW_TRUE (uint32_t)(0x0003001e)
#define X3F_IMAGE_RAW_MERRILL (uint32_t)(0x0001001e)
#define X3F_IMAGE_RAW_QUATTRO (uint32_t)(0x00010023)
#define X3F_IMAGE_RAW_SDQ (uint32_t)(0x00010025)
#define X3F_IMAGE_RAW_SDQH (uint32_t)(0x00010027)
#define X3F_IMAGE_RAW_SDQH2 (uint32_t)(0x00010029)

#define X3F_IMAGE_HEADER_SIZE 28
#define X3F_CAMF_HEADER_SIZE 28
#define X3F_PROPERTY_LIST_HEADER_SIZE 24

typedef uint16_t utf16_t;

typedef int bool_t;

typedef enum x3f_extended_types_e
{
  X3F_EXT_TYPE_NONE = 0,
  X3F_EXT_TYPE_EXPOSURE_ADJUST = 1,
  X3F_EXT_TYPE_CONTRAST_ADJUST = 2,
  X3F_EXT_TYPE_SHADOW_ADJUST = 3,
  X3F_EXT_TYPE_HIGHLIGHT_ADJUST = 4,
  X3F_EXT_TYPE_SATURATION_ADJUST = 5,
  X3F_EXT_TYPE_SHARPNESS_ADJUST = 6,
  X3F_EXT_TYPE_RED_ADJUST = 7,
  X3F_EXT_TYPE_GREEN_ADJUST = 8,
  X3F_EXT_TYPE_BLUE_ADJUST = 9,
  X3F_EXT_TYPE_FILL_LIGHT_ADJUST = 10
} x3f_extended_types_t;

typedef struct x3f_property_s
{
  /* Read from file */
  uint32_t name_offset;
  uint32_t value_offset;

  /* Computed */
  utf16_t *name;  /* 0x0000 terminated UTF 16 */
  utf16_t *value; /* 0x0000 terminated UTF 16 */
} x3f_property_t;

typedef struct x3f_property_table_s
{
  uint32_t size;
  x3f_property_t *element;
} x3f_property_table_t;

typedef struct x3f_property_list_s
{
  /* 2.0 Fields */
  uint32_t num_properties;
  uint32_t character_format;
  uint32_t reserved;
  uint32_t total_length;

  x3f_property_table_t property_table;

  void *data;

  uint32_t data_size;

} x3f_property_list_t;

typedef struct x3f_table8_s
{
  uint32_t size;
  uint8_t *element;
} x3f_table8_t;

typedef struct x3f_table16_s
{
  uint32_t size;
  uint16_t *element;
} x3f_table16_t;

typedef struct x3f_table32_s
{
  uint32_t size;
  uint32_t *element;
} x3f_table32_t;

typedef struct
{
  uint8_t *data; /* Pointer to actual image data */
  void *buf;     /* Pointer to allocated buffer for free() */
  uint32_t rows;
  uint32_t columns;
  uint32_t channels;
  uint32_t row_stride;
} x3f_area8_t;

typedef struct
{
  uint16_t *data; /* Pointer to actual image data */
  void *buf;      /* Pointer to allocated buffer for free() */
  uint32_t rows;
  uint32_t columns;
  uint32_t channels;
  uint32_t row_stride;
} x3f_area16_t;

#define UNDEFINED_LEAF 0xffffffff

typedef struct x3f_huffnode_s
{
  struct x3f_huffnode_s *branch[2];
  uint32_t leaf;
} x3f_huffnode_t;

typedef struct x3f_hufftree_s
{
  uint32_t free_node_index; /* Free node index in huffman tree array */
  uint32_t total_node_index;
  x3f_huffnode_t *nodes;    /* Coding tree */
} x3f_hufftree_t;

typedef struct x3f_true_huffman_element_s
{
  uint8_t code_size;
  uint8_t code;
} x3f_true_huffman_element_t;

typedef struct x3f_true_huffman_s
{
  uint32_t size;
  x3f_true_huffman_element_t *element;
} x3f_true_huffman_t;

/* 0=bottom, 1=middle, 2=top */
#define TRUE_PLANES 3

typedef struct x3f_true_s
{
  uint16_t seed[TRUE_PLANES]; /* Always 512,512,512 */
  uint16_t unknown;           /* Always 0 */
  x3f_true_huffman_t table;   /* Huffman table - zero
                     terminated. size is the number of
                     leaves plus 1.*/

  x3f_table32_t plane_size;            /* Size of the 3 planes */
  uint8_t *plane_address[TRUE_PLANES]; /* computed offset to the planes */
  x3f_hufftree_t tree;                 /* Coding tree */
  x3f_area16_t x3rgb16;                /* 3x16 bit X3-RGB data */
} x3f_true_t;

typedef struct x3f_quattro_s
{
  struct
  {
    uint16_t columns;
    uint16_t rows;
  } plane[TRUE_PLANES];
  uint32_t unknown;

  bool_t quattro_layout;
  x3f_area16_t top16; /* Container for the bigger top layer */
} x3f_quattro_t;

typedef struct x3f_huffman_s
{
  x3f_table16_t mapping;     /* Value Mapping = X3F lossy compression */
  x3f_table32_t table;       /* Coding Table */
  x3f_hufftree_t tree;       /* Coding tree */
  x3f_table32_t row_offsets; /* Row offsets */
  x3f_area8_t rgb8;          /* 3x8 bit RGB data */
  x3f_area16_t x3rgb16;      /* 3x16 bit X3-RGB data */
} x3f_huffman_t;

typedef struct x3f_image_data_s
{
  /* 2.0 Fields */
  /* ------------------------------------------------------------------ */
  /* Known combinations of type and format are:
     1-6, 2-3, 2-11, 2-18, 3-6 */
  uint32_t type;        /* 1 = RAW X3 (SD1)
                           2 = thumbnail or maybe just RGB
                           3 = RAW X3 */
  uint32_t format;      /* 3 = 3x8 bit pixmap
                           6 = 3x10 bit huffman with map table
                           11 = 3x8 bit huffman
                           18 = JPEG */
  uint32_t type_format; /* type<<16 + format */
  /* ------------------------------------------------------------------ */

  uint32_t columns;    /* width / row size in pixels */
  uint32_t rows;       /* height */
  uint32_t row_stride; /* row size in bytes */

  /* NULL if not used */
  x3f_huffman_t *huffman; /* Huffman help data */
  x3f_true_t *tru;        /* TRUE help data */
  x3f_quattro_t *quattro; /* Quattro help data */

  void *data; /* Take from file if NULL. Otherwise,
                 this is the actual data bytes in
                 the file. */
  uint32_t data_size;

} x3f_image_data_t;

typedef struct camf_dim_entry_s
{
  uint32_t size;
  uint32_t name_offset;
  uint32_t n; /* 0,1,2,3... */
  char *name;
} camf_dim_entry_t;

typedef enum
{
  M_FLOAT,
  M_INT,
  M_UINT
} matrix_type_t;

typedef struct camf_entry_s
{
  /* pointer into decoded data */
  void *entry;

  /* entry header */
  uint32_t id;
  uint32_t version;
  uint32_t entry_size;
  uint32_t name_offset;
  uint32_t value_offset;

  /* computed values */
  char *name_address;
  void *value_address;
  uint32_t name_size;
  uint32_t value_size;

  /* extracted values for explicit CAMF entry types*/
  uint32_t text_size;
  char *text;

  uint32_t property_num;
  char **property_name;
  uint8_t **property_value;

  uint32_t matrix_dim;
  camf_dim_entry_t *matrix_dim_entry;

  /* Offset, pointer and size and type of raw data */
  uint32_t matrix_type;
  uint32_t matrix_data_off;
  void *matrix_data;
  uint32_t matrix_element_size;

  /* Pointer and type of copied data */
  matrix_type_t matrix_decoded_type;
  void *matrix_decoded;

  /* Help data to try to estimate element size */
  uint32_t matrix_elements;
  uint32_t matrix_used_space;
  double matrix_estimated_element_size;

} camf_entry_t;

typedef struct camf_entry_table_s
{
  uint32_t size;
  camf_entry_t *element;
} camf_entry_table_t;

typedef struct x3f_camf_typeN_s
{
  uint32_t val0;
  uint32_t val1;
  uint32_t val2;
  uint32_t val3;
} x3f_camf_typeN_t;

typedef struct x3f_camf_type2_s
{
  uint32_t reserved;
  uint32_t infotype;
  uint32_t infotype_version;
  uint32_t crypt_key;
} x3f_camf_type2_t;

typedef struct x3f_camf_type4_s
{
  uint32_t decoded_data_size;
  uint32_t decode_bias;
  uint32_t block_size;
  uint32_t block_count;
} x3f_camf_type4_t;

typedef struct x3f_camf_type5_s
{
  uint32_t decoded_data_size;
  uint32_t decode_bias;
  uint32_t unknown2;
  uint32_t unknown3;
} x3f_camf_type5_t;

typedef struct x3f_camf_s
{

  /* Header info */
  uint32_t type;
  union {
    x3f_camf_typeN_t tN;
    x3f_camf_type2_t t2;
    x3f_camf_type4_t t4;
    x3f_camf_type5_t t5;
  };

  /* The encrypted raw data */
  void *data;
  uint32_t data_size;

  /* Help data for type 4 Huffman compression */
  x3f_true_huffman_t table;
  x3f_hufftree_t tree;
  uint8_t *decoding_start;
  uint32_t decoding_size;

  /* The decrypted data */
  void *decoded_data;
  uint32_t decoded_data_size;

  /* Pointers into the decrypted data */
  camf_entry_table_t entry_table;
} x3f_camf_t;

typedef struct x3f_directory_entry_header_s
{
  uint32_t identifier; /* Should be ´SECp´, "SECi", ... */
  uint32_t version;    /* 0x00020001 is version 2.1  */
  union {
    x3f_property_list_t property_list;
    x3f_image_data_t image_data;
    x3f_camf_t camf;
  } data_subsection;
} x3f_directory_entry_header_t;

typedef struct x3f_directory_entry_s
{
  struct
  {
    uint32_t offset;
    uint32_t size;
  } input, output;

  uint32_t type;

  x3f_directory_entry_header_t header;
} x3f_directory_entry_t;

typedef struct x3f_directory_section_s
{
  uint32_t identifier; /* Should be ´SECd´ */
  uint32_t version;    /* 0x00020001 is version 2.1  */

  /* 2.0 Fields */
  uint32_t num_directory_entries;
  x3f_directory_entry_t *directory_entry;
} x3f_directory_section_t;

typedef struct x3f_header_s
{
  /* 2.0 Fields */
  uint32_t identifier; /* Should be ´FOVb´ */
  uint32_t version;    /* 0x00020001 means 2.1 */
  uint8_t unique_identifier[SIZE_UNIQUE_IDENTIFIER];
  uint32_t mark_bits;
  uint32_t columns;  /* Columns and rows ... */
  uint32_t rows;     /* ... before rotation */
  uint32_t rotation; /* 0, 90, 180, 270 */

  char white_balance[SIZE_WHITE_BALANCE]; /* Introduced in 2.1 */
  char color_mode[SIZE_COLOR_MODE];       /* Introduced in 2.3 */

  /* Introduced in 2.1 and extended from 32 to 64 in 3.0 */
  uint8_t extended_types[NUM_EXT_DATA]; /* x3f_extended_types_t */
  float extended_data[NUM_EXT_DATA];    /* 32 bits, but do type differ? */
} x3f_header_t;

typedef struct x3f_info_s
{
  char *error;
  struct
  {
    LibRaw_abstract_datastream *file; /* Use if more data is needed */
  } input, output;
} x3f_info_t;

typedef struct x3f_s
{
  x3f_info_t info;
  x3f_header_t header;
  x3f_directory_section_t directory_section;
} x3f_t;

typedef enum x3f_return_e
{
  X3F_OK = 0,
  X3F_ARGUMENT_ERROR = 1,
  X3F_INFILE_ERROR = 2,
  X3F_OUTFILE_ERROR = 3,
  X3F_INTERNAL_ERROR = 4
} x3f_return_t;

x3f_return_t x3f_delete(x3f_t *x3f);

/* Hacky external flags                                                 */
/* --------------------------------------------------------------------- */

extern int legacy_offset;
extern bool_t auto_legacy_offset;

/* --------------------------------------------------------------------- */
/* Huffman Decode Macros                                                 */
/* --------------------------------------------------------------------- */

#define HUF_TREE_MAX_LENGTH 27
#define HUF_TREE_MAX_NODES(_leaves) ((HUF_TREE_MAX_LENGTH + 1) * (_leaves))
#define HUF_TREE_GET_LENGTH(_v) (((_v) >> 27) & 0x1f)
#define HUF_TREE_GET_CODE(_v) ((_v)&0x07ffffff)

x3f_t *x3f_new_from_file(LibRaw_abstract_datastream *infile);
x3f_return_t x3f_delete(x3f_t *x3f);
x3f_directory_entry_t *x3f_get_raw(x3f_t *x3f);
x3f_directory_entry_t *x3f_get_thumb_plain(x3f_t *x3f);
x3f_return_t x3f_load_data(x3f_t *x3f, x3f_directory_entry_t *DE);
x3f_directory_entry_t *x3f_get_thumb_huffman(x3f_t *x3f);
x3f_directory_entry_t *x3f_get_thumb_jpeg(x3f_t *x3f);
x3f_directory_entry_t *x3f_get_camf(x3f_t *x3f);
x3f_directory_entry_t *x3f_get_prop(x3f_t *x3f);
/* extern */ int64_t x3f_load_data_size(x3f_t *x3f, x3f_directory_entry_t *DE);

#endif