Blob Blame Raw


void filterNoise(Img *img) {
  if (!img->data) return;
  for(double *p = img->data, *e = p + 4*img->w*img->h; p < e; p += 4) {
    p[0] = randomFloat();
    p[1] = randomFloat();
    p[2] = randomFloat();
    p[3] = randomFloat();
  }
}


void filterMulNoise(Img *img, int w, int h, double vmin, double vmax) {
  if (!img->data || w <= 0 || h <= 0) return;
  Img imgC = {};
  imgInit(&imgC, w, h);
  filterNoise(&imgC);
  for(int y = 0; y < img->h; ++y) {
    for(int x = 0; x < img->w; ++x) {
      double pn[4];
      imgCubic(&imgC, (double)x/img->w*w, (double)y/img->h*h, pn);
      double *p = imgPixel(img, x, y);
      p[0] *= vmin + (vmax - vmin)*pn[0];
      p[1] *= vmin + (vmax - vmin)*pn[1];
      p[2] *= vmin + (vmax - vmin)*pn[2];
      p[3] *= vmin + (vmax - vmin)*pn[3];
    }
  }
  imgDestroy(&imgC);
}


void filterMulNoiseGray(Img *img, int w, int h, double vmin, double vmax) {
  if (!img->data || w <= 0 || h <= 0) return;
  Img imgC = {};
  imgInit(&imgC, w, h);
  filterNoise(&imgC);
  for(int y = 0; y < img->h; ++y) {
    for(int x = 0; x < img->w; ++x) {
      double pn = vmin + (vmax - vmin)*imgCubicCh(&imgC, 0, (double)x/img->w*w, (double)y/img->h*h);
      double *p = imgPixel(img, x, y);
      p[0] *= pn;
      p[1] *= pn;
      p[2] *= pn;
      //p[3] *= pn;
    }
  }
  imgDestroy(&imgC);
}