summaryrefslogtreecommitdiff
path: root/png2ff.c
blob: a5b96ce854c37aaad12c34c3a1319a638cbf3bec (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
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
164
165
166
167
168
169
170
171
/* 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>

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

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

int
main(int argc, char *argv[])
{
	cmsHPROFILE in_prof, out_prof;
	cmsHTRANSFORM trans;
	cmsToneCurve *gamma10, *out_curves[3];
	png_structp pngs;
	png_infop pngi;
	int icc_compression;
	uint32_t width, height, icc_len, outrowlen, tmp32, r, i;
	uint16_t *inrow, *outrow;
	uint8_t **png_row_p, *icc_data;
	char *icc_name;

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

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

	/* load png */
	pngs = png_create_read_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, stdin);
	if (png_get_valid(pngs, pngi, PNG_INFO_tRNS))
		png_set_tRNS_to_alpha(pngs);
	png_set_add_alpha(pngs, 255*257, PNG_FILLER_AFTER);
	png_set_expand_gray_1_2_4_to_8(pngs);
	png_set_gray_to_rgb(pngs);
	png_set_packing(pngs);
	png_read_png(pngs, pngi, PNG_TRANSFORM_PACKING |
	             PNG_TRANSFORM_EXPAND, NULL);
	width = png_get_image_width(pngs, pngi);
	height = png_get_image_height(pngs, pngi);
	png_row_p = png_get_rows(pngs, pngi);

	/* icc profile (output linear ROMM RGB (ISO 22028-2:2013)) */
	if (png_get_valid(pngs, pngi, PNG_INFO_iCCP)) {
		png_get_iCCP(pngs, pngi, &icc_name,
		             &icc_compression, &icc_data, &icc_len);
		if (!(in_prof = cmsOpenProfileFromMem(icc_data,
		                                         icc_len)))
			goto lcmserr;
	} else {
		if (!(in_prof = cmsCreate_sRGBProfile()))
			goto lcmserr;
	}
	if (!(gamma10 = cmsBuildGamma(NULL, 1.0)))
		goto lcmserr;
	out_curves[0] = out_curves[1] = out_curves[2] = gamma10;
	if (!(out_prof = cmsCreateRGBProfile(cmsD50_xyY(), &primaries,
	                                        out_curves)))
		goto lcmserr;

	/* allocate output row buffer */
	outrowlen = width * strlen("RGBA");
	if (!(outrow = malloc(outrowlen * sizeof(uint16_t)))) {
		fprintf(stderr, "%s: malloc: out of memory\n", argv0);
		return 1;
	}

	/* write header */
	fputs("farbfeld", stdout);
	tmp32 = htonl(width);
	if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
		goto writerr;
	tmp32 = htonl(height);
	if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1)
		goto writerr;

	/* write data */
	switch(png_get_bit_depth(pngs, pngi)) {
	case 8:
		if (!(trans = cmsCreateTransform(in_prof, TYPE_RGBA_8,
		                                 out_prof, TYPE_RGBA_16,
		                                 INTENT_RELATIVE_COLORIMETRIC,
		                                 0)))
			goto lcmserr;
		for (r = 0; r < height; ++r) {
			cmsDoTransform(trans, png_row_p[r], outrow, width);
			for (i = 0; i < outrowlen; i++) {
				/* re-add alpha */
				if (i >= 3 && (i - 3) % 4 == 0)
					outrow[i] = 257 * png_row_p[r][i];
				/* swap endiannes to BE */
				outrow[i] = htons(outrow[i]);
			}
			if (fwrite(outrow, sizeof(uint16_t), outrowlen,
			           stdout) != outrowlen)
				goto writerr;
		}
		break;
	case 16:
		if (!(trans = cmsCreateTransform(in_prof, TYPE_RGBA_16,
		                                 out_prof, TYPE_RGBA_16,
		                                 INTENT_RELATIVE_COLORIMETRIC,
		                                 0)))
			goto lcmserr;
		for (r = 0; r < height; ++r) {
			inrow = (uint16_t *)png_row_p[r];
			for (i = 0; i < outrowlen; i++) {
				/* swap endianness to LE */
				inrow[i] = ntohs(inrow[i]);
			}
			cmsDoTransform(trans, png_row_p[r], outrow, width);
			for (i = 0; i < outrowlen; ++i) {
				/* re-add alpha */
				if (i >= 3 && (i - 3) % 4 == 0)
					outrow[i] = inrow[i];
				/* swap endianness to BE */
				outrow[i] = htons(outrow[i]);
			}
			if (fwrite(outrow, sizeof(uint16_t), outrowlen,
			           stdout) != outrowlen)
				goto writerr;
		}
		break;
	default:
		fprintf(stderr, "%s: invalid bit-depth\n", argv0);
		return 1;
	}

	png_destroy_read_struct(&pngs, &pngi, NULL);

	return 0;
writerr:
	fprintf(stderr, "%s: fwrite: ", argv0);
	perror(NULL);

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

	return 1;
}