summaryrefslogtreecommitdiffstats
path: root/src/resources/palette.c
blob: b20b6d0330716b147a11618642e137ec729fd488 (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
/*
 * Copyright (C) 2013  Patrick "P. J." McDermott
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <SDL.h>
#include <config.h>
#include "../logging.h"
#include "palette.h"
#include "resource.h"

struct resource_table pal_res;

static void palette_gpl_parse(const char *, SDL_Palette *);

struct palette *
palette_get(const char *path)
{
	struct palette *pal;

	pal = (struct palette *) resource_get(&pal_res, path);
	if (pal != NULL) {
		resource_use((struct resource *) pal);
		return pal;
	}

	pal = resource_alloc(path, sizeof(*pal));
	pal->palette = malloc(sizeof(*pal->palette));
	if (pal->palette == NULL) {
		err(1, "Failed to load palette \"%s\" (out of memory)", path);
	}
	pal->palette->colors = malloc(sizeof(*pal->palette->colors) * 240);
	if (pal->palette->colors == NULL) {
		err(1, "Failed to load palette \"%s\" (out of memory)", path);
	}

	palette_gpl_parse(path, pal->palette);

#ifdef DEBUG_PALETTES
	{
		int col_i;

		debug("Palette \"%s\"", path);
		for (col_i = 0; col_i < pal->palette->ncolors; ++col_i) {
			debug("    Color %3.3d: 0x%2.2x%2.2x%2.2x",
					col_i,
					pal->palette->colors[col_i].r,
					pal->palette->colors[col_i].g,
					pal->palette->colors[col_i].b);
		}
	}
#endif

	resource_use((struct resource *) pal);
	resource_add(&pal_res, path, (struct resource *) pal);

	return pal;

}

static void
palette_gpl_parse(const char *path, SDL_Palette *palette)
{
	FILE *fp;
	char str[1024];
	int i;
	char *tok;

	/*
	 * See also:
	 *   * gimp_palette_load()
	 *     GIMP 2.8.2, app/core/gimppalette-load.c:53
	 */

	fp = fopen(path, "rb");
	if (fp == NULL) {
		err(1, "Failed to load palette \"%s\" (cannot open file)",
				path);
	}

	/* GIMP Palette */
	if (fgets(str, sizeof(str), fp) == NULL) {
		err(1, "Failed to load palette \"%s\" (cannot read line)",
				path);
	}
	if (strncmp(str, "GIMP Palette", 12) != 0) {
		err(1, "Failed to load palette \"%s\" (bad magic header)",
				path);
	}

	/* Name: ... */
	if (fgets(str, sizeof(str), fp) == NULL) {
		err(1, "Failed to load palette \"%s\" (cannot read line)",
				path);
	}
	if (strncmp(str, "Name: ", 6) == 0) {
		/* Columns: ... */
		if (fgets(str, sizeof(str), fp) == NULL) {
			err(1, "Failed to load palette \"%s\" "
					"(cannot read line)", path);
		}
		if (strncmp(str, "Columns: ", 9) == 0) {
			if (fgets(str, sizeof(str), fp) == NULL) {
				err(1, "Failed to load palette \"%s\" "
						"(cannot read line)", path);
			}
		}
	}

	i = 0;
	while (!feof(fp)) {
		if (str[0] != '#' && str[0] != '\n') {
			if (i >= 240) {
				warn("Ignoring extra colors in palette \"%s\"",
						path);
				break;
			}

			tok = strtok(str, " \t");
			if (tok != NULL) {
				palette->colors[i].r = atoi(tok);
			} else {
				warn("Bad color definition in palette \"%s\"",
						path);
			}

			tok = strtok(NULL, " \t");
			if (tok != NULL) {
				palette->colors[i].g = atoi(tok);
			} else {
				warn("Bad color definition in palette \"%s\"",
						path);
			}

			tok = strtok(NULL, " \t");
			if (tok != NULL) {
				palette->colors[i].b = atoi(tok);
			} else {
				warn("Bad color definition in palette \"%s\"",
						path);
			}

			++i;
		}

		if (fgets(str, sizeof(str), fp) == NULL) {
			if (feof(fp)) {
				break;
			}
			err(1, "Failed to load palette \"%s\" "
					"(cannot read line)", path);
		}
	}

	palette->ncolors = i;
	fclose(fp);
}

void
palette_free(struct palette *palette)
{
	SDL_Palette *data;

	if (palette == NULL) {
		return;
	}

	data = palette->palette;
	if (resource_free(&pal_res, (struct resource *) palette)) {
		free(data->colors);
		free(data);
	}
}