Blame filter.curve.inc.c

ade35a
ade35a
ade35a
double calcCurvePointEx(double *curveX, int stepX, double *curveY, int stepY, int count, double x) {
ade35a
  if (count <= 0) return 0;
ade35a
  if (count == 1) return *curveY;
ade35a
  if (x <= curveX[0]) return *curveY;
ade35a
  if (x >= curveX[(count-1)*stepX]) return curveY[(count-1)*stepY];
ade35a
ade35a
  int i = 0;
ade35a
  while(i+1 < count && curveX[(i+1)*stepX] < x)
ade35a
    ++i;
ade35a
ade35a
  int i0 = i > 0 ? i-1 : i;
ade35a
  int i1 = i;
ade35a
  int i2 = i1+1 < count ? i1+1 : i1;
ade35a
  int i3 = i2+1 < count ? i2+1 : i2;
ade35a
ade35a
  double p0[] = { curveX[i0*stepX], curveY[i0*stepY] };
ade35a
  double p1[] = { curveX[i1*stepX], curveY[i1*stepY] };
ade35a
  double p2[] = { curveX[i2*stepX], curveY[i2*stepY] };
ade35a
  double p3[] = { curveX[i3*stepX], curveY[i3*stepY] };
ade35a
ade35a
  double dx = p2[0] - p1[0];
ade35a
  if (dx <= IMG_PRECISION) return p1[1];
ade35a
ade35a
  double a = p1[1];
ade35a
  double b = p2[1];
ade35a
  double ta = p2[0]-p0[0]; ta = ta > IMG_PRECISION ? (p2[1]-p0[1])/ta*dx : 0;
ade35a
  double tb = p3[0]-p1[0]; tb = tb > IMG_PRECISION ? (p3[1]-p1[1])/tb*dx : 0;
ade35a
  double l = (x - p1[0])/dx;
ade35a
ade35a
  return a + (ta + (3*(b-a)-ta-ta-tb + (2*(a-b)+ta+tb)*l)*l)*l;
ade35a
}
ade35a
ade35a
ade35a
double calcCurvePoint(double *curve, int count, double x)
ade35a
  { return calcCurvePointEx(curve, 2, curve+1, 2, count, x); }
ade35a
ade35a
ade35a
double calcTCurvePointEx(
ade35a
  double *curveX, int stepX,
ade35a
  double *curveY, int stepY,
ade35a
  double *curveT, int stepT,
ade35a
  int count, double x)
ade35a
{
ade35a
  if (count <= 0) return 0;
ade35a
  if (count == 1) return *curveY;
ade35a
  if (x <= curveX[0]) return *curveY;
ade35a
  if (x >= curveX[(count-1)*stepX]) return curveY[(count-1)*stepY];
ade35a
ade35a
  int i0 = 0, i1 = 1;
ade35a
  while(i1+1 < count && curveX[i1*stepX] < x)
ade35a
    ++i0, ++i1;
ade35a
ade35a
  double x0 = curveX[i0*stepX];
ade35a
  double x1 = curveX[i1*stepX];
ade35a
  double dx = x1 - x0;
ade35a
  if (dx <= IMG_PRECISION) return curveY[i0*stepY];
ade35a
ade35a
  double a  = curveY[i0*stepY];
ade35a
  double b  = curveY[i1*stepY];
ade35a
  double ta = curveT[i0*stepT]*dx;
ade35a
  double tb = curveT[i1*stepT]*dx;
ade35a
  double l = (x - x0)/dx;
ade35a
ade35a
  return a + (ta + (3*(b-a)-ta-ta-tb + (2*(a-b)+ta+tb)*l)*l)*l;
ade35a
}
ade35a
ade35a
ade35a
double calcTCurvePoint(double *curve, int count, double x)
ade35a
  { return calcTCurvePointEx(curve, 3, curve+1, 3, curve+2, 3, count, x); }
ade35a
ade35a
ade35a
ade35a
void filterRasterizeCurve(Img *img, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double v = calcCurvePoint(curve, count, l0 + (l1 - l0)*x/(img->w - 1));
ade35a
    for(int y = 0; y < img->h; ++y) {
ade35a
      double *p = imgPixel(img, x, y);
ade35a
      p[0] = p[1] = p[2] = p[3] = v;
ade35a
    }
ade35a
  }
ade35a
}
ade35a
ade35a
ade35a
void filterRasterizeCurveCh(Img *img, int ch, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double v = calcCurvePoint(curve, count, l0 + (l1 - l0)*x/(img->w - 1));
ade35a
    for(int y = 0; y < img->h; ++y)
ade35a
      imgPixel(img, x, y)[ch] = v;
ade35a
  }
ade35a
}
ade35a
ade35a
ade35a
void filterRasterizeCurveColor(Img *img, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double l = l0 + (l1 - l0)*x/(img->w - 1);
ade35a
    double c[] = {
ade35a
      calcCurvePointEx(curve, 5, curve+1, 5, count, l),
ade35a
      calcCurvePointEx(curve, 5, curve+2, 5, count, l),
ade35a
      calcCurvePointEx(curve, 5, curve+3, 5, count, l),
ade35a
      calcCurvePointEx(curve, 5, curve+4, 5, count, l) };
ade35a
    for(int y = 0; y < img->h; ++y)
ade35a
      memcpy(imgPixel(img, x, y), c, sizeof(c));
ade35a
  }
ade35a
}
ade35a
ade35a
ade35a
void filterRasterizeTCurve(Img *img, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double v = calcTCurvePoint(curve, count, l0 + (l1 - l0)*x/(img->w - 1));
ade35a
    for(int y = 0; y < img->h; ++y) {
ade35a
      double *p = imgPixel(img, x, y);
ade35a
      p[0] = p[1] = p[2] = p[3] = v;
ade35a
    }
ade35a
  }
ade35a
}
ade35a
ade35a
ade35a
void filterRasterizeTCurveCh(Img *img, int ch, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double v = calcTCurvePoint(curve, count, l0 + (l1 - l0)*x/(img->w - 1));
ade35a
    for(int y = 0; y < img->h; ++y)
ade35a
      imgPixel(img, x, y)[ch] = v;
ade35a
  }
ade35a
}
ade35a
ade35a
ade35a
void filterRasterizeTCurveColor(Img *img, double *curve, int count, double l0, double l1) {
ade35a
  if (!img->data) return;
ade35a
  for(int x = 0; x < img->w; ++x) {
ade35a
    double l = l0 + (l1 - l0)*x/(img->w - 1);
ade35a
    double c[] = {
ade35a
      calcTCurvePointEx(curve, 6, curve+1, 6, curve+2, 6, count, l),
ade35a
      calcTCurvePointEx(curve, 6, curve+1, 6, curve+3, 6, count, l),
ade35a
      calcTCurvePointEx(curve, 6, curve+1, 6, curve+4, 6, count, l),
ade35a
      calcTCurvePointEx(curve, 6, curve+1, 6, curve+5, 6, count, l) };
ade35a
    for(int y = 0; y < img->h; ++y)
ade35a
      memcpy(imgPixel(img, x, y), c, sizeof(c));
ade35a
  }
ade35a
}
ade35a