summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorgolem galvanize <golemgalvanize@github.com>2018-01-15 23:38:10 -0500
committergolem galvanize <golemgalvanize@github.com>2018-01-15 23:38:10 -0500
commit5a5eb4ff59b11d87125c65ee9a236dd243087ea8 (patch)
tree07af21076a2feb5659b55f30053b5cdb7c261be3 /tools
parent11cb3275cc53c2bcea4fdcfe11d9d9d429ee9c02 (diff)
dumped most of pokenav.s
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)