From 7cf0ca63776d33545454bcce8f09bdf068fe2ecb Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 14 Aug 2016 00:14:42 +0100 Subject: generate output ppm if the decode was incomplete --- test/decode_bmp.c | 259 +++++++++++++++++++++++++++--------------------------- test/runtest.sh | 2 + 2 files changed, 132 insertions(+), 129 deletions(-) diff --git a/test/decode_bmp.c b/test/decode_bmp.c index e90515a..c0d6500 100644 --- a/test/decode_bmp.c +++ b/test/decode_bmp.c @@ -30,168 +30,169 @@ void bitmap_destroy(void *bitmap); int main(int argc, char *argv[]) { - bmp_bitmap_callback_vt bitmap_callbacks = { - bitmap_create, - bitmap_destroy, - bitmap_get_buffer, - bitmap_get_bpp - }; - bmp_result code; - bmp_image bmp; - size_t size; - unsigned short res = 0; - - if (argc != 2) { - fprintf(stderr, "Usage: %s image.bmp\n", argv[0]); - return 1; - } - - /* create our bmp image */ - bmp_create(&bmp, &bitmap_callbacks); - - /* load file into memory */ - unsigned char *data = load_file(argv[1], &size); - - /* analyse the BMP */ - code = bmp_analyse(&bmp, size, data); - if (code != BMP_OK) { - warning("bmp_analyse", code); - res = 1; - goto cleanup; - } - - /* decode the image */ - code = bmp_decode(&bmp); - /* code = bmp_decode_trans(&bmp, TRANSPARENT_COLOR); */ - if (code != BMP_OK) { - warning("bmp_decode", code); - /* allow partially decoded images */ - if (code != BMP_INSUFFICIENT_DATA) { - res = 1; - goto cleanup; - } + bmp_bitmap_callback_vt bitmap_callbacks = { + bitmap_create, + bitmap_destroy, + bitmap_get_buffer, + bitmap_get_bpp + }; + bmp_result code; + bmp_image bmp; + size_t size; + unsigned short res = 0; + + if (argc != 2) { + fprintf(stderr, "Usage: %s image.bmp\n", argv[0]); + return 1; + } + + /* create our bmp image */ + bmp_create(&bmp, &bitmap_callbacks); + + /* load file into memory */ + unsigned char *data = load_file(argv[1], &size); + + /* analyse the BMP */ + code = bmp_analyse(&bmp, size, data); + if (code != BMP_OK) { + warning("bmp_analyse", code); + res = 1; + goto cleanup; + } + + /* decode the image */ + code = bmp_decode(&bmp); + /* code = bmp_decode_trans(&bmp, TRANSPARENT_COLOR); */ + if (code != BMP_OK) { + warning("bmp_decode", code); + /* allow partially decoded images */ + if ((code != BMP_INSUFFICIENT_DATA) && + (code != BMP_DATA_ERROR)) { + res = 1; + goto cleanup; + } + /* skip if the decoded image would be ridiculously large */ - if ((bmp.width * bmp.height) > 2000000) { - res = 1; - goto cleanup; + if ((bmp.width * bmp.height) > 200000) { + res = 1; + goto cleanup; } - } - - printf("P3\n"); - printf("# %s\n", argv[1]); - printf("# width %u \n", bmp.width); - printf("# height %u \n", bmp.height); - printf("%u %u 256\n", bmp.width, bmp.height); - - { - uint16_t row, col; - uint8_t *image; - image = (uint8_t *) bmp.bitmap; - for (row = 0; row != bmp.height; row++) { - for (col = 0; col != bmp.width; col++) { - size_t z = (row * bmp.width + col) * BYTES_PER_PIXEL; - printf("%u %u %u ", image[z], - image[z + 1], - image[z + 2]); - } - printf("\n"); - } - } + } + + printf("P3\n"); + printf("# %s\n", argv[1]); + printf("# width %u \n", bmp.width); + printf("# height %u \n", bmp.height); + printf("%u %u 256\n", bmp.width, bmp.height); + + { + uint16_t row, col; + uint8_t *image; + image = (uint8_t *) bmp.bitmap; + for (row = 0; row != bmp.height; row++) { + for (col = 0; col != bmp.width; col++) { + size_t z = (row * bmp.width + col) * BYTES_PER_PIXEL; + printf("%u %u %u ", image[z], + image[z + 1], + image[z + 2]); + } + printf("\n"); + } + } cleanup: - /* clean up */ - bmp_finalise(&bmp); - free(data); + /* clean up */ + bmp_finalise(&bmp); + free(data); - return res; + return res; } unsigned char *load_file(const char *path, size_t *data_size) { - FILE *fd; - struct stat sb; - unsigned char *buffer; - size_t size; - size_t n; - - fd = fopen(path, "rb"); - if (!fd) { - perror(path); - exit(EXIT_FAILURE); - } - - if (stat(path, &sb)) { - perror(path); - exit(EXIT_FAILURE); - } - size = sb.st_size; - - buffer = malloc(size); - if (!buffer) { - fprintf(stderr, "Unable to allocate %lld bytes\n", - (long long) size); - exit(EXIT_FAILURE); - } - - n = fread(buffer, 1, size, fd); - if (n != size) { - perror(path); - exit(EXIT_FAILURE); - } - - fclose(fd); - - *data_size = size; - return buffer; + FILE *fd; + struct stat sb; + unsigned char *buffer; + size_t size; + size_t n; + + fd = fopen(path, "rb"); + if (!fd) { + perror(path); + exit(EXIT_FAILURE); + } + + if (stat(path, &sb)) { + perror(path); + exit(EXIT_FAILURE); + } + size = sb.st_size; + + buffer = malloc(size); + if (!buffer) { + fprintf(stderr, "Unable to allocate %lld bytes\n", + (long long) size); + exit(EXIT_FAILURE); + } + + n = fread(buffer, 1, size, fd); + if (n != size) { + perror(path); + exit(EXIT_FAILURE); + } + + fclose(fd); + + *data_size = size; + return buffer; } void warning(const char *context, bmp_result code) { - fprintf(stderr, "%s failed: ", context); - switch (code) { - case BMP_INSUFFICIENT_MEMORY: - fprintf(stderr, "BMP_INSUFFICIENT_MEMORY"); - break; - case BMP_INSUFFICIENT_DATA: - fprintf(stderr, "BMP_INSUFFICIENT_DATA"); - break; - case BMP_DATA_ERROR: - fprintf(stderr, "BMP_DATA_ERROR"); - break; - default: - fprintf(stderr, "unknown code %i", code); - break; - } - fprintf(stderr, "\n"); + fprintf(stderr, "%s failed: ", context); + switch (code) { + case BMP_INSUFFICIENT_MEMORY: + fprintf(stderr, "BMP_INSUFFICIENT_MEMORY"); + break; + case BMP_INSUFFICIENT_DATA: + fprintf(stderr, "BMP_INSUFFICIENT_DATA"); + break; + case BMP_DATA_ERROR: + fprintf(stderr, "BMP_DATA_ERROR"); + break; + default: + fprintf(stderr, "unknown code %i", code); + break; + } + fprintf(stderr, "\n"); } void *bitmap_create(int width, int height, unsigned int state) { - (void) state; /* unused */ - return calloc(width * height, BYTES_PER_PIXEL); + (void) state; /* unused */ + return calloc(width * height, BYTES_PER_PIXEL); } unsigned char *bitmap_get_buffer(void *bitmap) { - assert(bitmap); - return bitmap; + assert(bitmap); + return bitmap; } size_t bitmap_get_bpp(void *bitmap) { - (void) bitmap; /* unused */ - return BYTES_PER_PIXEL; + (void) bitmap; /* unused */ + return BYTES_PER_PIXEL; } void bitmap_destroy(void *bitmap) { - assert(bitmap); - free(bitmap); + assert(bitmap); + free(bitmap); } - diff --git a/test/runtest.sh b/test/runtest.sh index 5ac5a89..3c8245f 100755 --- a/test/runtest.sh +++ b/test/runtest.sh @@ -8,6 +8,8 @@ TEST_LOG=${TEST_PATH}/test.log mkdir -p ${TEST_OUT} +echo "Bitmap tests" > ${TEST_LOG} + # bitmap test directories # standard bitmap suite -- cgit v1.2.3