Beispiel #1
0
static int load_mask(uint8_t **mask, int *w, int *h,
                     const char *filename, void *log_ctx)
{
    int ret;
    enum PixelFormat pix_fmt;
    uint8_t *src_data[4], *gray_data[4];
    int src_linesize[4], gray_linesize[4];

    /* load image from file */
    if ((ret = ff_load_image(src_data, src_linesize, w, h, &pix_fmt, filename, log_ctx)) < 0)
        return ret;

    /* convert the image to GRAY8 */
    if ((ret = ff_scale_image(gray_data, gray_linesize, *w, *h, PIX_FMT_GRAY8,
                              src_data, src_linesize, *w, *h, pix_fmt,
                              log_ctx)) < 0)
        goto end;

    /* copy mask to a newly allocated array */
    *mask = av_malloc(*w * *h);
    if (!*mask)
        ret = AVERROR(ENOMEM);
    av_image_copy_plane(*mask, *w, gray_data[0], gray_linesize[0], *w, *h);

end:
    av_free(src_data[0]);
    av_free(gray_data[0]);
    return ret;
}
Beispiel #2
0
int load_image_into_frame(AVFrame *frame, const char *filename)
{
  int retval = -1, res;
  static struct SwsContext *sws_ctx;
  uint8_t *image_data[4];
  int linesize[4];
  int source_width, source_height;
  enum PixelFormat source_fmt;

  res = ff_load_image(image_data, linesize, &source_width, &source_height, &source_fmt, filename, NULL);
  check(res >= 0, "failed to load image");

  if (source_fmt != frame->format) {
    sws_ctx = sws_getContext(source_width, source_height, source_fmt,
        frame->width, frame->height, frame->format,
        sws_flags, NULL, NULL, NULL);
    check(sws_ctx, "unable to initialize scaling context");

    sws_scale(sws_ctx,
        (const uint8_t * const *)image_data, linesize,
        0, frame->height, frame->data, frame->linesize);
  }

  retval = 0;
error:
  av_freep(&image_data[0]);
  sws_freeContext(sws_ctx);
  return retval;
}