Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
dng_opcode.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2022 paolodepetrillo.
4
5 darktable is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 darktable is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with darktable. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <glib.h>
20#include <stdio.h>
21#include <string.h>
22
23#include "dng_opcode.h"
24#include "logging.h"
25
26#define OPCODE_ID_GAINMAP (9)
27
28static double get_double(uint8_t *ptr)
29{
30 guint64 in;
31 union {
32 guint64 out;
33 double v;
34 } u;
35 memcpy(&in, ptr, sizeof(in));
36 u.out = GUINT64_FROM_BE(in);
37 return u.v;
38}
39
40static float get_float(uint8_t *ptr)
41{
42 guint32 in;
43 union {
44 guint32 out;
45 float v;
46 } u;
47 memcpy(&in, ptr, sizeof(in));
48 u.out = GUINT32_FROM_BE(in);
49 return u.v;
50}
51
52static uint32_t get_long(uint8_t *ptr)
53{
54 uint32_t in;
55 memcpy(&in, ptr, sizeof(in));
56 return GUINT32_FROM_BE(in);
57}
58
59void dt_dng_opcode_process_opcode_list_2(uint8_t *buf, uint32_t buf_size, dt_image_t *img)
60{
61 g_list_free_full(img->dng_gain_maps, dt_free_gpointer);
62 img->dng_gain_maps = NULL;
63
64 if(buf_size < 4)
65 {
66 dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] OpcodeList2 buffer too small for opcode count\n");
67 return;
68 }
69
70 uint32_t count = get_long(&buf[0]);
71 uint64_t offset = 4;
72 while(count > 0)
73 {
74 if(offset + 16 > buf_size)
75 {
76 dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] Truncated opcode header in OpcodeList2\n");
77 return;
78 }
79
80 const uint32_t opcode_id = get_long(&buf[offset]);
81 const uint32_t flags = get_long(&buf[offset + 8]);
82 const uint32_t param_size = get_long(&buf[offset + 12]);
83 uint8_t *param = &buf[offset + 16];
84
85 if(offset + 16 + (uint64_t)param_size > buf_size)
86 {
87 dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] Invalid opcode size in OpcodeList2\n");
88 return;
89 }
90
91 if(opcode_id == OPCODE_ID_GAINMAP)
92 {
93 if(param_size < 76)
94 {
95 dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] Undersized GainMap opcode in OpcodeList2\n");
96 goto next;
97 }
98 uint32_t gain_count = (param_size - 76) / 4;
99 dt_dng_gain_map_t *gm = g_malloc(sizeof(dt_dng_gain_map_t) + gain_count * sizeof(float));
100 gm->top = get_long(&param[0]);
101 gm->left = get_long(&param[4]);
102 gm->bottom = get_long(&param[8]);
103 gm->right = get_long(&param[12]);
104 gm->plane = get_long(&param[16]);
105 gm->planes = get_long(&param[20]);
106 gm->row_pitch = get_long(&param[24]);
107 gm->col_pitch = get_long(&param[28]);
108 gm->map_points_v = get_long(&param[32]);
109 gm->map_points_h = get_long(&param[36]);
110 gm->map_spacing_v = get_double(&param[40]);
111 gm->map_spacing_h = get_double(&param[48]);
112 gm->map_origin_v = get_double(&param[56]);
113 gm->map_origin_h = get_double(&param[64]);
114 gm->map_planes = get_long(&param[72]);
115 gm->map_gain_count = gain_count;
116 for(uint32_t i = 0; i < gain_count; i++)
117 gm->map_gain[i] = get_float(&param[76 + 4*i]);
118
119 img->dng_gain_maps = g_list_append(img->dng_gain_maps, gm);
120 }
121 else
122 {
123 dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] OpcodeList2 has unsupported %s opcode %d\n",
124 flags & 1 ? "optional" : "mandatory", opcode_id);
125 }
126
127next:
128 offset += 16 + (uint64_t)param_size;
129 count--;
130 }
131}
132
const float v
const dt_colormatrix_t dt_aligned_pixel_t out
static uint32_t get_long(uint8_t *ptr)
Definition dng_opcode.c:52
static double get_double(uint8_t *ptr)
Definition dng_opcode.c:28
static float get_float(uint8_t *ptr)
Definition dng_opcode.c:40
void dt_dng_opcode_process_opcode_list_2(uint8_t *buf, uint32_t buf_size, dt_image_t *img)
Definition dng_opcode.c:59
#define OPCODE_ID_GAINMAP
Definition dng_opcode.c:26
@ DT_DEBUG_IMAGEIO
Definition logging.h:68
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
static void dt_free_gpointer(gpointer ptr)
g_free() one pointer, with the signature GDestroyNotify wants.
Definition mem_alloc.h:184
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
const float const float param
unsigned __int64 uint64_t
Definition strptime.c:75
uint32_t map_gain_count
Definition dng_opcode.h:66
uint32_t row_pitch
Definition dng_opcode.h:53
uint32_t map_planes
Definition dng_opcode.h:61
uint32_t map_points_v
Definition dng_opcode.h:55
uint32_t col_pitch
Definition dng_opcode.h:54
uint32_t map_points_h
Definition dng_opcode.h:56
GList * dng_gain_maps
Definition image.h:464