rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit fb09ccae13e75ad3fab2ee357eb61e61e89c6581
parent e1f2092ea7ea0743277c159e2d90f988a5a36f04
Author: vaplv <vincent.forest@meso-star.com>
Date:   Fri, 31 Mar 2017 15:55:41 +0200

Push further the image read ppm tests

Diffstat:
Msrc/image.c | 4+++-
Msrc/test_image.c | 93++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
2 files changed, 88 insertions(+), 9 deletions(-)

diff --git a/src/image.c b/src/image.c @@ -357,7 +357,9 @@ image_write_ppm_stream goto error; } - fprintf(stream, "%s %lu %lu\n", bin ? "P6" : "P3", img->width, img->height); + fprintf(stream, "%s %lu %lu\n", bin ? "P6" : "P3", + (unsigned long)img->width, + (unsigned long)img->height); switch(img->format) { /* Write Max val */ case IMAGE_RGB8: fprintf(stream, "255\n"); break; case IMAGE_RGB16: fprintf(stream, "65535\n"); break; diff --git a/src/test_image.c b/src/test_image.c @@ -20,9 +20,8 @@ #define WIDTH 64 #define HEIGHT 32 - static void -check_image +check_image_eq (const struct image* img, const char* ref_pixels, const size_t ref_width, @@ -60,7 +59,73 @@ check_image } static void -check +check_image_read + (FILE* fp, + const size_t width, + const size_t height, + const enum image_format fmt, + struct mem_allocator* allocator) +{ + struct image img; + size_t max_val; + int is_bin; + char id[2]; + + CHECK(image_init(allocator, &img), RES_OK); + + CHECK(fseek(fp, 0, SEEK_SET), 0); + CHECK(image_read_ppm_stream(&img, fp), RES_OK); + + CHECK(fseek(fp, 0, SEEK_SET), 0); + CHECK(fread(id, 1, 2, fp), 2); + CHECK(id[0], 'P'); + CHECK(id[1] == '3' || id[1] == '6', 1); + is_bin = id[1] == '6'; + + switch(fmt) { + case IMAGE_RGB8: max_val = 255; break; + case IMAGE_RGB16: max_val = 65535; break; + default: FATAL("Unreachable code.\n"); break; + } + + CHECK(fseek(fp, 3, SEEK_SET), 0); + fprintf(fp, "%lu %lu\n", (unsigned long)width+1, (unsigned long)height); + CHECK(fseek(fp, 0, SEEK_SET), 0); + CHECK(image_read_ppm_stream(&img, fp), RES_BAD_ARG); + CHECK(fseek(fp, 3, SEEK_SET), 0); + fprintf(fp, "%lu %lu\n", (unsigned long)width, (unsigned long)height+1); + CHECK(fseek(fp, 0, SEEK_SET), 0); + CHECK(image_read_ppm_stream(&img, fp), RES_BAD_ARG); + CHECK(fseek(fp, 3, SEEK_SET), 0); + fprintf(fp, "%lu %lu\n", (unsigned long)width, (unsigned long)height); + fprintf(fp, "%lu\n", (unsigned long)max_val+1); + CHECK(fseek(fp, 0, SEEK_SET), 0); + + if(is_bin) { + CHECK(image_read_ppm_stream(&img, fp), RES_BAD_ARG); + } else { + switch(fmt) { + case IMAGE_RGB8: + CHECK(image_read_ppm_stream(&img, fp), RES_OK); + break; + case IMAGE_RGB16: + CHECK(image_read_ppm_stream(&img, fp), RES_BAD_ARG); + break; + default: FATAL("Unreachable code.\n"); break; + } + } + + CHECK(fseek(fp, 3, SEEK_SET), 0); + fprintf(fp, "%lu %lu\n", (unsigned long)width, (unsigned long)height); + fprintf(fp, "%lu\n", (unsigned long)max_val); + CHECK(fseek(fp, 0, SEEK_SET), 0); + CHECK(image_read_ppm_stream(&img, fp), RES_OK); + + CHECK(image_release(&img), RES_OK); +} + +static void +check_image (const char* pixels, const size_t width, const size_t height, @@ -127,19 +192,31 @@ check CHECK(image_read_ppm_stream(&img, fp), RES_BAD_ARG); rewind(fp); CHECK(image_read_ppm_stream(&img, fp), RES_OK); - check_image(&img, pixels, width, height, fmt); + check_image_eq(&img, pixels, width, height, fmt); rewind(fp); CHECK(image_write_ppm_stream(&img, 1, fp), RES_OK); rewind(fp); CHECK(image_read_ppm_stream(&img, fp), RES_OK); - check_image(&img, pixels, width, height, fmt); + check_image_eq(&img, pixels, width, height, fmt); CHECK(image_write_ppm_stream(&img, 1, stdout), RES_OK); - CHECK(image_release(&img), RES_OK); + fclose(fp); + fp = tmpfile(); + NCHECK(fp, NULL); + CHECK(image_write_ppm_stream(&img, 0, fp), RES_OK); + check_image_read(fp, width, height, fmt, allocator); + fclose(fp); + + fp = tmpfile(); + NCHECK(fp, NULL); + CHECK(image_write_ppm_stream(&img, 1, fp), RES_OK); + check_image_read(fp, width, height, fmt, allocator); fclose(fp); + + CHECK(image_release(&img), RES_OK); } int @@ -181,7 +258,7 @@ main(int argc, char** argv) ((uint8_t*)pixels)[i++] = lut8[j][k][1]; ((uint8_t*)pixels)[i++] = lut8[j][k][2]; }} - check(pixels, WIDTH, HEIGHT, IMAGE_RGB8, &allocator); + check_image(pixels, WIDTH, HEIGHT, IMAGE_RGB8, &allocator); MEM_RM(&allocator, pixels); pixels = MEM_ALLOC(&allocator, WIDTH*sizeof_image_format(IMAGE_RGB16)*HEIGHT); @@ -196,7 +273,7 @@ main(int argc, char** argv) ((uint16_t*)pixels)[i++] = lut16[j][k][1]; ((uint16_t*)pixels)[i++] = lut16[j][k][2]; }} - check(pixels, WIDTH, HEIGHT, IMAGE_RGB16, &allocator); + check_image(pixels, WIDTH, HEIGHT, IMAGE_RGB16, &allocator); MEM_RM(&allocator, pixels); check_memory_allocator(&allocator);