Example #1
0
static void gd_loadimage_cairo(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
{
    cairo_t *cr = (cairo_t *) job->context; /* target context */
    unsigned int x, y, stride, width, height, px;
    unsigned char *data;
    cairo_surface_t *surface;    /* source surface */
    gdImagePtr im;

    if ((im = gd_loadimage(job, us))) {
	width = im->sx;
	height = im->sy;
// cairo_format_stride_for_width() not available prior to cairo-1.6.4 or so (fc9)
//stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
	stride = width*4;
	data = malloc (stride * height);
	surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
							width, height, stride);

	if (im->trueColor) {
	    for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
		    px = gdImageTrueColorPixel(im, x, y);
		    *data++ = gdTrueColorGetBlue(px);
		    *data++ = gdTrueColorGetGreen(px);
		    *data++ = gdTrueColorGetRed(px);
		    *data++ = (0x7F-gdTrueColorGetAlpha(px)) << 1;
		}
	    }
	}
	else {
	    for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
		    px = gdImagePalettePixel(im, x, y);
		    *data++ = im->blue[px];
		    *data++ = im->green[px];
		    *data++ = im->red[px];
		    *data++ = (px==im->transparent)?0x00:0xff;
		}
	    }
	}

        cairo_save(cr);
        cairo_translate(cr,
                (b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.),
                (-b.UR.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.));
        cairo_scale(cr,
                ((b.UR.x - b.LL.x) * (job->dpi.x) / (96. * us->w)),
                ((b.UR.y - b.LL.y) * (job->dpi.y) / (96. * us->h)));
        cairo_set_source_surface (cr, surface, 0, 0);
        cairo_paint (cr);
        cairo_restore(cr);

	cairo_surface_destroy(surface);
    }
}
Example #2
0
void plD_gd_optimise(PLStream *pls)
{
png_Dev *dev=(png_Dev *)pls->dev;
int i,j;
char *bbuf;

bbuf=calloc(256,(size_t) 1);    /* Allocate a buffer to "check off" colours as they are used */
if (bbuf==NULL) plexit("plD_gd_optimise: Out of memory.");

    for(i=0;i<(pls->xlength-1);i++)        /* Walk through the image pixel by pixel */
       {                                   /* checking to see what colour it is */
        for(j=0;j<(pls->ylength-1);j++)    /* and adding it to the list of used colours */
           {
            bbuf[gdImagePalettePixel(dev->im_out, i, j)]=1;
           }
       }

for (i=0;i<256;i++)     /* next walk over the colours and deallocate */
    {                   /* unused ones */
    if (bbuf[i]==0) gdImageColorDeallocate(dev->im_out,i);
    }

free(bbuf);
}
Example #3
0
static int puzzle_getview_from_gdimage(PuzzleContext * const context,
                                       PuzzleView * const view,
                                       gdImagePtr gdimage)
{
    unsigned int x, y;
    const unsigned int x0 = 0U, y0 = 0U;
    unsigned int x1, y1;
    unsigned char *maptr;
    int pixel;
    
    view->map = NULL;    
    view->width = (unsigned int) gdImageSX(gdimage);
    view->height = (unsigned int) gdImageSY(gdimage);
    view->sizeof_map = (size_t) (view->width * view->height);
    if (view->width > context->puzzle_max_width ||
        view->height > context->puzzle_max_height) {
        return -1;
    }
    if (view->sizeof_map <= (size_t) 0U ||
        INT_MAX / view->width < view->height ||
        SIZE_MAX / view->width < view->height ||
        (unsigned int) view->sizeof_map != view->sizeof_map) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    x1 = view->width - 1U;
    y1 = view->height - 1U;
    if (view->width <= 0U || view->height <= 0U) {
        puzzle_err_bug(__FILE__, __LINE__);
    }
    if ((view->map = calloc(view->sizeof_map, sizeof *view->map)) == NULL) {
        return -1;
    }
    if (x1 > INT_MAX || y1 > INT_MAX) { /* GD uses "int" for coordinates */
        puzzle_err_bug(__FILE__, __LINE__);
    }
    maptr = view->map;
    x = x1;
    if (gdImageTrueColor(gdimage) != 0) {
        do {
            y = y1;
            do {
                pixel = gdImageGetTrueColorPixel(gdimage, (int) x, (int) y);
                *maptr++ = (unsigned char)
                    ((gdTrueColorGetRed(pixel) * 77 +
                      gdTrueColorGetGreen(pixel) * 151 +
                      gdTrueColorGetBlue(pixel) * 28 + 128) / 256);
            } while (y-- != y0);
        } while (x-- != x0);
    } else {
        do {
            y = y1;
            do {
                pixel = gdImagePalettePixel(gdimage, x, y);
                *maptr++ = (unsigned char)
                    ((gdimage->red[pixel] * 77 +
                      gdimage->green[pixel] * 151 +
                      gdimage->blue[pixel] * 28 + 128) / 256);
            } while (y-- != y0);
        } while (x-- != x0);
    }
    return 0;
}
Example #4
0
static void gd_loadimage_ps(GVJ_t * job, usershape_t *us, boxf b, boolean filled)
{
    gdImagePtr im = NULL;
    int X, Y, x, y, px;

    if ((im = gd_loadimage(job, us))) {
	X = im->sx;
	Y = im->sy;

        gvputs(job, "save\n");

	/* define image data as string array (one per raster line) */
        gvputs(job, "/myctr 0 def\n");
        gvputs(job, "/myarray [\n");
        if (im->trueColor) {
            for (y = 0; y < Y; y++) {
		gvputs(job, "<");
                for (x = 0; x < X; x++) {
                    px = gdImageTrueColorPixel(im, x, y);
                    gvprintf(job, "%02x%02x%02x",
                        gdTrueColorGetRed(px),
                        gdTrueColorGetGreen(px),
                        gdTrueColorGetBlue(px));
                }
		gvputs(job, ">\n");
            }
	}
        else {
            for (y = 0; y < Y; y++) {
		gvputs(job, "<");
                for (x = 0; x < X; x++) {
                    px = gdImagePalettePixel(im, x, y);
                    gvprintf(job, "%02x%02x%02x",
                        im->red[px],
                        im->green[px],
                        im->blue[px]);
                }
		gvputs(job, ">\n");
            }
        }
	gvputs(job, "] def\n");
	gvputs(job,"/myproc { myarray myctr get /myctr myctr 1 add def } def\n");

        /* this sets the position of the image */
        gvprintf(job, "%g %g translate\n",
		(b.LL.x + (b.UR.x - b.LL.x) * (1. - (job->dpi.x) / 96.) / 2.),
	        (b.LL.y + (b.UR.y - b.LL.y) * (1. - (job->dpi.y) / 96.) / 2.));

        /* this sets the rendered size to fit the box */
        gvprintf(job,"%g %g scale\n",
		((b.UR.x - b.LL.x) * (job->dpi.x) / 96.),
		((b.UR.y - b.LL.y) * (job->dpi.y) / 96.));
    
        /* xsize ysize bits-per-sample [matrix] */
        gvprintf(job, "%d %d 8 [%d 0 0 %d 0 %d]\n", X, Y, X, -Y, Y);
    
        gvputs(job, "{myproc} false 3 colorimage\n");
    
        gvputs(job, "restore\n");
    }
}