2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2014-09-28 19:02:39 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-09-28 19:14:30 +04:00
|
|
|
#include <winpr/path.h>
|
|
|
|
#include <winpr/print.h>
|
2014-09-28 19:02:39 +04:00
|
|
|
#include <winpr/image.h>
|
2015-04-19 13:29:28 +03:00
|
|
|
#include <winpr/environment.h>
|
2014-09-28 19:02:39 +04:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 4096
|
|
|
|
#endif
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static void* read_image(const char* src, size_t* size)
|
2014-09-28 19:14:30 +04:00
|
|
|
{
|
2015-04-19 13:29:28 +03:00
|
|
|
int success = 0;
|
2017-11-15 11:11:12 +03:00
|
|
|
void* a = NULL;
|
2017-08-11 11:07:46 +03:00
|
|
|
INT64 src_size;
|
2017-11-15 11:11:12 +03:00
|
|
|
FILE* fsrc = fopen(src, "rb");
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
2017-08-11 11:07:46 +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");
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
2017-08-11 11:07:46 +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;
|
|
|
|
}
|
2017-08-11 11:07:46 +03:00
|
|
|
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");
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
2019-02-07 16:37:23 +03:00
|
|
|
a = malloc((size_t)src_size);
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
success = 1;
|
|
|
|
*size = src_size;
|
|
|
|
cleanup:
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (a && !success)
|
|
|
|
{
|
|
|
|
free(a);
|
|
|
|
a = NULL;
|
|
|
|
}
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (fsrc)
|
|
|
|
fclose(fsrc);
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static int img_compare(wImage* image, wImage* image2, BOOL ignoreType)
|
2015-04-19 13:29:28 +03:00
|
|
|
{
|
|
|
|
int rc = -1;
|
2017-11-15 11:11:12 +03:00
|
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
if (image->bytesPerPixel != image2->bytesPerPixel)
|
2015-06-02 18:53:16 +03:00
|
|
|
{
|
2017-11-15 11:11:12 +03:00
|
|
|
fprintf(stderr, "Image bytesPerPixel mismatch %d:%d\n", image->bytesPerPixel,
|
|
|
|
image2->bytesPerPixel);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
cleanup:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static wImage* get_image(const char* src)
|
2015-04-19 13:29:28 +03:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
wImage* image = NULL;
|
2014-09-28 19:14:30 +04:00
|
|
|
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!");
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2014-09-28 19:14:30 +04:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
status = winpr_image_read(image, src);
|
2014-09-28 19:14:30 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2015-04-19 13:29:28 +03:00
|
|
|
{
|
2015-06-02 18:54:27 +03:00
|
|
|
fprintf(stderr, "Failed to read image %s!", src);
|
2015-04-19 13:29:28 +03:00
|
|
|
winpr_image_free(image, TRUE);
|
|
|
|
image = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static int create_test(const char* src, const char* dst_png, const char* dst_bmp)
|
2015-04-19 13:29:28 +03:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
int ret = -1;
|
|
|
|
int status;
|
|
|
|
size_t bsize;
|
2017-11-15 11:11:12 +03:00
|
|
|
void* buffer = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
wImage *image = NULL, *image2 = NULL, *image3 = NULL, *image4 = NULL;
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
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);
|
2014-09-28 19:14:30 +04:00
|
|
|
return -1;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2014-09-28 19:14:30 +04:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
image = get_image(src);
|
|
|
|
|
|
|
|
/* Read from file using image methods. */
|
|
|
|
if (!image)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Write different formats to tmp. */
|
2014-09-28 19:14:30 +04:00
|
|
|
image->type = WINPR_IMAGE_BITMAP;
|
2015-04-19 13:29:28 +03:00
|
|
|
status = winpr_image_write(image, dst_bmp);
|
2014-09-28 19:14:30 +04:00
|
|
|
|
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2014-09-28 19:14:30 +04:00
|
|
|
|
|
|
|
image->type = WINPR_IMAGE_PNG;
|
2015-04-19 13:29:28 +03:00
|
|
|
status = winpr_image_write(image, dst_png);
|
2014-09-28 19:14:30 +04:00
|
|
|
|
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
/* Read image from buffer, compare. */
|
|
|
|
buffer = read_image(src, &bsize);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
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);
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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");
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +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");
|
2015-04-19 13:29:28 +03:00
|
|
|
goto cleanup;
|
2015-06-02 18:53:16 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
2015-06-02 16:17:54 +03:00
|
|
|
rc = img_compare(image, image2, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (rc)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
image3 = get_image(dst_png);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (!image3)
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-06-02 16:17:54 +03:00
|
|
|
rc = img_compare(image, image3, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (rc)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
image4 = get_image(dst_bmp);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (!image4)
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-06-02 16:17:54 +03:00
|
|
|
rc = img_compare(image, image4, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (rc)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (image)
|
|
|
|
winpr_image_free(image, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (image2)
|
|
|
|
winpr_image_free(image2, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (image3)
|
|
|
|
winpr_image_free(image3, TRUE);
|
2017-11-15 11:11:12 +03:00
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (image4)
|
|
|
|
winpr_image_free(image4, TRUE);
|
2015-05-11 10:07:39 +03:00
|
|
|
|
|
|
|
free(buffer);
|
2015-04-19 13:29:28 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-11-15 11:11:12 +03:00
|
|
|
static int test_image_png_to_bmp(void)
|
2015-04-19 13:29:28 +03:00
|
|
|
{
|
2017-11-15 11:11:12 +03:00
|
|
|
char* buffer = TEST_SOURCE_PATH;
|
2015-04-19 13:29:28 +03:00
|
|
|
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];
|
2017-11-15 11:11:12 +03:00
|
|
|
char* tmp = GetKnownPath(KNOWN_PATH_TEMP);
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
if (!tmp)
|
2014-09-28 19:14:30 +04:00
|
|
|
return -1;
|
|
|
|
|
2015-04-19 13:29:28 +03:00
|
|
|
if (!buffer)
|
2017-11-15 11:11:12 +03:00
|
|
|
{
|
|
|
|
free(tmp);
|
2015-04-19 13:29:28 +03:00
|
|
|
return -1;
|
2017-11-15 11:11:12 +03:00
|
|
|
}
|
2015-04-19 13:29:28 +03:00
|
|
|
|
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);
|
2017-11-15 11:11:12 +03:00
|
|
|
free(tmp);
|
2015-04-19 13:29:28 +03:00
|
|
|
|
|
|
|
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:14:30 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|