Cleanup code of rgbfix, rgbgfx and external libs

Follow Linux kernel coding style.

Signed-off-by: Antonio Niño Díaz <antonio_nd@outlook.com>
This commit is contained in:
Antonio Niño Díaz
2018-01-01 16:52:43 +01:00
parent f41c532400
commit 2ffaf72e39
17 changed files with 289 additions and 289 deletions

25
include/extern/err.h vendored
View File

@@ -2,10 +2,13 @@
#define EXTERN_ERR_H #define EXTERN_ERR_H
#ifdef ERR_IN_LIBC #ifdef ERR_IN_LIBC
#include <err.h> #include <err.h>
#else
#else /* ERR_IN_LIBC */
#include <stdarg.h> #include <stdarg.h>
#include "extern/stdnoreturn.h" #include "extern/stdnoreturn.h"
#define warn rgbds_warn #define warn rgbds_warn
@@ -18,16 +21,16 @@
#define errx rgbds_errx #define errx rgbds_errx
#define verrx rgbds_verrx #define verrx rgbds_verrx
void warn(const char *, ...); void warn(const char *fmt, ...);
void vwarn(const char *, va_list); void vwarn(const char *fmt, va_list ap);
void warnx(const char *, ...); void warnx(const char *fmt, ...);
void vwarnx(const char *, va_list); void vwarnx(const char *fmt, va_list ap);
noreturn void err(int, const char *, ...); noreturn void err(int status, const char *fmt, ...);
noreturn void verr(int, const char *, va_list); noreturn void verr(int status, const char *fmt, va_list ap);
noreturn void errx(int, const char *, ...); noreturn void errx(int status, const char *fmt, ...);
noreturn void verrx(int, const char *, va_list); noreturn void verrx(int status, const char *fmt, va_list ap);
#endif #endif /* ERR_IN_LIBC */
#endif #endif /* EXTERN_ERR_H */

View File

@@ -2,13 +2,14 @@
#define EXTERN_REALLOCARRAY_H #define EXTERN_REALLOCARRAY_H
#ifdef REALLOCARRAY_IN_LIBC #ifdef REALLOCARRAY_IN_LIBC
#include <stdlib.h> #include <stdlib.h>
#else
#else /* REALLOCARRAY_IN_LIBC */
#define reallocarray rgbds_reallocarray #define reallocarray rgbds_reallocarray
void *reallocarray(void *, size_t, size_t); void *reallocarray(void *, size_t, size_t);
#endif #endif /* REALLOCARRAY_IN_LIBC */
#endif #endif /* EXTERN_REALLOCARRAY_H */

View File

@@ -1,16 +1,21 @@
#ifndef EXTERN_STDNORETURN_H
#define EXTERN_STDNORETURN_H
#if __STDC_VERSION__ >= 201112L #if __STDC_VERSION__ >= 201112L
/* C11 or newer */ /* C11 or newer */
#define noreturn _Noreturn #define noreturn _Noreturn
#elif __cplusplus >= 201103L #elif __cplusplus >= 201103L
/* C++11 or newer */ /* C++11 or newer */
#define noreturn [[noreturn]] #define noreturn [[noreturn]]
#elif __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ >= 5)) #elif __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ >= 5))
/* GCC 2.5 or newer */ /* GCC 2.5 or newer */
#define noreturn __attribute__ ((noreturn)) #define noreturn __attribute__ ((noreturn))
#elif _MSC_VER >= 1310 #elif _MSC_VER >= 1310
/* MS Visual Studio 2003/.NET Framework 1.1 or newer */ /* MS Visual Studio 2003/.NET Framework 1.1 or newer */
#define noreturn _declspec( noreturn) #define noreturn _declspec(noreturn)
#else #else
/* unsupported, but no need to throw a fit */ /* Unsupported, but no need to throw a fit */
#define noreturn #define noreturn
#endif #endif
#endif /* EXTERN_STDNORETURN_H */

18
include/extern/strl.h vendored
View File

@@ -1,13 +1,17 @@
#ifndef STRL_H #ifndef EXTERN_STRL_H
#define STRL_H #define EXTERN_STRL_H
#ifdef STRL_IN_LIBC #ifdef STRL_IN_LIBC
#include <string.h> #include <string.h>
#else
#else /* STRL_IN_LIBC */
#define strlcpy rgbds_strlcpy #define strlcpy rgbds_strlcpy
#define strlcat rgbds_strlcat #define strlcat rgbds_strlcat
size_t strlcpy(char *, const char *, size_t); size_t strlcpy(char *dst, const char *src, size_t dsize);
size_t strlcat(char *, const char *, size_t); size_t strlcat(char *dst, const char *src, size_t dsize);
#endif
#endif #endif /* STRL_IN_LIBC */
#endif /* EXTERN_STRL_H */

View File

@@ -14,8 +14,13 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#ifndef EXTERN_VERSION_H
#define EXTERN_VERSION_H
#define PACKAGE_VERSION_MAJOR (0) #define PACKAGE_VERSION_MAJOR (0)
#define PACKAGE_VERSION_MINOR (3) #define PACKAGE_VERSION_MINOR (3)
#define PACKAGE_VERSION_PATCH (3) #define PACKAGE_VERSION_PATCH (3)
const char * get_package_version_string(void); const char *get_package_version_string(void);
#endif /* EXTERN_VERSION_H */

View File

@@ -20,11 +20,14 @@
#include <stdint.h> #include <stdint.h>
#include "gfx/main.h" #include "gfx/main.h"
void png_to_gb(struct PNGImage png, struct GBImage *gb); void png_to_gb(const struct PNGImage png, struct GBImage *gb);
void output_file(struct Options opts, struct GBImage gb); void output_file(const struct Options opts, const struct GBImage gb);
int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size); int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles,
void create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap); int tile_size);
void output_tilemap_file(struct Options opts, struct Tilemap tilemap); void create_tilemap(const struct Options opts, struct GBImage *gb,
void output_palette_file(struct Options opts, struct PNGImage png); struct Tilemap *tilemap);
void output_tilemap_file(const struct Options opts,
const struct Tilemap tilemap);
void output_palette_file(const struct Options opts, const struct PNGImage png);
#endif #endif

View File

@@ -72,4 +72,4 @@ int depth, colors;
#include "gfx/makepng.h" #include "gfx/makepng.h"
#include "gfx/gb.h" #include "gfx/gb.h"
#endif #endif /* RGBDS_GFX_MAIN_H */

View File

@@ -19,10 +19,10 @@
#include "gfx/main.h" #include "gfx/main.h"
void input_png_file(struct Options opts, struct PNGImage *img); void input_png_file(const struct Options opts, struct PNGImage *img);
void get_text(struct PNGImage *png); void get_text(struct PNGImage *png);
void set_text(struct PNGImage *png); void set_text(const struct PNGImage *png);
void output_png_file(struct Options opts, struct PNGImage *png); void output_png_file(const struct Options opts, const struct PNGImage *png);
void free_png_data(struct PNGImage *png); void free_png_data(const struct PNGImage *png);
#endif #endif /* RGBDS_GFX_PNG_H */

29
src/extern/err.c vendored
View File

@@ -21,16 +21,17 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "extern/err.h" #include "extern/err.h"
void rgbds_vwarn(const char *fmt, va_list ap) void rgbds_vwarn(const char *fmt, va_list ap)
{ {
fprintf (stderr, "warning"); fprintf(stderr, "warning");
if (fmt) { if (fmt) {
fputs (": ", stderr); fputs(": ", stderr);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
putc('\n', stderr); putc('\n', stderr);
@@ -39,9 +40,9 @@ void rgbds_vwarn(const char *fmt, va_list ap)
void rgbds_vwarnx(const char *fmt, va_list ap) void rgbds_vwarnx(const char *fmt, va_list ap)
{ {
fprintf (stderr, "warning"); fprintf(stderr, "warning");
if (fmt) { if (fmt) {
fputs (": ", stderr); fputs(": ", stderr);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
putc('\n', stderr); putc('\n', stderr);
@@ -49,9 +50,9 @@ void rgbds_vwarnx(const char *fmt, va_list ap)
noreturn void rgbds_verr(int status, const char *fmt, va_list ap) noreturn void rgbds_verr(int status, const char *fmt, va_list ap)
{ {
fprintf (stderr, "error"); fprintf(stderr, "error");
if (fmt) { if (fmt) {
fputs (": ", stderr); fputs(": ", stderr);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
putc('\n', stderr); putc('\n', stderr);
@@ -60,11 +61,11 @@ noreturn void rgbds_verr(int status, const char *fmt, va_list ap)
noreturn void rgbds_verrx(int status, const char *fmt, va_list ap) noreturn void rgbds_verrx(int status, const char *fmt, va_list ap)
{ {
fprintf (stderr, "error"); fprintf(stderr, "error");
if (fmt) { if (fmt) {
fputs (": ", stderr); fputs(": ", stderr);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
putc('\n', stderr); putc('\n', stderr);
exit(status); exit(status);
} }
@@ -72,6 +73,7 @@ noreturn void rgbds_verrx(int status, const char *fmt, va_list ap)
void rgbds_warn(const char *fmt, ...) void rgbds_warn(const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vwarn(fmt, ap); vwarn(fmt, ap);
va_end(ap); va_end(ap);
@@ -80,6 +82,7 @@ void rgbds_warn(const char *fmt, ...)
void rgbds_warnx(const char *fmt, ...) void rgbds_warnx(const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vwarnx(fmt, ap); vwarnx(fmt, ap);
va_end(ap); va_end(ap);
@@ -88,6 +91,7 @@ void rgbds_warnx(const char *fmt, ...)
noreturn void rgbds_err(int status, const char *fmt, ...) noreturn void rgbds_err(int status, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
verr(status, fmt, ap); verr(status, fmt, ap);
va_end(ap); va_end(ap);
@@ -96,6 +100,7 @@ noreturn void rgbds_err(int status, const char *fmt, ...)
noreturn void rgbds_errx(int status, const char *fmt, ...) noreturn void rgbds_errx(int status, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
verrx(status, fmt, ap); verrx(status, fmt, ap);
va_end(ap); va_end(ap);

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ /* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */
/* /*
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
* *
@@ -15,22 +15,21 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/types.h>
#include <errno.h> #include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
/* /*
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
* if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
*/ */
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
void * void *rgbds_reallocarray(void *optr, size_t nmemb, size_t size)
rgbds_reallocarray(void *optr, size_t nmemb, size_t size)
{ {
if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && if (((nmemb >= MUL_NO_OVERFLOW) || (size >= MUL_NO_OVERFLOW)) &&
nmemb > 0 && SIZE_MAX / nmemb < size) { (nmemb > 0) && (SIZE_MAX / nmemb < size)) {
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */ /* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */
/* /*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -16,8 +16,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/types.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
/* /*
* Appends src to string dst of size dsize (unlike strncat, dsize is the * Appends src to string dst of size dsize (unlike strncat, dsize is the
@@ -26,8 +26,7 @@
* Returns strlen(src) + MIN(dsize, strlen(initial dst)). * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= dsize, truncation occurred. * If retval >= dsize, truncation occurred.
*/ */
size_t size_t rgbds_strlcat(char *dst, const char *src, size_t dsize)
rgbds_strlcat(char *dst, const char *src, size_t dsize)
{ {
const char *odst = dst; const char *odst = dst;
const char *osrc = src; const char *osrc = src;
@@ -51,5 +50,5 @@ rgbds_strlcat(char *dst, const char *src, size_t dsize)
} }
*dst = '\0'; *dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */ return dlen + (src - osrc); /* count does not include NUL */
} }

16
src/extern/strlcpy.c vendored
View File

@@ -1,4 +1,4 @@
/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ /* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
/* /*
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -16,16 +16,15 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <sys/types.h>
#include <string.h> #include <string.h>
#include <sys/types.h>
/* /*
* Copy string src to buffer dst of size dsize. At most dsize-1 * Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0). * chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred. * Returns strlen(src); if retval >= dsize, truncation occurred.
*/ */
size_t size_t rgbds_strlcpy(char *dst, const char *src, size_t dsize)
rgbds_strlcpy(char *dst, const char *src, size_t dsize)
{ {
const char *osrc = src; const char *osrc = src;
size_t nleft = dsize; size_t nleft = dsize;
@@ -33,7 +32,10 @@ rgbds_strlcpy(char *dst, const char *src, size_t dsize)
/* Copy as many bytes as will fit. */ /* Copy as many bytes as will fit. */
if (nleft != 0) { if (nleft != 0) {
while (--nleft != 0) { while (--nleft != 0) {
if ((*dst++ = *src++) == '\0') char n = *src++;
*dst++ = n;
if (n == '\0')
break; break;
} }
} }
@@ -41,10 +43,10 @@ rgbds_strlcpy(char *dst, const char *src, size_t dsize)
/* Not enough room in dst, add NUL and traverse rest of src. */ /* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) { if (nleft == 0) {
if (dsize != 0) if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */ *dst = '\0'; /* NUL-terminate dst */
while (*src++) while (*src++)
; ;
} }
return(src - osrc - 1); /* count does not include NUL */ return (src - osrc - 1); /* count does not include NUL */
} }

View File

@@ -19,14 +19,14 @@
#include "extern/version.h" #include "extern/version.h"
const char * get_package_version_string(void) const char *get_package_version_string(void)
{ {
static char s[50]; static char s[50];
/* The following conditional should be simplified by the compiler. */ /* The following conditional should be simplified by the compiler. */
if (strlen(BUILD_VERSION_STRING) == 0) { if (strlen(BUILD_VERSION_STRING) == 0) {
snprintf(s, sizeof(s), "v%d.%d.%d", PACKAGE_VERSION_MAJOR, snprintf(s, sizeof(s), "v%d.%d.%d", PACKAGE_VERSION_MAJOR,
PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCH); PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCH);
return s; return s;
} else { } else {
return BUILD_VERSION_STRING; return BUILD_VERSION_STRING;

View File

@@ -24,8 +24,7 @@
#include "extern/err.h" #include "extern/err.h"
#include "extern/version.h" #include "extern/version.h"
static void static void print_usage(void)
usage(void)
{ {
printf( printf(
"usage: rgbfix [-CcjsVv] [-i game_id] [-k licensee_str] [-l licensee_id]\n" "usage: rgbfix [-CcjsVv] [-i game_id] [-k licensee_str] [-l licensee_id]\n"
@@ -34,8 +33,7 @@ usage(void)
exit(1); exit(1);
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
FILE *rom; FILE *rom;
int ch; int ch;
@@ -81,10 +79,9 @@ main(int argc, char *argv[])
case 'i': case 'i':
setid = true; setid = true;
if (strlen(optarg) != 4) { if (strlen(optarg) != 4)
errx(1, "Game ID %s must be exactly 4 " errx(1, "Game ID %s must be exactly 4 characters",
"characters", optarg); optarg);
}
id = optarg; id = optarg;
break; break;
@@ -94,10 +91,9 @@ main(int argc, char *argv[])
case 'k': case 'k':
setnewlicensee = true; setnewlicensee = true;
if (strlen(optarg) != 2) { if (strlen(optarg) != 2)
errx(1, "New licensee code %s is not the " errx(1, "New licensee code %s is not the correct length of 2 characters",
"correct length of 2 characters", optarg); optarg);
}
newlicensee = optarg; newlicensee = optarg;
break; break;
@@ -105,61 +101,59 @@ main(int argc, char *argv[])
setlicensee = true; setlicensee = true;
licensee = strtoul(optarg, &ep, 0); licensee = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') { if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'l'"); errx(1, "Invalid argument for option 'l'");
}
if (licensee < 0 || licensee > 0xFF) { if (licensee < 0 || licensee > 0xFF)
errx(1, "Argument for option 'l' must be " errx(1, "Argument for option 'l' must be between 0 and 255");
"between 0 and 255");
}
break; break;
case 'm': case 'm':
setcartridge = true; setcartridge = true;
cartridge = strtoul(optarg, &ep, 0); cartridge = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') { if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'm'"); errx(1, "Invalid argument for option 'm'");
}
if (cartridge < 0 || cartridge > 0xFF) { if (cartridge < 0 || cartridge > 0xFF)
errx(1, "Argument for option 'm' must be " errx(1, "Argument for option 'm' must be between 0 and 255");
"between 0 and 255");
}
break; break;
case 'n': case 'n':
setversion = true; setversion = true;
version = strtoul(optarg, &ep, 0); version = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') {
if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'n'"); errx(1, "Invalid argument for option 'n'");
}
if (version < 0 || version > 0xFF) { if (version < 0 || version > 0xFF)
errx(1, "Argument for option 'n' must be " errx(1, "Argument for option 'n' must be between 0 and 255");
"between 0 and 255");
}
break; break;
case 'p': case 'p':
resize = true; resize = true;
padvalue = strtoul(optarg, &ep, 0); padvalue = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') {
if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'p'"); errx(1, "Invalid argument for option 'p'");
}
if (padvalue < 0 || padvalue > 0xFF) { if (padvalue < 0 || padvalue > 0xFF)
errx(1, "Argument for option 'p' must be " errx(1, "Argument for option 'p' must be between 0 and 255");
"between 0 and 255");
}
break; break;
case 'r': case 'r':
setramsize = true; setramsize = true;
ramsize = strtoul(optarg, &ep, 0); ramsize = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') {
if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'r'"); errx(1, "Invalid argument for option 'r'");
}
if (ramsize < 0 || ramsize > 0xFF) { if (ramsize < 0 || ramsize > 0xFF)
errx(1, "Argument for option 'r' must be " errx(1, "Argument for option 'r' must be between 0 and 255");
"between 0 and 255");
}
break; break;
case 's': case 's':
super = true; super = true;
@@ -167,14 +161,13 @@ main(int argc, char *argv[])
case 't': case 't':
settitle = true; settitle = true;
if (strlen(optarg) > 16) { if (strlen(optarg) > 16)
errx(1, "Title %s is greater than the " errx(1, "Title \"%s\" is greater than the maximum of 16 characters",
"maximum of 16 characters", optarg); optarg);
}
if (strlen(optarg) == 16) if (strlen(optarg) == 16)
warnx("Title %s is 16 chars, it is best to " warnx("Title \"%s\" is 16 chars, it is best to keep it to 15 or fewer",
"keep it to 15 or fewer", optarg); optarg);
title = optarg; title = optarg;
break; break;
@@ -185,7 +178,7 @@ main(int argc, char *argv[])
validate = true; validate = true;
break; break;
default: default:
usage(); print_usage();
/* NOTREACHED */ /* NOTREACHED */
} }
} }
@@ -194,15 +187,16 @@ main(int argc, char *argv[])
argv += optind; argv += optind;
if (argc == 0) if (argc == 0)
usage(); print_usage();
/* /*
* Open the ROM file * Open the ROM file
*/ */
if ((rom = fopen(argv[argc - 1], "rb+")) == NULL) { rom = fopen(argv[argc - 1], "rb+");
if (rom == NULL)
err(1, "Error opening file %s", argv[argc - 1]); err(1, "Error opening file %s", argv[argc - 1]);
}
/* /*
* Write changes to ROM * Write changes to ROM
@@ -266,7 +260,7 @@ main(int argc, char *argv[])
* characters). * characters).
*/ */
fseek(rom,0x13F,SEEK_SET); fseek(rom, 0x13F, SEEK_SET);
fwrite(id, 1, 4, rom); fwrite(id, 1, 4, rom);
} }
@@ -332,8 +326,7 @@ main(int argc, char *argv[])
*/ */
if (!setlicensee) if (!setlicensee)
warnx("You should probably set both '-s' and " warnx("You should probably set both '-s' and '-l 0x33'");
"'-l 0x33'");
fseek(rom, 0x146, SEEK_SET); fseek(rom, 0x146, SEEK_SET);
fputc(3, rom); fputc(3, rom);
@@ -360,6 +353,7 @@ main(int argc, char *argv[])
long romsize, newsize; long romsize, newsize;
int headbyte; int headbyte;
uint8_t *buf; uint8_t *buf;
fseek(rom, 0, SEEK_END); fseek(rom, 0, SEEK_END);
romsize = ftell(rom); romsize = ftell(rom);
newsize = 0x8000; newsize = 0x8000;
@@ -433,9 +427,8 @@ main(int argc, char *argv[])
* Offset 0x14D: Header Checksum * Offset 0x14D: Header Checksum
*/ */
uint8_t headcksum; uint8_t headcksum = 0;
headcksum = 0;
fseek(rom, 0x134, SEEK_SET); fseek(rom, 0x134, SEEK_SET);
for (int i = 0; i < (0x14D - 0x134); ++i) for (int i = 0; i < (0x14D - 0x134); ++i)
headcksum = headcksum - fgetc(rom) - 1; headcksum = headcksum - fgetc(rom) - 1;
@@ -447,15 +440,14 @@ main(int argc, char *argv[])
* Offset 0x14E0x14F: Global Checksum * Offset 0x14E0x14F: Global Checksum
*/ */
uint16_t globalcksum; uint16_t globalcksum = 0;
globalcksum = 0;
rewind(rom); rewind(rom);
for (int i = 0; i < 0x14E; ++i) for (int i = 0; i < 0x14E; ++i)
globalcksum += fgetc(rom); globalcksum += fgetc(rom);
int byte; int byte;
fseek(rom, 0x150, SEEK_SET); fseek(rom, 0x150, SEEK_SET);
while ((byte = fgetc(rom)) != EOF) while ((byte = fgetc(rom)) != EOF)
globalcksum += byte; globalcksum += byte;

View File

@@ -19,8 +19,7 @@
#include "gfx/main.h" #include "gfx/main.h"
void void transpose_tiles(struct GBImage *gb, int width)
transpose_tiles(struct GBImage *gb, int width)
{ {
uint8_t *newdata; uint8_t *newdata;
int i; int i;
@@ -29,7 +28,9 @@ transpose_tiles(struct GBImage *gb, int width)
newdata = calloc(gb->size, 1); newdata = calloc(gb->size, 1);
for (i = 0; i < gb->size; i++) { for (i = 0; i < gb->size; i++) {
newbyte = i / (8 * depth) * width * 8 * depth; newbyte = i / (8 * depth) * width * 8 * depth;
newbyte = newbyte % gb->size + 8 * depth * (newbyte / gb->size) + i % (8 * depth); newbyte = newbyte % gb->size
+ 8 * depth * (newbyte / gb->size)
+ i % (8 * depth);
newdata[newbyte] = gb->data[i]; newdata[newbyte] = gb->data[i];
} }
@@ -38,8 +39,7 @@ transpose_tiles(struct GBImage *gb, int width)
gb->data = newdata; gb->data = newdata;
} }
void void png_to_gb(const struct PNGImage png, struct GBImage *gb)
png_to_gb(struct PNGImage png, struct GBImage *gb)
{ {
int x, y, byte; int x, y, byte;
png_byte index; png_byte index;
@@ -50,55 +50,55 @@ png_to_gb(struct PNGImage png, struct GBImage *gb)
index &= (1 << depth) - 1; index &= (1 << depth) - 1;
if (!gb->horizontal) { if (!gb->horizontal) {
byte = y * depth + x / 8 * png.height / 8 * 8 * depth; byte = y * depth
+ x / 8 * png.height / 8 * 8 * depth;
} else { } else {
byte = y * depth + x / 8 * png.height / 8 * 8 * depth; byte = y * depth
+ x / 8 * png.height / 8 * 8 * depth;
} }
gb->data[byte] |= (index & 1) << (7 - x % 8); gb->data[byte] |= (index & 1) << (7 - x % 8);
if (depth == 2) { if (depth == 2) {
gb->data[byte + 1] |= (index >> 1) << (7 - x % 8); gb->data[byte + 1] |=
(index >> 1) << (7 - x % 8);
} }
} }
} }
if (!gb->horizontal) { if (!gb->horizontal)
transpose_tiles(gb, png.width / 8); transpose_tiles(gb, png.width / 8);
}
} }
void void output_file(const struct Options opts, const struct GBImage gb)
output_file(struct Options opts, struct GBImage gb)
{ {
FILE *f; FILE *f;
f = fopen(opts.outfile, "wb"); f = fopen(opts.outfile, "wb");
if (!f) { if (!f)
err(1, "Opening output file '%s' failed", opts.outfile); err(1, "Opening output file '%s' failed", opts.outfile);
}
fwrite(gb.data, 1, gb.size - gb.trim * 8 * depth, f); fwrite(gb.data, 1, gb.size - gb.trim * 8 * depth, f);
fclose(f); fclose(f);
} }
int int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size)
get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size)
{ {
int i, j; int i, j;
for (i = 0; i < num_tiles; i++) { for (i = 0; i < num_tiles; i++) {
for (j = 0; j < tile_size; j++) { for (j = 0; j < tile_size; j++) {
if (tile[j] != tiles[i][j]) { if (tile[j] != tiles[i][j])
break; break;
}
} }
if (j >= tile_size) {
if (j >= tile_size)
return i; return i;
}
} }
return -1; return -1;
} }
void void create_tilemap(const struct Options opts, struct GBImage *gb,
create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap) struct Tilemap *tilemap)
{ {
int i, j; int i, j;
int gb_i; int gb_i;
@@ -113,7 +113,7 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
tile_size = sizeof(uint8_t) * depth * 8; tile_size = sizeof(uint8_t) * depth * 8;
gb_size = gb->size - (gb->trim * tile_size); gb_size = gb->size - (gb->trim * tile_size);
max_tiles = gb_size / tile_size; max_tiles = gb_size / tile_size;
tiles = malloc(sizeof(uint8_t*) * max_tiles); tiles = malloc(sizeof(uint8_t *) * max_tiles);
num_tiles = 0; num_tiles = 0;
tilemap->data = malloc(sizeof(uint8_t) * max_tiles); tilemap->data = malloc(sizeof(uint8_t) * max_tiles);
@@ -127,7 +127,8 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
gb_i++; gb_i++;
} }
if (opts.unique) { if (opts.unique) {
index = get_tile_index(tile, tiles, num_tiles, tile_size); index = get_tile_index(tile, tiles, num_tiles,
tile_size);
if (index < 0) { if (index < 0) {
index = num_tiles; index = num_tiles;
tiles[num_tiles] = tile; tiles[num_tiles] = tile;
@@ -147,39 +148,35 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
gb->data = malloc(tile_size * num_tiles); gb->data = malloc(tile_size * num_tiles);
for (i = 0; i < num_tiles; i++) { for (i = 0; i < num_tiles; i++) {
tile = tiles[i]; tile = tiles[i];
for (j = 0; j < tile_size; j++) { for (j = 0; j < tile_size; j++)
gb->data[i * tile_size + j] = tile[j]; gb->data[i * tile_size + j] = tile[j];
}
} }
gb->size = i * tile_size; gb->size = i * tile_size;
} }
for (i = 0; i < num_tiles; i++) { for (i = 0; i < num_tiles; i++)
free(tiles[i]); free(tiles[i]);
}
free(tiles); free(tiles);
} }
void void output_tilemap_file(const struct Options opts,
output_tilemap_file(struct Options opts, struct Tilemap tilemap) const struct Tilemap tilemap)
{ {
FILE *f; FILE *f;
f = fopen(opts.mapfile, "wb"); f = fopen(opts.mapfile, "wb");
if (!f) { if (!f)
err(1, "Opening tilemap file '%s' failed", opts.mapfile); err(1, "Opening tilemap file '%s' failed", opts.mapfile);
}
fwrite(tilemap.data, 1, tilemap.size, f); fwrite(tilemap.data, 1, tilemap.size, f);
fclose(f); fclose(f);
if (opts.mapout) { if (opts.mapout)
free(opts.mapfile); free(opts.mapfile);
}
} }
void void output_palette_file(const struct Options opts, const struct PNGImage png)
output_palette_file(struct Options opts, struct PNGImage png)
{ {
FILE *f; FILE *f;
int i, colors, color; int i, colors, color;
@@ -188,16 +185,18 @@ output_palette_file(struct Options opts, struct PNGImage png)
if (png_get_PLTE(png.png, png.info, &palette, &colors)) { if (png_get_PLTE(png.png, png.info, &palette, &colors)) {
f = fopen(opts.palfile, "wb"); f = fopen(opts.palfile, "wb");
if (!f) { if (!f) {
err(1, "Opening palette file '%s' failed", opts.palfile); err(1, "Opening palette file '%s' failed",
opts.palfile);
} }
for (i = 0; i < colors; i++) { for (i = 0; i < colors; i++) {
color = palette[i].blue >> 3 << 10 | palette[i].green >> 3 << 5 | palette[i].red >> 3; color = palette[i].blue >> 3 << 10
| palette[i].green >> 3 << 5
| palette[i].red >> 3;
fwrite(&color, 2, 1, f); fwrite(&color, 2, 1, f);
} }
fclose(f); fclose(f);
} }
if (opts.palout) { if (opts.palout)
free(opts.palfile); free(opts.palfile);
}
} }

View File

@@ -19,10 +19,10 @@
#include <unistd.h> #include <unistd.h>
#include "extern/version.h" #include "extern/version.h"
#include "gfx/main.h" #include "gfx/main.h"
static void static void print_usage(void)
usage(void)
{ {
printf( printf(
"usage: rgbgfx [-DFfhPTuVv] [-d #] [-o outfile] [-p palfile] [-t mapfile]\n" "usage: rgbgfx [-DFfhPTuVv] [-d #] [-o outfile] [-p palfile] [-t mapfile]\n"
@@ -30,8 +30,7 @@ usage(void)
exit(1); exit(1);
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
int ch, size; int ch, size;
struct Options opts = {0}; struct Options opts = {0};
@@ -41,9 +40,8 @@ main(int argc, char *argv[])
char *ext; char *ext;
const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line."; const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line.";
if (argc == 1) { if (argc == 1)
usage(); print_usage();
}
opts.mapfile = ""; opts.mapfile = "";
opts.palfile = ""; opts.palfile = "";
@@ -51,8 +49,8 @@ main(int argc, char *argv[])
depth = 2; depth = 2;
while((ch = getopt(argc, argv, "Dd:Ffho:Tt:uPp:Vvx:")) != -1) { while ((ch = getopt(argc, argv, "Dd:Ffho:Tt:uPp:Vvx:")) != -1) {
switch(ch) { switch (ch) {
case 'D': case 'D':
opts.debug = true; opts.debug = true;
break; break;
@@ -95,22 +93,21 @@ main(int argc, char *argv[])
opts.trim = strtoul(optarg, NULL, 0); opts.trim = strtoul(optarg, NULL, 0);
break; break;
default: default:
usage(); print_usage();
/* NOTREACHED */ /* NOTREACHED */
} }
} }
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc == 0) { if (argc == 0)
usage(); print_usage();
}
opts.infile = argv[argc - 1]; opts.infile = argv[argc - 1];
if (depth != 1 && depth != 2) { if (depth != 1 && depth != 2)
errx(1, "Depth option must be either 1 or 2."); errx(1, "Depth option must be either 1 or 2.");
}
colors = 1 << depth; colors = 1 << depth;
input_png_file(opts, &png); input_png_file(opts, &png);
@@ -121,82 +118,77 @@ main(int argc, char *argv[])
get_text(&png); get_text(&png);
if (png.horizontal != opts.horizontal) { if (png.horizontal != opts.horizontal) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "horizontal"); warnx(errmsg, "horizontal");
}
if (opts.hardfix) { if (opts.hardfix)
png.horizontal = opts.horizontal; png.horizontal = opts.horizontal;
}
}
if (png.horizontal) {
opts.horizontal = png.horizontal;
} }
if (png.horizontal)
opts.horizontal = png.horizontal;
if (png.trim != opts.trim) { if (png.trim != opts.trim) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "trim"); warnx(errmsg, "trim");
}
if (opts.hardfix) { if (opts.hardfix)
png.trim = opts.trim; png.trim = opts.trim;
}
} }
if (png.trim) {
if (png.trim)
opts.trim = png.trim; opts.trim = png.trim;
}
if (opts.trim > png.width / 8 - 1) { if (opts.trim > png.width / 8 - 1) {
errx(1, "Trim (%i) for input png file '%s' too large (max: %i)", opts.trim, opts.infile, png.width / 8 - 1); errx(1, "Trim (%i) for input png file '%s' too large (max: %i)",
opts.trim, opts.infile, png.width / 8 - 1);
} }
if (strcmp(png.mapfile, opts.mapfile) != 0) { if (strcmp(png.mapfile, opts.mapfile) != 0) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "tilemap file"); warnx(errmsg, "tilemap file");
}
if (opts.hardfix) { if (opts.hardfix)
png.mapfile = opts.mapfile; png.mapfile = opts.mapfile;
}
} }
if (!*opts.mapfile) { if (!*opts.mapfile)
opts.mapfile = png.mapfile; opts.mapfile = png.mapfile;
}
if (png.mapout != opts.mapout) { if (png.mapout != opts.mapout) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "tilemap file"); warnx(errmsg, "tilemap file");
}
if (opts.hardfix) { if (opts.hardfix)
png.mapout = opts.mapout; png.mapout = opts.mapout;
}
} }
if (png.mapout) { if (png.mapout)
opts.mapout = png.mapout; opts.mapout = png.mapout;
}
if (strcmp(png.palfile, opts.palfile) != 0) { if (strcmp(png.palfile, opts.palfile) != 0) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "palette file"); warnx(errmsg, "palette file");
}
if (opts.hardfix) { if (opts.hardfix)
png.palfile = opts.palfile; png.palfile = opts.palfile;
}
} }
if (!*opts.palfile) { if (!*opts.palfile)
opts.palfile = png.palfile; opts.palfile = png.palfile;
}
if (png.palout != opts.palout) { if (png.palout != opts.palout) {
if (opts.verbose) { if (opts.verbose)
warnx(errmsg, "palette file"); warnx(errmsg, "palette file");
}
if (opts.hardfix) { if (opts.hardfix)
png.palout = opts.palout; png.palout = opts.palout;
}
}
if (png.palout) {
opts.palout = png.palout;
} }
if (png.palout)
opts.palout = png.palout;
if (!*opts.mapfile && opts.mapout) { if (!*opts.mapfile && opts.mapout) {
if ((ext = strrchr(opts.infile, '.')) != NULL) { ext = strrchr(opts.infile, '.');
if (ext != NULL) {
size = ext - opts.infile + 9; size = ext - opts.infile + 9;
opts.mapfile = malloc(size); opts.mapfile = malloc(size);
strncpy(opts.mapfile, opts.infile, size); strncpy(opts.mapfile, opts.infile, size);
@@ -210,7 +202,9 @@ main(int argc, char *argv[])
} }
if (!*opts.palfile && opts.palout) { if (!*opts.palfile && opts.palout) {
if ((ext = strrchr(opts.infile, '.')) != NULL) { ext = strrchr(opts.infile, '.');
if (ext != NULL) {
size = ext - opts.infile + 5; size = ext - opts.infile + 5;
opts.palfile = malloc(size); opts.palfile = malloc(size);
strncpy(opts.palfile, opts.infile, size); strncpy(opts.palfile, opts.infile, size);
@@ -233,17 +227,14 @@ main(int argc, char *argv[])
create_tilemap(opts, &gb, &tilemap); create_tilemap(opts, &gb, &tilemap);
} }
if (*opts.outfile) { if (*opts.outfile)
output_file(opts, gb); output_file(opts, gb);
}
if (*opts.mapfile) { if (*opts.mapfile)
output_tilemap_file(opts, tilemap); output_tilemap_file(opts, tilemap);
}
if (*opts.palfile) { if (*opts.palfile)
output_palette_file(opts, png); output_palette_file(opts, png);
}
if (opts.fix || opts.debug) { if (opts.fix || opts.debug) {
set_text(&png); set_text(&png);

View File

@@ -16,10 +16,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "gfx/main.h" #include "gfx/main.h"
void void input_png_file(const struct Options opts, struct PNGImage *img)
input_png_file(struct Options opts, struct PNGImage *img)
{ {
FILE *f; FILE *f;
int i, y, num_trans; int i, y, num_trans;
@@ -30,24 +30,21 @@ input_png_file(struct Options opts, struct PNGImage *img)
png_color *palette; png_color *palette;
f = fopen(opts.infile, "rb"); f = fopen(opts.infile, "rb");
if (!f) { if (!f)
err(1, "Opening input png file '%s' failed", opts.infile); err(1, "Opening input png file '%s' failed", opts.infile);
}
img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
if (!img->png) { NULL, NULL, NULL);
if (!img->png)
errx(1, "Creating png structure failed"); errx(1, "Creating png structure failed");
}
img->info = png_create_info_struct(img->png); img->info = png_create_info_struct(img->png);
if (!img->info) { if (!img->info)
errx(1, "Creating png info structure failed"); errx(1, "Creating png info structure failed");
}
/* Better error handling here? */ /* TODO: Better error handling here? */
if (setjmp(png_jmpbuf(img->png))) { if (setjmp(png_jmpbuf(img->png)))
exit(1); exit(1);
}
png_init_io(img->png, f); png_init_io(img->png, f);
@@ -58,41 +55,39 @@ input_png_file(struct Options opts, struct PNGImage *img)
img->depth = png_get_bit_depth(img->png, img->info); img->depth = png_get_bit_depth(img->png, img->info);
img->type = png_get_color_type(img->png, img->info); img->type = png_get_color_type(img->png, img->info);
if (img->type & PNG_COLOR_MASK_ALPHA) { if (img->type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(img->png); png_set_strip_alpha(img->png);
}
if (img->depth != depth) { if (img->depth != depth) {
if (opts.verbose) { if (opts.verbose) {
warnx("Image bit depth is not %i (is %i).", depth, warnx("Image bit depth is not %i (is %i).", depth,
img->depth); img->depth);
} }
} }
if (img->type == PNG_COLOR_TYPE_GRAY) { if (img->type == PNG_COLOR_TYPE_GRAY) {
if (img->depth < 8) { if (img->depth < 8)
png_set_expand_gray_1_2_4_to_8(img->png); png_set_expand_gray_1_2_4_to_8(img->png);
}
png_set_gray_to_rgb(img->png); png_set_gray_to_rgb(img->png);
} else { } else {
if (img->depth < 8) { if (img->depth < 8)
png_set_expand_gray_1_2_4_to_8(img->png); png_set_expand_gray_1_2_4_to_8(img->png);
}
has_palette = png_get_PLTE(img->png, img->info, &palette, has_palette = png_get_PLTE(img->png, img->info, &palette,
&colors); &colors);
} }
if (png_get_tRNS(img->png, img->info, &trans_alpha, &num_trans, if (png_get_tRNS(img->png, img->info, &trans_alpha, &num_trans,
&trans_values)) { &trans_values)) {
if (img->type == PNG_COLOR_TYPE_PALETTE) { if (img->type == PNG_COLOR_TYPE_PALETTE) {
full_alpha = malloc(sizeof(bool) * num_trans); full_alpha = malloc(sizeof(bool) * num_trans);
for (i = 0; i < num_trans; i++) { for (i = 0; i < num_trans; i++) {
if (trans_alpha[i] > 0) { if (trans_alpha[i] > 0)
full_alpha[i] = false; full_alpha[i] = false;
} else { else
full_alpha[i] = true; full_alpha[i] = true;
}
} }
for (i = 0; i < num_trans; i++) { for (i = 0; i < num_trans; i++) {
@@ -182,9 +177,8 @@ input_png_file(struct Options opts, struct PNGImage *img)
png_read_update_info(img->png, img->info); png_read_update_info(img->png, img->info);
img->data = malloc(sizeof(png_byte *) * img->height); img->data = malloc(sizeof(png_byte *) * img->height);
for (y = 0; y < img->height; y++) { for (y = 0; y < img->height; y++)
img->data[y] = malloc(png_get_rowbytes(img->png, img->info)); img->data[y] = malloc(png_get_rowbytes(img->png, img->info));
}
png_read_image(img->png, img->data); png_read_image(img->png, img->data);
png_read_end(img->png, img->info); png_read_end(img->png, img->info);
@@ -192,8 +186,7 @@ input_png_file(struct Options opts, struct PNGImage *img)
fclose(f); fclose(f);
} }
void void get_text(struct PNGImage *png)
get_text(struct PNGImage *png)
{ {
png_text *text; png_text *text;
int i, numtxts, numremoved; int i, numtxts, numremoved;
@@ -221,11 +214,14 @@ get_text(struct PNGImage *png)
} }
} }
/* TODO: Remove this and simply change the warning function not to warn instead. */ /*
* TODO: Remove this and simply change the warning function not to warn
* instead.
*/
for (i = 0, numremoved = 0; i < numtxts; i++) { for (i = 0, numremoved = 0; i < numtxts; i++) {
if (text[i].key == NULL) { if (text[i].key == NULL)
numremoved++; numremoved++;
}
text[i].key = text[i + numremoved].key; text[i].key = text[i + numremoved].key;
text[i].text = text[i + numremoved].text; text[i].text = text[i + numremoved].text;
text[i].compression = text[i + numremoved].compression; text[i].compression = text[i + numremoved].compression;
@@ -233,8 +229,7 @@ get_text(struct PNGImage *png)
png_set_text(png->png, png->info, text, numtxts - numremoved); png_set_text(png->png, png->info, text, numtxts - numremoved);
} }
void void set_text(const struct PNGImage *png)
set_text(struct PNGImage *png)
{ {
png_text *text; png_text *text;
char buffer[3]; char buffer[3];
@@ -282,14 +277,16 @@ set_text(struct PNGImage *png)
free(text); free(text);
} }
void void output_png_file(const struct Options opts, const struct PNGImage *png)
output_png_file(struct Options opts, struct PNGImage *png)
{ {
FILE *f; FILE *f;
char *outfile; char *outfile;
png_struct *img; png_struct *img;
/* Variable outfile is for debugging purposes. Eventually, opts.infile will be used directly. */ /*
* TODO: Variable outfile is for debugging purposes. Eventually,
* opts.infile will be used directly.
*/
if (opts.debug) { if (opts.debug) {
outfile = malloc(strlen(opts.infile) + 5); outfile = malloc(strlen(opts.infile) + 5);
strcpy(outfile, opts.infile); strcpy(outfile, opts.infile);
@@ -299,19 +296,16 @@ output_png_file(struct Options opts, struct PNGImage *png)
} }
f = fopen(outfile, "wb"); f = fopen(outfile, "wb");
if (!f) { if (!f)
err(1, "Opening output png file '%s' failed", outfile); err(1, "Opening output png file '%s' failed", outfile);
}
img = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); img = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!img) { if (!img)
errx(1, "Creating png structure failed"); errx(1, "Creating png structure failed");
}
/* Better error handling here? */ /* TODO: Better error handling here? */
if (setjmp(png_jmpbuf(img))) { if (setjmp(png_jmpbuf(img)))
exit(1); exit(1);
}
png_init_io(img, f); png_init_io(img, f);
@@ -322,18 +316,16 @@ output_png_file(struct Options opts, struct PNGImage *png)
fclose(f); fclose(f);
if (opts.debug) { if (opts.debug)
free(outfile); free(outfile);
}
} }
void void free_png_data(const struct PNGImage *png)
free_png_data(struct PNGImage *png)
{ {
int y; int y;
for (y = 0; y < png->height; y++) { for (y = 0; y < png->height; y++)
free(png->data[y]); free(png->data[y]);
}
free(png->data); free(png->data);
} }