示例#1
0
static void
AllocateColors ()
{
    double	angle;
    double	step;
    XcmsColor	cms_color;
    int		i;
    XColor	hard, exact;

    XAllocNamedColor (display, cmap, "black", &hard, &exact);
    black_pixel = hard.pixel;
    step = 360.0 / NUM_COLORS;
    for (i = 0; i < NUM_COLORS; i++) {
	angle = i * step;
	cms_color.spec.TekHVC.H = angle;
	cms_color.spec.TekHVC.V = 75.0;
	cms_color.spec.TekHVC.C = 75.0;
	cms_color.format = XcmsTekHVCFormat;
	XcmsAllocColor (display, cmap, &cms_color, XcmsRGBFormat);
	pixels[i] = cms_color.pixel;
    }
}
示例#2
0
int XAbstractGui::SetColors(Display *dpy)
{
    int screen;
    XcmsColor xcolor;
    XcmsColor exact;
    int i, scale;
    int rw_color;   /* is it a read/write color ?*/
    int visualClass;
    unsigned long pixels[1 << COLOR_PLANES];

    screen = DefaultScreen(dpy);
    visual = GetBestVisual(dpy, &visualClass, &depth);

    if (!visual)
    {
        fprintf(stderr, "Unable to find an appropriate visual\n");
        return 0;
    }

    rw_color = (visualClass == DirectColor || visualClass == PseudoColor ||
                visualClass == GrayScale);
    cmap = DefaultColormap(dpy, screen);

    if (rw_color)
    {
        /* on visuals with read/write color cells
           first allocate them */
        if (!XAllocColorCells(dpy, cmap, False, nullptr, 0, pixels,
                              1 << COLOR_PLANES))
        {
            /* try again with a own colormap */
            cmap = XCreateColormap(dpy,
                                   RootWindow(dpy, screen),
                                   visual, AllocNone);
            own_cmap = 1;

            if (!XAllocColorCells(dpy, cmap, False, nullptr, 0,
                                  pixels, 1 << COLOR_PLANES))
            {
                fprintf(stderr, "Unable to allocate %d colors\n",
                        1 << COLOR_PLANES);
                return 0; /* failed even with own colormap */
            }
        }
    }

    if (!XcmsLookupColor(dpy, cmap, color.c_str(), &xcolor, &exact,
                         XcmsRGBiFormat))
    {
        exact.spec.RGBi.blue  = 1.0;
        exact.spec.RGBi.red   = 1.0;
        exact.spec.RGBi.green = 1.0;
    }

    xcolor.format = XcmsRGBiFormat;

    for (i = 0; i < (1 << COLOR_PLANES); i++)
    {
        if (withColorScale)
        {
            // DEPENDANCIES:
            // the color plane masks used here depend on
            // the same masks used in CopyToZPixmap
            scale  = i & 0x20 ? 2 : 0;
            scale |= i & 0x04 ? 1 : 0;
            xcolor.spec.RGBi.green = 1.0 * scale / 3;
            scale  = i & 0x10 ? 2 : 0;
            scale |= i & 0x02 ? 1 : 0;
            xcolor.spec.RGBi.red   = 1.0 * scale / 3;
            scale  = i & 0x08 ? 2 : 0;
            scale |= i & 0x01 ? 1 : 0;
            xcolor.spec.RGBi.blue  = 1.0 * scale / 3;
        }
        else
        {
            xcolor.spec.RGBi.blue  =
                exact.spec.RGBi.blue  * i / (1 << COLOR_PLANES);
            xcolor.spec.RGBi.red   =
                exact.spec.RGBi.red   * i / (1 << COLOR_PLANES);
            xcolor.spec.RGBi.green =
                exact.spec.RGBi.green * i / (1 << COLOR_PLANES);
        }

        if (rw_color)
        {
            xcolor.pixel = pixels[i];

            if (XcmsStoreColor(dpy, cmap,
                               &xcolor) == XcmsFailure)
            {
                fprintf(stderr, "Error storing a color\n");
                return 0;
            }
        }
        else
        {
            if (XcmsAllocColor(dpy, cmap,
                               &xcolor, XcmsRGBiFormat) == XcmsFailure)
            {
                fprintf(stderr, "Error allocating a color\n");
                return 0;
            }
        }

        pens[i] = xcolor.pixel;
    }

    //gcVal.plane_mask = 0xFFFFFFFF;

    return 1;
}
int main (int argc, char* argv[])
{
	/* Determine number of iterations. */
	if (2 != argc)
	{
		printf("usage: %s <num-iterations>\n", argv[0]);
		exit(1);
	}
	int numIterations = atoi(argv[1]);

	int returnCode = 0;
	Display* xDisplay = NULL;
	Window xWindow = 0;
	Picture xPictureWindow = 0;
	Pixmap xPixmapMain = 0;
	Picture xPictureMain = 0;
	Pixmap xPixmapOverlay = 0;
	Picture xPictureOverlay = 0;
	Pixmap xPixmapMask = 0;
	Picture xPictureMask = 0;
	GC gcMask = 0;

	/* Access X display. */
	if (NULL == (xDisplay = XOpenDisplay(NULL)))
	{
		printf("XOpenDisplay(NULL) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Access info about the screen. */
	Screen* xScreen = XDefaultScreenOfDisplay(xDisplay);
	GC gc = XDefaultGCOfScreen(xScreen);
	Colormap xColormap = XDefaultColormapOfScreen(xScreen);

	/* Create main X window */
	xWindow = XCreateSimpleWindow(
			xDisplay,
			RootWindow(xDisplay, 0),
			0, 0,
			IMAGE_WIDTH*2,
			IMAGE_HEIGHT*2,
			0,
			BlackPixel(xDisplay, 0),
			BlackPixel(xDisplay, 0));
	if (0 == xWindow)
	{
		printf("XCreateSimpleWindow failed\n");
		returnCode = 1;
		goto error;
	}
	XMapWindow(xDisplay, xWindow);
	XSync(xDisplay, False);

	/* Get the attributes associated with the main window. */
	XWindowAttributes xWindowAttr;
	if (!XGetWindowAttributes(xDisplay, xWindow, &xWindowAttr))
	{
		printf("XGetWindowAttributes failed\n");
		returnCode = 1;
		goto error;
	}

	/* Find the X render picture format associated with the visual */
	/* for the main window */
	XRenderPictFormat* xRenderPictFormatWindow =
		XRenderFindVisualFormat(xDisplay, xWindowAttr.visual);
	if (NULL == xRenderPictFormatWindow)
	{
		printf("XRenderFindVisualFormat failed\n");
		returnCode = 1;
		goto error;
	}

	/* Find the X render picture format associated with 8 bit alpha. */
	XRenderPictFormat xRenderPictFormatTemplate;
	xRenderPictFormatTemplate.depth = 8;
	xRenderPictFormatTemplate.type = PictTypeDirect;
	xRenderPictFormatTemplate.direct.alphaMask = 0x0FF;
	unsigned long xRenderPictFormatTemplateMask =
			PictFormatDepth | PictFormatType | PictFormatAlphaMask;
	XRenderPictFormat* xRenderPictFormatMask =
		XRenderFindFormat(
			xDisplay,
			xRenderPictFormatTemplateMask,
			&xRenderPictFormatTemplate,
			0);
	if (NULL == xRenderPictFormatMask)
	{
		printf("XRenderFindFormat failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture associated with the screen. */
	/* Having the same visual format as the window. */
	xPictureWindow = XRenderCreatePicture(
				xDisplay,
				xWindow,
				xRenderPictFormatWindow,
				0,
				NULL);
	if (0 == xPictureWindow)
	{
		printf("XRenderCreatePicture (window) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create backing pixmap for the main window. */
	xPixmapMain = XCreatePixmap(
			xDisplay,
			xWindow,
			xWindowAttr.width,
			xWindowAttr.height,
			xWindowAttr.depth);
	if (0 == xPixmapMain)
	{
		printf("XCreatePixmap (main) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture associated with the backing pixmap. */
	/* Having the same visual format as the window. */
	xPictureMain = XRenderCreatePicture(
			xDisplay,
			xPixmapMain,
			xRenderPictFormatWindow,
			0,
			NULL);
	if (0 == xPictureMain)
	{
		printf("XRenderCreatePicture (main) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Draw concentric rectangles of different gray. */
	unsigned i;
	for (i = 0; i < 256; ++i)
	{
		float fGray = i / 255.0;

		/* Find the color gray. */
		XcmsColor xColorGray;
		xColorGray.spec.RGBi.red = fGray;
		xColorGray.spec.RGBi.green = fGray;
		xColorGray.spec.RGBi.blue = fGray;
		xColorGray.format = XcmsRGBiFormat;
		if (0 == XcmsAllocColor(
				xDisplay,
				xColormap,
				&xColorGray,
				XcmsRGBFormat))
		{
			printf("XcmsAllocColor failed\n");
			returnCode = 1;
			goto error;
		}

		/* Change the drawing color for the main window. */
		XSetForeground(xDisplay, gc, xColorGray.pixel);

		XDrawRectangle(
			xDisplay,
			xPixmapMain,
			gc,
			i, i,
			(IMAGE_WIDTH - i) * 2 - 1,
			(IMAGE_HEIGHT - i) * 2 - 1);
	}
	XRenderComposite(
		xDisplay,
		PictOpSrc,
		xPictureMain,	/* src */
		0,		/* mask */
		xPictureWindow,	/* dst */
		0, 0,		/* src (x,y) */
		0, 0,		/* mask (x,y) */
		0, 0,		/* dst (x,y) */
		xWindowAttr.width,
		xWindowAttr.height);
	XSync(xDisplay, False);

	/* Create pixmap for the overlay content. */
	xPixmapOverlay = XCreatePixmap(
				xDisplay,
				xWindow,
				IMAGE_WIDTH,
				IMAGE_HEIGHT,
				xWindowAttr.depth);
	if (0 == xPixmapOverlay)
	{
		printf("XCreatePixmap (overlay) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture assocaited with the overlay pixmap. */
	/* Having the same visual format as the window. */
	xPictureOverlay = XRenderCreatePicture(
				xDisplay,
				xPixmapOverlay,
				xRenderPictFormatWindow,
				0,
				NULL);
	if (0 == xPictureOverlay)
	{
		printf("XRenderCreatePicture (overlay) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Fill the overlay with black to be used for overlay color. */
	XSetForeground(xDisplay, gc, XBlackPixelOfScreen(xScreen));
	XFillRectangle(
		xDisplay,
		xPixmapOverlay,
		gc,
		0, 0,
		IMAGE_WIDTH,
		IMAGE_HEIGHT);

	/* Create pixmap for the mask content. */
	xPixmapMask = XCreatePixmap(
				xDisplay,
				xWindow,
				IMAGE_WIDTH,
				IMAGE_HEIGHT,
				8);
	if (0 == xPixmapMask)
	{
		printf("XCreatePixmap (mask) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create X render picture assocaited with the mask pixmap. */
	xPictureMask = XRenderCreatePicture(
				xDisplay,
				xPixmapMask,
				xRenderPictFormatMask,
				0,
				NULL);
	if (0 == xPictureMask)
	{
		printf("XRenderCreatePicture (mask) failed\n");
		returnCode = 1;
		goto error;
	}

	/* Create a GC to go with mask */
	gcMask = XCreateGC(xDisplay, xPixmapMask, 0, NULL);
	XSetForeground(xDisplay, gcMask, 0x00000000);
	XFillRectangle(
		xDisplay,
		xPixmapMask,
		gcMask,
		0, 0,
		IMAGE_WIDTH,
		IMAGE_HEIGHT);
	XSetForeground(xDisplay, gcMask, 0x40404040);
	XDrawRectangle(
		xDisplay,
		xPixmapMask,
		gcMask,
		0, 0,
		IMAGE_WIDTH-1,
		IMAGE_HEIGHT-1);
	XFillArc(
		xDisplay,
		xPixmapMask,
		gcMask,
		100, 100,
		100, 100,
		0,		/* start angle-degrees * 64 */
		360 * 64);	/* extent angle-degrees * 64 */

	Bool bIncX = True;
	Bool bIncY = True;
	Bool bNextRow = False;
	int x = 0;
	int y = 0;
	struct timeval timeStart;
	gettimeofday(&timeStart, NULL);
	int iter;
	for (iter = 0; iter < numIterations; ++iter)
	{
		XRenderComposite(
			xDisplay,
			PictOpSrc,
			xPictureMain,	/* src */
			0,		/* mask */
			xPictureWindow,	/* dst */
			x, y,		/* src (x,y) */
			0, 0,		/* mask (x,y) */
			x,		/* dst x */
			y,		/* dst y */
			IMAGE_WIDTH,
			IMAGE_HEIGHT);

		if (bNextRow)
		{
			if (bIncY)
			{
				if ((y += 10) >= IMAGE_HEIGHT)
				{
					y = IMAGE_HEIGHT - 1;
					bIncY = False;
				}
			}
			else
			{
				if ((y -= 10) < 0)
				{
					y = 0;
					bIncY = True;
				}
			}

			bNextRow = False;
		}
		else
		{
			if (bIncX)
			{
				if (++x >= IMAGE_WIDTH)
				{
					x = IMAGE_WIDTH - 1;
					bIncX = False;
					bNextRow = True;
				}
			}
			else
			{
				if (--x < 0)
				{
					x = 0;
					bIncX = True;
					bNextRow = True;
				}
			}
		}

		XRenderComposite(
			xDisplay,
			PictOpOver,
			xPictureOverlay,/* src */
			xPictureMask,	/* mask */
			xPictureWindow,	/* dst */
			0, 0,		/* src (x,y) */
			0, 0,		/* mask (x,y) */
			x,		/* dst x */
			y,		/* dst y */
			IMAGE_WIDTH,
			IMAGE_HEIGHT);
	}
	XSync(xDisplay, False);

	struct timeval timeEnd;
	gettimeofday(&timeEnd, NULL);
	double elapsedSec =
		getElapsedMicroseconds(&timeStart, &timeEnd) / 1000000L;
	double fps = numIterations / elapsedSec;
	printf("average update rate = %.1lf FPS\n", fps);

error:

	if (0 != gcMask)
	{
		XFreeGC(xDisplay, gcMask);
		gcMask = 0;
	}

	if (0 != xPictureMask)
	{
		XRenderFreePicture(xDisplay, xPictureMask);
		xPictureMask = 0;
	}

	if (0 != xPixmapMask)
	{
		XFreePixmap(xDisplay, xPixmapMask);
		xPixmapMask = 0;
	}

	if (0 != xPictureOverlay)
	{
		XRenderFreePicture(xDisplay, xPictureOverlay);
		xPictureOverlay = 0;
	}

	if (0 != xPixmapOverlay)
	{
		XFreePixmap(xDisplay, xPixmapOverlay);
		xPixmapOverlay = 0;
	}

	if (0 != xPictureMain)
	{
		XRenderFreePicture(xDisplay, xPictureMain);
		xPictureMain = 0;
	}

	if (0 != xPixmapMain)
	{
		XFreePixmap(xDisplay, xPixmapMain);
		xPixmapMain = 0;
	}

	if (0 != xPictureWindow)
	{
		XRenderFreePicture(xDisplay, xPictureWindow);
		xPictureWindow = 0;
	}

	if (0 != xWindow)
	{
		XDestroyWindow(xDisplay, xWindow);
		xWindow = 0;
	}

	if (NULL != xDisplay)
	{
		XCloseDisplay(xDisplay);
		xDisplay = NULL;
	}

	return returnCode;
}