(video_texture_image.c) Cleanups - we use nbio now in video_texture_image_load

for both TGA and PNG, and we remove the file I/O based RPNG function
This commit is contained in:
twinaphex 2016-05-07 04:04:09 +02:00
parent 9ac9ed4b36
commit b8644536d2
3 changed files with 161 additions and 121 deletions

View File

@ -45,14 +45,20 @@ bool video_texture_image_set_color_shifts(
unsigned *r_shift, unsigned *g_shift, unsigned *b_shift, unsigned *r_shift, unsigned *g_shift, unsigned *b_shift,
unsigned *a_shift) unsigned *a_shift)
{ {
bool use_rgba = video_driver_ctl(
RARCH_DISPLAY_CTL_SUPPORTS_RGBA, NULL);
*a_shift = 24; *a_shift = 24;
*r_shift = use_rgba ? 0 : 16; *r_shift = 16;
*g_shift = 8; *g_shift = 8;
*b_shift = use_rgba ? 16 : 0; *b_shift = 0;
return use_rgba; if (video_driver_ctl(
RARCH_DISPLAY_CTL_SUPPORTS_RGBA, NULL))
{
*r_shift = 0;
*b_shift = 16;
return true;
}
return false;
} }
bool video_texture_image_color_convert(unsigned r_shift, bool video_texture_image_color_convert(unsigned r_shift,
@ -109,6 +115,7 @@ bool video_texture_image_color_convert(unsigned r_shift,
static bool video_texture_image_rpng_gx_convert_texture32( static bool video_texture_image_rpng_gx_convert_texture32(
struct texture_image *image) struct texture_image *image)
{ {
int ret;
unsigned tmp_pitch, width2, i; unsigned tmp_pitch, width2, i;
const uint16_t *src = NULL; const uint16_t *src = NULL;
uint16_t *dst = NULL; uint16_t *dst = NULL;
@ -149,18 +156,36 @@ static bool video_texture_image_rpng_gx_convert_texture32(
#endif #endif
static bool video_texture_image_load_png( static bool video_texture_image_load_png(
const char *path, void *ptr,
struct texture_image *out_img, struct texture_image *out_img,
unsigned a_shift, unsigned r_shift, unsigned a_shift, unsigned r_shift,
unsigned g_shift, unsigned b_shift) unsigned g_shift, unsigned b_shift)
{ {
if (!rpng_load_image_argb(path, int ret;
&out_img->pixels, &out_img->width, &out_img->height)) rpng_t *rpng = rpng_alloc();
if (!rpng)
goto error;
if (!rpng_set_buf_ptr(rpng, (uint8_t*)ptr))
goto error;
if (!rpng_nbio_load_image_argb_start(rpng))
goto error;
while (rpng_nbio_load_image_argb_iterate(rpng));
if (!rpng_is_valid(rpng))
goto error;
do
{ {
out_img->pixels = NULL; ret = rpng_nbio_load_image_argb_process(rpng, &out_img->pixels, &out_img->width,
out_img->width = out_img->height = 0; &out_img->height);
return false; }while(ret == IMAGE_PROCESS_NEXT);
}
if (ret == IMAGE_PROCESS_ERROR || ret == IMAGE_PROCESS_ERROR_END)
goto error;
video_texture_image_color_convert(r_shift, g_shift, b_shift, video_texture_image_color_convert(r_shift, g_shift, b_shift,
a_shift, out_img); a_shift, out_img);
@ -169,11 +194,21 @@ static bool video_texture_image_load_png(
if (!video_texture_image_rpng_gx_convert_texture32(out_img)) if (!video_texture_image_rpng_gx_convert_texture32(out_img))
{ {
video_texture_image_free(out_img); video_texture_image_free(out_img);
return false; goto error;
} }
#endif #endif
rpng_nbio_load_image_free(rpng);
return true; return true;
error:
out_img->pixels = NULL;
out_img->width = 0;
out_img->height = 0;
if (rpng)
rpng_nbio_load_image_free(rpng);
return false;
} }
#endif #endif
@ -188,35 +223,7 @@ void video_texture_image_free(struct texture_image *img)
memset(img, 0, sizeof(*img)); memset(img, 0, sizeof(*img));
} }
static bool video_texture_image_load_tga( static enum video_image_format video_texture_image_get_type(const char *path)
const char *path,
struct texture_image *out_img,
unsigned a_shift, unsigned r_shift,
unsigned g_shift, unsigned b_shift)
{
ssize_t len;
void *raw_buf = NULL;
uint8_t *buf = NULL;
bool ret = filestream_read_file(path, &raw_buf, &len);
if (!ret || len < 0)
{
RARCH_ERR("Failed to read image: %s.\n", path);
return false;
}
buf = (uint8_t*)raw_buf;
ret = rtga_image_load_shift(buf, out_img,
a_shift, r_shift, g_shift, b_shift);
if (buf)
free(buf);
return ret;
}
enum video_image_format video_texture_image_get_type(const char *path)
{ {
if (strstr(path, ".tga")) if (strstr(path, ".tga"))
return IMAGE_FORMAT_TGA; return IMAGE_FORMAT_TGA;
@ -228,28 +235,62 @@ enum video_image_format video_texture_image_get_type(const char *path)
bool video_texture_image_load(struct texture_image *out_img, bool video_texture_image_load(struct texture_image *out_img,
const char *path) const char *path)
{ {
size_t file_len;
unsigned r_shift, g_shift, b_shift, a_shift; unsigned r_shift, g_shift, b_shift, a_shift;
struct nbio_t *handle = NULL;
void *ptr = NULL;
enum video_image_format fmt = video_texture_image_get_type(path); enum video_image_format fmt = video_texture_image_get_type(path);
video_texture_image_set_color_shifts(&r_shift, &g_shift, &b_shift, video_texture_image_set_color_shifts(&r_shift, &g_shift, &b_shift,
&a_shift); &a_shift);
switch (fmt)
{
case IMAGE_FORMAT_NONE:
break;
default:
handle = (struct nbio_t*)nbio_open(path, NBIO_READ);
if (!handle)
goto error;
nbio_begin_read(handle);
while (!nbio_iterate(handle));
ptr = nbio_get_ptr(handle, &file_len);
if (!ptr)
goto error;
break;
}
switch (fmt) switch (fmt)
{ {
case IMAGE_FORMAT_TGA: case IMAGE_FORMAT_TGA:
return video_texture_image_load_tga(path, out_img, if (rtga_image_load_shift(ptr, out_img,
a_shift, r_shift, g_shift, b_shift); a_shift, r_shift, g_shift, b_shift))
goto success;
break;
case IMAGE_FORMAT_PNG: case IMAGE_FORMAT_PNG:
#ifdef HAVE_RPNG #ifdef HAVE_RPNG
return video_texture_image_load_png(path, out_img, if (video_texture_image_load_png(ptr, out_img,
a_shift, r_shift, g_shift, b_shift); a_shift, r_shift, g_shift, b_shift))
#else goto success;
break;
#endif #endif
break;
default: default:
case IMAGE_FORMAT_NONE: case IMAGE_FORMAT_NONE:
break; break;
} }
error:
if (handle)
nbio_free(handle);
return false; return false;
success:
if (handle)
nbio_free(handle);
return true;
} }

View File

@ -30,7 +30,6 @@
#endif #endif
#include <boolean.h> #include <boolean.h>
#include <file/nbio.h>
#include <formats/rpng.h> #include <formats/rpng.h>
#include <file/archive_file.h> #include <file/archive_file.h>
@ -1037,75 +1036,3 @@ rpng_t *rpng_alloc(void)
return NULL; return NULL;
return rpng; return rpng;
} }
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;
}
if (!rpng_nbio_load_image_argb_start(rpng))
{
ret = false;
goto end;
}
while (rpng_nbio_load_image_argb_iterate(rpng));
if (!rpng_is_valid(rpng))
{
ret = false;
goto end;
}
do
{
retval = rpng_nbio_load_image_argb_process(rpng, data, width, height);
}while(retval == PNG_PROCESS_NEXT);
if (retval == PNG_PROCESS_ERROR || retval == PNG_PROCESS_ERROR_END)
ret = false;
end:
if (handle)
nbio_free(handle);
if (rpng)
rpng_nbio_load_image_free(rpng);
rpng = NULL;
if (!ret)
free(*data);
return ret;
}

View File

@ -31,6 +31,78 @@
#include <file/nbio.h> #include <file/nbio.h>
#include <formats/rpng.h> #include <formats/rpng.h>
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;
}
if (!rpng_nbio_load_image_argb_start(rpng))
{
ret = false;
goto end;
}
while (rpng_nbio_load_image_argb_iterate(rpng));
if (!rpng_is_valid(rpng))
{
ret = false;
goto end;
}
do
{
retval = rpng_nbio_load_image_argb_process(rpng, data, width, height);
}while(retval == PNG_PROCESS_NEXT);
if (retval == PNG_PROCESS_ERROR || retval == PNG_PROCESS_ERROR_END)
ret = false;
end:
if (handle)
nbio_free(handle);
if (rpng)
rpng_nbio_load_image_free(rpng);
rpng = NULL;
if (!ret)
free(*data);
return ret;
}
static int test_rpng(const char *in_path) static int test_rpng(const char *in_path)
{ {
#ifdef HAVE_IMLIB2 #ifdef HAVE_IMLIB2