1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/* See LICENSE file for copyright and license details. */
#include <arpa/inet.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#include <lcms2.h>
static char *argv0;
/* ROMM RGB primaries (ISO 22028-2:2013) */
static cmsCIExyYTRIPLE primaries = {
/* x, y, Y */
{ 0.7347, 0.2653, 0.288040 }, /* red */
{ 0.1596, 0.8404, 0.711874 }, /* green */
{ 0.0366, 0.0001, 0.000086 }, /* blue */
};
METHODDEF(void)
jpeg_error(j_common_ptr cinfo)
{
fprintf(stderr, "%s: libjpeg: ", argv0);
(*cinfo->err->output_message)(cinfo);
exit(1);
}
int
main(int argc, char *argv[])
{
cmsHPROFILE in_profile = NULL, out_profile;
cmsHTRANSFORM transform;
cmsToneCurve *gamma10, *out_curves[3];
struct jpeg_decompress_struct cinfo;
jpeg_saved_marker_ptr marker;
struct jpeg_error_mgr jerr;
uint32_t width, height, val_be;
uint16_t *ff_row;
size_t jpeg_row_len, ff_row_len, i, dx, sx;
JSAMPARRAY buffer; /* output row buffer */
argv0 = argv[0], argc--, argv++;
if (argc > 1) {
fprintf(stderr, "usage: %s\n", argv0);
return 1;
}
/* load jpg */
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = jpeg_error;
jpeg_create_decompress(&cinfo);
jpeg_save_markers(&cinfo, JPEG_APP0 + 1, 0xffff); /* exif data */
jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 0xffff); /* icc data */
jpeg_stdio_src(&cinfo, stdin);
jpeg_read_header(&cinfo, TRUE);
width = cinfo.image_width;
height = cinfo.image_height;
/* set output format */
cinfo.output_components = 3; /* color components per pixel */
cinfo.out_color_space = JCS_RGB; /* input color space */
/* extract metadata */
marker = cinfo.marker_list;
for(; marker; marker = marker->next) {
if (!marker->data || !marker->data_length)
continue;
if (marker->marker == JPEG_APP0 + 1) {
/* exif data marker */
/* todo: Should we handle icc data from exif? */
} else if (marker->marker == JPEG_APP0 + 2) {
/* icc data marker */
if (!(in_profile = cmsOpenProfileFromMem(
marker->data + 14,
marker->data_length - 14)))
goto lcmserr;
}
}
jpeg_start_decompress(&cinfo);
jpeg_row_len = width * cinfo.output_components;
/* create output buffers */
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,
JPOOL_IMAGE, jpeg_row_len, 1);
ff_row_len = strlen("RGBA") * width;
if(!(ff_row = malloc(ff_row_len * sizeof(uint16_t)))) {
fprintf(stderr, "%s: malloc: out of memory\n", argv0);
return 1;
}
/* icc profile (output linear ROMM RGB (ISO 22028-2:2013)) */
if (!in_profile && !(in_profile = cmsCreate_sRGBProfile()))
goto lcmserr;
if (!(gamma10 = cmsBuildGamma(NULL, 1.0)))
goto lcmserr;
out_curves[0] = out_curves[1] = out_curves[2] = gamma10;
if (!(out_profile = cmsCreateRGBProfile(cmsD50_xyY(), &primaries,
out_curves)))
goto lcmserr;
if (!(transform = cmsCreateTransform(in_profile, TYPE_RGBA_16,
out_profile, TYPE_RGBA_16,
INTENT_RELATIVE_COLORIMETRIC, 0)))
goto lcmserr;
/* write header */
fprintf(stdout, "farbfeld");
val_be = htonl(width);
if (fwrite(&val_be, sizeof(uint32_t), 1, stdout) != 1)
goto writerr;
val_be = htonl(height);
if (fwrite(&val_be, sizeof(uint32_t), 1, stdout) != 1)
goto writerr;
while (cinfo.output_scanline < cinfo.output_height) {
/* jpeg_read_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient. */
jpeg_read_scanlines(&cinfo, buffer, 1);
for (i = 0, dx = 0, sx = 0; i < width; i++, sx += 3, dx += 4) {
ff_row[dx] = buffer[0][sx] * 257;
ff_row[dx+1] = buffer[0][sx+1] * 257;
ff_row[dx+2] = buffer[0][sx+2] * 257;
ff_row[dx+3] = 65535;
}
cmsDoTransform(transform, ff_row, ff_row, ff_row_len / 4);
for (i = 0; i < ff_row_len; i++) {
/* re-add alpha */
if (i >= 3 && (i - 3) % 4 == 0)
ff_row[i] = 65535;
/* swap endianness to BE */
ff_row[i] = htons(ff_row[i]);
}
/* write data */
if (fwrite(ff_row, 2, ff_row_len, stdout) != ff_row_len)
goto writerr;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return 0;
writerr:
fprintf(stderr, "%s: fwrite: ", argv0);
perror(NULL);
return 1;
lcmserr:
fprintf(stderr, "%s: lcms error\n", argv0);
return 1;
}
|