From 8925f10962d13d9c3cbcb2e198b892177366c09f Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 14 Sep 2019 22:06:00 +0200 Subject: image_writer: move convert_image() to player/screenshot.c --- player/screenshot.c | 37 +++++++++++++++++++++++++++++++++++++ player/screenshot.h | 8 ++++++++ video/image_loader.c | 2 +- video/image_writer.c | 6 +++--- video/image_writer.h | 6 ------ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/player/screenshot.c b/player/screenshot.c index e24ca051f1..df8da9697c 100644 --- a/player/screenshot.c +++ b/player/screenshot.c @@ -38,6 +38,7 @@ #include "video/mp_image_pool.h" #include "video/out/vo.h" #include "video/image_writer.h" +#include "video/sws_utils.h" #include "sub/osd.h" #include "video/csputils.h" @@ -387,6 +388,42 @@ static struct mp_image *screenshot_get(struct MPContext *mpctx, int mode, return image; } +struct mp_image *convert_image(struct mp_image *image, int destfmt, + struct mp_log *log) +{ + int d_w, d_h; + mp_image_params_get_dsize(&image->params, &d_w, &d_h); + + struct mp_image_params p = { + .imgfmt = destfmt, + .w = d_w, + .h = d_h, + .p_w = 1, + .p_h = 1, + }; + mp_image_params_guess_csp(&p); + + if (mp_image_params_equal(&p, &image->params)) + return mp_image_new_ref(image); + + struct mp_image *dst = mp_image_alloc(p.imgfmt, p.w, p.h); + if (!dst) { + mp_err(log, "Out of memory.\n"); + return NULL; + } + mp_image_copy_attributes(dst, image); + + dst->params = p; + + if (mp_image_swscale(dst, image, mp_sws_hq_flags) < 0) { + mp_err(log, "Error when converting image.\n"); + talloc_free(dst); + return NULL; + } + + return dst; +} + // mode is the same as in screenshot_get() static struct mp_image *screenshot_get_rgb(struct MPContext *mpctx, int mode) { diff --git a/player/screenshot.h b/player/screenshot.h index f479fca3f8..1ccee790d6 100644 --- a/player/screenshot.h +++ b/player/screenshot.h @@ -21,6 +21,8 @@ #include struct MPContext; +struct mp_image; +struct mp_log; // One time initialization at program start. void screenshot_init(struct MPContext *mpctx); @@ -28,6 +30,12 @@ void screenshot_init(struct MPContext *mpctx); // Called by the playback core code when a new frame is displayed. void screenshot_flip(struct MPContext *mpctx); +/* Return the image converted to the given format. If the pixel aspect ratio is + * not 1:1, the image is scaled as well. Returns NULL on failure. + */ +struct mp_image *convert_image(struct mp_image *image, int destfmt, + struct mp_log *log); + // Handlers for the user-facing commands. void cmd_screenshot(void *p); void cmd_screenshot_to_file(void *p); diff --git a/video/image_loader.c b/video/image_loader.c index 9efc8b7310..77deea0f8d 100644 --- a/video/image_loader.c +++ b/video/image_loader.c @@ -2,7 +2,7 @@ #include "common/common.h" #include "mp_image.h" -#include "image_writer.h" +#include "player/screenshot.h" #include "image_loader.h" diff --git a/video/image_writer.c b/video/image_writer.c index 6a092aa17f..347adb350a 100644 --- a/video/image_writer.c +++ b/video/image_writer.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include @@ -27,6 +26,7 @@ #include "config.h" #if HAVE_JPEG +#include #include #endif @@ -291,8 +291,8 @@ int image_writer_format_from_ext(const char *ext) return 0; } -struct mp_image *convert_image(struct mp_image *image, int destfmt, - struct mp_log *log) +static struct mp_image *convert_image(struct mp_image *image, int destfmt, + struct mp_log *log) { int d_w, d_h; mp_image_params_get_dsize(&image->params, &d_w, &d_h); diff --git a/video/image_writer.h b/video/image_writer.h index 98cb95a898..c9932e93ee 100644 --- a/video/image_writer.h +++ b/video/image_writer.h @@ -61,11 +61,5 @@ int image_writer_format_from_ext(const char *ext); bool write_image(struct mp_image *image, const struct image_writer_opts *opts, const char *filename, struct mp_log *log); -/* Return the image converted to the given format. If the pixel aspect ratio is - * not 1:1, the image is scaled as well. Returns NULL on failure. - */ -struct mp_image *convert_image(struct mp_image *image, int destfmt, - struct mp_log *log); - // Debugging helper. void dump_png(struct mp_image *image, const char *filename, struct mp_log *log); -- cgit v1.2.3