# HG changeset patch # User Matti Hamalainen # Date 1317019528 -10800 # Node ID 2badfa44a37aa52244a29d80c246e71b0949c1dd # Parent 50c9fa99e7b4713a23f7c7d8e8f254ff2efca247 Cleanup palette exporting code. diff -r 50c9fa99e7b4 -r 2badfa44a37a src/mkpalette.cc --- a/src/mkpalette.cc Mon Sep 26 09:39:42 2011 +0300 +++ b/src/mkpalette.cc Mon Sep 26 09:45:28 2011 +0300 @@ -36,92 +36,12 @@ #include "wads.h" -/* - * make_gimp_palette - * Generate a Gimp palette file for the th - * palette in the PLAYPAL entry. - * Return 0 on success, non-zero on failure. - */ -int make_gimp_palette(int playpalnum, const char *filename) +static int palette_read_from_wad(int playpalnum, u8 **dpal) { - int rc = 0; - MDirPtr dir; - u8 *dpal = 0; - FILE *output_fp = 0; - const char *lump_name = "PLAYPAL"; - dir = FindMasterDir(MasterDir, lump_name); - if (dir == 0) - { - warn("%s: lump not found\n", lump_name); - return 1; - } - - int playpal_count = dir->dir.size / (3 * DOOM_COLOURS); - if (playpalnum < 0 || playpalnum >= playpal_count) - { - warn("playpalnum %d out of range (0-%d), using #0 instead\n", - playpalnum, playpal_count - 1); - playpalnum = 0; - } - - output_fp = fopen(filename, "w"); - if (output_fp == 0) - { - warn("%s: %s\n", filename, strerror(errno)); - return 1; - } - fprintf(output_fp, - "GIMP Palette\n" "# Generated by Yadex %s\n", yadex_version); - dpal = (u8 *) GetMemory(3 * DOOM_COLOURS); - const Wad_file *wf = dir->wadfile; - wf->seek(dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS); - if (wf->error()) - { - err("%s: seek error", lump_name); - rc = 1; - goto byebye; - } - wf->read_bytes(dpal, 3 * DOOM_COLOURS); - if (wf->error()) - { - err("%s: read error", lump_name); - rc = 1; - goto byebye; - } - for (size_t n = 0; n < DOOM_COLOURS; n++) - fprintf(output_fp, - "%3d %3d %3d Index = %d (%02Xh) RGB = %d, %d, %d\n", - dpal[3 * n], dpal[3 * n + 1], dpal[3 * n + 2], n, n, - dpal[3 * n], dpal[3 * n + 1], dpal[3 * n + 2]); - - byebye: - if (dpal != 0) - FreeMemory(dpal); - if (output_fp != 0) - if (fclose(output_fp)) - return 1; - return rc; -} - - -/* - * make_palette_ppm - * Generate a 256 x 128 raw PPM image showing all the - * colours in the palette. - * Return 0 on success, non-zero on failure. - */ -int make_palette_ppm(int playpalnum, const char *filename) -{ - int rc = 0; - MDirPtr dir; - u8 *dpal = 0; - FILE *output_fp = 0; - - const char *lump_name = "PLAYPAL"; - dir = FindMasterDir(MasterDir, lump_name); - if (dir == 0) + MDirPtr dir = FindMasterDir(MasterDir, lump_name); + if (dir == NULL) { warn("%s: lump not found\n", lump_name); return 1; @@ -135,44 +55,120 @@ playpalnum = 0; } - output_fp = fopen(filename, "wb"); - if (output_fp == 0) + *dpal = (u8 *) GetMemory(3 * DOOM_COLOURS); + if (*dpal == NULL) { - warn("%s: %s\n", filename, strerror(errno)); - return 1; + err("Could not allocate memory for the palette entry"); + return -1; } - - const int width = 128; - const int height = 128; - const int columns = 16; + + const Wad_file *wf = dir->wadfile; - fputs("P6", output_fp); - fnewline(output_fp); - fprintf(output_fp, "# Generated by Yadex %s", yadex_version); - fnewline(output_fp); - fprintf(output_fp, "%d %d", width, height); - fnewline(output_fp); - fputs("255\n", output_fp); // Always \n (must be a single character) - - int rect_w = width / columns; - int rect_h = height / (DOOM_COLOURS / columns); - - dpal = (u8 *) GetMemory(3 * DOOM_COLOURS); - const Wad_file *wf = dir->wadfile; wf->seek(dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS); if (wf->error()) { err("%s: seek error", lump_name); - rc = 1; - goto byebye; + return -2; } - wf->read_bytes(dpal, 3 * DOOM_COLOURS); + + wf->read_bytes(*dpal, 3 * DOOM_COLOURS); if (wf->error()) { err("%s: read error", lump_name); - rc = 1; + return -3; + } + + return 0; +} + +static FILE *palette_open_outfile(const char *filename, const char *mode) +{ + FILE *f = fopen(filename, mode); + if (f == NULL) + warn("%s: %s\n", filename, strerror(errno)); + return f; +} + +static void palette_cleanup(FILE *outfile, u8 *dpal, int *rc) +{ + FreeMemory(dpal); + if (outfile != NULL) + { + if (fclose(outfile)) + *rc = 1; + } +} + +/* + * make_gimp_palette + * Generate a Gimp palette file for the th + * palette in the PLAYPAL entry. + * Return 0 on success, non-zero on failure. + */ +int make_gimp_palette(int playpalnum, const char *filename) +{ + int rc = 0; + u8 *dpal = NULL; + FILE *outfile = NULL; + + if ((rc = palette_read_from_wad(playpalnum, &dpal)) != 0) + goto byebye; + + if ((outfile = palette_open_outfile(filename, "w")) == NULL) + { + rc = -1; goto byebye; } + + fprintf(outfile, + "GIMP Palette\n" "# Generated by Yadex %s\n", yadex_version); + + for (size_t n = 0; n < DOOM_COLOURS; n++) + fprintf(outfile, + "%3d %3d %3d Index = %d (%02Xh) RGB = %d, %d, %d\n", + dpal[3 * n], dpal[3 * n + 1], dpal[3 * n + 2], n, n, + dpal[3 * n], dpal[3 * n + 1], dpal[3 * n + 2]); + +byebye: + palette_cleanup(outfile, dpal, &rc); + return rc; +} + + +/* + * make_palette_ppm + * Generate a 256 x 128 raw PPM image showing all the + * colours in the palette. + * Return 0 on success, non-zero on failure. + */ +int make_palette_ppm(int playpalnum, const char *filename) +{ + int rc = 0; + u8 *dpal = NULL; + FILE *outfile = NULL; + const int width = 128; + const int height = 128; + const int columns = 16; + int rect_w = width / columns; + int rect_h = height / (DOOM_COLOURS / columns); + + if ((rc = palette_read_from_wad(playpalnum, &dpal)) != 0) + goto byebye; + + if ((outfile = palette_open_outfile(filename, "wb")) == NULL) + { + rc = -1; + goto byebye; + } + + fputs("P6", outfile); + fnewline(outfile); + fprintf(outfile, "# Generated by Yadex %s", yadex_version); + fnewline(outfile); + fprintf(outfile, "%d %d", width, height); + fnewline(outfile); + fputs("255\n", outfile); // Always \n (must be a single character) + for (size_t n = 0; n < DOOM_COLOURS; n += columns) for (int subrow = 0; subrow < rect_h; subrow++) for (int c = 0; c < columns; c++) @@ -180,24 +176,20 @@ { if (subrow == 0 && subcol == 0) { - putc(0, output_fp); - putc(0, output_fp); - putc(0, output_fp); + putc(0, outfile); + putc(0, outfile); + putc(0, outfile); } else { - putc(dpal[3 * (n + c)], output_fp); - putc(dpal[3 * (n + c) + 1], output_fp); - putc(dpal[3 * (n + c) + 2], output_fp); + putc(dpal[3 * (n + c)], outfile); + putc(dpal[3 * (n + c) + 1], outfile); + putc(dpal[3 * (n + c) + 2], outfile); } } - byebye: - if (dpal != 0) - FreeMemory(dpal); - if (output_fp != 0) - if (fclose(output_fp)) - return 1; +byebye: + palette_cleanup(outfile, dpal, &rc); return rc; } @@ -210,73 +202,37 @@ int make_palette_ppm_2(int playpalnum, const char *filename) { int rc = 0; - MDirPtr dir; - u8 *dpal = 0; - FILE *output_fp = 0; - - const char *lump_name = "PLAYPAL"; - dir = FindMasterDir(MasterDir, lump_name); - if (dir == 0) - { - warn("%s: lump not found", lump_name); - return 1; - } - - int playpal_count = dir->dir.size / (3 * DOOM_COLOURS); - if (playpalnum < 0 || playpalnum >= playpal_count) - { - warn("playpalnum %d out of range (0-%d), using #0 instead", - playpalnum, playpal_count - 1); - playpalnum = 0; - } - - output_fp = fopen(filename, "wb"); - if (output_fp == 0) - { - warn("%s: %s\n", filename, strerror(errno)); - return 1; - } - + u8 *dpal = NULL; + FILE *outfile = NULL; const int width = DOOM_COLOURS; const int height = DOOM_COLOURS; - fputs("P6", output_fp); - fnewline(output_fp); - fprintf(output_fp, "# Generated by Yadex %s", yadex_version); - fnewline(output_fp); - fprintf(output_fp, "%d %d", width, height); - fnewline(output_fp); - fputs("255\n", output_fp); // Always \n (must be a single character) + if ((rc = palette_read_from_wad(playpalnum, &dpal)) != 0) + goto byebye; - dpal = (u8 *) GetMemory(3 * DOOM_COLOURS); - const Wad_file *wf = dir->wadfile; - wf->seek(dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS); - if (wf->error()) + if ((outfile = palette_open_outfile(filename, "wb")) == NULL) { - err("%s: seek error", lump_name); - rc = 1; + rc = -1; goto byebye; } - wf->read_bytes(dpal, 3 * DOOM_COLOURS); - if (wf->error()) - { - err("%s: read error", lump_name); - rc = 1; - goto byebye; - } + + fputs("P6", outfile); + fnewline(outfile); + fprintf(outfile, "# Generated by Yadex %s", yadex_version); + fnewline(outfile); + fprintf(outfile, "%d %d", width, height); + fnewline(outfile); + fputs("255\n", outfile); // Always \n (must be a single character) + for (int l = 0; l < height; l++) for (int c = 0; c < width; c++) { - putc(dpal[3 * ((c + l) % DOOM_COLOURS)], output_fp); - putc(dpal[3 * ((c + l) % DOOM_COLOURS) + 1], output_fp); - putc(dpal[3 * ((c + l) % DOOM_COLOURS) + 2], output_fp); + putc(dpal[3 * ((c + l) % DOOM_COLOURS)], outfile); + putc(dpal[3 * ((c + l) % DOOM_COLOURS) + 1], outfile); + putc(dpal[3 * ((c + l) % DOOM_COLOURS) + 2], outfile); } - byebye: - if (dpal != 0) - FreeMemory(dpal); - if (output_fp != 0) - if (fclose(output_fp)) - return 1; +byebye: + palette_cleanup(outfile, dpal, &rc); return rc; }