summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorcamthesaxman <cameronghall@cox.net>2018-01-21 22:29:35 -0600
committercamthesaxman <cameronghall@cox.net>2018-01-21 22:29:35 -0600
commit663ecd00145da49fda7d2509d629d737ea633cd8 (patch)
treea4cfc7323151501d5603f1cd34e68a73f328434e /tools
parenta78b0636f50c012c3618d55a9b83f834319de6e0 (diff)
parentf57340b729d741acae17213c9c748738fcc1dcb3 (diff)
Merge branch 'master' of https://github.com/pret/pokeemerald into battle_refactor
Diffstat (limited to 'tools')
-rw-r--r--tools/gbagfx/convert_png.c48
1 files changed, 45 insertions, 3 deletions
diff --git a/tools/gbagfx/convert_png.c b/tools/gbagfx/convert_png.c
index 37904318c..cdfa39a7a 100644
--- a/tools/gbagfx/convert_png.c
+++ b/tools/gbagfx/convert_png.c
@@ -45,6 +45,40 @@ static FILE *PngReadOpen(char *path, png_structp *pngStruct, png_infop *pngInfo)
return fp;
}
+static unsigned char *ConvertBitDepth(unsigned char *src, int srcBitDepth, int destBitDepth, int numPixels)
+{
+ // Round the number of bits up to the next 8 and divide by 8 to get the number of bytes.
+ int srcSize = ((numPixels * srcBitDepth + 7) & ~7) / 8;
+ int destSize = ((numPixels * destBitDepth + 7) & ~7) / 8;
+ unsigned char *output = calloc(destSize, 1);
+ unsigned char *dest = output;
+ int i;
+ int j;
+ int destBit = 8 - destBitDepth;
+
+ for (i = 0; i < srcSize; i++)
+ {
+ unsigned char srcByte = src[i];
+
+ for (j = 8 - srcBitDepth; j >= 0; j -= srcBitDepth)
+ {
+ unsigned char pixel = (srcByte >> j) % (1 << srcBitDepth);
+
+ if (pixel >= (1 << destBitDepth))
+ FATAL_ERROR("Image exceeds the maximum color value for a %ibpp image.\n", destBitDepth);
+ *dest |= pixel << destBit;
+ destBit -= destBitDepth;
+ if (destBit < 0)
+ {
+ dest++;
+ destBit = 8 - destBitDepth;
+ }
+ }
+ }
+
+ return output;
+}
+
void ReadPng(char *path, struct Image *image)
{
png_structp png_ptr;
@@ -54,9 +88,6 @@ void ReadPng(char *path, struct Image *image)
int bit_depth = png_get_bit_depth(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);
-
int color_type = png_get_color_type(png_ptr, info_ptr);
if (color_type != PNG_COLOR_TYPE_GRAY && color_type != PNG_COLOR_TYPE_PALETTE)
@@ -93,6 +124,17 @@ void ReadPng(char *path, struct Image *image)
free(row_pointers);
fclose(fp);
+
+ if (bit_depth != image->bitDepth)
+ {
+ unsigned char *src = image->pixels;
+
+ if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && bit_depth != 8)
+ FATAL_ERROR("Bit depth of image must be 1, 2, 4, or 8.\n");
+ image->pixels = ConvertBitDepth(image->pixels, bit_depth, image->bitDepth, image->width * image->height);
+ free(src);
+ image->bitDepth = bit_depth;
+ }
}
void ReadPngPalette(char *path, struct Palette *palette)