[core] add stb_image to read more supported picture formats

This commit is contained in:
Seven Du 2019-03-06 09:48:20 +08:00 committed by Andrey Volk
parent 9d2cb8b190
commit 58d5442dc5
6 changed files with 7619 additions and 0 deletions

BIN
images/cluecon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
images/cluecon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

7547
libs/stb/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -396,6 +396,9 @@ SWITCH_DECLARE(switch_status_t) switch_png_open(switch_png_t **pngP, const char
SWITCH_DECLARE(void) switch_png_free(switch_png_t **pngP);
SWITCH_DECLARE(switch_status_t) switch_img_data_url_png(switch_image_t *img, char **urlP);
/*!\brief Read an image file to switch_image_t */
SWITCH_DECLARE(switch_image_t *) switch_img_read_from_file(const char *file_name, switch_img_fmt_t img_fmt);
/*!\brief put a small img over a big IMG at position x,y, with alpha transparency
*
* Both IMG and img must be non-NULL

View File

@ -48,6 +48,9 @@
#include <gd.h>
#endif
#define STB_IMAGE_IMPLEMENTATION
#include "../libs/stb/stb_image.h"
#ifdef SWITCH_HAVE_YUV
static inline void switch_img_get_yuv_pixel(switch_image_t *img, switch_yuv_color_t *yuv, int x, int y);
#endif
@ -3100,6 +3103,51 @@ SWITCH_DECLARE(switch_status_t) switch_img_data_url_png(switch_image_t *img, cha
#endif
SWITCH_DECLARE(switch_image_t *) switch_img_read_from_file(const char* file_name, switch_img_fmt_t img_fmt)
{
int width = 0, height = 0, channels = 8;
int comp = STBI_rgb;
unsigned char *data = NULL;
if (img_fmt == SWITCH_IMG_FMT_I420) {
comp = STBI_rgb;
} else if (img_fmt == SWITCH_IMG_FMT_ARGB) {
comp = STBI_rgb_alpha;
} else {
return NULL;
}
data = stbi_load(file_name, &width, &height, &channels, comp);
// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%dx%d channels=%d\n", width, height, channels);
if (data && width > 0 && height > 0) {
switch_image_t *img = switch_img_alloc(NULL, img_fmt, width, height, 1);
switch_assert(img);
if (img_fmt == SWITCH_IMG_FMT_I420) {
RAWToI420(data, width * 3,
img->planes[SWITCH_PLANE_Y], img->stride[SWITCH_PLANE_Y],
img->planes[SWITCH_PLANE_U], img->stride[SWITCH_PLANE_U],
img->planes[SWITCH_PLANE_V], img->stride[SWITCH_PLANE_V],
width, height);
} else if (img_fmt == SWITCH_IMG_FMT_ARGB) {
#if SWITCH_BYTE_ORDER == __BIG_ENDIAN
RGBAToARGB(data, width * 4, img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED], width, height);
#else
ABGRToARGB(data, width * 4, img->planes[SWITCH_PLANE_PACKED], img->stride[SWITCH_PLANE_PACKED], width, height);
#endif
}
stbi_image_free(data);
return img;
} else if (data) {
stbi_image_free(data);
}
return NULL;
}
SWITCH_DECLARE(switch_status_t) switch_img_letterbox(switch_image_t *img, switch_image_t **imgP, int width, int height, const char *color)
{
int img_w = 0, img_h = 0;

View File

@ -199,6 +199,27 @@ FST_CORE_BEGIN("./conf")
switch_img_free(&timg_small);
}
FST_TEST_END()
FST_TEST_BEGIN(read_from_file)
{
switch_image_t *img;
img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_I420);
fst_requires(img);
switch_img_write_png(img, "cluecon-rgb.png");
switch_img_free(&img);
img = switch_img_read_from_file("../../images/cluecon.png", SWITCH_IMG_FMT_ARGB);
fst_requires(img);
switch_img_write_png(img, "cluecon-argb.png");
switch_img_free(&img);
img = switch_img_read_from_file("../../images/cluecon.jpg", SWITCH_IMG_FMT_I420);
fst_requires(img);
switch_img_write_png(img, "cluecon-jpg.png");
switch_img_free(&img);
}
FST_TEST_END()
}
FST_SUITE_END()
}