Skip to content

Commit c37c664

Browse files
committed
More init and cleanup/free function, refactor and fix
1 parent 94699b6 commit c37c664

File tree

4 files changed

+436
-332
lines changed

4 files changed

+436
-332
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ FILE(GLOB SOURCE_FILES
134134
"develop/tiling.c"
135135
"common/dwt.c"
136136
"common/heal.c"
137-
"common/colorchecker_CGATS.c"
137+
"common/colorchecker.c"
138138
"develop/masks/brush.c"
139139
"develop/masks/circle.c"
140140
"develop/masks/group.c"

src/common/colorchecker_CGATS.c renamed to src/common/colorchecker.c

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131

3232
#include "colorchecker.h"
33-
//#include "colorspaces.h"
3433
#include "common/colorspaces_inline_conversions.h"
3534
#include "darktable.h"
3635
#include "file_location.h"
@@ -54,7 +53,7 @@ static inline dt_colorchecker_CGATS_types _dt_CGATS_get_type_val(const char *typ
5453
return t;
5554
}
5655

57-
const dt_colorchecker_CGATS_spec_t *_dt_get_CGATS_spec(const char *type)
56+
const dt_colorchecker_CGATS_spec_t _dt_get_CGATS_spec(const char *type)
5857
{
5958
if(type)
6059
{
@@ -63,16 +62,16 @@ const dt_colorchecker_CGATS_spec_t *_dt_get_CGATS_spec(const char *type)
6362
switch(t)
6463
{
6564
case CGATS_TYPE_IT8_7_1:
66-
return &IT8_7_1;
65+
return IT8_7_1;
6766
case CGATS_TYPE_CGATS_17:
68-
return &CGATS_17;
67+
return CGATS_17;
6968
case CGATS_TYPE_LAST:
7069
fprintf(stderr, "Unknown CGATS type: %s\n", type);
71-
return &IT8_7_1;
70+
return IT8_7_1;
7271
}
7372
}
7473

75-
return &IT8_7_1;
74+
return IT8_7_1;
7675
}
7776

7877
/**
@@ -198,6 +197,19 @@ static inline const char *_dt_CGATS_get_manufacturer(const cmsHANDLE *hIT8)
198197
return manufacturer ? manufacturer : "Unknown Manufacturer";
199198
}
200199

200+
/**
201+
* @brief Get the name of a built-in color checker.
202+
*
203+
* @param target_type The type of colorchecker
204+
* @return char* The name of the colorchecker
205+
*/
206+
static inline const char *dt_get_builtin_colorchecker_name(const dt_color_checker_targets target_type)
207+
{
208+
const dt_color_checker_t *color_checker = dt_get_color_checker(target_type, NULL);
209+
const char *name = color_checker->name;
210+
fprintf(stdout, "dt_get_builtin_colorchecker_name: %s\n", name);
211+
return name;
212+
}
201213

202214
/**
203215
* @brief build a name for the colorchecker.
@@ -307,7 +319,7 @@ static inline void _dt_CGATS_find_whitest_blackest_greyest(const dt_color_checke
307319
* @param bwg the float* [3] that will get the blackest, whitest and greyest patches to be found
308320
* @return NULL if error, dt_color_checker_patch* with the colors otherwise.
309321
*/
310-
int _dt_colorchecker_CGATS_fill_patch_values(cmsHANDLE hIT8, dt_color_checker_patch *values, size_t *bwg)
322+
int dt_colorchecker_CGATS_fill_patch_values(cmsHANDLE hIT8, dt_color_checker_patch *values, size_t *bwg)
311323
{
312324
int error = 0;
313325
if(!values) error = 1;
@@ -323,7 +335,7 @@ int _dt_colorchecker_CGATS_fill_patch_values(cmsHANDLE hIT8, dt_color_checker_pa
323335
int n_columns = cmsIT8EnumDataFormat(hIT8, &sample_names);
324336
size_t num_patches = (size_t)cmsIT8GetPropertyDbl(hIT8, "NUMBER_OF_SETS");
325337
const char *CGATS_type = cmsIT8GetSheetType(hIT8);
326-
const dt_colorchecker_CGATS_spec_t *CGATS_spec = _dt_get_CGATS_spec(CGATS_type);
338+
const dt_colorchecker_CGATS_spec_t CGATS_spec = _dt_get_CGATS_spec(CGATS_type);
327339

328340
gboolean use_XYZ = FALSE;
329341
if(n_columns == -1)
@@ -378,15 +390,15 @@ int _dt_colorchecker_CGATS_fill_patch_values(cmsHANDLE hIT8, dt_color_checker_pa
378390
}
379391

380392
// IT8 chart dimensions
381-
const int cols = CGATS_spec->colums;
382-
const int rows = CGATS_spec->rows;
393+
const int cols = CGATS_spec.colums;
394+
const int rows = CGATS_spec.rows;
383395
// Patch size in ratio of the chart size
384-
const float patch_size_x = CGATS_spec->patch_width;
385-
const float patch_size_y = CGATS_spec->patch_height;
396+
const float patch_size_x = CGATS_spec.patch_width;
397+
const float patch_size_y = CGATS_spec.patch_height;
386398

387399
// Offset ratio of the center of the patch from the border of the chart
388-
const float patch_offset_x = CGATS_spec->patch_offset_x;
389-
const float patch_offset_y = CGATS_spec->patch_offset_y;
400+
const float patch_offset_x = CGATS_spec.patch_offset_x;
401+
const float patch_offset_y = CGATS_spec.patch_offset_y;
390402

391403
#ifdef OPENMP
392404
#pragma omp parallel for
@@ -457,8 +469,7 @@ int _dt_colorchecker_CGATS_fill_patch_values(cmsHANDLE hIT8, dt_color_checker_pa
457469
return error;
458470
}
459471

460-
461-
dt_color_checker_t *_dt_colorchecker_user_ref_create(const char *filename)
472+
dt_color_checker_t *dt_colorchecker_user_ref_create(const char *filename)
462473
{
463474
int error = 0;
464475
cmsHANDLE hIT8 = cmsIT8LoadFromFile(NULL, filename);
@@ -471,11 +482,12 @@ dt_color_checker_t *_dt_colorchecker_user_ref_create(const char *filename)
471482
}
472483

473484
const char *type = cmsIT8GetSheetType(hIT8);
474-
const dt_colorchecker_CGATS_spec_t *CGATS_spec = _dt_get_CGATS_spec(type);
485+
const dt_colorchecker_CGATS_spec_t CGATS_spec = _dt_get_CGATS_spec(type);
475486
size_t num_patches = (size_t)cmsIT8GetPropertyDbl(hIT8, "NUMBER_OF_SETS");
476487
size_t total_size = sizeof(dt_color_checker_t) + num_patches * sizeof(dt_color_checker_patch);
477488

478-
dt_color_checker_t *checker = malloc(total_size);
489+
dt_color_checker_t *checker = dt_colorchecker_init(total_size);
490+
479491
if(!checker)
480492
{
481493
fprintf(stderr, "Error: can't allocate memory for the color checker from IT8 chart\n");
@@ -488,19 +500,18 @@ dt_color_checker_t *_dt_colorchecker_user_ref_create(const char *filename)
488500
checker->date = g_strdup(_dt_CGATS_get_date(&hIT8));
489501
checker->manufacturer = g_strdup(_dt_CGATS_get_manufacturer(&hIT8));
490502
checker->type = COLOR_CHECKER_USER_REF;
491-
checker->radius = CGATS_spec->radius;
492-
checker->ratio = CGATS_spec->ratio;
503+
checker->radius = CGATS_spec.radius;
504+
checker->ratio = CGATS_spec.ratio;
493505
checker->patches = num_patches;
494-
checker->size[0] = CGATS_spec->size[0];
495-
checker->size[1] = CGATS_spec->size[1];
496-
checker->middle_grey = CGATS_spec->middle_grey;
497-
checker->white = CGATS_spec->white;
498-
checker->black = CGATS_spec->black;
506+
checker->size[0] = CGATS_spec.size[0];
507+
checker->size[1] = CGATS_spec.size[1];
508+
checker->middle_grey = CGATS_spec.middle_grey;
509+
checker->white = CGATS_spec.white;
510+
checker->black = CGATS_spec.black;
499511

500512
// blackest, whitest and greyest patches will be found while filling the color values
501513
size_t bwg[3] = { 0, 0, 0 };
502-
503-
_dt_colorchecker_CGATS_fill_patch_values(hIT8, checker->values, bwg);
514+
dt_colorchecker_CGATS_fill_patch_values(hIT8, checker->values, bwg);
504515

505516
checker->black = bwg[0];
506517
checker->white = bwg[1];
@@ -511,7 +522,13 @@ dt_color_checker_t *_dt_colorchecker_user_ref_create(const char *filename)
511522

512523
end:
513524
if(hIT8) cmsIT8Free(hIT8);
514-
return error ? NULL : checker;
525+
if(error)
526+
{
527+
free(checker);
528+
return NULL;
529+
}
530+
else
531+
return checker;
515532
}
516533

517534
static dt_colorchecker_label_t *_dt_colorchecker_user_ref_add_label(const gchar *filename, const gchar *user_it8_dir)
@@ -526,25 +543,46 @@ static dt_colorchecker_label_t *_dt_colorchecker_user_ref_add_label(const gchar
526543
if(hIT8 && _dt_CGATS_is_valid(&hIT8))
527544
{
528545
gchar *label = _dt_CGATS_get_name(&hIT8, filename);
529-
size_t label_size = safe_strlen(label) + safe_strlen(filepath) + sizeof(dt_color_checker_targets);
530-
dt_colorchecker_label_t *CGATS_label = malloc(label_size);
531-
532-
CGATS_label->label = g_strdup(label);
533-
CGATS_label->path = g_strdup(filepath);
534-
CGATS_label->type = COLOR_CHECKER_USER_REF;
535-
546+
dt_colorchecker_label_t *CGATS_label = dt_colorchecker_label_init(label, COLOR_CHECKER_USER_REF, filepath);
547+
536548
g_free(label);
537549
result = CGATS_label;
550+
if(!result) goto error;
538551
}
539552
cmsIT8Free(hIT8);
540553
}
541554
g_free(filepath);
542555

543556
return result;
557+
558+
error:
559+
free(result);
560+
return NULL;
544561
}
545562

563+
int dt_colorchecker_find_builtin(GList **colorcheckers_label)
564+
{
565+
int nb = 0;
566+
for(int k = 0; k < COLOR_CHECKER_USER_REF; k++)
567+
{
568+
const char *name = dt_get_builtin_colorchecker_name(k);
569+
dt_colorchecker_label_t *builtin_label = dt_colorchecker_label_init(name, k, NULL);
570+
571+
if(!builtin_label)
572+
{
573+
fprintf(stderr, "dt_colorchecker_find: failed to allocate memory for builtin colorchecker label %d\n", k);
574+
continue;
575+
}
576+
else
577+
{
578+
*colorcheckers_label = g_list_append(*colorcheckers_label, builtin_label);
579+
nb++;
580+
}
581+
}
582+
return nb;
583+
}
546584

547-
int _dt_colorchecker_find_CGAT_reference_files(GList **ref_colorcheckers_files)
585+
int dt_colorchecker_find_CGAT_reference_files(GList **ref_colorcheckers_files)
548586
{
549587
int nb = 0;
550588
char confdir[PATH_MAX] = { 0 };
@@ -563,6 +601,9 @@ int _dt_colorchecker_find_CGAT_reference_files(GList **ref_colorcheckers_files)
563601
*ref_colorcheckers_files = g_list_append(*ref_colorcheckers_files, CGATS_label);
564602
nb++;
565603
}
604+
else
605+
fprintf(stderr, "Error: failed to load CGATS file '%s' in %s\n", filename, user_it8_dir);
606+
566607
}
567608
g_dir_close(dir);
568609
}

0 commit comments

Comments
 (0)