diff options
author | P. J. McDermott <pjm@nac.net> | 2013-03-02 17:43:57 (EST) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2013-03-02 17:46:48 (EST) |
commit | 233368318175f741b466ed92761f12e62f1151a5 (patch) | |
tree | 5287c5be25e2a52bc77ae92320ff10d402403647 /src/resources | |
parent | 711cce4919c1f469edb4c7761bcb801a798e677c (diff) | |
download | overworld-rpg-233368318175f741b466ed92761f12e62f1151a5.zip overworld-rpg-233368318175f741b466ed92761f12e62f1151a5.tar.gz overworld-rpg-233368318175f741b466ed92761f12e62f1151a5.tar.bz2 |
Write a GIMP palette parser.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/Makefile.am | 3 | ||||
-rw-r--r-- | src/resources/palette.c | 166 | ||||
-rw-r--r-- | src/resources/palette.h | 15 |
3 files changed, 183 insertions, 1 deletions
diff --git a/src/resources/Makefile.am b/src/resources/Makefile.am index 4dbacdf..5f70688 100644 --- a/src/resources/Makefile.am +++ b/src/resources/Makefile.am @@ -1,4 +1,5 @@ src_resources_SOURCES = \ src/resources/resource.h src/resources/resource.c \ src/resources/image.h src/resources/image.c \ - src/resources/map.h src/resources/map.c + src/resources/map.h src/resources/map.c \ + src/resources/palette.h src/resources/palette.c diff --git a/src/resources/palette.c b/src/resources/palette.c new file mode 100644 index 0000000..3cb1a85 --- /dev/null +++ b/src/resources/palette.c @@ -0,0 +1,166 @@ +#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) { + 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); + } +} diff --git a/src/resources/palette.h b/src/resources/palette.h new file mode 100644 index 0000000..f11a70b --- /dev/null +++ b/src/resources/palette.h @@ -0,0 +1,15 @@ +#ifndef RESOURCES_PALETTE_H +#define RESOURCES_PALETTE_H + +#include <SDL.h> +#include "resource.h" + +struct palette { + struct resource res; + SDL_Palette *palette; +}; + +struct palette *palette_get(const char *path); +void palette_free(struct palette *palette); + +#endif |