summaryrefslogtreecommitdiff
path: root/png2ff.c
blob: 5a5da6ce102f453ecbe608d5d210930d1ed65485 (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
/* See LICENSE file for copyright and license details. */
#include <arpa/inet.h>

#include <errno.h>
#include <setjmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <png.h>

int
main(int argc, char *argv[])
{
	png_structp png_struct_p;
	png_infop png_info_p;
	png_bytepp png_row_p;
	int depth, color, ret = 0;
	uint32_t width, height, png_row_len, tmp32, r, i;
	uint16_t tmp16;

	if (argc > 1) {
		fprintf(stderr, "usage: %s\n", argv[0]);
		return 1;
	}

	/* load png */
	png_struct_p = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
	                                      NULL, NULL);
	png_info_p = png_create_info_struct(png_struct_p);

	if (!png_struct_p || !png_info_p) {
		fprintf(stderr, "%s: failed to initialize libpng\n", argv[0]);
		return 1;
	}
	if (setjmp(png_jmpbuf(png_struct_p)))
		return 1;
	png_init_io(png_struct_p, stdin);
	if (png_get_valid(png_struct_p, png_info_p, PNG_INFO_tRNS))
		png_set_tRNS_to_alpha(png_struct_p);
	png_set_add_alpha(png_struct_p, 255*257, PNG_FILLER_AFTER);
	png_set_expand_gray_1_2_4_to_8(png_struct_p);
	png_set_gray_to_rgb(png_struct_p);
	png_set_packing(png_struct_p);
	png_read_png(png_struct_p, png_info_p, PNG_TRANSFORM_PACKING |
	             PNG_TRANSFORM_EXPAND, NULL);
	png_get_IHDR(png_struct_p, png_info_p, &width, &height, &depth,
	             &color, NULL, NULL, NULL);
	png_row_len = png_get_rowbytes(png_struct_p, png_info_p);
	png_row_p = png_get_rows(png_struct_p, png_info_p);

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

	/* write data */
	switch(depth) {
	case 8:
		for (r = 0; r < height; ++r) {
			for (i = 0; i < png_row_len; i++) {
				/* ((2^16-1) / 255) == 257 */
				tmp16 = htons(257 * png_row_p[r][i]);
				fwrite(&tmp16, sizeof(uint16_t), 1, stdout);
			}
		}
		break;
	case 16:
		for (r = 0; r < height; ++r) {
			for (i = 0; i < png_row_len / 2; i++) {
				tmp16 = *((uint16_t *)
				                (png_row_p[r] + 2 * i));
				fwrite(&tmp16, sizeof(uint16_t), 1, stdout);
			}
		}
		break;
	default:
		fprintf(stderr, "%s: format error\n", argv[0]);
		return 1;
	}

	png_destroy_read_struct(&png_struct_p, &png_info_p, NULL);

	/* flush output */
	if (fflush(stdout)) {
		fprintf(stderr, "%s: fflush stdout: ", argv[0]);
		perror(NULL);
		ret = 1;
	}
	if (fclose(stdout) && !ret) {
		fprintf(stderr, "%s: fclose stdout: ", argv[0]);
		perror(NULL);
		ret = 1;
	}

	return ret;
}