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

View File

@@ -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;
@@ -113,7 +113,7 @@ create_tilemap(struct Options opts, struct GBImage *gb, struct Tilemap *tilemap)
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 @@ 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);
}
}

View File

@@ -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 = "";
@@ -51,8 +49,8 @@ main(int argc, char *argv[])
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,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) {
opts.horizontal = 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) {
opts.palout = 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);

View File

@@ -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,41 +55,39 @@ 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) {
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 @@ 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);
}