commit 6afb2ac8bee36a3021daebc21e23728c807a8fe7
parent fb09ccae13e75ad3fab2ee357eb61e61e89c6581
Author: vaplv <vincent.forest@meso-star.com>
Date: Fri, 31 Mar 2017 16:04:17 +0200
Fix the image_<read|write>_ppm functions
Diffstat:
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/src/image.c b/src/image.c
@@ -168,7 +168,6 @@ write_raw_ppm(const struct image* img, FILE* stream)
return RES_OK;
}
-
/*******************************************************************************
* Exported functions
******************************************************************************/
@@ -243,11 +242,11 @@ image_read_ppm(struct image* img, const char* filename)
res_T res = RES_OK;
if(!img || !filename) {
- res = RES_OK;
+ res = RES_BAD_ARG;
goto error;
}
- stream = fopen(filename, "w");
+ stream = fopen(filename, "r");
if(!stream) {
res = RES_IO_ERR;
goto error;
@@ -324,11 +323,11 @@ image_write_ppm
res_T res = RES_OK;
if(!img || !filename) {
- res = RES_OK;
+ res = RES_BAD_ARG;
goto error;
}
- stream = fopen(filename, "r");
+ stream = fopen(filename, "w");
if(!stream) {
res = RES_IO_ERR;
goto error;
@@ -357,7 +356,7 @@ image_write_ppm_stream
goto error;
}
- fprintf(stream, "%s %lu %lu\n", bin ? "P6" : "P3",
+ fprintf(stream, "%s %lu %lu\n", bin ? "P6" : "P3",
(unsigned long)img->width,
(unsigned long)img->height);
switch(img->format) { /* Write Max val */
diff --git a/src/test_image.c b/src/test_image.c
@@ -200,6 +200,23 @@ check_image
CHECK(image_read_ppm_stream(&img, fp), RES_OK);
check_image_eq(&img, pixels, width, height, fmt);
+ CHECK(image_write_ppm(NULL, 0, NULL), RES_BAD_ARG);
+ CHECK(image_write_ppm(&img, 0, NULL), RES_BAD_ARG);
+ CHECK(image_write_ppm(NULL, 0, "test.ppm"), RES_BAD_ARG);
+ CHECK(image_write_ppm(&img, 0, "test.ppm"), RES_OK);
+
+ CHECK(image_read_ppm(NULL, NULL), RES_BAD_ARG);
+ CHECK(image_read_ppm(&img, NULL), RES_BAD_ARG);
+ CHECK(image_read_ppm(NULL, "test_bad.ppm"), RES_BAD_ARG);
+ CHECK(image_read_ppm(&img, "test_bad.ppm"), RES_IO_ERR);
+ CHECK(image_read_ppm(&img, "test.ppm"), RES_OK);
+
+ check_image_eq(&img, pixels, width, height, fmt);
+
+ CHECK(image_write_ppm(&img, 1, "test.ppm"), RES_OK);
+ CHECK(image_read_ppm(&img, "test.ppm"), RES_OK);
+ check_image_eq(&img, pixels, width, height, fmt);
+
CHECK(image_write_ppm_stream(&img, 1, stdout), RES_OK);
fclose(fp);