153 lines
4.1 KiB
C
Raw Normal View History

2015-02-11 02:30:56 +01:00
/* Copyright (C) 2010-2015 The RetroArch team
2013-01-12 14:05:05 +01:00
*
2015-02-11 02:30:56 +01:00
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (rpng_test.c).
* ---------------------------------------------------------------------------------------
2013-01-12 14:05:05 +01:00
*
2015-02-11 02:30:56 +01:00
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2013-01-12 14:05:05 +01:00
*/
2015-02-11 02:30:56 +01:00
#include <formats/rpng.h>
2013-01-12 14:05:05 +01:00
#include <stdio.h>
#include <stdlib.h>
2013-01-19 01:31:24 +01:00
#include <stdint.h>
#include <string.h>
2015-02-20 21:10:32 +01:00
#ifdef HAVE_IMLIB2
2013-01-19 01:31:24 +01:00
#include <Imlib2.h>
2015-02-20 21:10:32 +01:00
#endif
2013-01-12 14:05:05 +01:00
2015-02-20 23:36:53 +01:00
static bool test_write_image(const char *path)
2013-01-12 14:05:05 +01:00
{
2013-01-19 01:31:24 +01:00
const uint32_t test_data[] = {
2014-09-08 19:28:10 +02:00
0xff000000 | 0x50, 0xff000000 | 0x80,
0xff000000 | 0x40, 0xff000000 | 0x88,
0xff000000 | 0x50, 0xff000000 | 0x80,
0xff000000 | 0x40, 0xff000000 | 0x88,
0xff000000 | 0xc3, 0xff000000 | 0xd3,
0xff000000 | 0xc3, 0xff000000 | 0xd3,
0xff000000 | 0xc3, 0xff000000 | 0xd3,
0xff000000 | 0xc3, 0xff000000 | 0xd3,
2013-01-19 01:31:24 +01:00
};
2015-01-26 19:05:45 +01:00
2015-02-20 23:36:53 +01:00
if (!rpng_save_image_argb(path, test_data, 4, 4, 16))
return false;
return true;
}
2013-01-19 01:31:24 +01:00
2015-02-20 23:36:53 +01:00
static bool test_load_image(const char *path, uint32_t *data)
{
unsigned width = 0;
unsigned height = 0;
2013-01-19 01:31:24 +01:00
2015-02-20 23:36:53 +01:00
if (!rpng_load_image_argb(path, &data, &width, &height))
return false;
2013-01-12 14:05:05 +01:00
2015-02-20 23:36:53 +01:00
fprintf(stderr, "Path: %s.\n", path);
2013-01-12 14:05:05 +01:00
fprintf(stderr, "Got image: %u x %u.\n", width, height);
#if 0
2013-01-19 01:31:24 +01:00
fprintf(stderr, "\nRPNG:\n");
2013-01-12 14:05:05 +01:00
for (unsigned h = 0; h < height; h++)
{
2015-01-26 19:05:45 +01:00
unsigned w;
for (w = 0; w < width; w++)
2013-01-12 14:05:05 +01:00
fprintf(stderr, "[%08x] ", data[h * width + w]);
fprintf(stderr, "\n");
}
#endif
2013-01-19 01:31:24 +01:00
2015-02-20 23:36:53 +01:00
return true;
}
static bool test_with_imlib(const char *in_path, uint32_t *data)
{
2015-02-20 21:10:32 +01:00
#ifdef HAVE_IMLIB2
2015-02-20 23:36:53 +01:00
Imlib_Image img;
unsigned width, height;
const uint32_t *imlib_data = NULL;
2015-01-26 19:05:45 +01:00
/* Validate with imlib2 as well. */
img = imlib_load_image(in_path);
2013-01-19 01:31:24 +01:00
if (!img)
2015-02-20 23:36:53 +01:00
return false;
2013-01-19 01:31:24 +01:00
imlib_context_set_image(img);
2015-01-26 19:05:45 +01:00
width = imlib_image_get_width();
height = imlib_image_get_width();
imlib_data = imlib_image_get_data_for_reading_only();
2013-01-19 01:31:24 +01:00
#if 0
2013-01-19 01:31:24 +01:00
fprintf(stderr, "\nImlib:\n");
for (unsigned h = 0; h < height; h++)
{
for (unsigned w = 0; w < width; w++)
fprintf(stderr, "[%08x] ", imlib_data[h * width + w]);
fprintf(stderr, "\n");
}
#endif
2013-01-19 01:31:24 +01:00
2015-02-20 23:36:53 +01:00
if (memcmp(imlib_data, &data, width * height * sizeof(uint32_t)) != 0)
{
fprintf(stderr, "Imlib and RPNG differs!\n");
2015-02-20 23:36:53 +01:00
return false;
}
else
fprintf(stderr, "Imlib and RPNG are equivalent!\n");
2013-01-19 01:31:24 +01:00
imlib_free_image();
2015-02-20 21:10:32 +01:00
#endif
2015-02-20 23:36:53 +01:00
return true;
}
static int test_blocking_rpng(const char *path, uint32_t *data)
{
if (!test_write_image("/tmp/test.png"))
return 1;
if (!test_load_image(path, data))
return 2;
#ifdef HAVE_IMLIB2
if (!test_with_imlib(path, data))
return 5;
#endif
return 0;
}
int main(int argc, char *argv[])
{
int ret = 0;
uint32_t *data = NULL;
const char *in_path = "/tmp/test.png";
if (argc > 2)
{
fprintf(stderr, "Usage: %s <png file>\n", argv[0]);
return 1;
}
if (argc == 2)
in_path = argv[1];
ret = test_blocking_rpng(in_path, data);
2013-01-12 14:05:05 +01:00
free(data);
2015-02-20 23:36:53 +01:00
return ret;
2013-01-12 14:05:05 +01:00
}