Skip to content

Commit

Permalink
Load default palette from file.
Browse files Browse the repository at this point in the history
Rather than using hard-coded reference to `doom_palette`.
Part of #2.
  • Loading branch information
fragglet committed Jul 15, 2024
1 parent dedd8ae commit 755170f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/conv/graphic.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ static VFILE *RGBABufferToPatch(uint8_t *buffer, size_t rowstep,
struct patch_header *hdr)
{
VFILE *result;
const struct palette *pal = PAL_DefaultPalette();
struct patch_header swapped_hdr;
uint32_t *column_offsets;
uint8_t *palettized, *post, alpha;
int x, y, post_len;

palettized = V_PalettizeRGBABuffer(&doom_palette, buffer, rowstep,
palettized = V_PalettizeRGBABuffer(pal, buffer, rowstep,
hdr->width, hdr->height);

result = vfopenmem(NULL, 0);
Expand Down Expand Up @@ -139,7 +140,8 @@ static VFILE *BufferToRaw(uint8_t *imgbuf, int rowstep,
VFILE *result = vfopenmem(NULL, 0);

palettized = V_PalettizeRGBABuffer(
&doom_palette, imgbuf, rowstep, hdr->width, hdr->height);
PAL_DefaultPalette(), imgbuf, rowstep,
hdr->width, hdr->height);
assert(vfwrite(palettized, hdr->width,
hdr->height, result) == hdr->height);
free(palettized);
Expand Down Expand Up @@ -284,7 +286,8 @@ VFILE *V_ToImageFile(VFILE *input)
goto fail;
}

result = V_WritePalettizedPNG(&hdr, imgbuf, &doom_palette, true);
result = V_WritePalettizedPNG(&hdr, imgbuf,
PAL_DefaultPalette(), true);

fail:
free(imgbuf);
Expand Down Expand Up @@ -313,7 +316,7 @@ VFILE *V_FlatToImageFile(VFILE *input)
hdr.height = buf_len / 64;
hdr.topoffset = 0;
hdr.leftoffset = 0;
result = V_WritePalettizedPNG(&hdr, buf, &doom_palette, false);
result = V_WritePalettizedPNG(&hdr, buf, PAL_DefaultPalette(), false);

fail:
free(buf);
Expand All @@ -337,7 +340,7 @@ VFILE *V_FullscreenToImageFile(VFILE *input)
hdr.height = FULLSCREEN_H;
hdr.topoffset = 0;
hdr.leftoffset = 0;
result = V_WritePalettizedPNG(&hdr, buf, &doom_palette, false);
result = V_WritePalettizedPNG(&hdr, buf, PAL_DefaultPalette(), false);
free(buf);

return result;
Expand Down
6 changes: 3 additions & 3 deletions src/conv/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ VFILE *V_ColormapToImageFile(VFILE *input)
hdr.height = buf_len / 256;
hdr.topoffset = 0;
hdr.leftoffset = 0;
result = V_WritePalettizedPNG(&hdr, buf, &doom_palette, false);
result = V_WritePalettizedPNG(&hdr, buf, PAL_DefaultPalette(), false);

fail:
free(buf);
Expand Down Expand Up @@ -98,8 +98,8 @@ VFILE *V_ColormapFromImageFile(VFILE *input)
goto fail;
}

palettized = V_PalettizeRGBABuffer(&doom_palette, imgbuf, rowstep,
hdr.width, hdr.height);
palettized = V_PalettizeRGBABuffer(PAL_DefaultPalette(), imgbuf,
rowstep, hdr.width, hdr.height);
result = vfopenmem(NULL, 0);
assert(vfwrite(palettized, hdr.width,
hdr.height, result) == hdr.height);
Expand Down
31 changes: 31 additions & 0 deletions src/palette/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#define PALETTE_SIZE (256 * 3)

static bool default_palette_loaded = false;
static struct palette default_palette;

struct palette_set *PAL_FromImageFile(VFILE *input)
{
struct png_context ctx;
Expand Down Expand Up @@ -221,6 +224,34 @@ char *PAL_ReadDefaultPointer(void)
return buf;
}

static void LoadDefaultPalette(void)
{
const char *dir = PAL_GetPalettesPath();
char *path = DefaultPointerPath(dir);
FILE *infile = fopen(path, "rb");
VFILE *in;
struct palette_set *set;

assert(infile != NULL);
in = vfwrapfile(infile);
set = PAL_FromImageFile(in);
free(path);

default_palette = set->palettes[0];
PAL_FreePaletteSet(set);

default_palette_loaded = true;
}

const struct palette *PAL_DefaultPalette(void)
{
if (!default_palette_loaded) {
LoadDefaultPalette();
}

return &default_palette;
}

static void WriteDoomPalette(const char *path)
{
FILE *fs = fopen(path, "wb");
Expand Down
1 change: 1 addition & 0 deletions src/palette/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ struct palette_set *PAL_UnmarshalPaletteSet(VFILE *input);
void PAL_FreePaletteSet(struct palette_set *set);
const char *PAL_GetPalettesPath(void);
char *PAL_ReadDefaultPointer(void);
const struct palette *PAL_DefaultPalette(void);

0 comments on commit 755170f

Please sign in to comment.