diff options
author | PikalaxALT <pikalaxalt@gmail.com> | 2017-12-23 12:57:46 -0500 |
---|---|---|
committer | PikalaxALT <pikalaxalt@gmail.com> | 2017-12-23 12:57:46 -0500 |
commit | fdd7e7cb848747a1e0ace8a63d29aaa22dfd1140 (patch) | |
tree | 6ddaed4de52bfcc517d573a8f30c5bd0a1650782 /tools | |
parent | bb8f652504f886af296ffcaac57bf76cdc893c97 (diff) | |
parent | 1c1ce902515ccb3ccecde29611711c9b1a3ce955 (diff) |
Merge branch 'master' into cable_car
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gbagfx/convert_png.c | 48 | ||||
-rw-r--r-- | tools/preproc/c_file.cpp | 2 | ||||
-rw-r--r-- | tools/preproc/preproc.cpp | 4 | ||||
-rw-r--r-- | tools/scaninc/c_file.cpp | 6 |
4 files changed, 53 insertions, 7 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) diff --git a/tools/preproc/c_file.cpp b/tools/preproc/c_file.cpp index 5bfdee086..24b3453e8 100644 --- a/tools/preproc/c_file.cpp +++ b/tools/preproc/c_file.cpp @@ -139,6 +139,7 @@ bool CFile::ConsumeNewline() { m_pos += 2; m_lineNum++; + std::putchar('\n'); return true; } @@ -146,6 +147,7 @@ bool CFile::ConsumeNewline() { m_pos++; m_lineNum++; + std::putchar('\n'); return true; } diff --git a/tools/preproc/preproc.cpp b/tools/preproc/preproc.cpp index 8320a2d27..c9c6042df 100644 --- a/tools/preproc/preproc.cpp +++ b/tools/preproc/preproc.cpp @@ -89,8 +89,8 @@ void PreprocAsmFile(std::string filename) if (globalLabel.length() != 0) { - std::printf("\t.global %s\n", globalLabel.c_str()); - std::printf("%s:\n", globalLabel.c_str()); + const char *s = globalLabel.c_str(); + std::printf("%s: ; .global %s\n", s, s); } else { diff --git a/tools/scaninc/c_file.cpp b/tools/scaninc/c_file.cpp index c55ca9a8c..f7acc833f 100644 --- a/tools/scaninc/c_file.cpp +++ b/tools/scaninc/c_file.cpp @@ -136,10 +136,10 @@ bool CFile::ConsumeComment() m_pos += 2; while (m_buffer[m_pos] != '*' && m_buffer[m_pos + 1] != '/') { + if (m_buffer[m_pos] == 0) + return false; if (!ConsumeNewline()) - { m_pos++; - } } m_pos += 2; return true; @@ -149,6 +149,8 @@ bool CFile::ConsumeComment() m_pos += 2; while (!ConsumeNewline()) { + if (m_buffer[m_pos] == 0) + return false; m_pos++; } return true; |