Example #1
0
static void init_gd()
{
	SP = 0;
	/* must create default background color first... */
	/* we have to force the background to be filled for some reason */
	white = gdImageColorResolve(im, gdRedMax,gdGreenMax,gdBlueMax);
	gdImageFilledRectangle(im, 0, 0, im->sx-1, im->sy-1, white);

	black = gdImageColorResolve(im, 0, 0, 0);

	/* in truecolor images we don't need to allocate a color
		for transparent */
	if (! im->trueColor) {
		/* transparent uses an rgb value very close to white
	   	so that formats like GIF that don't support
	   	transparency show a white background */
		transparent = gdImageColorResolveAlpha(im, gdRedMax,gdGreenMax,gdBlueMax-1, gdAlphaTransparent);
		gdImageColorTransparent(im, transparent);
	}

	cstk[0].pencolor = black;		/* set pen black*/
	cstk[0].fillcolor = black;		/* set fill black*/
	cstk[0].fontfam = "times";		/* font family name */
	cstk[0].fontopt = REGULAR;		/* modifier: REGULAR, BOLD or ITALIC */
	cstk[0].pen = P_SOLID;		/* pen pattern style, default is solid */
	cstk[0].fill = P_NONE;
	cstk[0].penwidth = WIDTH_NORMAL;
}
Example #2
0
File: mimg.c Project: adderly/cmoon
NEOERR* mimg_create_from_string(char *s, char *path, double size, void **pic)
{
    gdImagePtr im;
    int draw, back, brect[8], x, y;
    char *gderr;

    MCS_NOT_NULLB(s, pic);
    
    gderr = gdImageStringFT(NULL, &brect[0], 0, path, size, 0., 0, 0, s);
    if (gderr) return nerr_raise(NERR_ASSERT, "create image failure %s", gderr);
    
    x = brect[2]-brect[6] + 6;
    y = brect[3]-brect[7] + 6;
    im = gdImageCreate(x, y);

    if (!im) return nerr_raise(NERR_ASSERT, "create image failure");
    
    /* background color */
    back = gdImageColorResolve(im, 252, 252, 252);
    /* foreground color */
    draw = gdImageColorResolve(im, 150, 40, 40);
    
    x = 3 - brect[6];
    y = 3 - brect[7];
    
    gderr = gdImageStringFT(im, &brect[0], draw, path, size, 0.0, x, y, s);
    if (gderr) return nerr_raise(NERR_ASSERT, "rend image failure %s", gderr);
    
    *pic = (void*)im;

    return STATUS_OK;
}
int main(int argc, char *argv[])
{
#ifndef HAVE_LIBTTF
	fprintf(stderr, "gd was not compiled with HAVE_LIBTTF defined.\n");
	fprintf(stderr, "Install the FreeType library, including the\n");
	fprintf(stderr, "header files. Then edit the gd Makefile, type\n");
	fprintf(stderr, "make clean, and type make again.\n");
	return 1;
#else
	gdImagePtr im;
	int black;
	int white;
	int brect[8];
	int x, y;
	char *err;

#ifdef JISX0208
	char *s = "Hello. こんにちは Qyjpqg,"; /* String to draw. */
#else
	char *s = "Hello. Qyjpqg,"; /* String to draw. */
#endif
	double sz = 40.;
	double angle = DEG2RAD(-90);
#ifdef JISX0208
	char *f = "/usr/openwin/lib/locale/ja/X11/fonts/TT/HG-MinchoL.ttf"; /* UNICODE */
	/* char *f = "/usr/local/lib/fonts/truetype/DynaFont/dfpop1.ttf"; */ /* SJIS */
#else
	char *f = "/usr/local/lib/fonts/truetype/misc/times.ttf"; /* TrueType font */
#endif
	
	/* obtain brect so that we can size the image */
	err = gdImageStringTTF((gdImagePtr)NULL,&brect[0],0,f,sz,angle,0,0,s);
	if (err) {fprintf(stderr,err); return 1;}

	/* create an image just big enough for the string */
	x = MAXX(brect) - MINX(brect) + 6;
	y = MAXY(brect) - MINY(brect) + 6;
	im = gdImageCreate(x,y);

	/* Background color (first allocated) */
	white = gdImageColorResolve(im, 255, 255, 255);
	black = gdImageColorResolve(im, 0, 0, 0);

	/* render the string, offset origin to center string*/
	x = 0 - MINX(brect) + 3;
	y = 0 - MINY(brect) + 3;
	err = gdImageStringTTF(im,&brect[0],black,f,sz,angle,x,y,s);
	if (err) {fprintf(stderr,err); return 1;}

	/* Write img to stdout */
	gdImagePng(im, stdout);

	/* Destroy it */
	gdImageDestroy(im);

	return 0;
#endif /* HAVE_TTF */
}	
/* prints the arrow into the image poined to by the pointer with given
   color*/
void ocin_vis_arrow::print(gdImagePtr im, float scale_val) {

  int thick = ((int)((float)thickness *scale_val)) + 1;

  int scaled_a_size = (int) ((float)(a_size - 4) *scale_val) + 4;

  // create points for arrow head based on scale factor
  if (dir == S) {
    points[1].x = x2 - scaled_a_size;
    points[1].y = y2 - scaled_a_size;
    points[2].x = x2 + scaled_a_size;
    points[2].y = y2 - scaled_a_size;
  } else if (dir == E) {
    points[1].x = x2 - scaled_a_size;
    points[1].y = y2 + scaled_a_size;
    points[2].x = x2 - scaled_a_size;
    points[2].y = y2 - scaled_a_size;
  } else if (dir == N) {
    points[1].x = x2 - scaled_a_size;
    points[1].y = y2 + scaled_a_size;
    points[2].x = x2 + scaled_a_size;
    points[2].y = y2 + scaled_a_size;
  } else if (dir == W) {
    points[1].x = x2 + scaled_a_size;
    points[1].y = y2 + scaled_a_size;
    points[2].x = x2 + scaled_a_size;
    points[2].y = y2 - scaled_a_size;
  }
  // setup the arrow head points
  points[0].x = x2;             // first point is end of the line
  points[0].y = y2;

  // select the color for the arrow head
  int cnt = 255 - ((int)(250.0*scale_val));
  cnt = cnt &0xfffffffe;	// make sure we have an even number
				// (?)
  int color;
  color = gdImageColorResolve(im, cnt, cnt, cnt);

  // if (cnt < 0) {
  //   color = gdImageColorResolve(im, 0, 255+cnt, (cnt*-1));
  // } else {
  //   color = gdImageColorResolve(im, cnt, 255-cnt, 0);
  // }


  
  // draw the arrowhead first (why not)
  gdImageFilledPolygon(im, points, 3, color);

  // then the line
  gdImageSetThickness(im, thick);
  gdImageLine(im, x1, y1, x2, y2, color);
  
 
  
}
Example #5
0
int msImageSetPenGD(gdImagePtr img, colorObj *color)
{
    if(MS_VALID_COLOR(*color))
        color->pen = gdImageColorResolve(img, color->red, color->green, color->blue);
    else
        color->pen = -1;

    return(MS_SUCCESS);
}
Example #6
0
void doColorAllocate(FILE *stream) {
  int r, g, b, idx;

  r = getNumber(stream);
  g = getNumber(stream);
  b = getNumber(stream);
  // idx = gdImageColorAllocate(image, r, g, b);
  idx = gdImageColorResolve(image, r, g, b);
  // fprintf(stderr, "(%d, %d, %d) -> %d [%d]\n", r, g, b, idx, colorptr);
  colors[colorptr] = idx;
  colorptr++;
  sprintf(cmdresult, "%d", idx);
}
Example #7
0
static int gd_resolve_color(char* name)
{
	color_t	color;
 
	if (!(strcmp(name,"transparent"))) {
	    	/* special case for "transparent" color */
		return gdImageGetTransparent(im);
	} 
	else {
		colorxlate(name,&color,RGBA_BYTE);
		return gdImageColorResolve(im,color.u.rgba[0],color.u.rgba[1],color.u.rgba[2]);
	}
}
Example #8
0
void plD_set_pixel_gd (PLStream *pls, short x, short y, PLINT colour)
{
png_Dev *dev=(png_Dev *)pls->dev;
int R,G,B;
int Colour;

   G=GetGValue(colour);
   R=GetRValue(colour);
   B=GetBValue(colour);

   Colour=gdImageColorResolve(dev->im_out,R,G,B);
   gdImageSetPixel(dev->im_out, x, y,Colour);
}
int 
main (int argc, char *argv[])
{
  gdImagePtr im = gdImageCreate (WIDTH, HEIGHT);
  int white = gdImageColorResolve (im, 0xFF, 0xFF, 0xFF), black = gdImageColorResolve (im, 0, 0, 0),
    red = gdImageColorResolve (im, 0xFF, 0xA0, 0xA0);
  FILE *out;

  /* filled arc - circle */
  gdImageFilledArc (im, WIDTH / 5, HEIGHT / 4, 200, 200, 45, 90, red, gdPie);
  gdImageArc (im, WIDTH / 5, HEIGHT / 4, 200, 200, 45, 90, black);

  /* filled arc - ellipse */
  gdImageFilledArc (im, WIDTH / 2, HEIGHT / 4, 200, 150, 45, 90, red, gdPie);
  gdImageArc (im, WIDTH / 2, HEIGHT / 4, 200, 150, 45, 90, black);


  /* reference lines */
  gdImageLine (im, 0, HEIGHT / 4, WIDTH, HEIGHT / 4, black);
  gdImageLine (im, WIDTH / 5, 0, WIDTH / 5, HEIGHT, black);
  gdImageLine (im, WIDTH / 2, 0, WIDTH / 2, HEIGHT, black);
  gdImageLine (im, WIDTH / 2, HEIGHT / 4, WIDTH / 2 + 300, HEIGHT / 4 + 300, black);
  gdImageLine (im, WIDTH / 5, HEIGHT / 4, WIDTH / 5 + 300, HEIGHT / 4 + 300, black);

  /* TBB: Write img to test/arctest.png */
  out = fopen ("test/arctest.png", "wb");
  if (!out)
    {
      fprintf (stderr, "Can't create test/arctest.png\n");
      exit (1);
    }
  gdImagePng (im, out);
  fclose (out);
  fprintf (stderr, "Test image written to test/arctest.png\n");
  /* Destroy it */
  gdImageDestroy (im);

  return 0;
}
Example #10
0
result_t Image::colorResolve(int32_t red, int32_t green, int32_t blue,
                             int32_t &retVal)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    if (red < 0 || red > 255 || green < 0 || green > 255 || blue < 0
            || blue > 255)
        return CHECK_ERROR(CALL_E_INVALIDARG);

    retVal = gdImageColorResolve(m_image, red, green, blue);
    return 0;
}
Example #11
0
//for libgd2 < 2.99.999
gdImagePtr
gdImageCreateFromBmpPtr(int size, void *data)
{
	gdImagePtr ret;
	bmphandle_t bh;
	int h, w, i, j;
	struct bgrpixel r;
	int color;
	bh = bmp_open(data, size);
	if (bh == NULL)
		return NULL;
	h = bmp_height(bh);
	w = bmp_width(bh);
	ret = gdImageCreateTrueColor(w, h);
	for (i = 0; i < w; i++) {
		for (j = 0; j < h; j++) {
			r = bmp_getpixel(bh, i, j);
			color = gdImageColorResolve(ret, r.r, r.g, r.b);
			gdImageSetPixel(ret, i, j, color);
		}
	}
	bmp_close(bh);
	return ret;
}
Example #12
0
File: gdxpm.c Project: johlim/study
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm(char *filename)
{
	XpmInfo info;
	XpmImage image;
	unsigned int i, j, k, number, len;
	char buf[5];
	gdImagePtr im = 0;
	int *pointer;
	int red = 0, green = 0, blue = 0;
	int *colors;
	int ret;

	ret = XpmReadFileToXpmImage(filename, &image, &info);
	if(ret != XpmSuccess) {
		return 0;
	}

	number = image.ncolors;
	if(overflow2(sizeof(int), number)) {
		goto done;
	}

	colors = (int *)gdMalloc(sizeof(int) * number);
	if(colors == NULL) {
		goto done;
	}

	if(!(im = gdImageCreate(image.width, image.height))) {
		gdFree(colors);
		goto done;
	}

	for(i = 0; i < number; i++) {
		char *c_color = image.colorTable[i].c_color;
		if(strcmp(c_color, "None") == 0) {
		  colors[i] = gdImageGetTransparent(im);
		  if(colors[i] == -1) colors[i] = gdImageColorAllocate(im, 0, 0, 0);
		  if(colors[i] != -1) gdImageColorTransparent(im, colors[i]);
		  continue;
		}
		len = strlen(c_color);
		if(len < 1) continue;
		if(c_color[0] == '#') {
			switch(len) {
			case 4:
				buf[2] = '\0';
				buf[0] = buf[1] = c_color[1];
				red = strtol(buf, NULL, 16);

				buf[0] = buf[1] = c_color[2];
				green = strtol(buf, NULL, 16);

				buf[0] = buf[1] = c_color[3];
				blue = strtol(buf, NULL, 16);
				break;

			case 7:
				buf[2] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				red = strtol(buf, NULL, 16);

				buf[0] = c_color[3];
				buf[1] = c_color[4];
				green = strtol(buf, NULL, 16);

				buf[0] = c_color[5];
				buf[1] = c_color[6];
				blue = strtol(buf, NULL, 16);
				break;

			case 10:
				buf[3] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				buf[2] = c_color[3];
				red = strtol(buf, NULL, 16);
				red /= 64;

				buf[0] = c_color[4];
				buf[1] = c_color[5];
				buf[2] = c_color[6];
				green = strtol(buf, NULL, 16);
				green /= 64;

				buf[0] = c_color[7];
				buf[1] = c_color[8];
				buf[2] = c_color[9];
				blue = strtol(buf, NULL, 16);
				blue /= 64;
				break;

			case 13:
				buf[4] = '\0';
				buf[0] = c_color[1];
				buf[1] = c_color[2];
				buf[2] = c_color[3];
				buf[3] = c_color[4];
				red = strtol(buf, NULL, 16);
				red /= 256;

				buf[0] = c_color[5];
				buf[1] = c_color[6];
				buf[2] = c_color[7];
				buf[3] = c_color[8];
				green = strtol(buf, NULL, 16);
				green /= 256;

				buf[0] = c_color[9];
				buf[1] = c_color[10];
				buf[2] = c_color[11];
				buf[3] = c_color[12];
				blue = strtol(buf, NULL, 16);
				blue /= 256;
				break;
			}
		} else if(!gdColorMapLookup(GD_COLOR_MAP_X11, c_color, &red, &green, &blue)) {
			continue;
		}

		colors[i] = gdImageColorResolve(im, red, green, blue);
	}

	pointer = (int *)image.data;

	for(i = 0; i < image.height; i++) {
		for(j = 0; j < image.width; j++) {
			k = *pointer++;
			gdImageSetPixel(im, j, i, colors[k]);
		}
	}

	gdFree(colors);

 done:
	XpmFreeXpmImage(&image);
	XpmFreeXpmInfo(&info);
	return im;
}
Example #13
0
static int setlinestyle (wmfAPI* API,wmfDC* dc)
{	wmf_gd_t* ddata = WMF_GD_GetData (API);

	gd_t* gd = (gd_t*) ddata->gd_data;

	wmfPen* pen = 0;

	wmfRGB* rgb = 0;

	int pen_width;
	int pen_height;

	int fg;
	int bg;

	int linestyle;

	int i;

	int style[14];

	pen = WMF_DC_PEN (dc);

	/* Color
	 */
	rgb = WMF_PEN_COLOR (pen);
	fg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b);

	rgb = WMF_DC_BACKGROUND (dc);
	bg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b);

	linestyle = fg;

	/* Size
	 */
	pen_width  = (int) ceil ((double) gd_width  (API,WMF_PEN_WIDTH  (pen)));
	pen_height = (int) ceil ((double) gd_height (API,WMF_PEN_HEIGHT (pen)));

	if (pen_width  < 1) pen_width  = 1;
	if (pen_height < 1) pen_height = 1;

	if ((pen_width == 1) && (pen_height == 1))
	{	/* Ordinary line */
	}
	else
	{	if (gd->pen.image == 0)
		{	/* Need to create a brush */
			gd->pen.image = gdImageCreateTrueColor (pen_width,pen_height);
		}
		else if ((pen_width == gd->pen.width) && (pen_height == gd->pen.height))
		{	/* Already have brush of correct size */
		}
		else
		{	/* Need to free current brush & create new one */
			gdImageDestroy (gd->pen.image);

			gd->pen.image = gdImageCreateTrueColor (pen_width,pen_height);
		}
		if (gd->pen.image)
		{	gd->pen.width  = pen_width;
			gd->pen.height = pen_height;

			rgb = WMF_PEN_COLOR (pen);
			fg = gdImageColorResolve (gd->pen.image,rgb->r,rgb->g,rgb->b);

			rgb = WMF_DC_BACKGROUND (dc);
			bg = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b);

			if (bg != fg) gdImageColorTransparent (gd->pen.image,bg);

			gdImageFilledRectangle (gd->pen.image,0,0,gd->pen.width,gd->pen.height,bg);

			/* `Square' nib
			 */
			gdImageFilledRectangle (gd->pen.image,0,0,gd->pen.width,gd->pen.height,fg);

			gdImageSetBrush (gd->image,gd->pen.image);

			linestyle = gdBrushed;
		}
	}

	switch (WMF_PEN_STYLE (pen))
	{
	case PS_DASH:
		for (i =  0; i <  5; i++) style[i] = fg;
		for (i =  5; i <  8; i++) style[i] = bg;

		gdImageSetStyle (gd->image,style, 8);

		if (linestyle == gdBrushed)
		{	linestyle = gdStyledBrushed;
		}
		else
		{	linestyle = gdStyled;
		}
	break;

	case PS_DOT:
		for (i =  0; i <  2; i++) style[i] = fg;
		for (i =  2; i <  4; i++) style[i] = bg;

		gdImageSetStyle (gd->image,style, 4);

		if (linestyle == gdBrushed)
		{	linestyle = gdStyledBrushed;
		}
		else
		{	linestyle = gdStyled;
		}
	break;

	case PS_DASHDOT:
		for (i =  0; i <  4; i++) style[i] = fg;
		for (i =  4; i <  7; i++) style[i] = bg;
		for (i =  7; i <  9; i++) style[i] = fg;
		for (i =  9; i < 12; i++) style[i] = bg;

		gdImageSetStyle (gd->image,style,12);

		if (linestyle == gdBrushed)
		{	linestyle = gdStyledBrushed;
		}
		else
		{	linestyle = gdStyled;
		}
	break;

	case PS_DASHDOTDOT:
		for (i =  0; i <  4; i++) style[i] = fg;
		for (i =  4; i <  6; i++) style[i] = bg;
		for (i =  6; i <  8; i++) style[i] = fg;
		for (i =  8; i < 10; i++) style[i] = bg;
		for (i = 10; i < 12; i++) style[i] = fg;
		for (i = 12; i < 14; i++) style[i] = bg;

		gdImageSetStyle (gd->image,style,14);

		if (linestyle == gdBrushed)
		{	linestyle = gdStyledBrushed;
		}
		else
		{	linestyle = gdStyled;
		}
	break;

	case PS_INSIDEFRAME: /* There is nothing to do in this case... */
	case PS_SOLID:
	default:
	break;
	}

	return (linestyle);
}
Example #14
0
static int setbrushstyle (wmfAPI* API,wmfDC* dc)
{	wmf_gd_t* ddata = WMF_GD_GetData (API);

	gd_t* gd = (gd_t*) ddata->gd_data;

	wmfBrush* brush = 0;

	wmfBMP* bmp = 0;

	wmfRGB* rgb = 0;

	wmfRGB pixel;

	int i;
	int j;

	int fg;
	int bg;

	int brushstyle;

	int opacity;

	unsigned int x;
	unsigned int y;

	unsigned char* bits = 0;

	brush = WMF_DC_BRUSH (dc);

	rgb = WMF_BRUSH_COLOR (brush);
	brushstyle = gdImageColorResolve (gd->image,rgb->r,rgb->g,rgb->b);

	switch (WMF_BRUSH_STYLE (brush))
	{
	case BS_NULL:
		WMF_ERROR (API,"Attempt to set null fill-style!");
		API->err = wmf_E_Glitch;
	break;

	case BS_HATCHED:
		switch (WMF_BRUSH_HATCH (brush))
		{
		case HS_HORIZONTAL:
			bits = HS_HORIZONTAL_bits;
		break;

		case HS_VERTICAL:
			bits = HS_VERTICAL_bits;
		break;

		case HS_FDIAGONAL:
			bits = HS_FDIAGONAL_bits;
		break;

		case HS_BDIAGONAL:
			bits = HS_BDIAGONAL_bits;
		break;

		case HS_CROSS:
			bits = HS_CROSS_bits;
		break;

		case HS_DIAGCROSS:
			bits = HS_DIAGCROSS_bits;
		break;

		default:
			if (API->flags & WMF_OPT_IGNORE_NONFATAL)
			{	WMF_DEBUG (API,"Unsupported brush/hatch style!");
				bits = HS_DIAGCROSS_bits;
			}
			else
			{	WMF_ERROR (API,"Unsupported brush/hatch style!");
				API->err = wmf_E_Glitch;
				bits = 0;
			}
		break;
		}
		if (bits == 0) break;

		if (gd->brush.hatch == 0) gd->brush.hatch = gdImageCreateTrueColor (8,8);

		if (gd->brush.hatch)
		{	rgb = WMF_DC_BACKGROUND (dc);
			bg = gdImageColorResolve (gd->brush.hatch,rgb->r,rgb->g,rgb->b);

			if (!WMF_DC_OPAQUE (dc))
			{	gdImageColorTransparent (gd->brush.hatch,bg);
			}

			rgb = WMF_BRUSH_COLOR (brush);
			fg = gdImageColorResolve (gd->brush.hatch,rgb->r,rgb->g,rgb->b);

			for (j = 0; j < 8; j++)
			{	for (i = 0; i < 8; i++)
				{	if (bits[j] & (1<<(7-i)))
					{	gdImageSetPixel (gd->brush.hatch,i,j,fg);
					}
					else
					{	gdImageSetPixel (gd->brush.hatch,i,j,bg);
					}
				}
			}

			gdImageSetTile (gd->image,gd->brush.hatch);

			brushstyle = gdTiled;
		}
	break;

	case BS_DIBPATTERN:
		bmp = WMF_BRUSH_BITMAP (brush);

		if (bmp->data == 0)
		{	if (API->flags & WMF_OPT_IGNORE_NONFATAL)
			{	WMF_DEBUG (API,"Attempt to fill with non-existent pattern!");
				break;
			}
			else
			{	WMF_ERROR (API,"Attempt to fill with non-existent pattern!");
				API->err = wmf_E_Glitch;
				break;
			}
		}
		if (gd->brush.image)
		{	if ((gd->brush.width  != (int) bmp->width )
			 || (gd->brush.height != (int) bmp->height))
			{	gdImageDestroy (gd->brush.image);
				gd->brush.image = 0;
			}
		}
		if (gd->brush.image == 0)
		{	gd->brush.width  = (int) bmp->width;
			gd->brush.height = (int) bmp->height;

			gd->brush.image = gdImageCreateTrueColor (gd->brush.width,gd->brush.height);
		}
		if (gd->brush.image)
		{	for (y = 0; y < (unsigned int) gd->brush.height; y++)
			{	for (x = 0; x < (unsigned int) gd->brush.width; x++)
				{	opacity = wmf_ipa_bmp_color (API,bmp,&pixel,x,y);

					fg = gdImageColorResolve (gd->brush.image,pixel.r,pixel.g,pixel.b);

					gdImageSetPixel (gd->brush.image,(int) x,(int) y,fg);
				}
			}

			gdImageSetTile (gd->image,gd->brush.image);

			brushstyle = gdTiled;
		}
	break;

	default:
		if (API->flags & WMF_OPT_IGNORE_NONFATAL)
		{	WMF_DEBUG (API,"Unsupported brush style!");
			/* no break here */
		}
		else
		{	WMF_ERROR (API,"Unsupported brush style!");
			API->err = wmf_E_Glitch;
			break;
		}
	case BS_SOLID:
	break;
	}

	return (brushstyle);
}
Example #15
0
File: gdxpm.c Project: kanbang/Colt
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm (char *filename)
{
  XpmInfo info;
  XpmImage image;
  int i, j, k, number;
  char buf[5];
  gdImagePtr im = 0;
  int *pointer;
  int red = 0, green = 0, blue = 0;
  int *colors;
  int ret;

  ret = XpmReadFileToXpmImage (filename, &image, &info);
  if (ret != XpmSuccess)
    return 0;

  if (!(im = gdImageCreate (image.width, image.height)))
    return 0;

  number = image.ncolors;
  colors = (int *) gdMalloc (sizeof (int) * number);
  if (colors == NULL)
    return (0);
  for (i = 0; i < number; i++)
    {
      switch (strlen (image.colorTable[i].c_color))
	{
	case 4:
	  buf[1] = '\0';
	  buf[0] = image.colorTable[i].c_color[1];
	  red = strtol (buf, NULL, 16);

	  buf[0] = image.colorTable[i].c_color[3];
	  green = strtol (buf, NULL, 16);

	  buf[0] = image.colorTable[i].c_color[5];
	  blue = strtol (buf, NULL, 16);
	  break;
	case 7:
	  buf[2] = '\0';
	  buf[0] = image.colorTable[i].c_color[1];
	  buf[1] = image.colorTable[i].c_color[2];
	  red = strtol (buf, NULL, 16);

	  buf[0] = image.colorTable[i].c_color[3];
	  buf[1] = image.colorTable[i].c_color[4];
	  green = strtol (buf, NULL, 16);

	  buf[0] = image.colorTable[i].c_color[5];
	  buf[1] = image.colorTable[i].c_color[6];
	  blue = strtol (buf, NULL, 16);
	  break;
	case 10:
	  buf[3] = '\0';
	  buf[0] = image.colorTable[i].c_color[1];
	  buf[1] = image.colorTable[i].c_color[2];
	  buf[2] = image.colorTable[i].c_color[3];
	  red = strtol (buf, NULL, 16);
	  red /= 64;

	  buf[0] = image.colorTable[i].c_color[4];
	  buf[1] = image.colorTable[i].c_color[5];
	  buf[2] = image.colorTable[i].c_color[6];
	  green = strtol (buf, NULL, 16);
	  green /= 64;

	  buf[0] = image.colorTable[i].c_color[7];
	  buf[1] = image.colorTable[i].c_color[8];
	  buf[2] = image.colorTable[i].c_color[9];
	  blue = strtol (buf, NULL, 16);
	  blue /= 64;
	  break;
	case 13:
	  buf[4] = '\0';
	  buf[0] = image.colorTable[i].c_color[1];
	  buf[1] = image.colorTable[i].c_color[2];
	  buf[2] = image.colorTable[i].c_color[3];
	  buf[3] = image.colorTable[i].c_color[4];
	  red = strtol (buf, NULL, 16);
	  red /= 256;

	  buf[0] = image.colorTable[i].c_color[5];
	  buf[1] = image.colorTable[i].c_color[6];
	  buf[2] = image.colorTable[i].c_color[7];
	  buf[3] = image.colorTable[i].c_color[8];
	  green = strtol (buf, NULL, 16);
	  green /= 256;

	  buf[0] = image.colorTable[i].c_color[9];
	  buf[1] = image.colorTable[i].c_color[10];
	  buf[2] = image.colorTable[i].c_color[11];
	  buf[3] = image.colorTable[i].c_color[12];
	  blue = strtol (buf, NULL, 16);
	  blue /= 256;
	  break;
	}


      colors[i] = gdImageColorResolve (im, red, green, blue);
      if (colors[i] == -1)
	fprintf (stderr, "ARRRGH\n");
    }

  pointer = (int *) image.data;
  for (i = 0; i < image.height; i++)
    {
      for (j = 0; j < image.width; j++)
	{
	  k = *pointer++;
	  gdImageSetPixel (im, j, i, colors[k]);
	}
    }
  gdFree (colors);
  return (im);
}
Example #16
0
/*
 * this is the funcion that writes the text to the image.
 * almost everything is taken from libgd examples
 */
int createimage(CONF *config, char *lines[IMG_TEXTLINES], char *path_for_picture)
{
	FILE *in, *out;
	char *path;
	gdImagePtr im;
	int white, i;
	int brect[8];

	if ( (in = fopen(config->font, "r")) == NULL) {
		perror("font not found\ncheck casrc\n");
		return 0;
	}
	fclose(in);

	if ( (in = fopen(config->source, "rb")) == NULL) {
		perror("source_image not found\ncheck casrc\n");
		return 0;
	}

	im = gdImageCreateFromPng(in);
	if( NULL == im) {
		perror("Error loading source image (not a valid png image file?).\n");
		return 0;
	}
	white = gdImageColorResolve(im, 255, 255, 255);

	for (i = 0; i < IMG_TEXTLINES; i++) {
		if (config->enabled[i] == 1) {
			gdImageStringFT(im, &brect[0], white, config->font, config->size,
					0.0, config->x[i], config->y[i], lines[i]);
		}
	}
	
	if (config->img_type==0) {
		path = get_amule_path("aMule-online-sign.png", 0, path_for_picture);
	} else {
		path = get_amule_path("aMule-online-sign.jpg", 0, path_for_picture);
	}
		
	if (path == NULL && config->img_type==0) {
		perror("could not get PNG path\n");
		return 0;
	} else if (path == NULL) {
		perror("could not get JPG path\n");
		return 0;
	}
	out = fopen(path, "w");
	free(path);
	
	if (config->img_type==0) {
		gdImagePng(im, out);
	} else {
		gdImageJpeg(im, out, -1);
	}
	
	fclose(out);
	printf("Online Signature picture created.\n");
	gdImageDestroy(im);

	return 1;
}
Example #17
0
int main(int argc, char **argv, char **env) {
/*=========== GENERIC VARIABLES ===========*/
	FILE *f_output = NULL, *f_input = NULL;
	int c, i;
	char *end;
	
/*=========== GETOPT VARIABLES ===========*/
	char const *const optstr = "hi:o:f:c:w:b:n:t:";
#ifdef _GNU_SOURCE
	static struct option long_opts[] = {
		{"help", 0, NULL, 'h'},
		{"infile", 1, NULL, 'i'},
		{"outfile", 1, NULL, 'o'},
		{"format", 1, NULL, 'f'},
		{"colors", 1, NULL, 'c'},
		{"width", 1, NULL, 'w'},
		{"bgcolor", 1, NULL, 'b'},
		{"newcolor", 1, NULL, 'n'},
		{"color-tolerance", 1, NULL, 't'},
		{NULL, 0, NULL, 0},
	};
#endif

/*=========== IMAGE-RELATED VARIABLES ===========*/
	int x, y;
	int color_idx = 0;
	int img_orig_h = 0, img_orig_w = 0;
	int img_targ_h = 0, img_targ_w = 0;
	int top = -1, bottom = -1, top_prev = -1, bottom_prev = -1;
	int color = -1, color_prev = -1;
	gdImagePtr image_data = NULL;
	gdImagePtr image_resized = NULL;
	gdImageCreateFunction image_openers[] = {
		NULL,
		(gdImageCreateFunction) gdImageCreateFromJpeg,
		(gdImageCreateFunction) gdImageCreateFromPng,
		(gdImageCreateFunction) gdImageCreateFromGif,
		(gdImageCreateFunction) gdImageCreateFromWBMP,
		(gdImageCreateFunction) gdImageCreateFromXpm	/* Accepts char * as first argument! */
	};

	
	while (true) {
#ifdef _GNU_SOURCE
		c = getopt_long(argc, argv, optstr, long_opts, NULL);
#else /* not _GNU_SOURCE */ 
		c = getopt(argc, argv, optstr);
#endif /* _GNU_SOURCE */
		if (c == -1) break;

		switch (c) {
			case 'h':
				help(stdout, EXEC_NAME);
				exit(ALL_OK);
				break;
			case 'i':
				i = strlen(optarg);
				if (i2u_conf.m_infile != NULL) free(i2u_conf.m_infile);
				if ((i2u_conf.m_infile = (char *) calloc(i+1, sizeof(char))) == NULL) {
					fprintf(stderr, "calloc() failed!\n");
					cleanup_main();
					exit(ERR_OUT_OF_MEMORY);
				}
				strncpy(i2u_conf.m_infile, optarg, i);
				break;
			case 'o':
				if (strncmp("-", optarg, 2) == 0) {
					if (i2u_conf.m_outfile != NULL) free(i2u_conf.m_outfile);
					i2u_conf.m_outfile = NULL;
					break;
				}
				else {
					i = strlen(optarg);
					if (i2u_conf.m_outfile != NULL) free(i2u_conf.m_outfile);
					if ((i2u_conf.m_outfile = (char *) calloc(i+1, sizeof(char))) == NULL) {
						fprintf(stderr, "calloc() failed!\n");
						cleanup_main();
						exit(ERR_OUT_OF_MEMORY);
					}
					strncpy(i2u_conf.m_outfile, optarg, i);
					break;
				}
			case 'f':
				i = (int) strtol(optarg, &end, 10);
				if (optarg != end) {
					i2u_conf.m_outformat = (i > 8) ? unicode : ansi;
				}
				else {	
					if (strncasecmp(optarg, "ansi", 5) == 0) i2u_conf.m_outformat = ansi;
					else {
						if (strncmp(optarg, "unicode", 8) == 0) i2u_conf.m_outformat = unicode;
						else fprintf(stderr, "Warning: invalid format specified. Defaulting to Unicode.\n"); 
					}
				}
				break;
			case 'c':
				i = (int) strtol(optarg, &end, 10);
				if (optarg != end) { 
					if (i < 9) i2u_conf.m_colors = low;
					else {
						if (i < 17) i2u_conf.m_colors = basic;
						else i2u_conf.m_colors = extended;
					}
				}
				else fprintf(stderr, "Warning: invalid color depth specified. Defaulting to 256.\n");
				break;
			case 'w':
				i = (int) strtol(optarg, &end, 10);
				if (optarg != end) i2u_conf.m_targ_width = i;
				else fprintf(stderr, "Warning: invalid width specified. Defaulting to 80.\n");
				break;
			case 't':
				i = (int) strtol(optarg, &end, 10);
				if (optarg != end) i2u_conf.m_toleranceradius = (i > 0) ? i : 0;
				else fprintf(stderr, "Warning: invalid color-tolerance radius specified. Defaulting to zero.\n");
				break;
			case 'b':
				i = strtol(optarg, &end, 16);
				if (optarg != end) i2u_conf.m_bgcolor = i;
				else fprintf(stderr, "Warning: invalid background color specified. No background-substitution will occur.\n");
				break;
			case 'n':
				i = strtol(optarg, &end, 16);
				if (optarg != end) i2u_conf.m_newcolor = i;
				else fprintf(stderr, "Warning: invalid substitution color specified. No background-substitution will occur.\n");
				break;
			case '?':
			default:
				help(stderr, EXEC_NAME);
				exit(ERR_INVALID_ARG);
		}
		if (c == 'x') break;
	}

		
		
	if (i2u_conf.m_infile == NULL) {
		fprintf(stderr, "Error: input file not set. Use option" 
#ifdef _GNU_SOURCE
			"s -i or --infile"
#else /* not _GNU_SOURCE */ 
			" -i"
#endif /* _GNU_SOURCE */
			" to specify one.\n");
		cleanup_main();
		exit(ERR_INVALID_ARG);
	}

/*=========== GD PART ==========*/
	
	i2u_conf.m_ftype = get_image_type(i2u_conf.m_infile);
	
	if (i2u_conf.m_ftype == UNKNOWN) {
		fprintf(stderr, "Error: unsupported input file type!\n" );
		cleanup_main();
		exit(ERR_UNSUPPORTED_FORMAT);
	}
	
	if (i2u_conf.m_ftype != xpm) {
			if ((f_input = fopen(i2u_conf.m_infile, "r")) == NULL) {
				perror("fopen(): ");
				cleanup_main();
				exit(ERR_FOPEN_FAIL);
			}
		
			image_data = image_openers[i2u_conf.m_ftype](f_input);
			
			fclose(f_input);
	}
	else {
			if ((image_data = image_openers[i2u_conf.m_ftype](i2u_conf.m_infile)) == NULL) {
				fprintf(stderr, "gdImageCreateFromXpm(): Error occured while opening file \'%s\'\n", i2u_conf.m_infile);
				cleanup_main();
				exit(ERR_GD_XPMOPEN_FAIL);
			}
	}
	
	img_orig_w = image_data->sx;
	img_orig_h = image_data->sy;
	img_targ_w = i2u_conf.m_targ_width;
	img_targ_h = (int) round(((double) img_targ_w)/((double) img_orig_w)*((double) img_orig_h));
	/* if (i2u_conf.m_outformat == ansi) img_targ_w /= 2; */
	
	/* change background, if requested */
	if ((i2u_conf.m_bgcolor > -1) && (i2u_conf.m_newcolor > -1)) {
			int bg_r, bg_g, bg_b;
			int nc_r, nc_g, nc_b;
			int bgcolor, newcolor;
			int radius = i2u_conf.m_toleranceradius;
			bg_r = (i2u_conf.m_bgcolor & 0x00ff0000) >> 16;
			bg_g = (i2u_conf.m_bgcolor & 0x0000ff00) >> 8;
			bg_b = (i2u_conf.m_bgcolor & 0x000000ff);
			nc_r = (i2u_conf.m_newcolor & 0x00ff0000) >> 16;
			nc_g = (i2u_conf.m_newcolor & 0x0000ff00) >> 8;
			nc_b = (i2u_conf.m_newcolor & 0x000000ff);
			
			if ((bgcolor = gdImageColorExact(image_data, bg_r, bg_g, bg_b)) != -1) {
				color_idx = gdImageGetPixel(image_data, 0, 0);
				newcolor = gdImageColorResolve(image_data, nc_r, nc_g, nc_b);
				for (y=0; y<img_orig_h; y++) {
					for (x=0; x<img_orig_w; x++) {
					  color_idx = gdImageGetPixel(image_data, x, y);
				    if (get_color_distance(gdImageRed(image_data, color_idx), gdImageRed(image_data, bgcolor), gdImageGreen(image_data, color_idx), gdImageGreen(image_data, bgcolor), gdImageBlue(image_data, color_idx), gdImageBlue(image_data, bgcolor)) <= radius) gdImageSetPixel(image_data, x, y, newcolor);
					}
				}
			}	
	}
Example #18
0
int main()
{
	gdImagePtr im;
	int black;
	int white;
	int brect[8];
	int x, y;
	char *err;

	char s[5];	/* String to draw. */
	double sz = 40.;
	char fontpath[256];
	unsigned int r;
	char buf[50];
	char userid[IDLEN + 1];
	struct MD5Context mdc;
	unsigned int pass[4];
	if (initbbsinfo(&bbsinfo) < 0)
		return -1;
	bzero(pass, sizeof(pass));
	strsncpy(buf, getsenv("QUERY_STRING"), sizeof (buf));
	__unhcode(buf);
	if (strncmp(buf, "userid=", 7)) {
		userid[0] = 0;
	} else {
		strsncpy(userid, buf+7, IDLEN+1);
	//	errlog("%s",userid);
	}
	if (userid[0] == 0) {
		s[0] = 0;
	} else {
		MD5Init(&mdc);
		MD5Update(&mdc, (void *)(&bbsinfo.ucachehashshm->regkey), sizeof(int) * 4);
		MD5Update(&mdc, userid, strlen(userid));
		MD5Final((char *)pass, &mdc);
		sprintf(s, "%d%d%d%d", pass[0]%10, pass[1] % 10, pass[2] % 10, pass[3] % 10);
	}
	getrandomint(&r);
	sprintf(fontpath, MY_BBS_HOME "/etc/fonts/%d.ttf", r % MAXFONTS);	/* User supplied font */

	fprintf(stdout, "Content-type: image/png\r\n\r\n");
/* obtain brect so that we can size the image */
	err = gdImageStringTTF(NULL, &brect[0], 0, fontpath, sz, 0., 0, 0, s);
	if (err) {
		return 1;
	}
/* create an image big enough for the string plus a little whitespace */
	x = brect[2] - brect[6] + 6;
	y = brect[3] - brect[7] + 6;
	im = gdImageCreate(x, y);

/* Background color (first allocated) */
	white = gdImageColorResolve(im, 255, 255, 255);
	black = gdImageColorResolve(im, 0, 0, 0);

/* render the string, offset origin to center string*/
/* note that we use top-left coordinate for adjustment
 * since gd origin is in top-left with y increasing downwards. */
	x = 3 - brect[6];
	y = 3 - brect[7];
	err = gdImageStringFT(im, &brect[0], black, fontpath, sz, 0.0, x, y, s);
	if (err) {
		return 1;
	}
/* Write img to stdout */
	gdImagePng(im, stdout);

/* Destroy it */
	gdImageDestroy(im);
	return 0;
}
Example #19
0
int main()
{
	gdImagePtr im;
	int error = 0;
	int c, c1, c2, c3, c4, color, i;

	im = gdImageCreateTrueColor(5, 5);
	c = gdImageColorResolve(im, 255, 0, 255);
	c2 = gdImageColorResolveAlpha(im, 255, 0, 255, 100);
	gdImageDestroy(im);

	if (gdTestAssert(c == 0xFF00FF) != 1) {
		error = -1;
	}
	if (gdTestAssert(c2 == 0x64FF00FF) != 1) {
		error = -1;
	}

	im = gdImageCreate(5, 5);

	c1 = gdImageColorResolve(im, 255, 0, 255);
	c2 = gdImageColorResolve(im, 255, 200, 0);
	c3 = gdImageColorResolveAlpha(im, 255, 0, 255, 100);
	c4 = gdImageColorResolveAlpha(im, 255, 34, 255, 100);

	if (gdTestAssert(c1 == 0) != 1) {
		error = -1;
	}
	if (gdTestAssert(c2 == 1) != 1) {
		error = -1;
	}
	if (gdTestAssert(c3 == 2) != 1) {
		error = -1;
	}
	if (gdTestAssert(c4 == 3) != 1) {
		error = -1;
	}

	color = gdTrueColorAlpha(gdImageRed(im, c1), gdImageGreen(im, c1),
					gdImageBlue(im, c1), 0);
	if (gdTestAssert(color == 0xFF00FF) != 1) {
		error = -1;
	}
	color = gdTrueColorAlpha(gdImageRed(im, c2), gdImageGreen(im, c2),
					gdImageBlue(im, c2), 0);
	if (gdTestAssert(color == 0xFFC800) != 1) {
		error = -1;
	}
	color = gdTrueColorAlpha(gdImageRed(im, c3), gdImageGreen(im, c3),
					gdImageBlue(im, c3), 0);
	if (gdTestAssert(color == 0xFF00FF) != 1) {
		error = -1;
	}
	color = gdTrueColorAlpha(gdImageRed(im, c4), gdImageGreen(im, c4),
					gdImageBlue(im, c4), 0);
	if (gdTestAssert(color == 0xFF22FF) != 1) {
		error = -1;
	}
	gdImageDestroy(im);

	return error;
}
Example #20
0
int
main (int argc, char *argv[])
{
	gdImagePtr im;
	int blue;
	int blueAlpha;
	int white;
	int brect[8];
	int x, y, sx, sy;
	char *err;
	FILE *out;
#ifdef JISX0208
	char *s = "Hello. ‚±‚ñ‚É‚¿‚Í Qyjpqg,";	/* String to draw. */
#else
	char *s = "Hello. �ん��� Qyjpqg,";	/* String to draw. */
#endif

	double sz = 40.;

#if 0
	double angle = 0.;
#else
	double angle = DEG2RAD (90);
#endif
	char *f;
	if (argc == 2) {
		f = argv[1];
	} else {
		/* 2.02: usage message. Defaulting to Times wasn't working well for the
		   many people with no /usr/share/fonts/truetype. */
		fprintf(stderr, "Usage: gdtestft fontfilename\n"
			 "If fontfilename is not a full or relative path, GDFONTPATH is searched for\n"
		         "it. If GDFONTPATH is not set, /usr/share/fonts/truetype is searched.\n");
		return 1;
	}
	/* obtain brect so that we can size the image */
	err =
	    gdImageStringFT ((gdImagePtr) NULL, &brect[0], 0, f, sz, angle, 0, 0, s);
	if (err) {
		fprintf(stderr, "%s\n", err);
		return 1;
	}

	/* create an image just big enough for the string (x3) */
	sx = MAXX (brect) - MINX (brect) + 6;
	sy = MAXY (brect) - MINY (brect) + 6;
#if 0
	/* Would be palette color 8-bit (which of course is still allowed,
	   but not impressive when used with a JPEG background and antialiasing
	   and alpha channel and so on!) */
	im = gdImageCreate (sx * 3, sy);
#else
	/* gd 2.0: true color images can use freetype too,
	   and they can do antialiasing against arbitrary
	   complex backgrounds. */
	im = gdImageCreateTrueColor (sx * 3, sy);
#endif
	/* Background color. gd 2.0: fill the image with it; truecolor
	   images have a black background otherwise. */
	white = gdImageColorResolve (im, 255, 255, 255);
	/* Load a pretty background and resample it to cover the entire image */
	{
		FILE *in = fopen ("eleanor.jpg", "rb");
		gdImagePtr imb = NULL;
		if (in) {
#ifdef HAVE_LIBJPEG
			imb = gdImageCreateFromJpeg (in);
#else
			fprintf(stderr, "No JPEG library support.\n");
#endif
			fclose(in);

			if (!imb) {
				fprintf(stderr, "gdImageCreateFromJpeg failed\n");
				return 1;
			}
			if (!im->trueColor) {
				/* If destination is not truecolor, convert the JPEG to a
				   reasonably high-quality palette version. This is not as good
				   as creating a truecolor output file, of course. Leave many
				   colors for text smoothing. */
#if 1
				gdImageTrueColorToPalette (imb, 0, 128);
#endif
			}
			/* Resample background image to cover new image exactly */
			gdImageCopyResampled (im, imb, 0, 0, 0, 0, sx * 3, sy,
			                      gdImageSX (imb), gdImageSY (imb));
		} else {
			/* Can't get background, so paint a simple one */
			/* Truecolor images start out black, so paint it white */
			gdImageFilledRectangle (im, 0, 0, sx * 3, sy, white);
		}
	}
	/* TBB 2.0.2: only black was working, and I didn't know it because
	   the test program used black. Funny, huh? Let's do a more interesting
	   color this time.  */
	blue = gdImageColorResolve (im, 128, 192, 255);
	/* Almost-transparent blue (alpha blending), with antialiasing */
	blueAlpha = gdImageColorResolveAlpha (im, 128, 192, 255, gdAlphaMax / 2);
	/* render the string, offset origin to center string */
	x = 0 - MINX (brect) + 3;
	y = 0 - MINY (brect) + 3;

	/* With antialiasing (positive color value) */
	err = gdImageStringFT (im, NULL, blue, f, sz, angle, x, y, s);
	if (err) {
		fprintf(stderr, "%s\n", err);
		return 1;
	}
	/* Without antialiasing (negative color value) */
	err = gdImageStringFT (im, NULL, -blue, f, sz, angle, sx + x, y, s);
	if (err) {
		fprintf(stderr, "%s\n", err);
		return 1;
	}
	/* With antialiasing, and 50% alpha blending (truecolor only) */
	err = gdImageStringFT (im, NULL, blueAlpha, f, sz, angle, sx * 2 + x, y, s);
	if (err) {
		fprintf(stderr, "%s\n", err);
		return 1;
	}
	/* TBB: Write img to test/fttest.jpg or test/fttest.png */
	if (im->trueColor) {
#ifdef HAVE_LIBJPEG
		out = fopen ("test/fttest.jpg", "wb");
		if (!out) {
			fprintf(stderr, "Can't create test/fttest.jpg\n");
			exit (1);
		}
		/* Fairly high JPEG quality setting */
		gdImageJpeg (im, out, 90);
		fclose (out);
		fprintf(stderr, "Test image written to test/fttest.jpg\n");
#else
		fprintf(stderr, "Test image not written; No JPEG library support.\n");
#endif
	} else {
#ifdef HAVE_LIBPNG
		out = fopen ("test/fttest.png", "wb");
		if (!out) {
			fprintf(stderr, "Can't create test/fttest.png\n");
			exit (1);
		}
		/* 2.0.10: correct ifdef, thanks to Gabriele Verzeletti */
		gdImagePng (im, out);
		fclose (out);
		fprintf(stderr, "Test image written to test/fttest.png\n");
#else
		fprintf(stderr, "Test image not written; No PNG library support.\n");
#endif
	}
	/* Destroy it */
	gdImageDestroy (im);

	return 0;
}