Ejemplo n.º 1
0
/**
 * \brief Initializes our filter.
 *
 * \param args The arguments passed in from the command line go here. This
 *             filter expects only a single argument telling it where the PGM
 *             or PPM file that describes the logo region is.
 *
 * This sets up our instance variables and parses the arguments to the filter.
 */
static int vf_open(vf_instance_t *vf, char *args)
{
  vf->priv = safe_malloc(sizeof(vf_priv_s));
  vf->uninit = uninit;

  /* Load our filter image. */
  if (args)
    vf->priv->filter = load_pgm(args);
  else
  {
    mp_msg(MSGT_VFILTER, MSGL_ERR, "[vf]remove_logo usage: remove_logo=/path/to/filter_image_file.pgm\n");
    free(vf->priv);
    return 0;
  }

  if (vf->priv->filter == NULL)
  {
    /* Error message was displayed by load_pgm(). */
    free(vf->priv);
    return 0;
  }

  /* Create the scaled down filter image for the chroma planes. */
  convert_mask_to_strength_mask(vf, vf->priv->filter);
  vf->priv->half_size_filter = generate_half_size_image(vf, vf->priv->filter);

  /* Now that we know how many masks we need (the info is in vf), we can generate the masks. */
  initialize_masks(vf);

  /* Calculate our bounding rectangles, which determine in what region the logo resides for faster processing. */
  calculate_bounding_rectangle(&vf->priv->bounding_rectangle_posx1, &vf->priv->bounding_rectangle_posy1,
                               &vf->priv->bounding_rectangle_posx2, &vf->priv->bounding_rectangle_posy2,
                                vf->priv->filter);
  calculate_bounding_rectangle(&vf->priv->bounding_rectangle_half_size_posx1,
                               &vf->priv->bounding_rectangle_half_size_posy1,
                               &vf->priv->bounding_rectangle_half_size_posx2,
                               &vf->priv->bounding_rectangle_half_size_posy2,
                                vf->priv->half_size_filter);

  vf->config=config;
  vf->put_image=put_image;
  vf->query_format=query_format;
  return 1;
}
void load_kernel(char *filename, int w, int h, double ***fwd, double ***bwd)
{
    int i, j, x, y, kw, kh;
    float sum, max;
    unsigned char *input_image;
    int *ip;
    float **psf;
    double *work, **f, **b;
    
    fprintf(stderr, "loading kernel\n");
    if((input_image = load_pgm(filename, &kw, &kh)) == NULL) exit(1);
    fprintf(stderr, "kernel size: %d x %d\n", kw, kh);
    psf = malloc_float2d(kw, kh);
    max = 0;
    for(i = 0; i < kw; i++)
    for(j = 0; j < kh; j++)
    {
        psf[i][j] = (float)input_image[kw * j + i] / 255.0;
        if(max < psf[i][j]) max = psf[i][j];
    }
    free(input_image);
    sum = 0;
    for(i = 0; i < kw; i++)
    for(j = 0; j < kh; j++)
    {
        //if(psf[i][j] < max / 15.0) psf[i][j] = 0; // threshold
        //else sum += psf[i][j];
        sum += psf[i][j];
    }
    for(i = 0; i < kw; i++)
        for(j = 0; j < kh; j++) psf[i][j] /= sum;


    f = malloc_double2d(w, h * 2);
    b = malloc_double2d(w, h * 2);

    for(i = 0; i < w; i++)
    for(j = 0; j < h; j++)
    {
        x = (i + kw/2) % w;
        y = (j + kh/2) % h;
        if(x < kw && y < kh) f[i][j*2] = (double)psf[x][y];
        else f[i][j*2] = 0;
        f[i][j*2+1] = 0;
    }

    for(i = 0; i < w; i++)
    for(j = 0; j < h; j++)
    {
        x = (i + kw/2) % w;
        y = (j + kh/2) % h;
        if(x < kw && y < kh) b[i][j*2] = (double)psf[kw-1-x][kh-1-y];
        else b[i][j*2] = 0;
        b[i][j*2+1] = 0;
    }

    work = malloc_double1d((w > h ? w : h) / 2);
    ip = malloc_int1d(2 + (int)sqrt((w > h ? w : h) + 0.5));
    ip[0] = 0;
    cdft2d(w, h * 2, -1, f, NULL, ip, work);
    cdft2d(w, h * 2, -1, b, NULL, ip, work);

    free_float2d(psf);
    free_double1d(work);
    free_int1d(ip);
    *fwd = f;
    *bwd = b;
}