Skip to content

Commit

Permalink
libobs: Add gs_create_texture_file_data function
Browse files Browse the repository at this point in the history
Allows loading the file to memory first so the graphics subsystem
doesn't necessarily have to be locked when loading image files.
  • Loading branch information
jp9000 committed Jan 23, 2016
1 parent b0ca6ea commit 9f63554
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
22 changes: 14 additions & 8 deletions libobs/graphics/graphics-ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,27 @@ static inline enum gs_color_format convert_format(enum AVPixelFormat format)
return GS_BGRX;
}

gs_texture_t *gs_texture_create_from_file(const char *file)
uint8_t *gs_create_texture_file_data(const char *file,
enum gs_color_format *format,
uint32_t *cx_out, uint32_t *cy_out)
{
struct ffmpeg_image image;
gs_texture_t *tex = NULL;
uint8_t *data = NULL;

if (ffmpeg_image_init(&image, file)) {
uint8_t *data = malloc(image.cx * image.cy * 4);
data = bmalloc(image.cx * image.cy * 4);

if (ffmpeg_image_decode(&image, data, image.cx * 4)) {
tex = gs_texture_create(image.cx, image.cy,
convert_format(image.format),
1, (const uint8_t**)&data, 0);
*format = convert_format(image.format);
*cx_out = (uint32_t)image.cx;
*cy_out = (uint32_t)image.cy;
} else {
bfree(data);
data = NULL;
}

ffmpeg_image_free(&image);
free(data);
}
return tex;

return data;
}
22 changes: 13 additions & 9 deletions libobs/graphics/graphics-magick.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ void gs_free_image_deps()
MagickCoreTerminus();
}

gs_texture_t *gs_texture_create_from_file(const char *file)
uint8_t *gs_create_texture_file_data(const char *file,
enum gs_color_format *format,
uint32_t *cx_out, uint32_t *cy_out)
{
gs_texture_t *tex = NULL;
uint8_t *data = NULL;
ImageInfo *info;
ExceptionInfo *exception;
Image *image;
Expand All @@ -32,19 +34,21 @@ gs_texture_t *gs_texture_create_from_file(const char *file)
if (image) {
size_t cx = image->magick_columns;
size_t cy = image->magick_rows;
uint8_t *data = malloc(cx * cy * 4);
data = bmalloc(cx * cy * 4);

ExportImagePixels(image, 0, 0, cx, cy, "BGRA", CharPixel,
data, exception);
if (exception->severity == UndefinedException)
tex = gs_texture_create(cx, cy, GS_BGRA, 1,
(const uint8_t**)&data, 0);
else
if (exception->severity != UndefinedException) {
blog(LOG_WARNING, "magickcore warning/error getting "
"pixels from file '%s': %s", file,
exception->reason);
bfree(data);
data = NULL;
}

free(data);
*format = GS_BGRA;
*cx_out = (uint32_t)cx;
*cy_out = (uint32_t)cy;
DestroyImage(image);

} else if (exception->severity != UndefinedException) {
Expand All @@ -55,5 +59,5 @@ gs_texture_t *gs_texture_create_from_file(const char *file)
DestroyImageInfo(info);
DestroyExceptionInfo(exception);

return tex;
return data;
}
17 changes: 17 additions & 0 deletions libobs/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,23 @@ gs_shader_t *gs_pixelshader_create_from_file(const char *file,
return shader;
}

gs_texture_t *gs_texture_create_from_file(const char *file)
{
enum gs_color_format format;
uint32_t cx;
uint32_t cy;
uint8_t *data = gs_create_texture_file_data(file, &format, &cx, &cy);
gs_texture_t *tex = NULL;

if (data) {
tex = gs_texture_create(cx, cy, format, 1,
(const uint8_t**)&data, 0);
bfree(data);
}

return tex;
}

static inline void assign_sprite_rect(float *start, float *end, float size,
bool flip)
{
Expand Down
2 changes: 2 additions & 0 deletions libobs/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ EXPORT gs_shader_t *gs_pixelshader_create_from_file(const char *file,
char **error_string);

EXPORT gs_texture_t *gs_texture_create_from_file(const char *file);
EXPORT uint8_t *gs_create_texture_file_data(const char *file,
enum gs_color_format *format, uint32_t *cx, uint32_t *cy);

#define GS_FLIP_U (1<<0)
#define GS_FLIP_V (1<<1)
Expand Down

0 comments on commit 9f63554

Please sign in to comment.