summaryrefslogtreecommitdiff
path: root/ff2png.c
blob: 9f562b803eccf8ffa641d7aa600129790a8602b1 (plain)
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
/* 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 <lcms2.h>
#include <png.h>

#define HEADER "farbfeld########"

static char *argv0;

/* ProPhoto RGB */
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  */
};

void
pngerr(png_structp pngs, const char *msg)
{
	fprintf(stderr, "%s: libpng: %s\n", argv0, msg);
	exit(1);
}

int
main(int argc, char *argv[])
{
	cmsContext icc_context;
	cmsHPROFILE out_prof;
	cmsMLU *mlu1, *mlu2;
	cmsToneCurve *gamma18, *out_curve[3];
	png_structp pngs;
	png_infop pngi;
	size_t png_row_len, j;
	uint32_t icclen, width, height, i;
	uint16_t tmp16, *png_row;
	uint8_t hdr[16], *icc;

	argv0 = argv[0], argc--, argv++;

	if (argc) {
		fprintf(stderr, "usage: %s\n", argv0);
		return 1;
	}

	/* header */
	if (fread(hdr, 1, strlen(HEADER), stdin) != strlen(HEADER)) {
		fprintf(stderr, "%s: incomplete header\n", argv0);
		return 1;
	}
	if (memcmp("farbfeld", hdr, strlen("farbfeld"))) {
		fprintf(stderr, "%s: invalid magic value\n", argv0);
		return 1;
	}
	width = ntohl(*((uint32_t *)(hdr + 8)));
	height = ntohl(*((uint32_t *)(hdr + 12)));

	/* icc profile (ProPhoto RGB) */
	if (!(icc_context = cmsCreateContext(NULL, NULL)))
		goto lcmserr;
	if (!(gamma18 = cmsBuildGamma(icc_context, 1.8)))
		goto lcmserr;
	out_curve[0] = out_curve[1] = out_curve[2] = gamma18;
	if (!(out_prof = cmsCreateRGBProfileTHR(icc_context, cmsD50_xyY(),
	                                        &primaries, out_curve)))
		goto lcmserr;
	cmsSetHeaderFlags(out_prof, cmsEmbeddedProfileTrue | cmsUseAnywhere);
	cmsSetHeaderRenderingIntent(out_prof, INTENT_RELATIVE_COLORIMETRIC);
	cmsSetDeviceClass(out_prof, cmsSigColorSpaceClass);
	if (!(mlu1 = cmsMLUalloc(NULL, 1)) || !(mlu2 = cmsMLUalloc(NULL, 1)))
		goto lcmserr;
	cmsMLUsetASCII(mlu1, "en", "US", "Public Domain");
	cmsWriteTag(out_prof, cmsSigCopyrightTag, mlu1);
	cmsMLUsetASCII(mlu2, "en", "US", "ProPhoto RGB");
	cmsWriteTag(out_prof, cmsSigProfileDescriptionTag, mlu2);
	cmsWriteTag(out_prof, cmsSigDeviceModelDescTag, mlu2);
	cmsSaveProfileToMem(out_prof, NULL, &icclen);
	if (!(icc = malloc(icclen))) {
		fprintf(stderr, "%s: malloc: out of memory\n", argv0);
		return 1;
	}
	cmsSaveProfileToMem(out_prof, icc, &icclen);

	/* load png */
	pngs = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, pngerr,
	                               NULL);
	pngi = png_create_info_struct(pngs);

	if (!pngs || !pngi) {
		fprintf(stderr, "%s: failed to initialize libpng\n", argv0);
		return 1;
	}
	png_init_io(pngs, stdout);
	png_set_IHDR(pngs, pngi, width, height, 16, PNG_COLOR_TYPE_RGB_ALPHA,
	             PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
	             PNG_FILTER_TYPE_BASE);
	png_set_iCCP(pngs, pngi, "ProPhoto RGB", 0, icc, icclen);
	png_write_info(pngs, pngi);

	/* write rows */
	png_row_len = strlen("RGBA") * width * sizeof(uint16_t);
	if (!(png_row = malloc(png_row_len))) {
		fprintf(stderr, "%s: malloc: out of memory\n", argv0);
		return 1;
	}
	for (i = 0; i < height; ++i) {
		for (j = 0; j < png_row_len / sizeof(uint16_t); ++j) {
			if (fread(&tmp16, sizeof(uint16_t), 1, stdin) != 1) {
				fprintf(stderr, "%s: unexpected EOF\n", argv0);
				return 1;
			}
			png_row[j] = tmp16;
		}
		png_write_row(pngs, (uint8_t *)png_row);
	}
	png_write_end(pngs, NULL);
	png_destroy_write_struct(&pngs, NULL);

	return 0;
lcmserr:
	fprintf(stderr, "%s: lcms error\n", argv0);

	return 1;
}