Пример #1
0
int pngtofile(FILE *fin, FILE *fout)
{
    gdImagePtr im = gdImageCreateFromPng(fin);

    int c = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) - 1);
    int data_size = (gdImageRed(im, c) << 8*2) + (gdImageGreen(im, c) << 8*1) + (gdImageBlue(im, c));

    unsigned char buf[3];
    long written_bytes = 0;
    int x, y;
    int nb = 0;
    for(y = 0; y < gdImageSY(im); y++) {
        for(x = 0; x < gdImageSX(im); x++) {
            c = gdImageGetPixel(im, x, y);
            buf[0] = gdImageRed(im, c);
            buf[1] = gdImageGreen(im, c);
            buf[2] = gdImageBlue(im, c);
            if(written_bytes >= data_size) {
                break; /* FIXME */
            } else {
                nb = written_bytes + 3 > data_size ?
                    data_size - written_bytes : 3;
                written_bytes += fwrite(buf, 1, nb, fout);
            }
        }
    }

    gdImageDestroy(im);
    return 1;
}
Пример #2
0
/*
 * Bueno, vamos a ver si podemos obtener la pieza
 * */
int    tipojuego_get_tpieza_png( Tipojuego* tj, char* color, char* tpieza, int flags, void** png, int* width, int* height ){
#if GRAPH_ENABLED
    Tipopieza* tp = tj->tipo_piezas->data[ GETTIPOPIEZA(tj,tpieza) ];
    int  col      = GETCOLOR(tj,color);
    gdImagePtr gd = graph_tpieza_get_gd( tp, col );
    int  size = 0;

    if( flags == GETPNG_PIEZA_CAPTURADA ){
        gdImagePtr gd2 = gdImageCreate( gdImageSX( gd ) / 2, gdImageSY( gd ) / 2 );

        int transp = gdImageColorAllocate( gd2, 0, 255, 0 );
        gdImageColorTransparent( gd2, transp );
        gdImageFill( gd2, 0, 0 , transp );

        gdImageCopyResized( gd2, gd, 0, 0, 0, 0,
          gdImageSX(gd2), gdImageSY(gd2),
          gdImageSX(gd), gdImageSY(gd) );
        
        if( png ) *png = gdImagePngPtr( gd2, &size );
        if( width ) *width = gdImageSX( gd2 );
        if( height ) *height = gdImageSY( gd2 );
        gdImageDestroy( gd2 );
        return size;
    } else {
        if( png ) *png = gdImagePngPtr( gd, &size );
        if( width ) *width = gdImageSX( gd );
        if( height ) *height = gdImageSY( gd );
        return size;
    }
#endif
    LOGPRINT( 2, "No compilado con el modulo GD tpieza = %s", tpieza );
    return 0;
}
Пример #3
0
int renderPixmapSymbolGD(imageObj *img, double x, double y, symbolObj *symbol, symbolStyleObj *style)
{
    gdImagePtr ip,pp;
    if(!(ip = MS_IMAGE_GET_GDIMAGEPTR(img))) return MS_FAILURE;
    assert(symbol->pixmap_buffer && symbol->pixmap_buffer->type == MS_BUFFER_GD);
    pp = symbol->pixmap_buffer->data.gd_img;
    /* gdImageAlphaBlending(ip,1); */
    /* gdImageAlphaBlending(pp,1); */

    if(symbol->transparent)
        gdImageColorTransparent(pp,symbol->transparentcolor);
    if(style->scale == 1.0 && style->rotation == 0.0) { /* don't scale */
        x -= .5*symbol->pixmap_buffer->width;
        y -= .5*symbol->pixmap_buffer->height;
        gdImageCopy(ip, pp, x, y, 0, 0, symbol->pixmap_buffer->width,symbol->pixmap_buffer->height);
    } else {
        int bRotated = MS_FALSE;
        if(style->rotation) {
            bRotated = MS_TRUE;
            pp = rotatePixmapGD(pp,style->rotation);
        }
        x -=  .5*gdImageSX(pp)*style->scale;
        y -=  .5*gdImageSY(pp)*style->scale;
        gdImageCopyResampled(ip, pp, x, y, 0, 0,
                             (int)(gdImageSX(pp) * style->scale),
                             (int)(gdImageSY(pp) * style->scale),
                             gdImageSX(pp),gdImageSY(pp));
        if(bRotated) {
            gdImageDestroy(pp);
        }
    }
    /* gdImageAlphaBlending(ip,0); */
    return MS_SUCCESS;
}
Пример #4
0
static void gdImageBrushApply(gdImagePtr im, int x, int y)
{
	int lx, ly;
	int hy;
	int hx;
	int x1, y1, x2, y2;
	int srcx, srcy;
	if (!im->brush) {
		return;
	}
	hy = gdImageSY(im->brush)/2;
	y1 = y - hy;
	y2 = y1 + gdImageSY(im->brush);	
	hx = gdImageSX(im->brush)/2;
	x1 = x - hx;
	x2 = x1 + gdImageSX(im->brush);
	srcy = 0;
	for (ly = y1; (ly < y2); ly++) {
		srcx = 0;
		for (lx = x1; (lx < x2); lx++) {
			int p;
			p = gdImageGetPixel(im->brush, srcx, srcy);
			/* Allow for non-square brushes! */
			if (p != gdImageGetTransparent(im->brush)) {
				gdImageSetPixel(im, lx, ly,
					im->brushColorMap[p]);
			}
			srcx++;
		}
		srcy++;
	}	
}		
Пример #5
0
int main(void)
{
	/* 2.0.22: can't depend on PNG either  */
#ifndef HAVE_LIBPNG
	fprintf(stderr, "Requires PNG support, gd was compiled without it\n");
	return 0;
#else
	char *error;
	FILE *in = 0;
	FILE *out;
	gdImagePtr im;
	int radius;
	/* Create an image of text on a circle, with an
	 * alpha channel so that we can copy it onto a
	 * background
	 * TBB: 2.0.18: shouldn't depend on JPEG
	 */
#ifdef HAVE_LIBJPEG
	in = fopen("eleanor.jpg", "rb");
	if(!in) {
		im = gdImageCreateTrueColor(300, 300);
	} else {
		im = gdImageCreateFromJpeg(in);
		fclose(in);
	}
#else
	im = gdImageCreateTrueColor(300, 300);
#endif /* HAVE_LIBJPEG */
	if(!im) {
		fprintf(stderr, "gdImageCreateTrueColor failed \n");
		return 1;
	}
	if(gdImageSX(im) < gdImageSY(im)) {
		radius = gdImageSX(im) / 2;
	} else {
		radius = gdImageSY(im) / 2;
	}

	error = gdImageStringFTCircle(im,
	                              gdImageSX(im) / 2, gdImageSY(im) / 2,
	                              radius, radius / 2,
	                              0.8, "arial", 24, "top text", "bottom text",
	                              gdTrueColorAlpha(192, 100, 255, 32)
	                             );
	if(error)  {
		fprintf(stderr, "gdImageStringFTEx error: %s\n", error);
	}

	out = fopen("gdfx.png", "wb");
	if(!out) {
		fprintf(stderr, "Can't create gdfx.png\n");
		return 1;
	}

	gdImagePng(im, out);
	fclose(out);
	gdImageDestroy(im);
#endif /* HAVE_LIBPNG */
	return 0;
}
Пример #6
0
/* gdImageWBMPCtx
 *  --------------
 *  Write the image as a wbmp file
 *  Parameters are:
 *  image:  gd image structure;
 *  fg:     the index of the foreground color. any other value will be
 *          considered as background and will not be written
 *  out:    the stream where to write
 */
BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
{
	int x, y, pos;
	Wbmp *wbmp;

	/* create the WBMP */
	if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
		fprintf(stderr, "Could not create WBMP\n");
		return;
	}

	/* fill up the WBMP structure */
	pos = 0;
	for(y = 0; y < gdImageSY(image); y++) {
		for(x = 0; x < gdImageSX(image); x++) {
			if(gdImageGetPixel(image, x, y) == fg) {
				wbmp->bitmap[pos] = WBMP_BLACK;
			}
			pos++;
		}
	}

	/* write the WBMP to a gd file descriptor */
	if(writewbmp(wbmp, &gd_putout, out)) {
		fprintf(stderr, "Could not save WBMP\n");
	}

	/* des submitted this bugfix: gdFree the memory. */
	freewbmp(wbmp);
}
Пример #7
0
gdImage *fx_scale(gdImage *src, char *options)
{
	int w, h;
	gdImage *im;
	
	w = argtol(options, "x ", 0, 0, 10);
	h = argtol(options, "x ", 1, 0, 10);
	
	if(w < 0 || h < 0)
	{
		WARN("Invalid resolution: %s", options);
		return(src);
	}
	
	MSG("Scaling image from %ix%i -> %ix%i.",
	    gdImageSX(src), gdImageSY(src), w, h);
	
	im = gdImageCreateTrueColor(w, h);
	if(!im)
	{
		WARN("Out of memory.");
		return(src);
	}
	
	gdImageCopyResampled(im, src, 0, 0, 0, 0,
	   w, h, gdImageSX(src), gdImageSY(src));
	
	gdImageDestroy(src);
	
	return(im);
}
Пример #8
0
gdImage *fx_rotate(gdImage *src, char *options)
{
	int x, y;
	gdImage *im;
	int angle = atoi(options);
	
	/* Restrict angle to 0-360 range. */
	if((angle %= 360) < 0) angle += 360;
	
	/* Round to nearest right angle. */
	x = (angle + 45) % 360;
	angle = x - (x % 90);
	
	/* Not rotating 0 degrees. */
	if(angle == 0)
	{
		MSG("Not rotating 0 degrees.");
		return(src);
	}
	
	/* 180 can be done with two flips. */
	if(angle == 180)
	{
		fx_flip(src, "h,v");
		return(src);
	}
	
	MSG("Rotating image %i degrees. %ix%i -> %ix%i.", angle,
	    gdImageSX(src), gdImageSY(src),
	    gdImageSY(src), gdImageSX(src));
	
	/* Create rotated image. */
	im = gdImageCreateTrueColor(gdImageSY(src), gdImageSX(src));
	if(!im)
	{
		WARN("Out of memory.");
		return(src);
	}
	
	for(y = 0; y < gdImageSY(src); y++)
		for(x = 0; x < gdImageSX(src); x++)
		{
			int c = gdImageGetPixel(src, x, y);
			
			if(angle == 90)
			{
				gdImageSetPixel(im, gdImageSX(im) - y - 1, x, c);
			}
			else
			{
				gdImageSetPixel(im, y, gdImageSY(im) - x - 1, c);
			}
		}
	
	gdImageDestroy(src);
	
	return(im);
}
Пример #9
0
Файл: xbm.c Проект: 20uf/php-src
/* {{{ gdImageXbmCtx */
void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out)
{
	int x, y, c, b, sx, sy, p;
	char *name, *f;
	size_t i, l;

	name = file_name;
	if ((f = strrchr(name, '/')) != NULL) name = f+1;
	if ((f = strrchr(name, '\\')) != NULL) name = f+1;
	name = estrdup(name);
	if ((f = strrchr(name, '.')) != NULL && !strcasecmp(f, ".XBM")) *f = '\0';
	if ((l = strlen(name)) == 0) {
		efree(name);
		name = estrdup("image");
	} else {
		for (i=0; i<l; i++) {
			/* only in C-locale isalnum() would work */
			if (!isupper(name[i]) && !islower(name[i]) && !isdigit(name[i])) {
				name[i] = '_';
			}
		}
	}

	gdCtxPrintf(out, "#define %s_width %d\n", name, gdImageSX(image));
	gdCtxPrintf(out, "#define %s_height %d\n", name, gdImageSY(image));
	gdCtxPrintf(out, "static unsigned char %s_bits[] = {\n  ", name);

	efree(name);

	b = 1;
	p = 0;
	c = 0;
	sx = gdImageSX(image);
	sy = gdImageSY(image);
	for (y = 0; y < sy; y++) {
		for (x = 0; x < sx; x++) {
			if (gdImageGetPixel(image, x, y) == fg) {
				c |= b;
			}
			if ((b == 128) || (x == sx - 1)) {
				b = 1;
				if (p) {
					gdCtxPrintf(out, ", ");
					if (!(p%12)) {
						gdCtxPrintf(out, "\n  ");
						p = 12;
					}
				}
				p++;
				gdCtxPrintf(out, "0x%02X", c);
				c = 0;
			} else {
				b <<= 1;
			}
		}
	}
	gdCtxPrintf(out, "};\n");
}
Пример #10
0
/* If palette is true, we convert from truecolor to palette at the end,
   to test gdImageTrueColorToPalette and see file size/
   quality tradeoffs. */
void
testDrawing (gdImagePtr im_in,
	     double scale, int blending, int palette, char *filename)
{
	gdImagePtr im_out;
	FILE *out;
	/* Create output image. */
	im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
					 (int) (gdImageSY (im_in) * scale));
	/*
	   Request alpha blending. This causes future
	   drawing operations to perform alpha channel blending
	   with the background, resulting in an opaque image.
	   Without this call, pixels in the foreground color are
	   copied literally, *including* the alpha channel value,
	   resulting in an output image which is potentially
	   not opaque. This flag can be set and cleared as often
	   as desired. */
	gdImageAlphaBlending (im_out, blending);

	/* Flood with light blue. */
	gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
		     (int) (gdImageSY (im_in) * scale / 2),
		     gdTrueColor (192, 192, 255));
	/* Copy the source image. Alpha blending should result in
	   compositing against red. With blending turned off, the
	   browser or viewer will composite against its preferred
	   background, or, if it does not support an alpha channel,
	   we will see the original colors for the pixels that
	   ought to be transparent or semitransparent. */
	gdImageCopyResampled (im_out, im_in,
			      0, 0,
			      0, 0,
			      (int) (gdImageSX (im_in) * scale),
			      (int) (gdImageSY (im_in) * scale), gdImageSX (im_in),
			      gdImageSY (im_in));
	/* Write PNG */
	out = fopen (filename, "wb");

	/* If this image is the result of alpha channel blending,
	   it will not contain an interesting alpha channel itself.
	   Save a little file size by not saving the alpha channel.
	   Otherwise the file would typically be slightly larger. */
	gdImageSaveAlpha (im_out, !blending);

	/* If requested, convert from truecolor to palette. */
	if (palette) {
		/* Dithering, 256 colors. */
		gdImageTrueColorToPalette (im_out, 1, 256);
	}

	gdImagePng (im_out, out);
	fclose (out);

	gdImageDestroy (im_out);
}
Пример #11
0
gdImage* fswc_gdImageDuplicate(gdImage* src)
{
	gdImage *dst;
	
	dst = gdImageCreateTrueColor(gdImageSX(src), gdImageSY(src));
	if(!dst) return(NULL);
	
	gdImageCopy(dst, src, 0, 0, 0, 0, gdImageSX(src), gdImageSY(src));
	
	return(dst);
}
Пример #12
0
JBoolean
PadColormap
	(
	gdImagePtr image
	)
{
	JSize colorCount = gdImageColorsTotal(image);
	if (colorCount >= kMinColorCount)
		{
		return kJFalse;
		}

	const JSize extraColorCount = kMinColorCount - colorCount;

	int x = gdImageSX(image) - extraColorCount;
	if (x < 0)
		{
		cerr << "image is too small to fit extra colors on single raster line" << endl;
		exit(1);
		}

	int y = gdImageSY(image) - 1;

	int c = gdImageGetPixel(image, x,y);
	int r = gdImageRed  (image, c);
	int g = gdImageGreen(image, c);
	int b = gdImageBlue (image, c);

	int delta = -1;
	if (r < 127 || g < 127 || b < 127)
		{
		delta = +1;
		}

	for (JIndex i=1; i<=extraColorCount; i++)
		{
		assert( x < gdImageSX(image) );

		while ((c = gdImageColorExact(image, r,g,b)) != -1)
			{
			r = JMax(0, JMin(r + delta, 255));
			g = JMax(0, JMin(g + delta, 255));
			b = JMax(0, JMin(b + delta, 255));
			}

		c = gdImageColorAllocate(image, r,g,b);
		assert( c != -1 );
		gdImageSetPixel(image, x,y, c);

		x++;
		}

	return kJTrue;
}
Пример #13
0
int main()
{
	gdImagePtr im, im2;
	int error = 0;

	im = gdImageCreateTrueColor(5, 5);
	if (!im) {
		printf("can't create the src truecolor image\n");
		return 1;
	}

	gdImageFilledRectangle(im, 0, 0, 49, 49, 0x00FFFFFF);
	gdImageColorTransparent(im, 0xFFFFFF);
	gdImageFilledRectangle(im, 1, 1, 4, 4, 0xFF00FF);

	im2 = gdImageCreateTrueColor(20, 20);
	if (!im2) {
		printf("can't create the dst truecolor image\n");
		gdImageDestroy(im);
		return 1;
	}

	gdImageCopy(im2, im, 2, 2 , 0, 0, gdImageSX(im), gdImageSY(im));

	if (!gdAssertImageEqualsToFile("gdimagecopy/bug00081_exp.png", im2)) {
		error = 1;
		printf("Reference image and destination differ\n");
	}

	gdImageDestroy(im);
	gdImageDestroy(im2);
	return error;
}
Пример #14
0
/*
 * This is a very simple and experimental deinterlacer,
 * there is a lot of room for improvement here.
 *
 * (For example: Making it work)
 *
*/
gdImage *fx_deinterlace(gdImage *src, char *options)
{
	int x, y;
	
	MSG("Deinterlacing image.");
	
	for(y = 1; y < gdImageSY(src) - 1; y += 2)
	{
		for(x = 0; x < gdImageSX(src); x++)
		{
			int c, cu, cd, d;
			
			c  = gdImageGetPixel(src, x, y);
			cu = gdImageGetPixel(src, x, y - 1);
			cd = gdImageGetPixel(src, x, y + 1);
			
			/* Calculate the difference of the pixel (x,y) from
			 * the average of it's neighbours above and below. */
			d = 0xFF - abs(GREY(cu) - GREY(cd));
			d = (abs(GREY(cu) - (0xFF - GREY(c)) - GREY(cd)) * d) / 0xFF;
			
			c = RGBMIX(c, RGBMIX(cu, cd, 128), d);
			
			gdImageSetPixel(src, x, y, c);
		}
	}
	
	return(src);
}
Пример #15
0
static void crop(Cmd *cmd) {
  char *buff = cmd->data;
  int len = cmd->size;
  Gd *gd = cmd->gd;
  int index = 0;
  long width, height;
  int srcW, srcH, srcX, srcY, destX, destY, playX, playY;
  gdImagePtr destination = NULL;
  
  ei_decode_version(buff, &index, NULL);
  ei_decode_tuple_header(buff, &index, NULL);
  ei_decode_long(buff, &index, &width);
  ei_decode_long(buff, &index, &height);
  
  if (NULL == gd->image) {
    driver_failure_atom(gd->port, "null_image");
    return;
  }
  
  srcW = gdImageSX(gd->image);
  srcH = gdImageSY(gd->image);
  
  destination = gdImageCreateTrueColor(width, height);
  if (NULL == destination) {
    driver_failure_posix(gd->port, ENOMEM);
    return;
  }
  gdImageFilledRectangle(destination, 0, 0, width, height, gdImageColorAllocate(destination, 255, 255, 255));
  destX = (width - srcW) / 2;
  destY = (height - srcH) / 2;
  gdImageCopy(destination, gd->image, destX, destY, 0, 0, srcW, srcH);
  gdImageDestroy(gd->image);
  gd->image = destination;
  send_atom(gd->port, "ok");
}
Пример #16
0
static int
szm_do_scale_png(char *in, int in_len, uint8_t scale, char **out, int *out_len)
{
	gdImagePtr im_in, im_out;
	int x, y, new_x, new_y;

	im_in = gdImageCreateFromPngPtr(in_len, in);
	if (!im_in)
		return -SZM_ERR_IMGFMT;
	x = gdImageSX(im_in);
	y = gdImageSY(im_in);
	new_x = x / scale;
	new_y = y / scale;
	if (new_x == 0)
		new_x = 1;
	if (new_y == 0)
		new_y = 1;
	im_out = gdImageCreateTrueColor(new_x, new_y);
	if (!im_out) {
		gdImageDestroy(im_in);
		return -SZM_ERR_IMGPROC;
	}
	gdImageCopyResized(im_out, im_in, 0, 0, 0, 0,
			   im_out->sx, im_out->sy, im_in->sx, im_in->sy);
	*out = gdImagePngPtr(im_out, out_len);

	gdImageDestroy(im_in);
	gdImageDestroy(im_out);
	return 0;
}
Пример #17
0
static int
szm_do_resize_jpg(char *in, int in_len, uint16_t new_x,
		  uint16_t new_y, char **out, int *out_len)
{
	int x, y;
	gdImagePtr im_in, im_out;

	im_in = gdImageCreateFromJpegPtr(in_len, in);
	if (!im_in)
		return -SZM_ERR_IMGFMT;
	x = gdImageSX(im_in);
	y = gdImageSY(im_in);
	if (new_x == 0)
		new_x = 1;
	if (new_y == 0)
		new_y = 1;
	im_out = gdImageCreateTrueColor(new_x, new_y);
	if (!im_out) {
		gdImageDestroy(im_in);
		return -SZM_ERR_IMGPROC;
	}
	gdImageCopyResized(im_out, im_in, 0, 0, 0, 0,
			   im_out->sx, im_out->sy, im_in->sx, im_in->sy);
	*out = gdImageJpegPtr(im_out, out_len, -1);

	gdImageDestroy(im_in);
	gdImageDestroy(im_out);
	return 0;
}
Пример #18
0
int main(int argc, char *argv[]) {
 FILE *pngout = {0};
 gdImagePtr img;
 int fgcol, bgcol;
 char *str = NULL;
 char *fname = NULL;

 if(argc != 5) {
  fprintf(stderr, "Usage: ./imstr \"STRING\" FFFFFF 000000 image.png\n");
  return 1;
 } else {
  str = argv[1];
  img = gdImageCreate((gdFontGiant->w * strlen(str)) + 2, gdFontGiant->h + 2);
  setupcolor(argv[2]);
  bgcol = gdImageColorAllocate(img, red, green, blue);
  setupcolor(argv[3]);
  fgcol = gdImageColorAllocate(img, red, green, blue);
  fname = argv[4];
 }

 gdImageString(img, gdFontGiant,
  gdImageSX(img) / 2 - (strlen(str) * gdFontGiant->w / 2),
  gdImageSY(img) / 2 - gdFontGiant->h / 2, str, fgcol);

 if((pngout = fopen(fname, "w")) == NULL)
  error(1, 0, "Error - fopen(): %s", fname);
 else {
  gdImagePng(img, pngout);
  fclose(pngout);
 }

 gdImageDestroy(img);
 return 0;
}
Пример #19
0
unsigned char *readpng(const char *filename,int *width, int *height){

  FILE *file;
  gdImagePtr image;
  unsigned char *dataptr,*dptr;
  int i,j;
  unsigned int intrgb;

  file = fopen(filename, "rb");
  if(file == NULL)return NULL;
  image = gdImageCreateFromPng(file);
  fclose(file);
  *width=gdImageSX(image);
  *height=gdImageSY(image);
  if( NewMemory((void **)&dataptr,(unsigned int)(4*(*width)*(*height)) )==0){
    gdImageDestroy(image);
    return NULL;
  }
  dptr=dataptr;
  for (i = 0; i<*height; i++){
    for(j=0;j<*width;j++){
      intrgb=(unsigned int)gdImageGetPixel(image,j,(unsigned int)(*height-(1+i)));
      *dptr++ = (intrgb>>16)&255;
      *dptr++ = (intrgb>>8)&255;
      *dptr++ = intrgb&255;
      *dptr++ = 0xff;
    }
  }
  gdImageDestroy(image);
  return dataptr;

}
Пример #20
0
result_t Image::affine(v8::Local<v8::Array> affine, int32_t x, int32_t y, int32_t width, int32_t height,
                       obj_ptr<Image_base>& retVal)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (x == -1 && y == -1 && width == -1 && height == -1) {
        width = gdImageSX(m_image);
        height = gdImageSY(m_image);
    }
    else if (x < 0 || y < 0 || width < 0 || height < 0 || affine->Length() != 6 )
        return CHECK_ERROR(CALL_E_INVALIDARG);

    obj_ptr<Image> dst = new Image();

    gdRect rect;
    rect.x = x;
    rect.y = y;
    rect.width = width;
    rect.height = height;

    double affineMatrix[6];
    for ( int32_t i = 0; i <= 5; i++)
        affineMatrix[i] = affine->Get(i)->NumberValue();

    gdTransformAffineGetImage(&dst->m_image, m_image, &rect, affineMatrix);
    dst->setExtMemory();

    retVal = dst;

    return 0;
}
Пример #21
0
result_t Image::resample(int32_t width, int32_t height,
                         obj_ptr<Image_base> &retVal, AsyncEvent *ac)
{
    if (width <= 0 || height <= 0)
        return CHECK_ERROR(CALL_E_INVALIDARG);

    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (!ac)
        return CHECK_ERROR(CALL_E_NOSYNC);

    obj_ptr<Image> dst;
    result_t hr = New(width, height, dst);
    if (hr < 0)
        return hr;

    gdImageAlphaBlending(dst->m_image, 0);
    gdImageCopyResampled(dst->m_image, m_image, 0, 0, 0, 0, width, height,
                         gdImageSX(m_image), gdImageSY(m_image));
    gdImageAlphaBlending(dst->m_image, 1);

    retVal = dst;
    return 0;
}
Пример #22
0
static void resize(Cmd *cmd) {
  char *buff = cmd->data;
  int len = cmd->size;
  Gd *gd = cmd->gd;
  int index = 0;
  unsigned long width, height, srcW, srcH;
  gdImagePtr destination = NULL;
  
  ei_decode_version(buff, &index, NULL);
  ei_decode_tuple_header(buff, &index, NULL);
  ei_decode_ulong(buff, &index, &width);
  ei_decode_ulong(buff, &index, &height);
  
  if (NULL == gd->image) {
    driver_failure_atom(gd->port, "null_image");
    return;
  }
  
  srcW = gdImageSX(gd->image);
  srcH = gdImageSY(gd->image);
  
  destination = gdImageCreateTrueColor(width, height);
  
  if (NULL == destination) {
    driver_failure_posix(gd->port, ENOMEM);
    return;
  }
  
  gdImageCopyResampled(destination, gd->image, 0, 0, 0, 0, width, height, srcW, srcH);
  gdImageDestroy(gd->image);
  gd->image = destination;
  
  send_atom(gd->port, "ok");
}
Пример #23
0
int main(int argc, char *argv[]){
  FILE *fp;
  gdImagePtr im1, im2, imOut;
  int i,x,y,xsize,ysize,c;
  int c1,c2;

  if(argc != 4){
    fprintf(stderr, "[%s] compiled [%s/%s %s]\n", argv[0], __DATE__, __TIME__, DIRE);
    fprintf(stderr, "Usage : %s in-gif1 in-gif2 out-gif\n", argv[0]);
    exit(1);
  }

  im1 = fromGif(argv[1]);
  im2 = fromGif(argv[2]);

  outfile = argv[3];

  xsize = gdImageSX(im1);
  ysize = gdImageSY(im2);

  imOut = gdImageCreate(xsize, ysize);

  for(i=0; i<gdImageColorsTotal(im1); i++){
    int r,g,b;
    r = gdImageRed(  im1, i);
    g = gdImageGreen(im1, i);
    b = gdImageBlue( im1, i);
  }

  for(y=0; y<ysize; y++){
    int r,g,b;
    for(x=0; x<xsize; x++){
      c1 = gdImageGetPixel(im1, x, y);
      c2 = gdImageGetPixel(im2, x, y);
      r = gdImageRed(  im1, c1) - gdImageRed(  im2, c2);
      g = gdImageGreen(im1, c1) - gdImageGreen(im2, c2);
      b = gdImageBlue( im1, c1) - gdImageBlue( im2, c2);
      r = (r + 256) % 256;
      g = (g + 256) % 256;
      b = (b + 256) % 256;
      c = allocOrExact(imOut, r, g, b);
      gdImageSetPixel(imOut, x, y, c);
    }
  }
  gdImageDestroy(im1);
  gdImageDestroy(im2);

  fp = fopen(outfile, "wb");
  if(!fp){
    fprintf(stderr, "Can't open [%s]\n", outfile);
    exit(1);
  }
  gdImageGif(imOut, fp);
  fclose(fp);

  gdImageDestroy(imOut);

  return 0;
}
Пример #24
0
result_t Image::get_width(int32_t &retVal)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    retVal = gdImageSX(m_image);
    return 0;
}
Пример #25
0
uint8_t LcdWriteImagePng(LcdSpi* lcd, const char *filename)
{
	uint16_t x = 100;
	uint16_t y = 100;
	uint16_t xm = 0;
	uint16_t ym = 0;
	uint16_t i = 0;
	int c;
	gdImagePtr img;
	FILE *filePtr;
	uint8_t b;
	
	if (!lcd) {
		return 1;
	}
	if (!filename) {
		return 2;
	}

	filePtr = fopen(filename, "rb");
	if (filePtr == NULL) {
		return 3;
	}
	img = gdImageCreateFromPng(filePtr);
	if (img == NULL) {
		return 4;
	}
	xm = gdImageSX(img);
	ym = gdImageSY(img);
	printf("%i x %i\n", xm, ym);
	
	if (xm != 132 || ym != 32) {
		return 5;
	}
	
	for (y = 0; y < ym; y += 8) {
		LcdSetPos(lcd, y / 8, 0);
		
		GpioSetValue(PIN_LCD_A0, 1);
		for (x = 0; x < xm; x++) {
			b = 0;
			for (i = 0; i < 8; i++) {
				c = gdImageGetPixel(img, x, y + i);
				if (img->red[c] < 0x7f || img->green[c] < 0x7f || img->blue[c] < 0x7f) {
				//if (c) {
					b |= (1 << i);
				}
			}
			LcdWriteByte(lcd->mS0, b);
		}
	}
	fclose(filePtr);
	gdImageDestroy(img);
	
	return 0;
}
Пример #26
0
void hqx_scale(gdImagePtr srcIm, int factor, gdImagePtr* result)
{
    gdImagePtr dstIm;
    uint32_t *srcBuffer, *dstBuffer;
    size_t srcSize, dstSize;
    uint32_t w, h;
    uint32_t x, y;

    w = gdImageSX(srcIm);
    h = gdImageSY(srcIm);

    srcSize = sizeof(uint32_t) * w * h;
    dstSize = sizeof(uint32_t) * factor * w * factor * h;

    // Unfortunately it doesn't work to simply pass the gd buffer from the
    // gdImage struct to hqx, and the gd documentation explicitly says
    // not to access member fields directly. Thus we allocate two buffers
    // and copy the data back and forth as a workaround.

    srcBuffer = (uint32_t*)emalloc(srcSize);
    for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
            srcBuffer[w*y + x] = gdImageGetPixel(srcIm, x, y);
        }
    }

    dstBuffer = (uint32_t*)emalloc(dstSize);
    if (factor == 4) {
        hq4x_32_rb(srcBuffer, sizeof(uint32_t)*w,
                   dstBuffer, sizeof(uint32_t)*factor*w,
                   w, h);
    } else if (factor == 3) {
        hq3x_32_rb(srcBuffer, sizeof(uint32_t)*w,
                   dstBuffer, sizeof(uint32_t)*factor*w,
                   w, h);
    } else if (factor == 2) {
        hq2x_32_rb(srcBuffer, sizeof(uint32_t)*w,
                   dstBuffer, sizeof(uint32_t)*factor*w,
                   w, h);
    }

    dstIm = gdImageCreateTrueColor(factor*w, factor*h);
    gdImageAlphaBlending(dstIm, 0);
    gdImageSaveAlpha(dstIm, 1);

    for (y = 0; y < factor*h; y++) {
        for (x = 0; x < factor*w; x++) {
            gdImageSetPixel(dstIm, x, y, dstBuffer[factor*w*y + x]);
        }
    }

    efree(srcBuffer);
    efree(dstBuffer);

    *result = dstIm;
}
Пример #27
0
gdImagePtr rotatePixmapGD(gdImagePtr img, double angle_rad)
{
    gdImagePtr rimg = NULL;
    double cos_a, sin_a;
    double x1 = 0.0, y1 = 0.0; /* destination rectangle */
    double x2 = 0.0, y2 = 0.0;
    double x3 = 0.0, y3 = 0.0;
    double x4 = 0.0, y4 = 0.0;

    long minx, miny, maxx, maxy;

    int width=0, height=0;
    /* int color; */

    sin_a = sin(angle_rad);
    cos_a = cos(angle_rad);

    /* compute distination rectangle (x1,y1 is known) */
    x1 = 0 ;
    y1 = 0 ;
    x2 = img->sy * sin_a;
    y2 = -img->sy * cos_a;
    x3 = (img->sx * cos_a) + (img->sy * sin_a);
    y3 = (img->sx * sin_a) - (img->sy * cos_a);
    x4 = (img->sx * cos_a);
    y4 = (img->sx * sin_a);

    minx = (long) MS_MIN(x1,MS_MIN(x2,MS_MIN(x3,x4)));
    miny = (long) MS_MIN(y1,MS_MIN(y2,MS_MIN(y3,y4)));
    maxx = (long) MS_MAX(x1,MS_MAX(x2,MS_MAX(x3,x4)));
    maxy = (long) MS_MAX(y1,MS_MAX(y2,MS_MAX(y3,y4)));

    width = (int)ceil(maxx-minx);
    height = (int)ceil(maxy-miny);

    /* create the new image based on the computed width/height */

    if (gdImageTrueColor(img)) {
        rimg = gdImageCreateTrueColor(width, height);
        gdImageAlphaBlending(rimg, 0);
        gdImageFilledRectangle(rimg, 0, 0, width, height, gdImageColorAllocateAlpha(rimg, 0, 0, 0, gdAlphaTransparent));
    } else {
        int tc = gdImageGetTransparent(img);
        rimg = gdImageCreate(width, height);
        if(tc != -1)
            gdImageColorTransparent(rimg, gdImageColorAllocate(rimg, gdImageRed(img, tc), gdImageGreen(img, tc), gdImageBlue(img, tc)));
    }
    if(!rimg) {
        msSetError(MS_GDERR,"failed to create rotated pixmap","rotatePixmapGD()");
        return NULL;
    }

    gdImageCopyRotated (rimg, img, width*0.5, height*0.5, 0, 0, gdImageSX(img), gdImageSY(img), angle_rad*MS_RAD_TO_DEG);
    return rimg;
}
Пример #28
0
int filetopng(FILE *fin, FILE *fout, int im_w, char *banner)
{
    struct stat fin_stat;
    gdImagePtr im = NULL; 

    fstat(fileno(fin), &fin_stat);
    long data_size = fin_stat.st_size;

    int im_w_bytes = im_w * 3;
    int im_h = ((data_size + im_w_bytes - 1) / im_w_bytes) + BANNER_HEIGHT;  /* ceil((float)data_size / im_w_bytes) + BANNER_HEIGHT */
    im = gdImageCreateTrueColor(im_w, im_h);

    unsigned char buf[3];
    long bytes_read = 0;
    long total_bytes = 0;
    int x = 0;
    int y = 0;
    while((bytes_read = fread(buf, 1, 3, fin)) > 0) {
        total_bytes += bytes_read;
        gdImageSetPixel(im, x, y, gdImageColorAllocate(im, buf[0], buf[1], buf[2]));

        if(x + 1 < im_w) {
            x++;
        } else {
            x = 0;
            y++;
        }
    }

    gdImageFilledRectangle(im, 0, gdImageSY(im) - BANNER_HEIGHT,
        im_w - 1, gdImageSY(im) + BANNER_HEIGHT, gdImageColorAllocate(im, 255, 255, 255));
    gdImageString(im, (gdFontPtr) gdFontGetTiny(), 5, gdImageSY(im) - BANNER_HEIGHT,
        (unsigned char *)banner, gdImageColorAllocate(im, 0, 0, 0));
    /* store data_size at last pixel */
    gdImageSetPixel(im, gdImageSX(im) - 1, gdImageSY(im) - 1,
        gdImageColorAllocate(im, (data_size & 0xff0000) >> 8*2, (data_size & 0xff00) >> 8*1, data_size & 0xff));
    
    if(verbose)
        fprintf(stderr, "Width:  %d\nHeight: %d\n", im_w, im_h);

    gdImagePng(im, fout);

/*
    int c = gdImageGetPixel(im, gdImageSX(im) - 1, gdImageSY(im) - 1);
    int ds = (gdImageRed(im, c) << 8*2) + (gdImageGreen(im, c) << 8*1) + (gdImageBlue(im, c));
    printf("debug: ds %d, data_size %d\n", ds, data_size);
    //printf("c: %d %d %d\n", (data_size & 0xff0000) >> 8*2, (data_size & 0xff00) >> 8*1, data_size & 0xff);
    //printf("d: %d %d %d\n", (gdImageRed(im, c) << 8*2), (gdImageGreen(im, c) << 8*1), (gdImageBlue(im, c)));
*/

    gdImageDestroy(im);
    return 1;
}
Пример #29
0
void
MessageEncryption::encrypt(std::string imgpath,std::string outpath){
    std::default_random_engine rand (this->key);
    int currand; int i, j;
    int width, height;
    FILE* in, *out; gdImagePtr img;
    printf("o\n");
    int ext = getexten(imgpath);

    printf("a\n");
    
    in = fopen(imgpath.c_str(),"rb");
    if(ext==0){
        img=gdImageCreateFromJpeg(in);
    } else if(ext==1){
        img=gdImageCreateFromPng(in);
    } else{
        exit(1);
    }
    fclose(in);

    printf("b\n");

    width = gdImageSX(img); height = gdImageSY(img);
    
    for(i=0;i<ITER_COUNT;i++){
        currand = rand()%FLIP_CHANCE;
        if(currand==0){ // flip horizontally
            fliphoriz(img);
        } else if(currand==1){ // flip vertically
            flipvert(img);
        } else if(currand<=1+FLIP_CHANCE/2){ // shift some rows
            for(j=0;j<=(currand%(width/3));j++){
                shiftcol(img,(3*i+j)%width,(7*currand+4*i)%height);
            }
        } else{  // shift some columns
            for(j=0;j<=(currand%(height/3));j++){
                shiftrow(img,(3*i+j)%height,(7*currand+4*i)%width);
            }
        }
    }
    printf("c\n");
    ext = getexten(outpath);
    out = fopen(outpath.c_str(),"wb");
    if(ext==0){
        gdImageJpeg(img,out,-1);
    } else if(ext==1){
        gdImagePng(img,out);
    } else{
        exit(1);
    }
    fclose(out); gdImageDestroy(img);
}
Пример #30
0
int main(int argc, char **argv)
{
    gdImagePtr A, B, C;
    unsigned char black, white;
    unsigned int minSX, minSY, maxSX, maxSY;
    bool rc;
#ifdef HAVE_GD_PNG
    FILE *f;
#endif

    if (argc < 3) {
        fprintf(stderr, "Usage: diffimg image1 image2 [outimage]\n");
        exit(EX_USAGE);
    }
    A = imageLoad(argv[1]);
    B = imageLoad(argv[2]);

    minSX = (gdImageSX(A) < gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
    minSY = (gdImageSY(A) < gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
    maxSX = (gdImageSX(A) > gdImageSX(B)) ? gdImageSX(A) : gdImageSX(B);
    maxSY = (gdImageSY(A) > gdImageSY(B)) ? gdImageSY(A) : gdImageSY(B);
    
    C = gdImageCreatePalette (maxSX, maxSY);

    white = gdImageColorAllocate(C, gdRedMax, gdGreenMax, gdBlueMax);
    black = gdImageColorAllocate(C, 0, 0, 0);

    if (maxSX > minSX && maxSY > minSY)
	gdImageFilledRectangle(C, minSX, minSY, maxSX-1, maxSY-1, black);

    rc = imageDiff (A, B, C, minSX, minSY, black, white);

#ifdef HAVE_GD_PNG
    if ((argc > 3) && ((f = fopen(argv[3], "wb")))) {
	gdImagePng (C, f);
	fclose(f);
    }
    else
        gdImagePng (C, stdout);
#else

    fprintf(stderr, "PNG output support is not available\n");
#endif

    gdImageDestroy(A);
    gdImageDestroy(B);
    gdImageDestroy(C);

    return (rc ? EXIT_FAILURE : EXIT_SUCCESS);
}