Added proper bitmap and png image test.
authorArmin Novak <armin.novak@gmail.com>
Sun, 19 Apr 2015 10:29:28 +0000 (12:29 +0200)
committerArmin Novak <armin.novak@gmail.com>
Sun, 19 Apr 2015 10:29:28 +0000 (12:29 +0200)
winpr/libwinpr/utils/test/CMakeLists.txt
winpr/libwinpr/utils/test/TestImage.c
winpr/libwinpr/utils/test/lodepng_32bit.bmp [new file with mode: 0644]
winpr/libwinpr/utils/test/lodepng_32bit.png [new file with mode: 0644]

index 134015c..02ec09a 100644 (file)
@@ -31,6 +31,8 @@ create_test_sourcelist(${MODULE_PREFIX}_SRCS
        ${${MODULE_PREFIX}_DRIVER}
        ${${MODULE_PREFIX}_TESTS})
 
+add_definitions(-DTEST_SOURCE_PATH="${CMAKE_CURRENT_SOURCE_DIR}")
+
 add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
 
 target_link_libraries(${MODULE_NAME} winpr)
index 72e8fc2..8bd66b5 100644 (file)
 #include <winpr/path.h>
 #include <winpr/print.h>
 #include <winpr/image.h>
+#include <winpr/environment.h>
 
-int test_image_png_to_bmp()
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+static void *read_image(const char *src, size_t *size)
 {
-       int status;
-       wImage* image;
+       int success = 0;
+       void *a = NULL;
+       long src_size;
+       FILE *fsrc = fopen(src, "r");
+
+       if (!fsrc)
+               goto cleanup;
+
+       if (fseek(fsrc, 0, SEEK_END))
+               goto cleanup;
+
+       src_size = ftell(fsrc);
+
+       if (fseek(fsrc, 0, SEEK_SET))
+               goto cleanup;
+
+       a = malloc(src_size);
+
+       if (!a)
+               goto cleanup;
+
+       if (fread(a, sizeof(char), src_size,  fsrc) != src_size)
+               goto cleanup;
+
+       success = 1;
+       *size = src_size;
+
+cleanup:
+       if (a && !success)
+       {
+               free(a);
+               a = NULL;
+       }
+       if (fsrc)
+               fclose(fsrc);
+
+       return a;
+}
+
+
+static int compare(const char *src, const char *dst)
+{
+       int cmp = -1;
+       size_t asize, bsize;
+       void *a, *b;
+
+       a = read_image(src, &asize);
+       b = read_image(dst, &bsize);
+
+       if (!a || !b || (asize != bsize))
+               goto cleanup;
 
-       if (!PathFileExistsA("/tmp/test.png"))
-               return 1; /* skip */
+       cmp = memcmp(a, b, asize);
+
+cleanup:
+       if (a)
+               free(a);
+       if (b)
+               free(b);
+
+       return cmp;
+}
+
+static int img_compare(wImage *image, wImage *image2)
+{
+       int rc = -1;
+       if (image->type != image2->type)
+               goto cleanup;
+
+       if (image->width != image2->width)
+               goto cleanup;
+
+       if (image->height != image2->height)
+               goto cleanup;
+
+       if (image->scanline != image2->scanline)
+               goto cleanup;
+
+       if (image->bitsPerPixel != image2->bitsPerPixel)
+               goto cleanup;
+
+       if (image->bytesPerPixel != image2->bytesPerPixel)
+               goto cleanup;
+
+       rc = memcmp(image->data, image2->data, image->scanline * image->height);
+
+cleanup:
+       return rc;
+}
+
+static wImage *get_image(const char *src)
+{
+       int status;
+       wImage* image = NULL;
 
        image = winpr_image_new();
 
        if (!image)
-               return -1;
+               goto cleanup;
 
-       status = winpr_image_read(image, "/tmp/test.png");
+       status = winpr_image_read(image, src);
 
        if (status < 0)
+       {
+               winpr_image_free(image, TRUE);
+               image = NULL;
+       }
+
+cleanup:
+
+       return image;
+}
+
+static int create_test(const char *src, const char *dst_png, const char *dst_bmp)
+{
+       int rc = -1;
+       int ret = -1;
+       int status;
+       size_t bsize;
+       void *buffer = NULL;
+       wImage* image = NULL, *image2 = NULL, *image3 = NULL, *image4 = NULL;
+
+       if (!PathFileExistsA(src))
                return -1;
 
+       image = get_image(src);
+
+       /* Read from file using image methods. */
+       if (!image)
+               goto cleanup;
+
+       /* Write different formats to tmp. */
        image->type = WINPR_IMAGE_BITMAP;
-       status = winpr_image_write(image, "/tmp/test_out.bmp");
+       status = winpr_image_write(image, dst_bmp);
 
        if (status < 0)
-               return -1;
+               goto cleanup;
 
        image->type = WINPR_IMAGE_PNG;
-       status = winpr_image_write(image, "/tmp/test_out.png");
+       status = winpr_image_write(image, dst_png);
 
        if (status < 0)
+               goto cleanup;
+
+       /* Read image from buffer, compare. */
+       buffer = read_image(src, &bsize);
+       if (!buffer)
+               goto cleanup;
+
+       image2 = winpr_image_new();
+
+       if (!image2)
+               goto cleanup;
+
+       status = winpr_image_read_buffer(image2, buffer, bsize);
+
+       if (status < 0)
+               goto cleanup;
+
+       rc = img_compare(image, image2);
+       if (rc)
+               goto cleanup;
+
+       image3 = get_image(dst_png);
+       if (!image3)
+               goto cleanup;
+
+       rc = img_compare(image, image3);
+       if (rc)
+               goto cleanup;
+
+       image4 = get_image(dst_bmp);
+       if (!image4)
+               goto cleanup;
+
+       image->type = WINPR_IMAGE_BITMAP;
+       rc = img_compare(image, image4);
+       if (rc)
+               goto cleanup;
+
+       ret = 0;
+cleanup:
+       if (image)
+               winpr_image_free(image, TRUE);
+       if (image2)
+               winpr_image_free(image2, TRUE);
+       if (image3)
+               winpr_image_free(image3, TRUE);
+       if (image4)
+               winpr_image_free(image4, TRUE);
+       if (buffer)
+               free(buffer);
+
+       return ret;
+}
+
+int test_image_png_to_bmp()
+{
+       char *buffer = TEST_SOURCE_PATH;
+       char src_png[PATH_MAX];
+       char src_bmp[PATH_MAX];
+       char dst_png[PATH_MAX];
+       char dst_bmp[PATH_MAX];
+       char dst_png2[PATH_MAX];
+       char dst_bmp2[PATH_MAX];
+       char *tmp = GetKnownPath(KNOWN_PATH_TEMP);
+
+       if (!tmp)
                return -1;
 
-       winpr_image_free(image, TRUE);
+       if (!buffer)
+               return -1;
+
+       snprintf(src_png, sizeof(src_png), "%s/lodepng_32bit.png", buffer);
+       snprintf(src_bmp, sizeof(src_bmp), "%s/lodepng_32bit.bmp", buffer);
+       snprintf(dst_png, sizeof(dst_png), "%s/lodepng_32bit.png", tmp);
+       snprintf(dst_bmp, sizeof(dst_bmp), "%s/lodepng_32bit.bmp", tmp);
+       snprintf(dst_png2, sizeof(dst_png2), "%s/lodepng_32bit-2.png", tmp);
+       snprintf(dst_bmp2, sizeof(dst_bmp2), "%s/lodepng_32bit-2.bmp", tmp);
+
+       if (create_test(src_png, dst_png, dst_bmp))
+               return -1;
+
+       if (create_test(src_bmp, dst_png2, dst_bmp2))
+               return -1;
+
+#if 0
+       if (compare(dst_png2, dst_png))
+               return -1;
+
+       if (compare(dst_bmp2, dst_bmp))
+               return -1;
+#endif
 
        return 1;
 }
diff --git a/winpr/libwinpr/utils/test/lodepng_32bit.bmp b/winpr/libwinpr/utils/test/lodepng_32bit.bmp
new file mode 100644 (file)
index 0000000..d52d34c
Binary files /dev/null and b/winpr/libwinpr/utils/test/lodepng_32bit.bmp differ
diff --git a/winpr/libwinpr/utils/test/lodepng_32bit.png b/winpr/libwinpr/utils/test/lodepng_32bit.png
new file mode 100644 (file)
index 0000000..9c55f28
Binary files /dev/null and b/winpr/libwinpr/utils/test/lodepng_32bit.png differ