File indexing completed on 2025-01-05 03:56:46
0001 /* -*- C++ -*- 0002 * Copyright 2019-2021 LibRaw LLC (info@libraw.org) 0003 * 0004 0005 LibRaw is free software; you can redistribute it and/or modify 0006 it under the terms of the one of two licenses as you choose: 0007 0008 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1 0009 (See file LICENSE.LGPL provided in LibRaw distribution archive for details). 0010 0011 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 0012 (See file LICENSE.CDDL provided in LibRaw distribution archive for details). 0013 */ 0014 0015 /* Library for accessing X3F Files 0016 ---------------------------------------------------------------- 0017 BSD-style License 0018 ---------------------------------------------------------------- 0019 0020 * Copyright (c) 2010, Roland Karlsson (roland@proxel.se) 0021 * All rights reserved. 0022 * 0023 * Redistribution and use in source and binary forms, with or without 0024 * modification, are permitted provided that the following conditions are met: 0025 * * Redistributions of source code must retain the above copyright 0026 * notice, this list of conditions and the following disclaimer. 0027 * * Redistributions in binary form must reproduce the above copyright 0028 * notice, this list of conditions and the following disclaimer in the 0029 * documentation and/or other materials provided with the distribution. 0030 * * Neither the name of the organization nor the 0031 * names of its contributors may be used to endorse or promote products 0032 * derived from this software without specific prior written permission. 0033 * 0034 * THIS SOFTWARE IS PROVIDED BY ROLAND KARLSSON ''AS IS'' AND ANY 0035 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 0036 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 0037 * DISCLAIMED. IN NO EVENT SHALL ROLAND KARLSSON BE LIABLE FOR ANY 0038 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 0039 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 0040 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 0041 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0042 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0043 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0044 0045 */ 0046 0047 #ifndef X3F_TOOLS_H 0048 #define X3F_TOOLS_H 0049 0050 #include <stdio.h> 0051 #include <string.h> 0052 #include <stdlib.h> 0053 #include <math.h> 0054 #include <stdio.h> 0055 #include "../libraw/libraw_datastream.h" 0056 0057 /* From X3F_IO.H */ 0058 0059 #define SIZE_UNIQUE_IDENTIFIER 16 0060 #define SIZE_WHITE_BALANCE 32 0061 #define SIZE_COLOR_MODE 32 0062 #define NUM_EXT_DATA_2_1 32 0063 #define NUM_EXT_DATA_3_0 64 0064 #define NUM_EXT_DATA NUM_EXT_DATA_3_0 0065 0066 #define X3F_VERSION(MAJ, MIN) (uint32_t)(((MAJ) << 16) + MIN) 0067 #define X3F_VERSION_2_0 X3F_VERSION(2, 0) 0068 #define X3F_VERSION_2_1 X3F_VERSION(2, 1) 0069 #define X3F_VERSION_2_2 X3F_VERSION(2, 2) 0070 #define X3F_VERSION_2_3 X3F_VERSION(2, 3) 0071 #define X3F_VERSION_3_0 X3F_VERSION(3, 0) 0072 #define X3F_VERSION_4_0 X3F_VERSION(4, 0) 0073 0074 /* Main file identifier */ 0075 #define X3F_FOVb (uint32_t)(0x62564f46) 0076 /* Directory identifier */ 0077 #define X3F_SECd (uint32_t)(0x64434553) 0078 /* Property section identifiers */ 0079 #define X3F_PROP (uint32_t)(0x504f5250) 0080 #define X3F_SECp (uint32_t)(0x70434553) 0081 /* Image section identifiers */ 0082 #define X3F_IMAG (uint32_t)(0x46414d49) 0083 #define X3F_IMA2 (uint32_t)(0x32414d49) 0084 #define X3F_SECi (uint32_t)(0x69434553) 0085 /* CAMF section identifiers */ 0086 #define X3F_CAMF (uint32_t)(0x464d4143) 0087 #define X3F_SECc (uint32_t)(0x63434553) 0088 /* CAMF entry identifiers */ 0089 #define X3F_CMbP (uint32_t)(0x50624d43) 0090 #define X3F_CMbT (uint32_t)(0x54624d43) 0091 #define X3F_CMbM (uint32_t)(0x4d624d43) 0092 #define X3F_CMb (uint32_t)(0x00624d43) 0093 /* SDQ section identifiers ? - TODO */ 0094 #define X3F_SPPA (uint32_t)(0x41505053) 0095 #define X3F_SECs (uint32_t)(0x73434553) 0096 0097 #define X3F_IMAGE_THUMB_PLAIN (uint32_t)(0x00020003) 0098 #define X3F_IMAGE_THUMB_HUFFMAN (uint32_t)(0x0002000b) 0099 #define X3F_IMAGE_THUMB_JPEG (uint32_t)(0x00020012) 0100 #define X3F_IMAGE_THUMB_SDQ (uint32_t)(0x00020019) /* SDQ ? - TODO */ 0101 0102 #define X3F_IMAGE_RAW_HUFFMAN_X530 (uint32_t)(0x00030005) 0103 #define X3F_IMAGE_RAW_HUFFMAN_10BIT (uint32_t)(0x00030006) 0104 #define X3F_IMAGE_RAW_TRUE (uint32_t)(0x0003001e) 0105 #define X3F_IMAGE_RAW_MERRILL (uint32_t)(0x0001001e) 0106 #define X3F_IMAGE_RAW_QUATTRO (uint32_t)(0x00010023) 0107 #define X3F_IMAGE_RAW_SDQ (uint32_t)(0x00010025) 0108 #define X3F_IMAGE_RAW_SDQH (uint32_t)(0x00010027) 0109 #define X3F_IMAGE_RAW_SDQH2 (uint32_t)(0x00010029) 0110 0111 #define X3F_IMAGE_HEADER_SIZE 28 0112 #define X3F_CAMF_HEADER_SIZE 28 0113 #define X3F_PROPERTY_LIST_HEADER_SIZE 24 0114 0115 typedef uint16_t utf16_t; 0116 0117 typedef int bool_t; 0118 0119 typedef enum x3f_extended_types_e 0120 { 0121 X3F_EXT_TYPE_NONE = 0, 0122 X3F_EXT_TYPE_EXPOSURE_ADJUST = 1, 0123 X3F_EXT_TYPE_CONTRAST_ADJUST = 2, 0124 X3F_EXT_TYPE_SHADOW_ADJUST = 3, 0125 X3F_EXT_TYPE_HIGHLIGHT_ADJUST = 4, 0126 X3F_EXT_TYPE_SATURATION_ADJUST = 5, 0127 X3F_EXT_TYPE_SHARPNESS_ADJUST = 6, 0128 X3F_EXT_TYPE_RED_ADJUST = 7, 0129 X3F_EXT_TYPE_GREEN_ADJUST = 8, 0130 X3F_EXT_TYPE_BLUE_ADJUST = 9, 0131 X3F_EXT_TYPE_FILL_LIGHT_ADJUST = 10 0132 } x3f_extended_types_t; 0133 0134 typedef struct x3f_property_s 0135 { 0136 /* Read from file */ 0137 uint32_t name_offset; 0138 uint32_t value_offset; 0139 0140 /* Computed */ 0141 utf16_t *name; /* 0x0000 terminated UTF 16 */ 0142 utf16_t *value; /* 0x0000 terminated UTF 16 */ 0143 } x3f_property_t; 0144 0145 typedef struct x3f_property_table_s 0146 { 0147 uint32_t size; 0148 x3f_property_t *element; 0149 } x3f_property_table_t; 0150 0151 typedef struct x3f_property_list_s 0152 { 0153 /* 2.0 Fields */ 0154 uint32_t num_properties; 0155 uint32_t character_format; 0156 uint32_t reserved; 0157 uint32_t total_length; 0158 0159 x3f_property_table_t property_table; 0160 0161 void *data; 0162 0163 uint32_t data_size; 0164 0165 } x3f_property_list_t; 0166 0167 typedef struct x3f_table8_s 0168 { 0169 uint32_t size; 0170 uint8_t *element; 0171 } x3f_table8_t; 0172 0173 typedef struct x3f_table16_s 0174 { 0175 uint32_t size; 0176 uint16_t *element; 0177 } x3f_table16_t; 0178 0179 typedef struct x3f_table32_s 0180 { 0181 uint32_t size; 0182 uint32_t *element; 0183 } x3f_table32_t; 0184 0185 typedef struct 0186 { 0187 uint8_t *data; /* Pointer to actual image data */ 0188 void *buf; /* Pointer to allocated buffer for free() */ 0189 uint32_t rows; 0190 uint32_t columns; 0191 uint32_t channels; 0192 uint32_t row_stride; 0193 } x3f_area8_t; 0194 0195 typedef struct 0196 { 0197 uint16_t *data; /* Pointer to actual image data */ 0198 void *buf; /* Pointer to allocated buffer for free() */ 0199 uint32_t rows; 0200 uint32_t columns; 0201 uint32_t channels; 0202 uint32_t row_stride; 0203 } x3f_area16_t; 0204 0205 #define UNDEFINED_LEAF 0xffffffff 0206 0207 typedef struct x3f_huffnode_s 0208 { 0209 struct x3f_huffnode_s *branch[2]; 0210 uint32_t leaf; 0211 } x3f_huffnode_t; 0212 0213 typedef struct x3f_hufftree_s 0214 { 0215 uint32_t free_node_index; /* Free node index in huffman tree array */ 0216 uint32_t total_node_index; 0217 x3f_huffnode_t *nodes; /* Coding tree */ 0218 } x3f_hufftree_t; 0219 0220 typedef struct x3f_true_huffman_element_s 0221 { 0222 uint8_t code_size; 0223 uint8_t code; 0224 } x3f_true_huffman_element_t; 0225 0226 typedef struct x3f_true_huffman_s 0227 { 0228 uint32_t size; 0229 x3f_true_huffman_element_t *element; 0230 } x3f_true_huffman_t; 0231 0232 /* 0=bottom, 1=middle, 2=top */ 0233 #define TRUE_PLANES 3 0234 0235 typedef struct x3f_true_s 0236 { 0237 uint16_t seed[TRUE_PLANES]; /* Always 512,512,512 */ 0238 uint16_t unknown; /* Always 0 */ 0239 x3f_true_huffman_t table; /* Huffman table - zero 0240 terminated. size is the number of 0241 leaves plus 1.*/ 0242 0243 x3f_table32_t plane_size; /* Size of the 3 planes */ 0244 uint8_t *plane_address[TRUE_PLANES]; /* computed offset to the planes */ 0245 x3f_hufftree_t tree; /* Coding tree */ 0246 x3f_area16_t x3rgb16; /* 3x16 bit X3-RGB data */ 0247 } x3f_true_t; 0248 0249 typedef struct x3f_quattro_s 0250 { 0251 struct 0252 { 0253 uint16_t columns; 0254 uint16_t rows; 0255 } plane[TRUE_PLANES]; 0256 uint32_t unknown; 0257 0258 bool_t quattro_layout; 0259 x3f_area16_t top16; /* Container for the bigger top layer */ 0260 } x3f_quattro_t; 0261 0262 typedef struct x3f_huffman_s 0263 { 0264 x3f_table16_t mapping; /* Value Mapping = X3F lossy compression */ 0265 x3f_table32_t table; /* Coding Table */ 0266 x3f_hufftree_t tree; /* Coding tree */ 0267 x3f_table32_t row_offsets; /* Row offsets */ 0268 x3f_area8_t rgb8; /* 3x8 bit RGB data */ 0269 x3f_area16_t x3rgb16; /* 3x16 bit X3-RGB data */ 0270 } x3f_huffman_t; 0271 0272 typedef struct x3f_image_data_s 0273 { 0274 /* 2.0 Fields */ 0275 /* ------------------------------------------------------------------ */ 0276 /* Known combinations of type and format are: 0277 1-6, 2-3, 2-11, 2-18, 3-6 */ 0278 uint32_t type; /* 1 = RAW X3 (SD1) 0279 2 = thumbnail or maybe just RGB 0280 3 = RAW X3 */ 0281 uint32_t format; /* 3 = 3x8 bit pixmap 0282 6 = 3x10 bit huffman with map table 0283 11 = 3x8 bit huffman 0284 18 = JPEG */ 0285 uint32_t type_format; /* type<<16 + format */ 0286 /* ------------------------------------------------------------------ */ 0287 0288 uint32_t columns; /* width / row size in pixels */ 0289 uint32_t rows; /* height */ 0290 uint32_t row_stride; /* row size in bytes */ 0291 0292 /* NULL if not used */ 0293 x3f_huffman_t *huffman; /* Huffman help data */ 0294 x3f_true_t *tru; /* TRUE help data */ 0295 x3f_quattro_t *quattro; /* Quattro help data */ 0296 0297 void *data; /* Take from file if NULL. Otherwise, 0298 this is the actual data bytes in 0299 the file. */ 0300 uint32_t data_size; 0301 0302 } x3f_image_data_t; 0303 0304 typedef struct camf_dim_entry_s 0305 { 0306 uint32_t size; 0307 uint32_t name_offset; 0308 uint32_t n; /* 0,1,2,3... */ 0309 char *name; 0310 } camf_dim_entry_t; 0311 0312 typedef enum 0313 { 0314 M_FLOAT, 0315 M_INT, 0316 M_UINT 0317 } matrix_type_t; 0318 0319 typedef struct camf_entry_s 0320 { 0321 /* pointer into decoded data */ 0322 void *entry; 0323 0324 /* entry header */ 0325 uint32_t id; 0326 uint32_t version; 0327 uint32_t entry_size; 0328 uint32_t name_offset; 0329 uint32_t value_offset; 0330 0331 /* computed values */ 0332 char *name_address; 0333 void *value_address; 0334 uint32_t name_size; 0335 uint32_t value_size; 0336 0337 /* extracted values for explicit CAMF entry types*/ 0338 uint32_t text_size; 0339 char *text; 0340 0341 uint32_t property_num; 0342 char **property_name; 0343 uint8_t **property_value; 0344 0345 uint32_t matrix_dim; 0346 camf_dim_entry_t *matrix_dim_entry; 0347 0348 /* Offset, pointer and size and type of raw data */ 0349 uint32_t matrix_type; 0350 uint32_t matrix_data_off; 0351 void *matrix_data; 0352 uint32_t matrix_element_size; 0353 0354 /* Pointer and type of copied data */ 0355 matrix_type_t matrix_decoded_type; 0356 void *matrix_decoded; 0357 0358 /* Help data to try to estimate element size */ 0359 uint32_t matrix_elements; 0360 uint32_t matrix_used_space; 0361 double matrix_estimated_element_size; 0362 0363 } camf_entry_t; 0364 0365 typedef struct camf_entry_table_s 0366 { 0367 uint32_t size; 0368 camf_entry_t *element; 0369 } camf_entry_table_t; 0370 0371 typedef struct x3f_camf_typeN_s 0372 { 0373 uint32_t val0; 0374 uint32_t val1; 0375 uint32_t val2; 0376 uint32_t val3; 0377 } x3f_camf_typeN_t; 0378 0379 typedef struct x3f_camf_type2_s 0380 { 0381 uint32_t reserved; 0382 uint32_t infotype; 0383 uint32_t infotype_version; 0384 uint32_t crypt_key; 0385 } x3f_camf_type2_t; 0386 0387 typedef struct x3f_camf_type4_s 0388 { 0389 uint32_t decoded_data_size; 0390 uint32_t decode_bias; 0391 uint32_t block_size; 0392 uint32_t block_count; 0393 } x3f_camf_type4_t; 0394 0395 typedef struct x3f_camf_type5_s 0396 { 0397 uint32_t decoded_data_size; 0398 uint32_t decode_bias; 0399 uint32_t unknown2; 0400 uint32_t unknown3; 0401 } x3f_camf_type5_t; 0402 0403 typedef struct x3f_camf_s 0404 { 0405 0406 /* Header info */ 0407 uint32_t type; 0408 union { 0409 x3f_camf_typeN_t tN; 0410 x3f_camf_type2_t t2; 0411 x3f_camf_type4_t t4; 0412 x3f_camf_type5_t t5; 0413 }; 0414 0415 /* The encrypted raw data */ 0416 void *data; 0417 uint32_t data_size; 0418 0419 /* Help data for type 4 Huffman compression */ 0420 x3f_true_huffman_t table; 0421 x3f_hufftree_t tree; 0422 uint8_t *decoding_start; 0423 uint32_t decoding_size; 0424 0425 /* The decrypted data */ 0426 void *decoded_data; 0427 uint32_t decoded_data_size; 0428 0429 /* Pointers into the decrypted data */ 0430 camf_entry_table_t entry_table; 0431 } x3f_camf_t; 0432 0433 typedef struct x3f_directory_entry_header_s 0434 { 0435 uint32_t identifier; /* Should be ´SECp´, "SECi", ... */ 0436 uint32_t version; /* 0x00020001 is version 2.1 */ 0437 union { 0438 x3f_property_list_t property_list; 0439 x3f_image_data_t image_data; 0440 x3f_camf_t camf; 0441 } data_subsection; 0442 } x3f_directory_entry_header_t; 0443 0444 typedef struct x3f_directory_entry_s 0445 { 0446 struct 0447 { 0448 uint32_t offset; 0449 uint32_t size; 0450 } input, output; 0451 0452 uint32_t type; 0453 0454 x3f_directory_entry_header_t header; 0455 } x3f_directory_entry_t; 0456 0457 typedef struct x3f_directory_section_s 0458 { 0459 uint32_t identifier; /* Should be ´SECd´ */ 0460 uint32_t version; /* 0x00020001 is version 2.1 */ 0461 0462 /* 2.0 Fields */ 0463 uint32_t num_directory_entries; 0464 x3f_directory_entry_t *directory_entry; 0465 } x3f_directory_section_t; 0466 0467 typedef struct x3f_header_s 0468 { 0469 /* 2.0 Fields */ 0470 uint32_t identifier; /* Should be ´FOVb´ */ 0471 uint32_t version; /* 0x00020001 means 2.1 */ 0472 uint8_t unique_identifier[SIZE_UNIQUE_IDENTIFIER]; 0473 uint32_t mark_bits; 0474 uint32_t columns; /* Columns and rows ... */ 0475 uint32_t rows; /* ... before rotation */ 0476 uint32_t rotation; /* 0, 90, 180, 270 */ 0477 0478 char white_balance[SIZE_WHITE_BALANCE]; /* Introduced in 2.1 */ 0479 char color_mode[SIZE_COLOR_MODE]; /* Introduced in 2.3 */ 0480 0481 /* Introduced in 2.1 and extended from 32 to 64 in 3.0 */ 0482 uint8_t extended_types[NUM_EXT_DATA]; /* x3f_extended_types_t */ 0483 float extended_data[NUM_EXT_DATA]; /* 32 bits, but do type differ? */ 0484 } x3f_header_t; 0485 0486 typedef struct x3f_info_s 0487 { 0488 char *error; 0489 struct 0490 { 0491 LibRaw_abstract_datastream *file; /* Use if more data is needed */ 0492 } input, output; 0493 } x3f_info_t; 0494 0495 typedef struct x3f_s 0496 { 0497 x3f_info_t info; 0498 x3f_header_t header; 0499 x3f_directory_section_t directory_section; 0500 } x3f_t; 0501 0502 typedef enum x3f_return_e 0503 { 0504 X3F_OK = 0, 0505 X3F_ARGUMENT_ERROR = 1, 0506 X3F_INFILE_ERROR = 2, 0507 X3F_OUTFILE_ERROR = 3, 0508 X3F_INTERNAL_ERROR = 4 0509 } x3f_return_t; 0510 0511 x3f_return_t x3f_delete(x3f_t *x3f); 0512 0513 /* Hacky external flags */ 0514 /* --------------------------------------------------------------------- */ 0515 0516 extern int legacy_offset; 0517 extern bool_t auto_legacy_offset; 0518 0519 /* --------------------------------------------------------------------- */ 0520 /* Huffman Decode Macros */ 0521 /* --------------------------------------------------------------------- */ 0522 0523 #define HUF_TREE_MAX_LENGTH 27 0524 #define HUF_TREE_MAX_NODES(_leaves) ((HUF_TREE_MAX_LENGTH + 1) * (_leaves)) 0525 #define HUF_TREE_GET_LENGTH(_v) (((_v) >> 27) & 0x1f) 0526 #define HUF_TREE_GET_CODE(_v) ((_v)&0x07ffffff) 0527 0528 x3f_t *x3f_new_from_file(LibRaw_abstract_datastream *infile); 0529 x3f_return_t x3f_delete(x3f_t *x3f); 0530 x3f_directory_entry_t *x3f_get_raw(x3f_t *x3f); 0531 x3f_directory_entry_t *x3f_get_thumb_plain(x3f_t *x3f); 0532 x3f_return_t x3f_load_data(x3f_t *x3f, x3f_directory_entry_t *DE); 0533 x3f_directory_entry_t *x3f_get_thumb_huffman(x3f_t *x3f); 0534 x3f_directory_entry_t *x3f_get_thumb_jpeg(x3f_t *x3f); 0535 x3f_directory_entry_t *x3f_get_camf(x3f_t *x3f); 0536 x3f_directory_entry_t *x3f_get_prop(x3f_t *x3f); 0537 /* extern */ int64_t x3f_load_data_size(x3f_t *x3f, x3f_directory_entry_t *DE); 0538 0539 #endif