RetroArch/libretro-common/samples/formats/png/rpng_test.c

208 lines
5.0 KiB
C
Raw Normal View History

/* Copyright (C) 2010-2017 The RetroArch team
2013-01-12 13:05:05 +00:00
*
2015-02-11 01:30:56 +00:00
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (rpng_test.c).
* ---------------------------------------------------------------------------------------
2013-01-12 13:05:05 +00:00
*
2015-02-11 01:30:56 +00: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 13:05:05 +00:00
*/
#include <stdio.h>
#include <stdlib.h>
2013-01-19 00:31:24 +00:00
#include <stdint.h>
#include <string.h>
2015-02-20 20:10:32 +00:00
#ifdef HAVE_IMLIB2
2013-01-19 00:31:24 +00:00
#include <Imlib2.h>
2015-02-20 20:10:32 +00:00
#endif
2013-01-12 13:05:05 +00:00
2015-09-18 23:29:03 +00:00
#include <file/nbio.h>
#include <formats/rpng.h>
2016-05-07 02:15:39 +00:00
#include <formats/image.h>
2015-09-18 23:29:03 +00:00
static bool rpng_load_image_argb(const char *path, uint32_t **data,
unsigned *width, unsigned *height)
{
int retval;
size_t file_len;
bool ret = true;
rpng_t *rpng = NULL;
void *ptr = NULL;
struct nbio_t* handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
if (!handle)
goto end;
nbio_begin_read(handle);
while (!nbio_iterate(handle));
ptr = nbio_get_ptr(handle, &file_len);
if (!ptr)
{
ret = false;
goto end;
}
rpng = rpng_alloc();
if (!rpng)
{
ret = false;
goto end;
}
if (!rpng_set_buf_ptr(rpng, (uint8_t*)ptr))
{
ret = false;
goto end;
}
2016-05-13 22:48:40 +00:00
if (!rpng_start(rpng))
{
ret = false;
goto end;
}
2016-05-13 22:48:40 +00:00
while (rpng_iterate_image(rpng));
if (!rpng_is_valid(rpng))
{
ret = false;
goto end;
}
do
{
2016-05-13 23:12:05 +00:00
retval = rpng_process_image(rpng,
(void**)data, file_len, width, height);
2016-05-07 02:15:39 +00:00
}while(retval == IMAGE_PROCESS_NEXT);
2016-05-07 02:15:39 +00:00
if (retval == IMAGE_PROCESS_ERROR || retval == IMAGE_PROCESS_ERROR_END)
ret = false;
end:
if (handle)
nbio_free(handle);
if (rpng)
2016-05-13 22:48:40 +00:00
rpng_free(rpng);
rpng = NULL;
if (!ret)
free(*data);
return ret;
}
2015-09-18 23:34:27 +00:00
static int test_rpng(const char *in_path)
2015-02-21 00:35:04 +00:00
{
#ifdef HAVE_IMLIB2
Imlib_Image img;
const uint32_t *imlib_data = NULL;
#endif
const uint32_t test_data[] = {
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,
};
uint32_t *data = NULL;
unsigned width = 0;
unsigned height = 0;
2015-02-20 23:03:32 +00:00
if (!rpng_save_image_argb("/tmp/test.png", test_data, 4, 4, 16))
return 1;
2013-01-19 00:31:24 +00:00
2015-02-20 23:03:32 +00:00
if (!rpng_load_image_argb(in_path, &data, &width, &height))
return 2;
fprintf(stderr, "Path: %s.\n", in_path);
2013-01-12 13:05:05 +00:00
fprintf(stderr, "Got image: %u x %u.\n", width, height);
#if 0
2013-01-19 00:31:24 +00:00
fprintf(stderr, "\nRPNG:\n");
2013-01-12 13:05:05 +00:00
for (unsigned h = 0; h < height; h++)
{
2015-01-26 18:05:45 +00:00
unsigned w;
for (w = 0; w < width; w++)
2013-01-12 13:05:05 +00:00
fprintf(stderr, "[%08x] ", data[h * width + w]);
fprintf(stderr, "\n");
}
#endif
2013-01-19 00:31:24 +00:00
2015-02-20 20:10:32 +00:00
#ifdef HAVE_IMLIB2
2015-01-26 18:05:45 +00:00
/* Validate with imlib2 as well. */
img = imlib_load_image(in_path);
2013-01-19 00:31:24 +00:00
if (!img)
2015-02-20 23:03:32 +00:00
return 4;
2013-01-19 00:31:24 +00:00
imlib_context_set_image(img);
2015-01-26 18:05:45 +00:00
width = imlib_image_get_width();
height = imlib_image_get_width();
imlib_data = imlib_image_get_data_for_reading_only();
2013-01-19 00:31:24 +00:00
#if 0
2013-01-19 00:31:24 +00: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 00:31:24 +00:00
2015-02-20 23:03:32 +00:00
if (memcmp(imlib_data, data, width * height * sizeof(uint32_t)) != 0)
{
fprintf(stderr, "Imlib and RPNG differs!\n");
2015-02-20 23:03:32 +00:00
return 5;
}
else
fprintf(stderr, "Imlib and RPNG are equivalent!\n");
2013-01-19 00:31:24 +00:00
imlib_free_image();
2015-02-20 20:10:32 +00:00
#endif
2013-01-12 13:05:05 +00:00
free(data);
2015-02-20 23:05:05 +00:00
return 0;
}
int main(int argc, char *argv[])
{
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];
2015-09-18 23:34:27 +00:00
fprintf(stderr, "Doing tests...\n");
2015-02-21 00:35:04 +00:00
2015-09-18 23:34:27 +00:00
if (test_rpng(in_path) != 0)
2015-02-21 00:35:04 +00:00
{
2015-09-18 23:34:27 +00:00
fprintf(stderr, "Test failed.\n");
2015-02-21 00:35:04 +00:00
return -1;
}
2015-09-18 23:34:27 +00:00
return 0;
2013-01-12 13:05:05 +00:00
}