mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
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:
25
include/extern/err.h
vendored
25
include/extern/err.h
vendored
@@ -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 */
|
||||
|
||||
9
include/extern/reallocarray.h
vendored
9
include/extern/reallocarray.h
vendored
@@ -2,13 +2,14 @@
|
||||
#define EXTERN_REALLOCARRAY_H
|
||||
|
||||
#ifdef REALLOCARRAY_IN_LIBC
|
||||
|
||||
#include <stdlib.h>
|
||||
#else
|
||||
|
||||
#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 */
|
||||
|
||||
7
include/extern/stdnoreturn.h
vendored
7
include/extern/stdnoreturn.h
vendored
@@ -1,3 +1,6 @@
|
||||
#ifndef EXTERN_STDNORETURN_H
|
||||
#define EXTERN_STDNORETURN_H
|
||||
|
||||
#if __STDC_VERSION__ >= 201112L
|
||||
/* C11 or newer */
|
||||
#define noreturn _Noreturn
|
||||
@@ -11,6 +14,8 @@
|
||||
/* MS Visual Studio 2003/.NET Framework 1.1 or newer */
|
||||
#define noreturn _declspec(noreturn)
|
||||
#else
|
||||
/* unsupported, but no need to throw a fit */
|
||||
/* Unsupported, but no need to throw a fit */
|
||||
#define noreturn
|
||||
#endif
|
||||
|
||||
#endif /* EXTERN_STDNORETURN_H */
|
||||
|
||||
18
include/extern/strl.h
vendored
18
include/extern/strl.h
vendored
@@ -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 */
|
||||
|
||||
5
include/extern/version.h
vendored
5
include/extern/version.h
vendored
@@ -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);
|
||||
|
||||
#endif /* EXTERN_VERSION_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
|
||||
|
||||
@@ -72,4 +72,4 @@ int depth, colors;
|
||||
#include "gfx/makepng.h"
|
||||
#include "gfx/gb.h"
|
||||
|
||||
#endif
|
||||
#endif /* RGBDS_GFX_MAIN_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 */
|
||||
|
||||
7
src/extern/err.c
vendored
7
src/extern/err.c
vendored
@@ -21,9 +21,10 @@
|
||||
* 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)
|
||||
@@ -72,6 +73,7 @@ noreturn void rgbds_verrx(int status, const char *fmt, va_list ap)
|
||||
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_warn(const char *fmt, ...)
|
||||
void rgbds_warnx(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vwarnx(fmt, ap);
|
||||
va_end(ap);
|
||||
@@ -88,6 +91,7 @@ void rgbds_warnx(const char *fmt, ...)
|
||||
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_err(int status, const char *fmt, ...)
|
||||
noreturn void rgbds_errx(int status, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
verrx(status, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
9
src/extern/reallocarray.c
vendored
9
src/extern/reallocarray.c
vendored
@@ -15,10 +15,10 @@
|
||||
* 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
|
||||
@@ -26,11 +26,10 @@
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
7
src/extern/strlcat.c
vendored
7
src/extern/strlcat.c
vendored
@@ -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 @@ rgbds_strlcat(char *dst, const char *src, size_t dsize)
|
||||
}
|
||||
*dst = '\0';
|
||||
|
||||
return(dlen + (src - osrc)); /* count does not include NUL */
|
||||
return dlen + (src - osrc); /* count does not include NUL */
|
||||
}
|
||||
|
||||
10
src/extern/strlcpy.c
vendored
10
src/extern/strlcpy.c
vendored
@@ -16,16 +16,15 @@
|
||||
* 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
|
||||
* 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 @@ rgbds_strlcpy(char *dst, const char *src, size_t dsize)
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
110
src/fix/main.c
110
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 @@ usage(void)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
FILE *rom;
|
||||
int ch;
|
||||
@@ -81,10 +79,9 @@ main(int argc, char *argv[])
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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 @@ main(int argc, char *argv[])
|
||||
validate = true;
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
print_usage();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
@@ -194,15 +187,16 @@ main(int argc, char *argv[])
|
||||
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
|
||||
@@ -332,8 +326,7 @@ main(int argc, char *argv[])
|
||||
*/
|
||||
|
||||
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 @@ main(int argc, char *argv[])
|
||||
long romsize, newsize;
|
||||
int headbyte;
|
||||
uint8_t *buf;
|
||||
|
||||
fseek(rom, 0, SEEK_END);
|
||||
romsize = ftell(rom);
|
||||
newsize = 0x8000;
|
||||
@@ -433,9 +427,8 @@ main(int argc, char *argv[])
|
||||
* 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 @@ main(int argc, char *argv[])
|
||||
* Offset 0x14E–0x14F: Global Checksum
|
||||
*/
|
||||
|
||||
uint16_t globalcksum;
|
||||
|
||||
globalcksum = 0;
|
||||
uint16_t 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;
|
||||
|
||||
77
src/gfx/gb.c
77
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 @@ transpose_tiles(struct GBImage *gb, int width)
|
||||
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 @@ transpose_tiles(struct GBImage *gb, int width)
|
||||
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 @@ png_to_gb(struct PNGImage png, struct GBImage *gb)
|
||||
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;
|
||||
@@ -127,7 +127,8 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
|
||||
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 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
|
||||
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 @@ output_palette_file(struct Options opts, struct PNGImage png)
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
107
src/gfx/main.c
107
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 @@ usage(void)
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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 = "";
|
||||
@@ -95,22 +93,21 @@ main(int argc, char *argv[])
|
||||
opts.trim = strtoul(optarg, NULL, 0);
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
print_usage();
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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 @@ main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
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 @@ main(int argc, char *argv[])
|
||||
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);
|
||||
|
||||
@@ -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 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
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,9 +55,8 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
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) {
|
||||
@@ -70,14 +66,14 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -88,12 +84,11 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
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++) {
|
||||
if (full_alpha[i]) {
|
||||
@@ -182,9 +177,8 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
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 @@ input_png_file(struct Options opts, struct PNGImage *img)
|
||||
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 @@ 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++) {
|
||||
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 @@ get_text(struct PNGImage *png)
|
||||
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 @@ set_text(struct PNGImage *png)
|
||||
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 @@ output_png_file(struct Options opts, struct PNGImage *png)
|
||||
}
|
||||
|
||||
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 @@ output_png_file(struct Options opts, struct PNGImage *png)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user