FreeRDP/winpr/libwinpr/utils/test/TestImage.c

300 lines
5.5 KiB
C
Raw Normal View History

#include <winpr/wtypes.h>
2014-09-28 19:02:39 +04:00
#include <winpr/crt.h>
#include <winpr/path.h>
#include <winpr/print.h>
2014-09-28 19:02:39 +04:00
#include <winpr/image.h>
#include <winpr/environment.h>
2014-09-28 19:02:39 +04:00
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
static void* read_image(const char* src, size_t* size)
{
int success = 0;
void* a = NULL;
INT64 src_size;
FILE* fsrc = fopen(src, "rb");
if (!fsrc)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to open file %s\n", src);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (_fseeki64(fsrc, 0, SEEK_END))
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to seek to file end\n");
goto cleanup;
2015-06-02 18:53:16 +03:00
}
src_size = _ftelli64(fsrc);
2019-02-07 16:37:23 +03:00
if (src_size < 0)
{
2019-11-06 17:24:51 +03:00
fprintf(stderr, "Invalid file position %" PRId64 "\n", src_size);
2019-02-07 16:37:23 +03:00
goto cleanup;
}
if (_fseeki64(fsrc, 0, SEEK_SET))
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to seek to SEEK_SET\n");
goto cleanup;
2015-06-02 18:53:16 +03:00
}
2019-02-07 16:37:23 +03:00
a = malloc((size_t)src_size);
if (!a)
2015-06-02 18:53:16 +03:00
{
2019-11-06 17:24:51 +03:00
fprintf(stderr, "Failed malloc %" PRId64 " bytes\n", src_size);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
2019-02-07 16:37:23 +03:00
if (fread(a, sizeof(char), (size_t)src_size, fsrc) != (size_t)src_size)
2015-06-02 18:53:16 +03:00
{
2019-11-06 17:24:51 +03:00
fprintf(stderr, "Failed read %" PRId64 " bytes\n", src_size);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
success = 1;
*size = src_size;
cleanup:
if (a && !success)
{
free(a);
a = NULL;
}
if (fsrc)
fclose(fsrc);
return a;
}
static int img_compare(wImage* image, wImage* image2, BOOL ignoreType)
{
int rc = -1;
2015-06-02 16:17:54 +03:00
if ((image->type != image2->type) && !ignoreType)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Image type mismatch %d:%d\n", image->type, image2->type);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (image->width != image2->width)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Image width mismatch %d:%d\n", image->width, image2->width);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (image->height != image2->height)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Image height mismatch %d:%d\n", image->height, image2->height);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (image->scanline != image2->scanline)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Image scanline mismatch %d:%d\n", image->scanline, image2->scanline);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (image->bitsPerPixel != image2->bitsPerPixel)
2015-06-02 18:53:16 +03:00
{
2019-11-06 17:24:51 +03:00
fprintf(stderr, "Image bitsPerPixel mismatch %d:%d\n", image->bitsPerPixel,
image2->bitsPerPixel);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
if (image->bytesPerPixel != image2->bytesPerPixel)
2015-06-02 18:53:16 +03:00
{
fprintf(stderr, "Image bytesPerPixel mismatch %d:%d\n", image->bytesPerPixel,
image2->bytesPerPixel);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
rc = memcmp(image->data, image2->data, image->scanline * image->height);
2015-06-02 18:53:16 +03:00
if (rc)
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Image data mismatch!\n");
2015-06-02 18:53:16 +03:00
cleanup:
return rc;
}
static wImage* get_image(const char* src)
{
int status;
wImage* image = NULL;
image = winpr_image_new();
if (!image)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to create image!");
goto cleanup;
2015-06-02 18:53:16 +03:00
}
status = winpr_image_read(image, src);
if (status < 0)
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to read image %s!", src);
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;
2019-11-06 17:24:51 +03:00
wImage *image = NULL, *image2 = NULL, *image3 = NULL, *image4 = NULL;
if (!PathFileExistsA(src))
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "File %s does not exist!", src);
return -1;
2015-06-02 18:53:16 +03:00
}
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, dst_bmp);
if (status < 0)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to write image %s!\n", dst_bmp);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
image->type = WINPR_IMAGE_PNG;
status = winpr_image_write(image, dst_png);
if (status < 0)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to write image %s!\n", dst_png);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
/* Read image from buffer, compare. */
buffer = read_image(src, &bsize);
if (!buffer)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to read image %s!\n", src);
goto cleanup;
2015-06-02 18:53:16 +03:00
}
image2 = winpr_image_new();
if (!image2)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to create image!\n");
goto cleanup;
2015-06-02 18:53:16 +03:00
}
status = winpr_image_read_buffer(image2, buffer, bsize);
if (status < 0)
2015-06-02 18:53:16 +03:00
{
2015-06-02 18:54:27 +03:00
fprintf(stderr, "Failed to read buffer!\n");
goto cleanup;
2015-06-02 18:53:16 +03:00
}
2015-06-02 16:17:54 +03:00
rc = img_compare(image, image2, TRUE);
if (rc)
goto cleanup;
image3 = get_image(dst_png);
if (!image3)
goto cleanup;
2015-06-02 16:17:54 +03:00
rc = img_compare(image, image3, TRUE);
if (rc)
goto cleanup;
image4 = get_image(dst_bmp);
if (!image4)
goto cleanup;
2015-06-02 16:17:54 +03:00
rc = img_compare(image, image4, TRUE);
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);
2015-05-11 10:07:39 +03:00
free(buffer);
return ret;
}
static int test_image_png_to_bmp(void)
{
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;
if (!buffer)
{
free(tmp);
return -1;
}
2015-05-07 20:35:39 +03:00
sprintf_s(src_png, sizeof(src_png), "%s/lodepng_32bit.png", buffer);
sprintf_s(src_bmp, sizeof(src_bmp), "%s/lodepng_32bit.bmp", buffer);
sprintf_s(dst_png, sizeof(dst_png), "%s/lodepng_32bit.png", tmp);
sprintf_s(dst_bmp, sizeof(dst_bmp), "%s/lodepng_32bit.bmp", tmp);
sprintf_s(dst_png2, sizeof(dst_png2), "%s/lodepng_32bit-2.png", tmp);
sprintf_s(dst_bmp2, sizeof(dst_bmp2), "%s/lodepng_32bit-2.bmp", tmp);
free(tmp);
if (create_test(src_png, dst_png, dst_bmp))
return -1;
if (create_test(src_bmp, dst_png2, dst_bmp2))
return -1;
2015-06-02 18:53:16 +03:00
return 0;
}
2014-09-28 19:02:39 +04:00
int TestImage(int argc, char* argv[])
{
2015-06-02 16:17:54 +03:00
int rc = test_image_png_to_bmp();
return rc;
2014-09-28 19:02:39 +04:00
}