Example #1
0
inline
void cvtcolor( View_Src src
               , View_Dst dst
             )
{
    if (  std::max( src.dimensions().x, dst.dimensions().x ) == 0
            && std::max( src.dimensions().y, dst.dimensions().y ) == 0
       )
    {
        throw std::exception( "Image doesn't have dimensions ( empty image )." );
    }



    if ( src.dimensions() != dst.dimensions() )
    {
        throw std::exception( "Image's dimensions don't match." );
    }

    // There is some special code for dealing with bit_aligned images.
    // OpenCV seems only to support rgb|bgr555 or rgb|bgr565 images.
    typedef boost::mpl::or_< is_bit_aligned< View_Src::value_type >::type
    , is_bit_aligned< View_Dst::value_type >::type
    >::type is_bit_aligned_t;

    cvtcolor( src
              , dst
              , is_bit_aligned_t()
            );
}
Example #2
0
/**
  * @brief applies the Scharr edge transform
  * @param[in] inimage [const cv::mat&] input image in cv_8uc1 format
  * @param[out] outimage [cv::mat*] the processed image in cv_8uc1 format
  * @return void
  */
void applyScharr(const cv::mat& inimage, cv::mat* outimage)
{   
    //!< appropriate values for scale, delta and ddepth
    int scale = 1;
    int delta = 0; //!< the value for the non-edges
    int ddepth = cv_16s;

    cv::mat edges;
    inimage.copyto(edges);
    cvtcolor( edges, edges, cv_rgb2gray );
    
    //!< generate grad_x and grad_y
    cv::mat grad_x, grad_y;
    cv::mat abs_grad_x, abs_grad_y;

    //!< gradient x
    cv::scharr(edges, grad_x, ddepth, 1, 0, scale, delta, cv::border_default);
    cv::convertscaleabs(grad_x, abs_grad_x);

    //!< gradient y
    cv::scharr(edges, grad_y, ddepth, 0, 1, scale, delta, cv::border_default);
    cv::convertscaleabs(grad_y, abs_grad_y);

    // total gradient (approximate)
    cv::mat grad_g;
    cv::addweighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad_g);

    *outimage = grad_g;
}
static void
picdebug(void)			/* put out debugging picture */
{
	static COLOR	blkcol = BLKCOLOR;
	COLOR	*scan;
	int	y, i;
	register int	x, rg;

	if (fseek(stdin, 0L, 0) == EOF) {
		fprintf(stderr, "%s: cannot seek on input picture\n", progname);
		exit(1);
	}
	getheader(stdin, NULL, NULL);		/* skip input header */
	fgetresolu(&xmax, &ymax, stdin);
						/* allocate scanline */
	scan = (COLOR *)malloc(xmax*sizeof(COLOR));
	if (scan == NULL) {
		perror(progname);
		exit(1);
	}
						/* finish debug header */
	fputformat(COLRFMT, debugfp);
	putc('\n', debugfp);
	fprtresolu(xmax, ymax, debugfp);
						/* write debug picture */
	for (y = ymax-1; y >= 0; y--) {
		if (freadscan(scan, xmax, stdin) < 0) {
			fprintf(stderr, "%s: error rereading input picture\n",
					progname);
			exit(1);
		}
		for (x = 0; x < xmax; x++) {
			rg = chartndx(x, y, &i);
			if (rg == RG_CENT) {
				if (!(1L<<i & gmtflags) || (x+y)&07) {
					copycolor(scan[x], mbRGB[i]);
					clipgamut(scan[x], bright(scan[x]),
						CGAMUT, colmin, colmax);
				} else
					copycolor(scan[x], blkcol);
			} else if (rg == RG_CORR)
				cvtcolor(scan[x], scan[x]);
			else if (rg != RG_ORIG)
				copycolor(scan[x], blkcol);
		}
		if (fwritescan(scan, xmax, debugfp) < 0) {
			fprintf(stderr, "%s: error writing debugging picture\n",
					progname);
			exit(1);
		}
	}
						/* clean up */
	fclose(debugfp);
	free((void *)scan);
}
Example #4
0
char *
material(void)			/* get (and print) current material */
{
	char	*mname = "mat";
	COLOR	radrgb, c2;
	double	d;

	if (c_cmname != NULL)
		mname = c_cmname;
	if (!c_cmaterial->clock)
		return(mname);		/* already current */
				/* else update output */
	c_cmaterial->clock = 0;
	if (c_cmaterial->ed > .1) {	/* emitter */
		cvtcolor(radrgb, &c_cmaterial->ed_c,
				emult*c_cmaterial->ed/(PI*WHTEFFICACY));
		if (glowdist < FHUGE) {		/* do a glow */
			fprintf(matfp, "\nvoid glow %s\n0\n0\n", mname);
			fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
					colval(radrgb,GRN),
					colval(radrgb,BLU), glowdist);
		} else {
			fprintf(matfp, "\nvoid light %s\n0\n0\n", mname);
			fprintf(matfp, "3 %f %f %f\n", colval(radrgb,RED),
					colval(radrgb,GRN),
					colval(radrgb,BLU));
		}
		return(mname);
	}
	d = c_cmaterial->rd + c_cmaterial->td +
			c_cmaterial->rs + c_cmaterial->ts;
	if ((d < 0.) | (d > 1.))
		return(NULL);
					/* check for glass/dielectric */
	if (c_cmaterial->nr > 1.1 &&
			c_cmaterial->ts > .25 && c_cmaterial->rs <= .125 &&
			c_cmaterial->td <= .01 && c_cmaterial->rd <= .01 &&
			c_cmaterial->rs_a <= .01 && c_cmaterial->ts_a <= .01) {
		cvtcolor(radrgb, &c_cmaterial->ts_c,
				c_cmaterial->ts + c_cmaterial->rs);
		if (c_cmaterial->sided) {		/* dielectric */
			colval(radrgb,RED) = pow(colval(radrgb,RED),
							1./C_1SIDEDTHICK);
			colval(radrgb,GRN) = pow(colval(radrgb,GRN),
							1./C_1SIDEDTHICK);
			colval(radrgb,BLU) = pow(colval(radrgb,BLU),
							1./C_1SIDEDTHICK);
			fprintf(matfp, "\nvoid dielectric %s\n0\n0\n", mname);
			fprintf(matfp, "5 %g %g %g %f 0\n", colval(radrgb,RED),
					colval(radrgb,GRN), colval(radrgb,BLU),
					c_cmaterial->nr);
			return(mname);
		}
							/* glass */
		fprintf(matfp, "\nvoid glass %s\n0\n0\n", mname);
		fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
				colval(radrgb,GRN), colval(radrgb,BLU),
				c_cmaterial->nr);
		return(mname);
		}
					/* check for trans */
	if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) {
		double	ts, a5, a6;

		if (c_cmaterial->sided) {
			ts = sqrt(c_cmaterial->ts);	/* approximate */
			a5 = .5;
		} else {
			ts = c_cmaterial->ts;
			a5 = 1.;
		}
						/* average colors */
		d = c_cmaterial->rd + c_cmaterial->td + ts;
		cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d);
		cvtcolor(c2, &c_cmaterial->td_c, c_cmaterial->td/d);
		addcolor(radrgb, c2);
		cvtcolor(c2, &c_cmaterial->ts_c, ts/d);
		addcolor(radrgb, c2);
		if (c_cmaterial->rs + ts > .0001)
			a5 = (c_cmaterial->rs*c_cmaterial->rs_a +
					ts*a5*c_cmaterial->ts_a) /
					(c_cmaterial->rs + ts);
		a6 = (c_cmaterial->td + ts) /
				(c_cmaterial->rd + c_cmaterial->td + ts);
		if (a6 < .999)
			d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6);
		else
			d = c_cmaterial->td + ts;
		scalecolor(radrgb, d);
		fprintf(matfp, "\nvoid trans %s\n0\n0\n", mname);
		fprintf(matfp, "7 %f %f %f\n", colval(radrgb,RED),
				colval(radrgb,GRN), colval(radrgb,BLU));
		fprintf(matfp, "\t%f %f %f %f\n", c_cmaterial->rs, a5, a6,
				ts/(ts + c_cmaterial->td));
		return(mname);
	}
					/* check for plastic */
	if (c_cmaterial->rs < .1) {
		cvtcolor(radrgb, &c_cmaterial->rd_c,
					c_cmaterial->rd/(1.-c_cmaterial->rs));
		fprintf(matfp, "\nvoid plastic %s\n0\n0\n", mname);
		fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
				colval(radrgb,GRN), colval(radrgb,BLU),
				c_cmaterial->rs, c_cmaterial->rs_a);
		return(mname);
	}
					/* else it's metal */
						/* average colors */
	cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd);
	cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs);
	addcolor(radrgb, c2);
	fprintf(matfp, "\nvoid metal %s\n0\n0\n", mname);
	fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
			colval(radrgb,GRN), colval(radrgb,BLU),
			c_cmaterial->rs/(c_cmaterial->rd + c_cmaterial->rs),
			c_cmaterial->rs_a);
	return(mname);
}
static void
compute(void)			/* compute color mapping */
{
	COLOR	clrin[24], clrout[24];
	long	cflags;
	COLOR	ctmp;
	register int	i, n;
					/* did we get what we need? */
	if ((inpflags & REQFLGS) != REQFLGS) {
		fprintf(stderr, "%s: missing required input colors\n",
				progname);
		exit(1);
	}
					/* compute piecewise luminance curve */
	for (i = 0; i < NMBNEU; i++) {
		copycolor(bramp[i][0], inpRGB[mbneu[i]]);
		for (n = i ? 3 : 0; n--; )
			if (colval(bramp[i][0],n) <=
					colval(bramp[i-1][0],n)+1e-7) {
				fprintf(stderr,
		"%s: non-increasing neutral patch\n", progname);
				exit(1);
			}
		copycolor(bramp[i][1], mbRGB[mbneu[i]]);
	}
					/* compute color space gamut */
	if (scanning) {
		copycolor(colmin, cblack);
		copycolor(colmax, cwhite);
		scalecolor(colmax, irrad);
	} else
		for (i = 0; i < 3; i++) {
			colval(colmin,i) = colval(bramp[0][0],i) -
				colval(bramp[0][1],i) *
				(colval(bramp[1][0],i)-colval(bramp[0][0],i)) /
				(colval(bramp[1][1],i)-colval(bramp[1][0],i));
			colval(colmax,i) = colval(bramp[NMBNEU-2][0],i) +
				(1.-colval(bramp[NMBNEU-2][1],i)) *
				(colval(bramp[NMBNEU-1][0],i) -
					colval(bramp[NMBNEU-2][0],i)) /
				(colval(bramp[NMBNEU-1][1],i) -
					colval(bramp[NMBNEU-2][1],i));
		}
					/* compute color mapping */
	do {
		cflags = inpflags & ~gmtflags;
		n = 0;				/* compute transform matrix */
		for (i = 0; i < 24; i++)
			if (cflags & 1L<<i) {
				bresp(clrin[n], inpRGB[i]);
				copycolor(clrout[n], mbRGB[i]);
				n++;
			}
		compsoln(clrin, clrout, n);
		if (irrad > 0.99 && irrad < 1.01)	/* check gamut */
			for (i = 0; i < 24; i++)
				if (cflags & 1L<<i && cvtcolor(ctmp, mbRGB[i]))
					gmtflags |= 1L<<i;
	} while (cflags & gmtflags);
	if (gmtflags & MODFLGS)
		fprintf(stderr,
		"%s: warning - some moderate colors are out of gamut\n",
				progname);
}