summaryrefslogtreecommitdiff
path: root/test/decode_gif.c
blob: 5789c41543c8294624f7f6636312774aff512ff4 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright 2008 Sean Fox <dyntryx@gmail.com>
 * Copyright 2008 James Bursa <james@netsurf-browser.org>
 *
 * This file is part of NetSurf's libnsgif, http://www.netsurf-browser.org/
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

#include "../include/nsgif.h"

#define BYTES_PER_PIXEL 4

static void *bitmap_create(int width, int height)
{
	/* Ensure a stupidly large bitmap is not created */
	if (width > 4096 || height > 4096) {
		return NULL;
	}

	return calloc(width * height, BYTES_PER_PIXEL);
}

static unsigned char *bitmap_get_buffer(void *bitmap)
{
	return bitmap;
}

static void bitmap_destroy(void *bitmap)
{
	free(bitmap);
}

static uint8_t *load_file(const char *path, size_t *data_size)
{
	FILE *fd;
	struct stat sb;
	unsigned char *buffer;
	size_t size;
	size_t n;

	fd = fopen(path, "rb");
	if (!fd) {
		perror(path);
		exit(EXIT_FAILURE);
	}

	if (stat(path, &sb)) {
		perror(path);
		exit(EXIT_FAILURE);
	}
	size = sb.st_size;

	buffer = malloc(size);
	if (!buffer) {
		fprintf(stderr, "Unable to allocate %lld bytes\n",
			(long long) size);
		exit(EXIT_FAILURE);
	}

	n = fread(buffer, 1, size, fd);
	if (n != size) {
		perror(path);
		exit(EXIT_FAILURE);
	}

	fclose(fd);

	*data_size = size;
	return buffer;
}

static void warning(const char *context, nsgif_result gif_res)
{
	fprintf(stderr, "%s failed: ", context);
	switch (gif_res) {
	case NSGIF_FRAME_DATA_ERROR:
		fprintf(stderr, "NSGIF_FRAME_DATA_ERROR");
		break;
	case NSGIF_INSUFFICIENT_DATA:
		fprintf(stderr, "NSGIF_INSUFFICIENT_DATA");
		break;
	case NSGIF_DATA_ERROR:
		fprintf(stderr, "NSGIF_DATA_ERROR");
		break;
	case NSGIF_INSUFFICIENT_MEMORY:
		fprintf(stderr, "NSGIF_INSUFFICIENT_MEMORY");
		break;
	default:
		fprintf(stderr, "unknown code %i", gif_res);
		break;
	}
	fprintf(stderr, "\n");
}

static void write_ppm(FILE* fh, const char *name, nsgif *gif, bool no_write)
{
	nsgif_result gif_res;
	uint32_t frame_prev = 0;
	const nsgif_info_t *info;

	info = nsgif_get_info(gif);

	if (!no_write) {
		fprintf(fh, "P3\n");
		fprintf(fh, "# %s\n", name);
		fprintf(fh, "# width                %u \n", info->width);
		fprintf(fh, "# height               %u \n", info->height);
		fprintf(fh, "# frame_count          %u \n", info->frame_count);
		fprintf(fh, "# loop_max             %u \n", info->loop_max);
		fprintf(fh, "%u %u 256\n", info->width,
				info->height * info->frame_count);
	}

	/* decode the frames */
	while (true) {
		const uint32_t *buffer;
		const uint8_t *image;
		uint32_t frame_new;
		uint32_t delay_cs;
		nsgif_rect area;

		gif_res = nsgif_frame_prepare(gif, &area,
				&delay_cs, &frame_new);
		if (gif_res != NSGIF_OK) {
			warning("nsgif_frame_prepare", gif_res);
			return;
		}

		if (frame_new < frame_prev) {
			/* Must be an animation that loops. We only care about
			 * decoding each frame once. */
			return;
		}
		frame_prev = frame_new;

		gif_res = nsgif_frame_decode(gif, frame_new, &buffer);
		if (gif_res != NSGIF_OK) {
			warning("nsgif_decode_frame", gif_res);
			return;
		}

		if (!no_write) {
			fprintf(fh, "# frame %u:\n", frame_new);
			image = (const uint8_t *) buffer;
			for (uint32_t y = 0; y != info->height; y++) {
				for (uint32_t x = 0; x != info->width; x++) {
					size_t z = (y * info->width + x) * 4;
					fprintf(fh, "%u %u %u ",
							image[z],
							image[z + 1],
							image[z + 2]);
				}
				fprintf(fh, "\n");
			}
		}
	}
}

int main(int argc, char *argv[])
{
	const nsgif_bitmap_cb_vt bitmap_callbacks = {
		.create     = bitmap_create,
		.destroy    = bitmap_destroy,
		.get_buffer = bitmap_get_buffer,
	};
	nsgif *gif;
	size_t size;
	uint8_t *data;
	FILE *outf = stdout;
	nsgif_result gif_res;
	bool no_write = false;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s image.gif [out]\n", argv[0]);
		fprintf(stderr, "\n");
		fprintf(stderr, "If [out] is NOWRITE, the gif will be docoded "
				"but not output.\n");
		fprintf(stderr, "Otherwise [out] is an output filename.\n");
		fprintf(stderr, "When [out] is unset, output is to stdout.\n");

		return 1;
	}

	if (argc > 2) {
		if (strcmp(argv[2], "NOWRITE") == 0) {
			no_write = true;
		} else {
			outf = fopen(argv[2], "w+");
			if (outf == NULL) {
				fprintf(stderr, "Unable to open %s for writing\n", argv[2]);
				return 2;
			}
		}
	}

	/* create our gif animation */
	gif_res = nsgif_create(&bitmap_callbacks, &gif);
	if (gif_res != NSGIF_OK) {
		return 1;
	}

	/* load file into memory */
	data = load_file(argv[1], &size);

	/* begin decoding */
	do {
		gif_res = nsgif_data_scan(gif, size, data);
		if (gif_res != NSGIF_OK && gif_res != NSGIF_WORKING) {
			warning("nsgif_data_scan", gif_res);
			nsgif_destroy(gif);
			free(data);
			return 1;
		}
	} while (gif_res != NSGIF_OK);

	write_ppm(outf, argv[1], gif, no_write);

	if (argc > 2 && !no_write) {
		fclose(outf);
	}

	/* clean up */
	nsgif_destroy(gif);
	free(data);

	return 0;
}