Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions data/kernels/basic.cl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,90 @@ rawprepare_1f_unnormalized_gainmap(read_only image2d_t in, write_only image2d_t
write_imagef(out, (int2)(x, y), pixel_scaled);
}

kernel void
rawprepare_1f_gainmap_rgb(read_only image2d_t in, write_only image2d_t out,
const int width, const int height,
const int cx, const int cy,
global const float *sub, global const float *div,
const int rx, const int ry,
read_only image2d_t map_r, read_only image2d_t map_g, read_only image2d_t map_b,
const int2 map_size, const float2 im_to_rel,
const float2 rel_to_map, const float2 map_origin,
const int4 bl_to_plane)
{
const int x = get_global_id(0);
const int y = get_global_id(1);

if(x >= width || y >= height) return;

const float pixel = read_imageui(in, sampleri, (int2)(x + cx, y + cy)).x;

const int id = BL(ry+cy+y, rx+cx+x);
float pixel_scaled = (pixel - sub[id]) / div[id];

// Add 0.5 to compensate for CLK_FILTER_LINEAR subtracting 0.5 from the specified coordinates
const float2 map_pt = ((float2)(rx+cx+x,ry+cy+y) * im_to_rel - map_origin) * rel_to_map + (float2)(0.5f, 0.5f);

int plane;
switch(id)
{
case 0: plane = bl_to_plane.s0; break;
case 1: plane = bl_to_plane.s1; break;
case 2: plane = bl_to_plane.s2; break;
default: plane = bl_to_plane.s3; break;
}
switch(plane)
{
case 0: pixel_scaled *= read_imagef(map_r, samplerf, map_pt).x; break;
case 1: pixel_scaled *= read_imagef(map_g, samplerf, map_pt).x; break;
default: pixel_scaled *= read_imagef(map_b, samplerf, map_pt).x; break;
}

write_imagef(out, (int2)(x, y), pixel_scaled);
}

kernel void
rawprepare_1f_unnormalized_gainmap_rgb(read_only image2d_t in, write_only image2d_t out,
const int width, const int height,
const int cx, const int cy,
global const float *sub, global const float *div,
const int rx, const int ry,
read_only image2d_t map_r, read_only image2d_t map_g, read_only image2d_t map_b,
const int2 map_size, const float2 im_to_rel,
const float2 rel_to_map, const float2 map_origin,
const int4 bl_to_plane)
{
const int x = get_global_id(0);
const int y = get_global_id(1);

if(x >= width || y >= height) return;

const float pixel = read_imagef(in, sampleri, (int2)(x + cx, y + cy)).x;

const int id = BL(ry+cy+y, rx+cx+x);
float pixel_scaled = (pixel - sub[id]) / div[id];

// Add 0.5 to compensate for CLK_FILTER_LINEAR subtracting 0.5 from the specified coordinates
const float2 map_pt = ((float2)(rx+cx+x,ry+cy+y) * im_to_rel - map_origin) * rel_to_map + (float2)(0.5f, 0.5f);

int plane;
switch(id)
{
case 0: plane = bl_to_plane.s0; break;
case 1: plane = bl_to_plane.s1; break;
case 2: plane = bl_to_plane.s2; break;
default: plane = bl_to_plane.s3; break;
}
switch(plane)
{
case 0: pixel_scaled *= read_imagef(map_r, samplerf, map_pt).x; break;
case 1: pixel_scaled *= read_imagef(map_g, samplerf, map_pt).x; break;
default: pixel_scaled *= read_imagef(map_b, samplerf, map_pt).x; break;
}

write_imagef(out, (int2)(x, y), pixel_scaled);
}

kernel void
rawprepare_4f(read_only image2d_t in, write_only image2d_t out,
const int width, const int height,
Expand Down
59 changes: 38 additions & 21 deletions src/common/dng_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ static uint32_t _get_long(uint8_t *ptr)
return GUINT32_FROM_BE(in);
}

static void _parse_and_append_gain_map(uint8_t *param, uint32_t param_size, dt_image_t *img,
const char *source)
{
uint32_t gain_count = (param_size - 76) / 4;
dt_dng_gain_map_t *gm = g_malloc(sizeof(dt_dng_gain_map_t) + gain_count * sizeof(float));
gm->top = _get_long(&param[0]);
gm->left = _get_long(&param[4]);
gm->bottom = _get_long(&param[8]);
gm->right = _get_long(&param[12]);
gm->plane = _get_long(&param[16]);
gm->planes = _get_long(&param[20]);
gm->row_pitch = _get_long(&param[24]);
gm->col_pitch = _get_long(&param[28]);
gm->map_points_v = _get_long(&param[32]);
gm->map_points_h = _get_long(&param[36]);
gm->map_spacing_v = _get_double(&param[40]);
gm->map_spacing_h = _get_double(&param[48]);
gm->map_origin_v = _get_double(&param[56]);
gm->map_origin_h = _get_double(&param[64]);
gm->map_planes = _get_long(&param[72]);
for(int i = 0; i < gain_count; i++)
gm->map_gain[i] = _get_float(&param[76 + 4 * i]);
img->dng_gain_maps = g_list_append(img->dng_gain_maps, gm);
dt_print(DT_DEBUG_IMAGEIO,
"[dng_opcode] %s GainMap parsed: top=%u left=%u bottom=%u right=%u "
"plane=%u planes=%u row_pitch=%u col_pitch=%u "
"map_points_v=%u map_points_h=%u map_planes=%u gain_count=%u",
source, gm->top, gm->left, gm->bottom, gm->right,
gm->plane, gm->planes, gm->row_pitch, gm->col_pitch,
gm->map_points_v, gm->map_points_h, gm->map_planes, gain_count);
}

void dt_dng_opcode_process_opcode_list_2(uint8_t *buf, uint32_t buf_size, dt_image_t *img)
{
g_list_free_full(img->dng_gain_maps, g_free);
Expand All @@ -79,27 +111,7 @@ void dt_dng_opcode_process_opcode_list_2(uint8_t *buf, uint32_t buf_size, dt_ima

if(opcode_id == OPCODE_ID_GAINMAP)
{
uint32_t gain_count = (param_size - 76) / 4;
dt_dng_gain_map_t *gm = g_malloc(sizeof(dt_dng_gain_map_t) + gain_count * sizeof(float));
gm->top = _get_long(&param[0]);
gm->left = _get_long(&param[4]);
gm->bottom = _get_long(&param[8]);
gm->right = _get_long(&param[12]);
gm->plane = _get_long(&param[16]);
gm->planes = _get_long(&param[20]);
gm->row_pitch = _get_long(&param[24]);
gm->col_pitch = _get_long(&param[28]);
gm->map_points_v = _get_long(&param[32]);
gm->map_points_h = _get_long(&param[36]);
gm->map_spacing_v = _get_double(&param[40]);
gm->map_spacing_h = _get_double(&param[48]);
gm->map_origin_v = _get_double(&param[56]);
gm->map_origin_h = _get_double(&param[64]);
gm->map_planes = _get_long(&param[72]);
for(int i = 0; i < gain_count; i++)
gm->map_gain[i] = _get_float(&param[76 + 4*i]);

img->dng_gain_maps = g_list_append(img->dng_gain_maps, gm);
_parse_and_append_gain_map(param, param_size, img, "OpcodeList2");
}
else
{
Expand Down Expand Up @@ -167,6 +179,11 @@ void dt_dng_opcode_process_opcode_list_3(uint8_t *buf, uint32_t buf_size, dt_ima
img->exif_correction_type = CORRECTION_TYPE_DNG;
}

else if(opcode_id == OPCODE_ID_GAINMAP)
{
_parse_and_append_gain_map(param, param_size, img, "OpcodeList3");
}

else
{
dt_print(DT_DEBUG_IMAGEIO, "[dng_opcode] OpcodeList3 has unsupported %s opcode %d",
Expand Down
2 changes: 1 addition & 1 deletion src/common/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ typedef struct dt_image_t
/* DefaultUserCrop */
dt_boundingbox_t usercrop;

/* GainMaps from DNG OpcodeList2 exif tag */
/* GainMaps from DNG OpcodeList2 or OpcodeList3 exif tag */
GList *dng_gain_maps;

/* convenience pointer back into the image cache, so we can return
Expand Down
Loading