diff options
author | camthesaxman <cameronghall@cox.net> | 2017-10-08 17:01:56 -0500 |
---|---|---|
committer | camthesaxman <cameronghall@cox.net> | 2017-10-08 17:01:56 -0500 |
commit | d2ca90a04ffba7f1b0836910334899aec4e1678d (patch) | |
tree | 1043b78a3bb61286e736545c9075b05c611ce3aa | |
parent | 3776a9fb4f0531535b0b5879dab7b3b6bd231736 (diff) |
add png -> gbapal conversion
-rw-r--r-- | tools/gbagfx/convert_png.c | 237 | ||||
-rw-r--r-- | tools/gbagfx/convert_png.h | 1 | ||||
-rw-r--r-- | tools/gbagfx/main.c | 9 |
3 files changed, 150 insertions, 97 deletions
diff --git a/tools/gbagfx/convert_png.c b/tools/gbagfx/convert_png.c index f6a30804a..37904318c 100644 --- a/tools/gbagfx/convert_png.c +++ b/tools/gbagfx/convert_png.c @@ -7,163 +7,206 @@ #include "convert_png.h" #include "gfx.h" -void ReadPng(char *path, struct Image *image) +static FILE *PngReadOpen(char *path, png_structp *pngStruct, png_infop *pngInfo) { - FILE *fp = fopen(path, "rb"); + FILE *fp = fopen(path, "rb"); + + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for reading.\n", path); + + unsigned char sig[8]; + + if (fread(sig, 8, 1, fp) != 1) + FATAL_ERROR("Failed to read PNG signature from \"%s\".\n", path); + + if (png_sig_cmp(sig, 0, 8)) + FATAL_ERROR("\"%s\" does not have a valid PNG signature.\n", path); + + png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + + if (!png_ptr) + FATAL_ERROR("Failed to create PNG read struct.\n"); + + png_infop info_ptr = png_create_info_struct(png_ptr); + + if (!info_ptr) + FATAL_ERROR("Failed to create PNG info struct.\n"); - if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for reading.\n", path); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Failed to init I/O for reading \"%s\".\n", path); - unsigned char sig[8]; + png_init_io(png_ptr, fp); + png_set_sig_bytes(png_ptr, 8); + png_read_info(png_ptr, info_ptr); - if (fread(sig, 8, 1, fp) != 1) - FATAL_ERROR("Failed to read PNG signature from \"%s\".\n", path); + *pngStruct = png_ptr; + *pngInfo = info_ptr; - if (png_sig_cmp(sig, 0, 8)) - FATAL_ERROR("\"%s\" does not have a valid PNG signature.\n", path); + return fp; +} + +void ReadPng(char *path, struct Image *image) +{ + png_structp png_ptr; + png_infop info_ptr; + + FILE *fp = PngReadOpen(path, &png_ptr, &info_ptr); - png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + int bit_depth = png_get_bit_depth(png_ptr, info_ptr); - if (!png_ptr) - FATAL_ERROR("Failed to create PNG read struct.\n"); + if (bit_depth != image->bitDepth) + FATAL_ERROR("\"%s\" has a bit depth of %d, but the expected bit depth is %d.\n", path, bit_depth, image->bitDepth); - png_infop info_ptr = png_create_info_struct(png_ptr); + int color_type = png_get_color_type(png_ptr, info_ptr); - if (!info_ptr) - FATAL_ERROR("Failed to create PNG info struct.\n"); + if (color_type != PNG_COLOR_TYPE_GRAY && color_type != PNG_COLOR_TYPE_PALETTE) + FATAL_ERROR("\"%s\" has an unsupported color type.\n", path); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Failed to init I/O for reading \"%s\".\n", path); + // Check if the image has a palette so that we can tell if the colors need to be inverted later. + // Don't read the palette because it's not needed for now. + image->hasPalette = (color_type == PNG_COLOR_TYPE_PALETTE); - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, 8); - png_read_info(png_ptr, info_ptr); + image->width = png_get_image_width(png_ptr, info_ptr); + image->height = png_get_image_height(png_ptr, info_ptr); - int bit_depth = png_get_bit_depth(png_ptr, info_ptr); + int rowbytes = png_get_rowbytes(png_ptr, info_ptr); - if (bit_depth != image->bitDepth) - FATAL_ERROR("\"%s\" has a bit depth of %d, but the expected bit depth is %d.\n", path, bit_depth, image->bitDepth); + image->pixels = malloc(image->height * rowbytes); - int color_type = png_get_color_type(png_ptr, info_ptr); + if (image->pixels == NULL) + FATAL_ERROR("Failed to allocate pixel buffer.\n"); - if (color_type != PNG_COLOR_TYPE_GRAY && color_type != PNG_COLOR_TYPE_PALETTE) - FATAL_ERROR("\"%s\" has an unsupported color type.\n", path); + png_bytepp row_pointers = malloc(image->height * sizeof(png_bytep)); - // Check if the image has a palette so that we can tell if the colors need to be inverted later. - // Don't read the palette because it's not needed for now. - image->hasPalette = (color_type == PNG_COLOR_TYPE_PALETTE); + if (row_pointers == NULL) + FATAL_ERROR("Failed to allocate row pointers.\n"); - image->width = png_get_image_width(png_ptr, info_ptr); - image->height = png_get_image_height(png_ptr, info_ptr); + for (int i = 0; i < image->height; i++) + row_pointers[i] = (png_bytep)(image->pixels + (i * rowbytes)); - int rowbytes = png_get_rowbytes(png_ptr, info_ptr); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Error reading from \"%s\".\n", path); - image->pixels = malloc(image->height * rowbytes); + png_read_image(png_ptr, row_pointers); - if (image->pixels == NULL) - FATAL_ERROR("Failed to allocate pixel buffer.\n"); + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + + free(row_pointers); + fclose(fp); +} + +void ReadPngPalette(char *path, struct Palette *palette) +{ + png_structp png_ptr; + png_infop info_ptr; + png_colorp colors; + int numColors; - png_bytepp row_pointers = malloc(image->height * sizeof(png_bytep)); + FILE *fp = PngReadOpen(path, &png_ptr, &info_ptr); - if (row_pointers == NULL) - FATAL_ERROR("Failed to allocate row pointers.\n"); + if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_PALETTE) + FATAL_ERROR("The image \"%s\" does not contain a palette.\n", path); - for (int i = 0; i < image->height; i++) - row_pointers[i] = (png_bytep)(image->pixels + (i * rowbytes)); + if (png_get_PLTE(png_ptr, info_ptr, &colors, &numColors) != PNG_INFO_PLTE) + FATAL_ERROR("Failed to retrieve palette from \"%s\".\n", path); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Error reading from \"%s\".\n", path); + if (numColors > 256) + FATAL_ERROR("Images with more than 256 colors are not supported.\n"); - png_read_image(png_ptr, row_pointers); + palette->numColors = numColors; + for (int i = 0; i < numColors; i++) { + palette->colors[i].red = colors[i].red; + palette->colors[i].green = colors[i].green; + palette->colors[i].blue = colors[i].blue; + } - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - free(row_pointers); - fclose(fp); + fclose(fp); } void SetPngPalette(png_structp png_ptr, png_infop info_ptr, struct Palette *palette) { - png_colorp colors = malloc(palette->numColors * sizeof(png_color)); + png_colorp colors = malloc(palette->numColors * sizeof(png_color)); - if (colors == NULL) - FATAL_ERROR("Failed to allocate PNG palette.\n"); + if (colors == NULL) + FATAL_ERROR("Failed to allocate PNG palette.\n"); - for (int i = 0; i < palette->numColors; i++) { - colors[i].red = palette->colors[i].red; - colors[i].green = palette->colors[i].green; - colors[i].blue = palette->colors[i].blue; - } + for (int i = 0; i < palette->numColors; i++) { + colors[i].red = palette->colors[i].red; + colors[i].green = palette->colors[i].green; + colors[i].blue = palette->colors[i].blue; + } - png_set_PLTE(png_ptr, info_ptr, colors, palette->numColors); + png_set_PLTE(png_ptr, info_ptr, colors, palette->numColors); - free(colors); + free(colors); } void WritePng(char *path, struct Image *image) { - FILE *fp = fopen(path, "wb"); + FILE *fp = fopen(path, "wb"); - if (fp == NULL) - FATAL_ERROR("Failed to open \"%s\" for writing.\n", path); + if (fp == NULL) + FATAL_ERROR("Failed to open \"%s\" for writing.\n", path); - png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) - FATAL_ERROR("Failed to create PNG write struct.\n"); + if (!png_ptr) + FATAL_ERROR("Failed to create PNG write struct.\n"); - png_infop info_ptr = png_create_info_struct(png_ptr); + png_infop info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) - FATAL_ERROR("Failed to create PNG info struct.\n"); + if (!info_ptr) + FATAL_ERROR("Failed to create PNG info struct.\n"); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Failed to init I/O for writing \"%s\".\n", path); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Failed to init I/O for writing \"%s\".\n", path); - png_init_io(png_ptr, fp); + png_init_io(png_ptr, fp); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Error writing header for \"%s\".\n", path); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Error writing header for \"%s\".\n", path); - int color_type = image->hasPalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY; + int color_type = image->hasPalette ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_GRAY; - png_set_IHDR(png_ptr, info_ptr, image->width, image->height, - image->bitDepth, color_type, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + png_set_IHDR(png_ptr, info_ptr, image->width, image->height, + image->bitDepth, color_type, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - if (image->hasPalette) { - SetPngPalette(png_ptr, info_ptr, &image->palette); + if (image->hasPalette) { + SetPngPalette(png_ptr, info_ptr, &image->palette); - if (image->hasTransparency) { - png_byte trans = 0; - png_set_tRNS(png_ptr, info_ptr, &trans, 1, 0); - } - } + if (image->hasTransparency) { + png_byte trans = 0; + png_set_tRNS(png_ptr, info_ptr, &trans, 1, 0); + } + } - png_write_info(png_ptr, info_ptr); + png_write_info(png_ptr, info_ptr); - png_bytepp row_pointers = malloc(image->height * sizeof(png_bytep)); + png_bytepp row_pointers = malloc(image->height * sizeof(png_bytep)); - if (row_pointers == NULL) - FATAL_ERROR("Failed to allocate row pointers.\n"); + if (row_pointers == NULL) + FATAL_ERROR("Failed to allocate row pointers.\n"); - int rowbytes = png_get_rowbytes(png_ptr, info_ptr); + int rowbytes = png_get_rowbytes(png_ptr, info_ptr); - for (int i = 0; i < image->height; i++) - row_pointers[i] = (png_bytep)(image->pixels + (i * rowbytes)); + for (int i = 0; i < image->height; i++) + row_pointers[i] = (png_bytep)(image->pixels + (i * rowbytes)); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Error writing \"%s\".\n", path); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Error writing \"%s\".\n", path); - png_write_image(png_ptr, row_pointers); + png_write_image(png_ptr, row_pointers); - if (setjmp(png_jmpbuf(png_ptr))) - FATAL_ERROR("Error ending write of \"%s\".\n", path); + if (setjmp(png_jmpbuf(png_ptr))) + FATAL_ERROR("Error ending write of \"%s\".\n", path); - png_write_end(png_ptr, NULL); + png_write_end(png_ptr, NULL); - fclose(fp); + fclose(fp); - png_destroy_write_struct(&png_ptr, &info_ptr); - free(row_pointers); + png_destroy_write_struct(&png_ptr, &info_ptr); + free(row_pointers); } diff --git a/tools/gbagfx/convert_png.h b/tools/gbagfx/convert_png.h index 55d3d6942..caf081b73 100644 --- a/tools/gbagfx/convert_png.h +++ b/tools/gbagfx/convert_png.h @@ -7,5 +7,6 @@ void ReadPng(char *path, struct Image *image); void WritePng(char *path, struct Image *image); +void ReadPngPalette(char *path, struct Palette *palette); #endif // CONVERT_PNG_H diff --git a/tools/gbagfx/main.c b/tools/gbagfx/main.c index 37d3441fe..97db60e84 100644 --- a/tools/gbagfx/main.c +++ b/tools/gbagfx/main.c @@ -134,6 +134,14 @@ void HandlePngToGbaCommand(char *inputPath, char *outputPath, int argc, char **a ConvertPngToGba(inputPath, outputPath, numTiles, bitDepth); } +void HandlePngToGbaPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) +{ + struct Palette palette; + + ReadPngPalette(inputPath, &palette); + WriteGbaPalette(outputPath, &palette); +} + void HandleGbaToJascPaletteCommand(char *inputPath, char *outputPath, int argc UNUSED, char **argv UNUSED) { struct Palette palette; @@ -353,6 +361,7 @@ int main(int argc, char **argv) { "png", "1bpp", HandlePngToGbaCommand }, { "png", "4bpp", HandlePngToGbaCommand }, { "png", "8bpp", HandlePngToGbaCommand }, + { "png", "gbapal", HandlePngToGbaPaletteCommand }, { "gbapal", "pal", HandleGbaToJascPaletteCommand }, { "pal", "gbapal", HandleJascToGbaPaletteCommand }, { "latfont", "png", HandleLatinFontToPngCommand }, |