image_writer: factor image conversion into a separate function

Needed for a later commit.
This commit is contained in:
wm4 2015-04-20 23:04:34 +02:00
parent 07678423fb
commit 2469cb5d1f
2 changed files with 37 additions and 25 deletions

View File

@ -279,15 +279,38 @@ const char *image_writer_file_ext(const struct image_writer_opts *opts)
return get_writer(opts)->file_ext; return get_writer(opts)->file_ext;
} }
bool write_image(struct mp_image *image, const struct image_writer_opts *opts, struct mp_image *convert_image(struct mp_image *image, int destfmt,
const char *filename, struct mp_log *log) struct mp_log *log)
{ {
struct mp_image *allocated_image = NULL;
struct image_writer_opts defs = image_writer_opts_defaults;
int d_w = image->params.d_w; int d_w = image->params.d_w;
int d_h = image->params.d_h; int d_h = image->params.d_h;
bool is_anamorphic = image->w != d_w || image->h != d_h; bool is_anamorphic = image->w != d_w || image->h != d_h;
// Caveat: no colorspace/levels conversion done if pixel formats equal
// it's unclear what colorspace/levels the target wants
if (image->imgfmt == destfmt && !is_anamorphic)
return mp_image_new_ref(image);
struct mp_image *dst = mp_image_alloc(destfmt, d_w, d_h);
if (!dst) {
mp_err(log, "Out of memory.\n");
return NULL;
}
mp_image_copy_attributes(dst, image);
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;
}
bool write_image(struct mp_image *image, const struct image_writer_opts *opts,
const char *filename, struct mp_log *log)
{
struct image_writer_opts defs = image_writer_opts_defaults;
if (!opts) if (!opts)
opts = &defs; opts = &defs;
@ -295,25 +318,9 @@ bool write_image(struct mp_image *image, const struct image_writer_opts *opts,
struct image_writer_ctx ctx = { log, opts, writer, image->fmt }; struct image_writer_ctx ctx = { log, opts, writer, image->fmt };
int destfmt = get_target_format(&ctx, image->imgfmt); int destfmt = get_target_format(&ctx, image->imgfmt);
// Caveat: no colorspace/levels conversion done if pixel formats equal struct mp_image *dst = convert_image(image, destfmt, log);
// it's unclear what colorspace/levels the target wants if (!dst)
if (image->imgfmt != destfmt || is_anamorphic) { return false;
struct mp_image *dst = mp_image_alloc(destfmt, d_w, d_h);
if (!dst) {
mp_err(log, "Out of memory.\n");
return 0;
}
mp_image_copy_attributes(dst, image);
if (mp_image_swscale(dst, image, mp_sws_hq_flags) < 0) {
mp_err(log, "Error when converting image.\n");
talloc_free(dst);
return 0;
}
allocated_image = dst;
image = dst;
}
FILE *fp = fopen(filename, "wb"); FILE *fp = fopen(filename, "wb");
bool success = false; bool success = false;
@ -326,8 +333,7 @@ bool write_image(struct mp_image *image, const struct image_writer_opts *opts,
mp_err(log, "Error writing file '%s'!\n", filename); mp_err(log, "Error writing file '%s'!\n", filename);
} }
talloc_free(allocated_image); talloc_free(dst);
return success; return success;
} }

View File

@ -51,5 +51,11 @@ const char *image_writer_file_ext(const struct image_writer_opts *opts);
bool write_image(struct mp_image *image, const struct image_writer_opts *opts, bool write_image(struct mp_image *image, const struct image_writer_opts *opts,
const char *filename, struct mp_log *log); 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. // Debugging helper.
void dump_png(struct mp_image *image, const char *filename, struct mp_log *log); void dump_png(struct mp_image *image, const char *filename, struct mp_log *log);