Skip to content

Commit 958a312

Browse files
committed
ICC mapping functions (broken due to no mBA lut)
1 parent cdb5546 commit 958a312

7 files changed

Lines changed: 305 additions & 61 deletions

File tree

59.5 KB
Binary file not shown.

examples/icc.c

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,75 @@
11
#include "color/icc.h"
22

3+
#include <stdbool.h>
34
#include <stdint.h>
45

56
#include "arena/arena.h"
67
#include "arena/common.h"
78
#include "color/icc_color.h"
8-
#include "color/icc_lut.h"
9-
#include "color/icc_tags.h"
109
#include "err/error.h"
10+
#include "logger/log.h"
1111
#include "parse_ctx/ctx.h"
1212

1313
int main(void) {
1414
Arena* arena = arena_new(128);
1515

16-
size_t buffer_len;
17-
const uint8_t* buffer = load_file_to_buffer(
16+
size_t swop_buffer_len;
17+
const uint8_t* swop_buffer = load_file_to_buffer(
1818
arena,
1919
"test-files/USWebCoatedSWOP.icc",
20-
&buffer_len
20+
&swop_buffer_len
2121
);
2222

23-
ParseCtx ctx = parse_ctx_new(buffer, buffer_len);
24-
ICCProfileHeader header;
25-
REQUIRE(icc_parse_header(&ctx, &header));
23+
size_t srgb_buffer_len;
24+
const uint8_t* srgb_buffer = load_file_to_buffer(
25+
arena,
26+
"assets/icc-profiles/sRGB_v4_ICC_preference.icc",
27+
&srgb_buffer_len
28+
);
2629

27-
ICCTagTable table;
28-
REQUIRE(icc_tag_table_new(&ctx, &table));
30+
ParseCtx swop_ctx = parse_ctx_new(swop_buffer, swop_buffer_len);
31+
ICCProfile swop_profile;
32+
REQUIRE(icc_parse_profile(swop_ctx, &swop_profile));
2933

30-
ParseCtx lut_ctx;
31-
REQUIRE(icc_tag_table_lookup(
32-
table,
33-
icc_tag_signature(ICC_TAG_B_TO_A0),
34-
&lut_ctx
35-
));
34+
ParseCtx srgb_ctx = parse_ctx_new(srgb_buffer, srgb_buffer_len);
35+
ICCProfile srgb_profile;
36+
REQUIRE(icc_parse_profile(srgb_ctx, &srgb_profile));
37+
38+
ICCColor input = {
39+
.channels = {1.0, 0.0, 0.0, 1.0},
40+
.color_space = ICC_COLOR_SPACE_CMYK
41+
};
42+
43+
ICCRenderingIntent intent = ICC_INTENT_MEDIA_RELATIVE;
3644

37-
ICCLut8 lut;
38-
REQUIRE(icc_parse_lut8(lut_ctx, &lut));
39-
40-
double out[15] = {0};
41-
REQUIRE(icc_lut8_map(
42-
lut,
43-
(ICCColor) {
44-
.channels = {0.3, 0.5, 0.7},
45-
.color_space = ICC_COLOR_SPACE_XYZ
46-
},
47-
out
45+
ICCPcsColor src_pcs, dst_pcs;
46+
REQUIRE(icc_device_to_pcs(
47+
swop_profile,
48+
ICC_INTENT_MEDIA_RELATIVE,
49+
input,
50+
&src_pcs
4851
));
52+
REQUIRE(icc_pcs_to_pcs(
53+
swop_profile,
54+
srgb_profile,
55+
false,
56+
intent,
57+
src_pcs,
58+
&dst_pcs
59+
));
60+
61+
ICCColor mapped_color;
62+
REQUIRE(icc_pcs_to_device(srgb_profile, intent, dst_pcs, &mapped_color));
63+
RELEASE_ASSERT(mapped_color.color_space == ICC_COLOR_SPACE_RGB);
64+
65+
LOG_DIAG(
66+
INFO,
67+
EXAMPLE,
68+
"Output sRGB: r=%f, g=%f, b=%f",
69+
mapped_color.channels[0],
70+
mapped_color.channels[1],
71+
mapped_color.channels[2]
72+
);
4973

5074
arena_free(arena);
5175
return 0;

libs/color/include/color/icc.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
#include <stdint.h>
44

5+
#include "color/icc_color.h"
56
#include "err/error.h"
67
#include "geom/vec3.h"
78
#include "parse_ctx/ctx.h"
89

910
typedef enum {
10-
ICC_RENDERING_INTENT_MEDIA_RELATIVE,
11-
ICC_RENDERING_INTENT_ICC_ABSOLUTE
11+
ICC_INTENT_MEDIA_RELATIVE,
12+
ICC_INTENT_ABSOLUTE,
13+
ICC_INTENT_PERCEPTUAL,
14+
ICC_INTENT_SATURATION
1215
} ICCRenderingIntent;
1316

1417
typedef struct {
@@ -74,11 +77,27 @@ typedef struct {
7477
} ICCProfile;
7578

7679
Error* icc_parse_profile(ParseCtx ctx, ICCProfile* profile);
77-
Error* icc_connect_pcs(
80+
bool icc_profile_is_pcsxyz(ICCProfile profile);
81+
82+
Error* icc_device_to_pcs(
83+
ICCProfile profile,
84+
ICCRenderingIntent rendering_intent,
85+
ICCColor color,
86+
ICCPcsColor* output
87+
);
88+
89+
Error* icc_pcs_to_device(
90+
ICCProfile profile,
91+
ICCRenderingIntent rendering_intent,
92+
ICCPcsColor color,
93+
ICCColor* output
94+
);
95+
96+
Error* icc_pcs_to_pcs(
7897
ICCProfile src_profile,
7998
ICCProfile dst_profile,
8099
bool src_is_absolute,
81100
ICCRenderingIntent intent,
82-
GeomVec3 src,
83-
GeomVec3* dst
101+
ICCPcsColor src,
102+
ICCPcsColor* dst
84103
);

libs/color/include/color/icc_color.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#pragma once
22

3+
#include <stdbool.h>
34
#include <stddef.h>
45
#include <stdint.h>
56

67
#include "geom/mat3.h"
8+
#include "geom/vec3.h"
79

810
typedef enum {
911
ICC_COLOR_SPACE_XYZ = 0,
@@ -46,3 +48,12 @@ typedef struct {
4648

4749
void icc_color_clamp(ICCColor* color);
4850
void icc_color_norm_pcs(ICCColor* color, GeomMat3 matrix);
51+
52+
typedef struct {
53+
GeomVec3 vec;
54+
bool is_xyz;
55+
} ICCPcsColor;
56+
57+
ICCPcsColor icc_pcs_color_to_lab(ICCPcsColor color);
58+
ICCPcsColor icc_pcs_color_to_xyz(ICCPcsColor color);
59+
ICCColor icc_pcs_to_color(ICCPcsColor color);

libs/color/include/color/icc_lut.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ typedef struct {
5353
} ICCLut16;
5454

5555
Error* icc_parse_lut16(ParseCtx ctx, ICCLut16* out);
56+
Error* icc_lut16_map(ICCLut16 lut, ICCColor input, double out[15]);

0 commit comments

Comments
 (0)