ref: 2ffaf72e3904e5a81b4ab73b1f8dfcd07258a201
parent: f41c5324000c281e29690de73054da7a0bef3b96
author: Antonio Niño Díaz <[email protected]>
date: Mon Jan 1 11:52:43 EST 2018
Cleanup code of rgbfix, rgbgfx and external libs Follow Linux kernel coding style. Signed-off-by: Antonio Niño Díaz <[email protected]>
--- a/include/extern/err.h
+++ b/include/extern/err.h
@@ -2,10 +2,13 @@
#define EXTERN_ERR_H
#ifdef ERR_IN_LIBC
+
#include <err.h>
-#else
+#else /* ERR_IN_LIBC */
+
#include <stdarg.h>
+
#include "extern/stdnoreturn.h"
#define warn rgbds_warn
@@ -18,16 +21,16 @@
#define errx rgbds_errx
#define verrx rgbds_verrx
-void warn(const char *, ...);
-void vwarn(const char *, va_list);
-void warnx(const char *, ...);
-void vwarnx(const char *, va_list);
+void warn(const char *fmt, ...);
+void vwarn(const char *fmt, va_list ap);
+void warnx(const char *fmt, ...);
+void vwarnx(const char *fmt, va_list ap);
-noreturn void err(int, const char *, ...);
-noreturn void verr(int, const char *, va_list);
-noreturn void errx(int, const char *, ...);
-noreturn void verrx(int, const char *, va_list);
+noreturn void err(int status, const char *fmt, ...);
+noreturn void verr(int status, const char *fmt, va_list ap);
+noreturn void errx(int status, const char *fmt, ...);
+noreturn void verrx(int status, const char *fmt, va_list ap);
-#endif
+#endif /* ERR_IN_LIBC */
-#endif
+#endif /* EXTERN_ERR_H */
--- a/include/extern/reallocarray.h
+++ b/include/extern/reallocarray.h
@@ -2,13 +2,14 @@
#define EXTERN_REALLOCARRAY_H
#ifdef REALLOCARRAY_IN_LIBC
+
#include <stdlib.h>
-#else
-#define reallocarray rgbds_reallocarray
+#else /* REALLOCARRAY_IN_LIBC */
+#define reallocarray rgbds_reallocarray
void *reallocarray(void *, size_t, size_t);
-#endif
+#endif /* REALLOCARRAY_IN_LIBC */
-#endif
+#endif /* EXTERN_REALLOCARRAY_H */
--- a/include/extern/stdnoreturn.h
+++ b/include/extern/stdnoreturn.h
@@ -1,16 +1,21 @@
+#ifndef EXTERN_STDNORETURN_H
+#define EXTERN_STDNORETURN_H
+
#if __STDC_VERSION__ >= 201112L
- /* C11 or newer */
- #define noreturn _Noreturn
+ /* C11 or newer */
+ #define noreturn _Noreturn
#elif __cplusplus >= 201103L
- /* C++11 or newer */
- #define noreturn [[noreturn]]
+ /* C++11 or newer */
+ #define noreturn [[noreturn]]
#elif __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ >= 5))
- /* GCC 2.5 or newer */
- #define noreturn __attribute__ ((noreturn))
+ /* GCC 2.5 or newer */
+ #define noreturn __attribute__ ((noreturn))
#elif _MSC_VER >= 1310
- /* MS Visual Studio 2003/.NET Framework 1.1 or newer */
- #define noreturn _declspec( noreturn)
+ /* MS Visual Studio 2003/.NET Framework 1.1 or newer */
+ #define noreturn _declspec(noreturn)
#else
- /* unsupported, but no need to throw a fit */
- #define noreturn
+ /* Unsupported, but no need to throw a fit */
+ #define noreturn
#endif
+
+#endif /* EXTERN_STDNORETURN_H */
--- a/include/extern/strl.h
+++ b/include/extern/strl.h
@@ -1,13 +1,17 @@
-#ifndef STRL_H
-#define STRL_H
+#ifndef EXTERN_STRL_H
+#define EXTERN_STRL_H
#ifdef STRL_IN_LIBC
+
#include <string.h>
-#else
+
+#else /* STRL_IN_LIBC */
+
#define strlcpy rgbds_strlcpy
#define strlcat rgbds_strlcat
-size_t strlcpy(char *, const char *, size_t);
-size_t strlcat(char *, const char *, size_t);
-#endif
+size_t strlcpy(char *dst, const char *src, size_t dsize);
+size_t strlcat(char *dst, const char *src, size_t dsize);
-#endif
+#endif /* STRL_IN_LIBC */
+
+#endif /* EXTERN_STRL_H */
--- a/include/extern/version.h
+++ b/include/extern/version.h
@@ -14,8 +14,13 @@
* 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_MINOR (3)
#define PACKAGE_VERSION_PATCH (3)
-const char * get_package_version_string(void);
+const char *get_package_version_string(void);
+
+#endif /* EXTERN_VERSION_H */
--- a/include/gfx/gb.h
+++ b/include/gfx/gb.h
@@ -20,11 +20,14 @@
#include <stdint.h>
#include "gfx/main.h"
-void png_to_gb(struct PNGImage png, struct GBImage *gb);
-void output_file(struct Options opts, struct GBImage gb);
-int get_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles, int tile_size);
-void create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap);
-void output_tilemap_file(struct Options opts, struct Tilemap tilemap);
-void output_palette_file(struct Options opts, struct PNGImage png);
+void png_to_gb(const struct PNGImage png, 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);
+void create_tilemap(const struct Options opts, struct GBImage *gb,
+ 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
--- a/include/gfx/main.h
+++ b/include/gfx/main.h
@@ -72,4 +72,4 @@
#include "gfx/makepng.h"
#include "gfx/gb.h"
-#endif
+#endif /* RGBDS_GFX_MAIN_H */
--- a/include/gfx/makepng.h
+++ b/include/gfx/makepng.h
@@ -19,10 +19,10 @@
#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 set_text(struct PNGImage *png);
-void output_png_file(struct Options opts, struct PNGImage *png);
-void free_png_data(struct PNGImage *png);
+void set_text(const struct PNGImage *png);
+void output_png_file(const struct Options opts, const struct PNGImage *png);
+void free_png_data(const struct PNGImage *png);
-#endif
+#endif /* RGBDS_GFX_PNG_H */
--- a/src/extern/err.c
+++ b/src/extern/err.c
@@ -21,16 +21,17 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
-#include <stdio.h>
#include <stdarg.h>
+#include <stdio.h>
#include <stdlib.h>
+
#include "extern/err.h"
void rgbds_vwarn(const char *fmt, va_list ap)
{
- fprintf (stderr, "warning");
+ fprintf(stderr, "warning");
if (fmt) {
- fputs (": ", stderr);
+ fputs(": ", stderr);
vfprintf(stderr, fmt, ap);
}
putc('\n', stderr);
@@ -39,9 +40,9 @@
void rgbds_vwarnx(const char *fmt, va_list ap)
{
- fprintf (stderr, "warning");
+ fprintf(stderr, "warning");
if (fmt) {
- fputs (": ", stderr);
+ fputs(": ", stderr);
vfprintf(stderr, fmt, ap);
}
putc('\n', stderr);
@@ -49,9 +50,9 @@
noreturn void rgbds_verr(int status, const char *fmt, va_list ap)
{
- fprintf (stderr, "error");
+ fprintf(stderr, "error");
if (fmt) {
- fputs (": ", stderr);
+ fputs(": ", stderr);
vfprintf(stderr, fmt, ap);
}
putc('\n', stderr);
@@ -60,11 +61,11 @@
noreturn void rgbds_verrx(int status, const char *fmt, va_list ap)
{
- fprintf (stderr, "error");
- if (fmt) {
- fputs (": ", stderr);
- vfprintf(stderr, fmt, ap);
- }
+ fprintf(stderr, "error");
+ if (fmt) {
+ fputs(": ", stderr);
+ vfprintf(stderr, fmt, ap);
+ }
putc('\n', stderr);
exit(status);
}
@@ -72,6 +73,7 @@
void rgbds_warn(const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
vwarn(fmt, ap);
va_end(ap);
@@ -80,6 +82,7 @@
void rgbds_warnx(const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
@@ -88,6 +91,7 @@
noreturn void rgbds_err(int status, const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
verr(status, fmt, ap);
va_end(ap);
@@ -96,6 +100,7 @@
noreturn void rgbds_errx(int status, const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
verrx(status, fmt, ap);
va_end(ap);
--- a/src/extern/reallocarray.c
+++ b/src/extern/reallocarray.c
@@ -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 <[email protected]>
*
@@ -15,22 +15,21 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
+#include <sys/types.h>
/*
* This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
* 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 *
-rgbds_reallocarray(void *optr, size_t nmemb, size_t size)
+void *rgbds_reallocarray(void *optr, size_t nmemb, size_t size)
{
- if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
- nmemb > 0 && SIZE_MAX / nmemb < size) {
+ if (((nmemb >= MUL_NO_OVERFLOW) || (size >= MUL_NO_OVERFLOW)) &&
+ (nmemb > 0) && (SIZE_MAX / nmemb < size)) {
errno = ENOMEM;
return NULL;
}
--- a/src/extern/strlcat.c
+++ b/src/extern/strlcat.c
@@ -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 <[email protected]>
@@ -16,8 +16,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
#include <string.h>
+#include <sys/types.h>
/*
* 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)).
* If retval >= dsize, truncation occurred.
*/
-size_t
-rgbds_strlcat(char *dst, const char *src, size_t dsize)
+size_t rgbds_strlcat(char *dst, const char *src, size_t dsize)
{
const char *odst = dst;
const char *osrc = src;
@@ -51,5 +50,5 @@
}
*dst = '\0';
- return(dlen + (src - osrc)); /* count does not include NUL */
+ return dlen + (src - osrc); /* count does not include NUL */
}
--- a/src/extern/strlcpy.c
+++ b/src/extern/strlcpy.c
@@ -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 <[email protected]>
@@ -16,8 +16,8 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
#include <string.h>
+#include <sys/types.h>
/*
* Copy string src to buffer dst of size dsize. At most dsize-1
@@ -24,8 +24,7 @@
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
-size_t
-rgbds_strlcpy(char *dst, const char *src, size_t dsize)
+size_t rgbds_strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
@@ -33,7 +32,10 @@
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
- if ((*dst++ = *src++) == '\0')
+ char n = *src++;
+ *dst++ = n;
+
+ if (n == '\0')
break;
}
}
@@ -41,10 +43,10 @@
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
- *dst = '\0'; /* NUL-terminate dst */
+ *dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
- return(src - osrc - 1); /* count does not include NUL */
+ return (src - osrc - 1); /* count does not include NUL */
}
--- a/src/extern/version.c
+++ b/src/extern/version.c
@@ -19,7 +19,7 @@
#include "extern/version.h"
-const char * get_package_version_string(void)
+const char *get_package_version_string(void)
{
static char s[50];
@@ -26,7 +26,7 @@
/* The following conditional should be simplified by the compiler. */
if (strlen(BUILD_VERSION_STRING) == 0) {
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;
} else {
return BUILD_VERSION_STRING;
--- a/src/fix/main.c
+++ b/src/fix/main.c
@@ -24,8 +24,7 @@
#include "extern/err.h"
#include "extern/version.h"
-static void
-usage(void)
+static void print_usage(void)
{
printf(
"usage: rgbfix [-CcjsVv] [-i game_id] [-k licensee_str] [-l licensee_id]\n"
@@ -34,8 +33,7 @@
exit(1);
}
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
FILE *rom;
int ch;
@@ -81,10 +79,9 @@
case 'i':
setid = true;
- if (strlen(optarg) != 4) {
- errx(1, "Game ID %s must be exactly 4 "
- "characters", optarg);
- }
+ if (strlen(optarg) != 4)
+ errx(1, "Game ID %s must be exactly 4 characters",
+ optarg);
id = optarg;
break;
@@ -94,10 +91,9 @@
case 'k':
setnewlicensee = true;
- if (strlen(optarg) != 2) {
- errx(1, "New licensee code %s is not the "
- "correct length of 2 characters", optarg);
- }
+ if (strlen(optarg) != 2)
+ errx(1, "New licensee code %s is not the correct length of 2 characters",
+ optarg);
newlicensee = optarg;
break;
@@ -105,61 +101,59 @@
setlicensee = true;
licensee = strtoul(optarg, &ep, 0);
- if (optarg[0] == '\0' || *ep != '\0') {
+ if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'l'");
- }
- if (licensee < 0 || licensee > 0xFF) {
- errx(1, "Argument for option 'l' must be "
- "between 0 and 255");
- }
+
+ if (licensee < 0 || licensee > 0xFF)
+ errx(1, "Argument for option 'l' must be between 0 and 255");
+
break;
case 'm':
setcartridge = true;
cartridge = strtoul(optarg, &ep, 0);
- if (optarg[0] == '\0' || *ep != '\0') {
+ if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'm'");
- }
- if (cartridge < 0 || cartridge > 0xFF) {
- errx(1, "Argument for option 'm' must be "
- "between 0 and 255");
- }
+
+ if (cartridge < 0 || cartridge > 0xFF)
+ errx(1, "Argument for option 'm' must be between 0 and 255");
+
break;
case 'n':
setversion = true;
version = strtoul(optarg, &ep, 0);
- if (optarg[0] == '\0' || *ep != '\0') {
+
+ if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'n'");
- }
- if (version < 0 || version > 0xFF) {
- errx(1, "Argument for option 'n' must be "
- "between 0 and 255");
- }
+
+ if (version < 0 || version > 0xFF)
+ errx(1, "Argument for option 'n' must be between 0 and 255");
+
break;
case 'p':
resize = true;
padvalue = strtoul(optarg, &ep, 0);
- if (optarg[0] == '\0' || *ep != '\0') {
+
+ if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'p'");
- }
- if (padvalue < 0 || padvalue > 0xFF) {
- errx(1, "Argument for option 'p' must be "
- "between 0 and 255");
- }
+
+ if (padvalue < 0 || padvalue > 0xFF)
+ errx(1, "Argument for option 'p' must be between 0 and 255");
+
break;
case 'r':
setramsize = true;
ramsize = strtoul(optarg, &ep, 0);
- if (optarg[0] == '\0' || *ep != '\0') {
+
+ if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'r'");
- }
- if (ramsize < 0 || ramsize > 0xFF) {
- errx(1, "Argument for option 'r' must be "
- "between 0 and 255");
- }
+
+ if (ramsize < 0 || ramsize > 0xFF)
+ errx(1, "Argument for option 'r' must be between 0 and 255");
+
break;
case 's':
super = true;
@@ -167,14 +161,13 @@
case 't':
settitle = true;
- if (strlen(optarg) > 16) {
- errx(1, "Title %s is greater than the "
- "maximum of 16 characters", optarg);
- }
+ if (strlen(optarg) > 16)
+ errx(1, "Title \"%s\" is greater than the maximum of 16 characters",
+ optarg);
if (strlen(optarg) == 16)
- warnx("Title %s is 16 chars, it is best to "
- "keep it to 15 or fewer", optarg);
+ warnx("Title \"%s\" is 16 chars, it is best to keep it to 15 or fewer",
+ optarg);
title = optarg;
break;
@@ -185,7 +178,7 @@
validate = true;
break;
default:
- usage();
+ print_usage();
/* NOTREACHED */
}
}
@@ -194,15 +187,16 @@
argv += optind;
if (argc == 0)
- usage();
+ print_usage();
/*
* 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]);
- }
/*
* Write changes to ROM
@@ -266,7 +260,7 @@
* characters).
*/
- fseek(rom,0x13F,SEEK_SET);
+ fseek(rom, 0x13F, SEEK_SET);
fwrite(id, 1, 4, rom);
}
@@ -332,8 +326,7 @@
*/
if (!setlicensee)
- warnx("You should probably set both '-s' and "
- "'-l 0x33'");
+ warnx("You should probably set both '-s' and '-l 0x33'");
fseek(rom, 0x146, SEEK_SET);
fputc(3, rom);
@@ -360,6 +353,7 @@
long romsize, newsize;
int headbyte;
uint8_t *buf;
+
fseek(rom, 0, SEEK_END);
romsize = ftell(rom);
newsize = 0x8000;
@@ -433,9 +427,8 @@
* Offset 0x14D: Header Checksum
*/
- uint8_t headcksum;
+ uint8_t headcksum = 0;
- headcksum = 0;
fseek(rom, 0x134, SEEK_SET);
for (int i = 0; i < (0x14D - 0x134); ++i)
headcksum = headcksum - fgetc(rom) - 1;
@@ -447,15 +440,14 @@
* Offset 0x14E–0x14F: Global Checksum
*/
- uint16_t globalcksum;
+ uint16_t globalcksum = 0;
- globalcksum = 0;
-
rewind(rom);
for (int i = 0; i < 0x14E; ++i)
globalcksum += fgetc(rom);
int byte;
+
fseek(rom, 0x150, SEEK_SET);
while ((byte = fgetc(rom)) != EOF)
globalcksum += byte;
--- a/src/gfx/gb.c
+++ b/src/gfx/gb.c
@@ -19,8 +19,7 @@
#include "gfx/main.h"
-void
-transpose_tiles(struct GBImage *gb, int width)
+void transpose_tiles(struct GBImage *gb, int width)
{
uint8_t *newdata;
int i;
@@ -29,7 +28,9 @@
newdata = calloc(gb->size, 1);
for (i = 0; i < gb->size; i++) {
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];
}
@@ -38,8 +39,7 @@
gb->data = newdata;
}
-void
-png_to_gb(struct PNGImage png, struct GBImage *gb)
+void png_to_gb(const struct PNGImage png, struct GBImage *gb)
{
int x, y, byte;
png_byte index;
@@ -50,55 +50,55 @@
index &= (1 << depth) - 1;
if (!gb->horizontal) {
- byte = y * depth + x / 8 * png.height / 8 * 8 * depth;
+ byte = y * depth
+ + x / 8 * png.height / 8 * 8 * depth;
} 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);
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);
- }
}
-void
-output_file(struct Options opts, struct GBImage gb)
+void output_file(const struct Options opts, const struct GBImage gb)
{
FILE *f;
f = fopen(opts.outfile, "wb");
- if (!f) {
+ if (!f)
err(1, "Opening output file '%s' failed", opts.outfile);
- }
+
fwrite(gb.data, 1, gb.size - gb.trim * 8 * depth, f);
fclose(f);
}
-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, int tile_size)
{
int i, j;
+
for (i = 0; i < num_tiles; i++) {
for (j = 0; j < tile_size; j++) {
- if (tile[j] != tiles[i][j]) {
+ if (tile[j] != tiles[i][j])
break;
- }
}
- if (j >= tile_size) {
+
+ if (j >= tile_size)
return i;
- }
}
return -1;
}
-void
-create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
+void create_tilemap(const struct Options opts, struct GBImage *gb,
+ struct Tilemap *tilemap)
{
int i, j;
int gb_i;
@@ -113,7 +113,7 @@
tile_size = sizeof(uint8_t) * depth * 8;
gb_size = gb->size - (gb->trim * 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;
tilemap->data = malloc(sizeof(uint8_t) * max_tiles);
@@ -127,7 +127,8 @@
gb_i++;
}
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) {
index = num_tiles;
tiles[num_tiles] = tile;
@@ -147,39 +148,35 @@
gb->data = malloc(tile_size * num_tiles);
for (i = 0; i < num_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->size = i * tile_size;
}
- for (i = 0; i < num_tiles; i++) {
+ for (i = 0; i < num_tiles; i++)
free(tiles[i]);
- }
+
free(tiles);
}
-void
-output_tilemap_file(struct Options opts, struct Tilemap tilemap)
+void output_tilemap_file(const struct Options opts,
+ const struct Tilemap tilemap)
{
FILE *f;
f = fopen(opts.mapfile, "wb");
- if (!f) {
+ if (!f)
err(1, "Opening tilemap file '%s' failed", opts.mapfile);
- }
fwrite(tilemap.data, 1, tilemap.size, f);
fclose(f);
- if (opts.mapout) {
+ if (opts.mapout)
free(opts.mapfile);
- }
}
-void
-output_palette_file(struct Options opts, struct PNGImage png)
+void output_palette_file(const struct Options opts, const struct PNGImage png)
{
FILE *f;
int i, colors, color;
@@ -188,16 +185,18 @@
if (png_get_PLTE(png.png, png.info, &palette, &colors)) {
f = fopen(opts.palfile, "wb");
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++) {
- 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);
}
fclose(f);
}
- if (opts.palout) {
+ if (opts.palout)
free(opts.palfile);
- }
}
--- a/src/gfx/main.c
+++ b/src/gfx/main.c
@@ -19,10 +19,10 @@
#include <unistd.h>
#include "extern/version.h"
+
#include "gfx/main.h"
-static void
-usage(void)
+static void print_usage(void)
{
printf(
"usage: rgbgfx [-DFfhPTuVv] [-d #] [-o outfile] [-p palfile] [-t mapfile]\n"
@@ -30,8 +30,7 @@
exit(1);
}
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
int ch, size;
struct Options opts = {0};
@@ -41,9 +40,8 @@
char *ext;
const char *errmsg = "Warning: The PNG's %s setting is not the same as the setting defined on the command line.";
- if (argc == 1) {
- usage();
- }
+ if (argc == 1)
+ print_usage();
opts.mapfile = "";
opts.palfile = "";
@@ -51,8 +49,8 @@
depth = 2;
- while((ch = getopt(argc, argv, "Dd:Ffho:Tt:uPp:Vvx:")) != -1) {
- switch(ch) {
+ while ((ch = getopt(argc, argv, "Dd:Ffho:Tt:uPp:Vvx:")) != -1) {
+ switch (ch) {
case 'D':
opts.debug = true;
break;
@@ -95,7 +93,7 @@
opts.trim = strtoul(optarg, NULL, 0);
break;
default:
- usage();
+ print_usage();
/* NOTREACHED */
}
}
@@ -102,15 +100,14 @@
argc -= optind;
argv += optind;
- if (argc == 0) {
- usage();
- }
+ if (argc == 0)
+ print_usage();
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.");
- }
+
colors = 1 << depth;
input_png_file(opts, &png);
@@ -121,82 +118,77 @@
get_text(&png);
if (png.horizontal != opts.horizontal) {
- if (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "horizontal");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.horizontal = opts.horizontal;
- }
}
- if (png.horizontal) {
+
+ if (png.horizontal)
opts.horizontal = png.horizontal;
- }
if (png.trim != opts.trim) {
- if (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "trim");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.trim = opts.trim;
- }
}
- if (png.trim) {
+
+ if (png.trim)
opts.trim = png.trim;
- }
+
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 (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "tilemap file");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.mapfile = opts.mapfile;
- }
}
- if (!*opts.mapfile) {
+ if (!*opts.mapfile)
opts.mapfile = png.mapfile;
- }
if (png.mapout != opts.mapout) {
- if (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "tilemap file");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.mapout = opts.mapout;
- }
}
- if (png.mapout) {
+ if (png.mapout)
opts.mapout = png.mapout;
- }
if (strcmp(png.palfile, opts.palfile) != 0) {
- if (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "palette file");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.palfile = opts.palfile;
- }
}
- if (!*opts.palfile) {
+ if (!*opts.palfile)
opts.palfile = png.palfile;
- }
if (png.palout != opts.palout) {
- if (opts.verbose) {
+ if (opts.verbose)
warnx(errmsg, "palette file");
- }
- if (opts.hardfix) {
+
+ if (opts.hardfix)
png.palout = opts.palout;
- }
}
- if (png.palout) {
+
+ if (png.palout)
opts.palout = png.palout;
- }
if (!*opts.mapfile && opts.mapout) {
- if ((ext = strrchr(opts.infile, '.')) != NULL) {
+ ext = strrchr(opts.infile, '.');
+
+ if (ext != NULL) {
size = ext - opts.infile + 9;
opts.mapfile = malloc(size);
strncpy(opts.mapfile, opts.infile, size);
@@ -210,7 +202,9 @@
}
if (!*opts.palfile && opts.palout) {
- if ((ext = strrchr(opts.infile, '.')) != NULL) {
+ ext = strrchr(opts.infile, '.');
+
+ if (ext != NULL) {
size = ext - opts.infile + 5;
opts.palfile = malloc(size);
strncpy(opts.palfile, opts.infile, size);
@@ -233,17 +227,14 @@
create_tilemap(opts, &gb, &tilemap);
}
- if (*opts.outfile) {
+ if (*opts.outfile)
output_file(opts, gb);
- }
- if (*opts.mapfile) {
+ if (*opts.mapfile)
output_tilemap_file(opts, tilemap);
- }
- if (*opts.palfile) {
+ if (*opts.palfile)
output_palette_file(opts, png);
- }
if (opts.fix || opts.debug) {
set_text(&png);
--- a/src/gfx/makepng.c
+++ b/src/gfx/makepng.c
@@ -16,10 +16,10 @@
#include <stdlib.h>
#include <string.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)
{
FILE *f;
int i, y, num_trans;
@@ -30,24 +30,21 @@
png_color *palette;
f = fopen(opts.infile, "rb");
- if (!f) {
+ if (!f)
err(1, "Opening input png file '%s' failed", opts.infile);
- }
- img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!img->png) {
+ img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (!img->png)
errx(1, "Creating png structure failed");
- }
img->info = png_create_info_struct(img->png);
- if (!img->info) {
+ if (!img->info)
errx(1, "Creating png info structure failed");
- }
- /* Better error handling here? */
- if (setjmp(png_jmpbuf(img->png))) {
+ /* TODO: Better error handling here? */
+ if (setjmp(png_jmpbuf(img->png)))
exit(1);
- }
png_init_io(img->png, f);
@@ -58,41 +55,39 @@
img->depth = png_get_bit_depth(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);
- }
if (img->depth != depth) {
if (opts.verbose) {
warnx("Image bit depth is not %i (is %i).", depth,
- img->depth);
+ img->depth);
}
}
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_gray_to_rgb(img->png);
} else {
- if (img->depth < 8) {
+ if (img->depth < 8)
png_set_expand_gray_1_2_4_to_8(img->png);
- }
+
has_palette = png_get_PLTE(img->png, img->info, &palette,
- &colors);
+ &colors);
}
if (png_get_tRNS(img->png, img->info, &trans_alpha, &num_trans,
- &trans_values)) {
+ &trans_values)) {
if (img->type == PNG_COLOR_TYPE_PALETTE) {
full_alpha = malloc(sizeof(bool) * num_trans);
for (i = 0; i < num_trans; i++) {
- if (trans_alpha[i] > 0) {
+ if (trans_alpha[i] > 0)
full_alpha[i] = false;
- } else {
+ else
full_alpha[i] = true;
- }
}
for (i = 0; i < num_trans; i++) {
@@ -182,9 +177,8 @@
png_read_update_info(img->png, img->info);
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));
- }
png_read_image(img->png, img->data);
png_read_end(img->png, img->info);
@@ -192,8 +186,7 @@
fclose(f);
}
-void
-get_text(struct PNGImage *png)
+void get_text(struct PNGImage *png)
{
png_text *text;
int i, numtxts, numremoved;
@@ -221,11 +214,14 @@
}
}
- /* 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++) {
- if (text[i].key == NULL) {
+ if (text[i].key == NULL)
numremoved++;
- }
+
text[i].key = text[i + numremoved].key;
text[i].text = text[i + numremoved].text;
text[i].compression = text[i + numremoved].compression;
@@ -233,8 +229,7 @@
png_set_text(png->png, png->info, text, numtxts - numremoved);
}
-void
-set_text(struct PNGImage *png)
+void set_text(const struct PNGImage *png)
{
png_text *text;
char buffer[3];
@@ -282,14 +277,16 @@
free(text);
}
-void
-output_png_file(struct Options opts, struct PNGImage *png)
+void output_png_file(const struct Options opts, const struct PNGImage *png)
{
FILE *f;
char *outfile;
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) {
outfile = malloc(strlen(opts.infile) + 5);
strcpy(outfile, opts.infile);
@@ -299,19 +296,16 @@
}
f = fopen(outfile, "wb");
- if (!f) {
+ if (!f)
err(1, "Opening output png file '%s' failed", outfile);
- }
img = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!img) {
+ if (!img)
errx(1, "Creating png structure failed");
- }
- /* Better error handling here? */
- if (setjmp(png_jmpbuf(img))) {
+ /* TODO: Better error handling here? */
+ if (setjmp(png_jmpbuf(img)))
exit(1);
- }
png_init_io(img, f);
@@ -322,18 +316,16 @@
fclose(f);
- if (opts.debug) {
+ if (opts.debug)
free(outfile);
- }
}
-void
-free_png_data(struct PNGImage *png)
+void free_png_data(const struct PNGImage *png)
{
int y;
- for (y = 0; y < png->height; y++) {
+ for (y = 0; y < png->height; y++)
free(png->data[y]);
- }
+
free(png->data);
}