void doStringUp(FILE *stream) { float x, y; int a, c, tmp; gdPoint size, point; x = getFloat(stream); y = getFloat(stream); c = getColor(getNumber(stream)); a = getNumber(stream); getLine(stream); // compute anchor position here size = stringSize(buffer, currentFontIdx); point.x = viewx(x); point.y = viewy(y); point = stringAnchor(point, size, a, 1); gdImageStringUp(image, currentFont, point.x, point.y, buffer, c); }
void stringUp(const std::string& text, const Point& point, const Color& color, Size size, int align) { // copy text into gd-friendly (unsigned char) buffer vector<unsigned char> buffer; copy(text.begin(), text.end(), back_inserter(buffer)); buffer.push_back('\0'); // choose font gdFontPtr font; switch (size) { case Tiny: font = gdFontGetTiny(); break; case Small: font = gdFontGetSmall(); break; case MediumBold: font = gdFontGetMediumBold(); break; case Large: font = gdFontGetLarge(); break; case Giant: font = gdFontGetGiant(); break; default: throw runtime_error("[ImageImpl::string()] This isn't happening."); } // calculate position Point position(SCALEX(point.x),SCALEY(point.y)); int length = (int)text.size() * font->w; int height = font->h; if (align & CenterX) position.x -= height/2; else if (align & Right) position.x -= height; if (align & CenterY) position.y -= length/2; else if (align & Bottom) position.y -= length; // draw the string vertically gdImageStringUp(im_, font, position.x, position.y, &buffer[0], color2gd(color)); }
int main (void) { #ifdef HAVE_LIBPNG /* Input and output files */ FILE *in; FILE *out; /* Input and output images */ gdImagePtr im_in = 0, im_out = 0; /* Brush image */ gdImagePtr brush; /* Color indexes */ int white; int blue; int red; int green; /* Points for polygon */ gdPoint points[3]; int i; /* Create output image, in true color. */ im_out = gdImageCreateTrueColor (256 + 384, 384); /* 2.0.2: first color allocated would automatically be background in a palette based image. Since this is a truecolor image, with an automatic background of black, we must fill it explicitly. */ white = gdImageColorAllocate (im_out, 255, 255, 255); gdImageFilledRectangle (im_out, 0, 0, gdImageSX (im_out), gdImageSY (im_out), white); /* Set transparent color. */ gdImageColorTransparent (im_out, white); /* Try to load demoin.png and paste part of it into the output image. */ in = fopen ("demoin.png", "rb"); if (!in) { fprintf (stderr, "Can't load source image; this demo\n"); fprintf (stderr, "is much more impressive if demoin.png\n"); fprintf (stderr, "is available.\n"); im_in = 0; } else { int a; im_in = gdImageCreateFromPng (in); fclose (in); /* Now copy, and magnify as we do so */ gdImageCopyResampled (im_out, im_in, 32, 32, 0, 0, 192, 192, 255, 255); /* Now display variously rotated space shuttles in a circle of our own */ for (a = 0; (a < 360); a += 45) { int cx = cos (a * .0174532925) * 128; int cy = -sin (a * .0174532925) * 128; gdImageCopyRotated (im_out, im_in, 256 + 192 + cx, 192 + cy, 0, 0, gdImageSX (im_in), gdImageSY (im_in), a); } } red = gdImageColorAllocate (im_out, 255, 0, 0); green = gdImageColorAllocate (im_out, 0, 255, 0); blue = gdImageColorAllocate (im_out, 0, 0, 255); /* Fat Rectangle */ gdImageSetThickness (im_out, 4); gdImageLine (im_out, 16, 16, 240, 16, green); gdImageLine (im_out, 240, 16, 240, 240, green); gdImageLine (im_out, 240, 240, 16, 240, green); gdImageLine (im_out, 16, 240, 16, 16, green); gdImageSetThickness (im_out, 1); /* Circle */ gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue); /* Arc */ gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue); /* Flood fill: doesn't do much on a continuously variable tone jpeg original. */ gdImageFill (im_out, 8, 8, blue); /* Polygon */ points[0].x = 64; points[0].y = 0; points[1].x = 0; points[1].y = 128; points[2].x = 128; points[2].y = 128; gdImageFilledPolygon (im_out, points, 3, green); /* 2.0.12: Antialiased Polygon */ gdImageSetAntiAliased (im_out, green); for (i = 0; (i < 3); i++) { points[i].x += 128; } gdImageFilledPolygon (im_out, points, 3, gdAntiAliased); /* Brush. A fairly wild example also involving a line style! */ if (im_in) { int style[8]; brush = gdImageCreateTrueColor (16, 16); gdImageCopyResized (brush, im_in, 0, 0, 0, 0, gdImageSX (brush), gdImageSY (brush), gdImageSX (im_in), gdImageSY (im_in)); gdImageSetBrush (im_out, brush); /* With a style, so they won't overprint each other. Normally, they would, yielding a fat-brush effect. */ style[0] = 0; style[1] = 0; style[2] = 0; style[3] = 0; style[4] = 0; style[5] = 0; style[6] = 0; style[7] = 1; gdImageSetStyle (im_out, style, 8); /* Draw the styled, brushed line */ gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed); } /* Text (non-truetype; see gdtestft for a freetype demo) */ gdImageString (im_out, gdFontGiant, 32, 32, (unsigned char *) "hi", red); gdImageStringUp (im_out, gdFontSmall, 64, 64, (unsigned char *) "hi", red); /* Random antialiased lines; coordinates all over the image, but the output will respect a small clipping rectangle */ gdImageSetClip(im_out, 0, gdImageSY(im_out) - 100, 100, gdImageSY(im_out)); /* Fixed seed for reproducibility of results */ srand(100); for (i = 0; (i < 100); i++) { int x1 = rand() % gdImageSX(im_out); int y1 = rand() % gdImageSY(im_out); int x2 = rand() % gdImageSX(im_out); int y2 = rand() % gdImageSY(im_out); gdImageSetAntiAliased(im_out, white); gdImageLine (im_out, x1, y1, x2, y2, gdAntiAliased); } /* Make output image interlaced (progressive, in the case of JPEG) */ gdImageInterlace (im_out, 1); out = fopen ("demoout.png", "wb"); /* Write PNG */ gdImagePng (im_out, out); fclose (out); /* 2.0.12: also write a paletteized version */ out = fopen ("demooutp.png", "wb"); gdImageTrueColorToPalette (im_out, 0, 256); gdImagePng (im_out, out); fclose (out); gdImageDestroy (im_out); if (im_in) { gdImageDestroy (im_in); } #else fprintf (stderr, "No PNG library support.\n"); #endif /* HAVE_LIBPNG */ return 0; }
/* ------------------------------------------------------------------ *\ * gd out a string with '\n's * handle FTs (TTFs) and gd fonts * gdImageString() draws from the upper left; * gdImageStringFT() draws from lower left (one font height, even with '\n's)! >:-| \* ------------------------------------------------------------------ */ int GDCImageStringNL( gdImagePtr im, struct GDC_FONT_T *f, char *ftfont, double ftptsz, double rad, int x, int y, char *str, int clr, GDC_justify_t justify, char **sts ) { int retval = 0; char *err = NULL; #ifdef HAVE_LIBFREETYPE /* TODO: honor justifies */ if( ftfont && ftptsz ) { /* need one line height */ /* remember last one (will likely be the same) */ /* is this needed? */ /* gdImageStringFT() utilizes some caching */ /* saves a couple floating point trig calls */ static int f1hgt = 0; static double xs, ys; static double lftptsz = 0.0; static char *lftfont = (char*)-1; if( !f1hgt || ( lftfont != ftfont || lftptsz != ftptsz ) ) { f1hgt = GDCfnt_sz( "Aj", 0, ftfont, ftptsz, rad, NULL ).h; xs = (double)f1hgt * sin(rad); ys = (double)(f1hgt-1) * cos(rad); } x += (int)xs; y += (int)ys; if( (err = gdImageStringFT( im, (int*)NULL, clr, ftfont, ftptsz, rad, x, y, str)) == NULL ) { if( sts ) *sts = err; return 0; } else { /* TTF failed */ retval = 1; /* fall through - default to gdFonts */ /* reinstate upper left reference */ x -= (int)xs; y -= (int)ys; } } #endif { int i; int len; int max_len; short strs_num = cnt_nl( str, &max_len ); CREATE_ARRAY1( sub_str, unsigned char, max_len+1 ); /* char sub_str[max_len+1]; */ len = -1; strs_num = -1; i = -1; do { ++i; ++len; sub_str[len] = *(str+i); if( *(str+i) == '\n' || *(str+i) == '\0' ) { int xpos; sub_str[len] = '\0'; ++strs_num; switch( justify ) { case GDC_JUSTIFY_LEFT: xpos = 0; break; case GDC_JUSTIFY_RIGHT: xpos = f->w*(max_len-len); break; case GDC_JUSTIFY_CENTER: default: xpos = f->w*(max_len-len)/2; } if( rad == 0.0 ) gdImageString( im, f->f, x + xpos, y + (f->h-1)*strs_num, sub_str, clr ); else /* if( rad == M_PI/2.0 ) */ gdImageStringUp( im, f->f, x + (f->h-1)*strs_num, y - xpos, sub_str, clr ); len = -1; } } while( *(str+i) ); } if( sts ) *sts = err; return retval; }
int main(void) { /* Input and output files */ FILE *in; FILE *out; /* Input and output images */ gdImagePtr im_in, im_out; /* Brush image */ gdImagePtr brush; /* Color indexes */ int white; int blue; int red; int green; /* Points for polygon */ gdPoint points[3]; /* Create output image, 128 by 128 pixels. */ im_out = gdImageCreate(128, 128); /* First color allocated is background. */ white = gdImageColorAllocate(im_out, 255, 255, 255); /* Set transparent color. */ gdImageColorTransparent(im_out, white); /* Try to load demoin.png and paste part of it into the output image. */ in = fopen("demoin.png", "rb"); if (!in) { fprintf(stderr, "Can't load source image; this demo\n"); fprintf(stderr, "is much more impressive if demoin.png\n"); fprintf(stderr, "is available.\n"); im_in = 0; } else { im_in = gdImageCreateFromPng(in); fclose(in); /* Now copy, and magnify as we do so */ gdImageCopyResized(im_out, im_in, 16, 16, 0, 0, 96, 96, 127, 127); } red = gdImageColorAllocate(im_out, 255, 0, 0); green = gdImageColorAllocate(im_out, 0, 255, 0); blue = gdImageColorAllocate(im_out, 0, 0, 255); /* Rectangle */ gdImageLine(im_out, 8, 8, 120, 8, green); gdImageLine(im_out, 120, 8, 120, 120, green); gdImageLine(im_out, 120, 120, 8, 120, green); gdImageLine(im_out, 8, 120, 8, 8, green); /* Circle */ gdImageArc(im_out, 64, 64, 30, 10, 0, 360, blue); /* Arc */ gdImageArc(im_out, 64, 64, 20, 20, 45, 135, blue); /* Flood fill */ gdImageFill(im_out, 4, 4, blue); /* Polygon */ points[0].x = 32; points[0].y = 0; points[1].x = 0; points[1].y = 64; points[2].x = 64; points[2].y = 64; gdImageFilledPolygon(im_out, points, 3, green); /* Brush. A fairly wild example also involving a line style! */ if (im_in) { int style[8]; brush = gdImageCreate(8, 8); gdImageCopyResized(brush, im_in, 0, 0, 0, 0, gdImageSX(brush), gdImageSY(brush), gdImageSX(im_in), gdImageSY(im_in)); gdImageSetBrush(im_out, brush); /* With a style, so they won't overprint each other. Normally, they would, yielding a fat-brush effect. */ style[0] = 0; style[1] = 0; style[2] = 0; style[3] = 0; style[4] = 0; style[5] = 0; style[6] = 0; style[7] = 1; gdImageSetStyle(im_out, style, 8); /* Draw the styled, brushed line */ gdImageLine(im_out, 0, 127, 127, 0, gdStyledBrushed); } /* Text */ gdImageString(im_out, gdFontGiant, 16, 16, (unsigned char *) "hi", red); gdImageStringUp(im_out, gdFontSmall, 32, 32, (unsigned char *) "hi", red); /* Make output image interlaced (allows "fade in" in some viewers, and in the latest web browsers) */ gdImageInterlace(im_out, 1); out = fopen("demoout.png", "wb"); /* Write PNG */ gdImagePng(im_out, out); fclose(out); gdImageDestroy(im_out); if (im_in) { gdImageDestroy(im_in); } return 0; }
/* -------------------------------------------------------------------- */ int avgb_perday( char *fname, /* PNG file name to use */ char *title, /* title for graph */ unsigned long long data1[31]) { /* data1 (users) */ int i,x1,y1,x2; unsigned long long maxval=0; char * msg_xlegend = "Day"; char * msg_ylegend = "Data"; char daydate[3] = ""; static int sleft = 29; char maxvaldata[255] = ""; /* initalize the graph image */ init_monthgraph(title); /* draw index lines */ y1=110/(INDEX_LINES+1); for (i=1;i<=INDEX_LINES;i++) gdImageLine(imgbuf_mon,20,(i*y1)+22,650,(i*y1)+22,dkgrey); /* draw the x-axis legend, 31 is how many x-axis values we will display */ for (i=0;i<31;i++) { snprintf(daydate, sizeof(daydate), "%02d", i+1); /* sleft is start pixel from left, 20 is the distance between each value */ gdImageString(imgbuf_mon, gdFontSmall, (sleft+1)+(i*20), /* 134 is the start pixel from top, black the font color */ 134, (unsigned char*) daydate, black); if (data1[i] > maxval) maxval = data1[i]; /* get max val */ } sprintf(maxvaldata, "%llu", maxval); calc_units(maxvaldata, maxvaltxt); gdImageStringUp(imgbuf_mon,gdFontSmall,6,19+(strlen(maxvaltxt)*6),(unsigned char*) maxvaltxt,black); /* print color coded legends */ /* X Legend */ i = (strlen(msg_xlegend)*6); gdImageString(imgbuf_mon,gdFontMediumBold,658-i,144,(unsigned char*) msg_xlegend,black); /* Y Legend */ i = (strlen(msg_ylegend)*6); gdImageStringUp(imgbuf_mon,gdFontMediumBold,6,132,(unsigned char*) msg_ylegend,black); /* draw the data bars here */ for (i=0; i<31; i++) { if(data1[i] > 0) { percent = ((float)data1[i] / (float)maxval); if (percent <= 0.0) continue; x1 = sleft + (i*20); x2 = x1 + 11; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_mon, x1, y1, x2, 132, dkblue); gdImageRectangle(imgbuf_mon, x1, y1, x2, 132, black); } } sprintf(maxvaldata, "%llu", maxval); calc_units(maxvaldata, maxvaltxt); /* save png image */ if ((pngout = fopen(fname, "wb")) != NULL) { gdImagePng(imgbuf_mon, pngout); fclose(pngout); } /* deallocate memory */ gdImageDestroy(imgbuf_mon); return (0); }
/* -------------------------------------------------------------------- */ int con_permonth( char *fname, /* PNG file name to use */ char *title, /* title for graph */ int fmonth, /* begin month number */ unsigned long long data1[18]) { /* data1 (connections) */ int i,x1,y1,x2,s_mth; unsigned long long maxval=0; char * msg_xlegend = "Months"; char * msg_ylegend = "Connections"; /* short month names MUST BE 3 CHARS in size... pad if needed*/ static char *s_month[12] ={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /* initalize the graph image */ init_yeargraph(title); /* draw index lines */ y1=110/(INDEX_LINES+1); for (i=1;i<=INDEX_LINES;i++) gdImageLine(imgbuf_year,20,(i*y1)+22,380,(i*y1)+22,dkgrey); /* draw the x-axis legend */ s_mth = fmonth; /* 18 is how many x-axis values we will display */ for (i=0;i<18;i++) { /* 23 is the start pixel from left, 20 is the distance between each value */ gdImageString(imgbuf_year, gdFontSmall, 23+(i*20), /* 134 is the start pixel from top, black the font color */ 134, (unsigned char*) s_month[s_mth-1], black); if(strcmp(s_month[s_mth-1], "Dec") == 0) { gdImageLine(imgbuf_year, 23+(i*20)+18, 21, 23+(i*20)+18, 132, dkgrey); } s_mth++; if (s_mth > 12) s_mth = 1; if (data1[i] > maxval) maxval = data1[i]; /* get max val */ } sprintf(maxvaltxt, "%llu", maxval); gdImageStringUp(imgbuf_year,gdFontSmall,6,19+(strlen(maxvaltxt)*6),(unsigned char*) maxvaltxt,black); /* print color coded legends */ /* X Legend */ i = (strlen(msg_xlegend)*6); gdImageString(imgbuf_year,gdFontMediumBold,388-i,144,(unsigned char*) msg_xlegend,black); /* Y Legend */ i = (strlen(msg_ylegend)*6); gdImageStringUp(imgbuf_year,gdFontMediumBold,6,132,(unsigned char*) msg_ylegend,black); /* draw the data bars here */ s_mth = fmonth; for (i=0; i<18; i++) { if(data1[17-i] > 0) { if (s_mth > 12) s_mth = 1; percent = ((float)data1[17-i] / (float)maxval); if (percent <= 0.0) continue; x1 = 25 + (i*20); x2 = x1 + 11; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_year, x1, y1, x2, 132, dkblue); gdImageRectangle(imgbuf_year, x1, y1, x2, 132, black); } } sprintf(maxvaltxt, "%llu", maxval); /* save png image */ if ((pngout = fopen(fname, "wb")) != NULL) { gdImagePng(imgbuf_year, pngout); fclose(pngout); } /* deallocate memory */ gdImageDestroy(imgbuf_year); return (0); }
/* -------------------------------------------------------------------- */ int bytes_perday(char *fname, /* PNG file name to use */ char *title, /* title for graph */ unsigned long long data1[31], /* data1 (bytes in) */ unsigned long long data2[31], /* data2 (bytes out) */ unsigned long long data3[31]) { /* data3 (total bytes) */ int i = 0; int x1 = 0; int y1 = 0; int x2 = 0; unsigned long long maxval1,maxval2,maxval3=0; char * msg_xlegend = "Day"; char * msg_ylegend = "Data"; char daydate[3] = ""; static int sleft = 29; char maxvaldata[255] =""; /* initalize the graph image */ init_monthgraph(title); /* allocate additional colors, ater init_monthgraph() */ int cyan = gdImageColorAllocate(imgbuf_mon, 0, 128, 128); int purple = gdImageColorAllocate(imgbuf_mon, 128, 0, 128); /* draw index lines */ y1=110/(INDEX_LINES+1); for (i=1;i<=INDEX_LINES;i++) gdImageLine(imgbuf_mon,20,(i*y1)+22,650,(i*y1)+22,dkgrey); /* draw the x-axis legend, 31 is how many x-axis values we will display */ for (i=0;i<31;i++) { snprintf(daydate, sizeof(daydate), "%02d", i+1); /* sleft is start pixel from left, 20 is the distance between each value */ gdImageString(imgbuf_mon, gdFontSmall, (sleft+1)+(i*20), /* 134 is the start pixel from top, black the font color */ 134, (unsigned char *) daydate, black); if (data1[i] > maxval1) maxval1 = data1[i]; /* get max val 1 */ if (data2[i] > maxval2) maxval2 = data2[i]; /* get max val 2 */ if (data3[i] > maxval3) maxval3 = data3[i]; /* get max val 3 */ } /* create the legend max value */ sprintf(maxvaldata, "%llu", maxval3); calc_units(maxvaldata, maxvaltxt); gdImageStringUp(imgbuf_mon,gdFontSmall,6,19+(strlen(maxvaltxt)*6),(unsigned char*) maxvaltxt,black); /* write the legends data description */ gdImageString(imgbuf_mon,gdFontMediumBold,560,4,(unsigned char*) "In",cyan); gdImageString(imgbuf_mon,gdFontMediumBold,590,4,(unsigned char*) "Out",purple); gdImageString(imgbuf_mon,gdFontMediumBold,620,4,(unsigned char*) "Total",dkblue); /* X Legend */ i = (strlen(msg_xlegend)*6); gdImageString(imgbuf_mon,gdFontMediumBold,658-i,144,(unsigned char*) msg_xlegend,black); /* Y Legend */ i = (strlen(msg_ylegend)*6); gdImageStringUp(imgbuf_mon,gdFontMediumBold,6,132,(unsigned char*) msg_ylegend,black); /* draw the data bars here, but only if there is data */ for (i=0; i<31; i++) { if(data1[i] > 0) { percent = ((float)data1[i] / (float)maxval3); if (percent <= 0.0) continue; x1 = (sleft-1) + (i*20); x2 = x1 + 12; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_mon, x1, y1, x1+5, 132, cyan); } if(data2[i] > 0) { percent = ((float)data2[i] / (float)maxval3); if (percent <= 0.0) continue; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_mon, x1+4, y1, x1+10, 132, purple); } if(data3[i] > 0) { percent = ((float)data3[i] / (float)maxval3); if (percent <= 0.0) continue; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_mon, x1+8, y1, x1+8+4, 132, dkblue); } } sprintf(maxvaldata, "%llu", maxval3); calc_units(maxvaldata, maxvaltxt); /* save png image */ if ((pngout = fopen(fname, "wb")) != NULL) { gdImagePng(imgbuf_mon, pngout); fclose(pngout); } /* deallocate memory */ gdImageDestroy(imgbuf_mon); return (0); } /* end bytes_perday() */
/* -------------------------------------------------------------------- */ int bytes_permonth(char *fname, /* PNG file name to use */ char *title, /* title for graph */ int fmonth, /* begin month number */ unsigned long long data1[18], /* data1 (bytes in) */ unsigned long long data2[18], /* data2 (bytes out) */ unsigned long long data3[18]) { /* data3 (total bytes) */ int i = 0; int x1 =0; int y1 = 0; int x2 = 0; int s_mth = 0; unsigned long long maxval1,maxval2,maxval3=0; char * msg_xlegend = "Months"; char * msg_ylegend = "Data"; char maxvaldata[255] = ""; /* short month names MUST BE 3 CHARS in size... pad if needed */ static char *s_month[12] ={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; /* initalize the graph image */ init_yeargraph(title); /* allocate additional colors, ater init_yeargraph() */ int cyan = gdImageColorAllocate(imgbuf_year, 0, 128, 128); int purple = gdImageColorAllocate(imgbuf_year, 128, 0, 128); /* draw index lines */ y1=110/(INDEX_LINES+1); for (i=1;i<=INDEX_LINES;i++) gdImageLine(imgbuf_year,20,(i*y1)+22,380,(i*y1)+22,dkgrey); /* draw the x-axis legend */ s_mth = fmonth; /* 18 is how many x-axis values we will display */ for (i=0;i<18;i++) { /* 23 is the start pixel from left, 20 is the distance between each value */ gdImageString(imgbuf_year, gdFontSmall, 23+(i*20), /* 134 is the start pixel from top, black the font color */ 134, (unsigned char*) s_month[s_mth-1], black); /* print a line between years (Dec and Jan) */ if(strcmp(s_month[s_mth-1], "Dec") == 0) { gdImageLine(imgbuf_year, 23+(i*20)+18, 21, 23+(i*20)+18, 132, dkgrey); } s_mth++; if (s_mth > 12) s_mth = 1; if (data1[i] > maxval1) maxval1 = data1[i]; /* get max val 1 */ if (data2[i] > maxval2) maxval2 = data2[i]; /* get max val 2 */ if (data3[i] > maxval3) maxval3 = data3[i]; /* get max val 3 */ } sprintf(maxvaldata, "%llu", maxval3); calc_units(maxvaldata, maxvaltxt); gdImageStringUp(imgbuf_year,gdFontSmall,6,19+(strlen(maxvaltxt)*6),(unsigned char*) maxvaltxt,black); /* print color coded legends */ gdImageString(imgbuf_year,gdFontMediumBold,310,4,(unsigned char*) "In",cyan); gdImageString(imgbuf_year,gdFontMediumBold,328,4,(unsigned char*) "Out",purple); gdImageString(imgbuf_year,gdFontMediumBold,350,4,(unsigned char*) "Total",dkblue); /* X Legend */ i = (strlen(msg_xlegend)*6); gdImageString(imgbuf_year,gdFontMediumBold,388-i,144,(unsigned char*) msg_xlegend,black); /* Y Legend */ i = (strlen(msg_ylegend)*6); gdImageStringUp(imgbuf_year,gdFontMediumBold,6,132,(unsigned char*) msg_ylegend,black); /* draw the data bars here */ s_mth = fmonth; for (i=0; i<18; i++) { if(data1[17-i] > 0) { if (s_mth > 12) s_mth = 1; percent = ((float)data1[17-i] / (float)maxval3); if (percent <= 0.0) continue; x1 = 24 + (i*20); x2 = x1 + 12; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_year, x1, y1, x1+5, 132, cyan); } if(data2[17-i] > 0) { percent = ((float)data2[17-i] / (float)maxval3); if (percent <= 0.0) continue; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_year, x1+4, y1, x1+10, 132, purple); } if(data3[17-i] > 0) { percent = ((float)data3[17-i] / (float)maxval3); if (percent <= 0.0) continue; y1 = 132 - (percent * 110); gdImageFilledRectangle(imgbuf_year, x1+8, y1, x1+8+4, 132, dkblue); } } sprintf(maxvaldata, "%llu", maxval3); calc_units(maxvaldata, maxvaltxt); /* save png image */ if ((pngout = fopen(fname, "wb")) != NULL) { gdImagePng(imgbuf_year, pngout); fclose(pngout); } /* deallocate memory */ gdImageDestroy(imgbuf_year); return (0); } /* end bytes_permonth() */
/* draw the gif file, based on the tentry desciption */ void draw_gif ( FILE * gif ) { #define c_blank 245,245,245 /* base colors */ #define c_light 194,194,194 #define c_dark 100,100,100 #define c_black 0,0,0 #define c_white 255,255,0 #define c_blue 0,0,255 #define c_red 255,0,0 #define c_green 0,255,0 gdImagePtr graph; int i_light,i_dark,i_blank, i_black, i_white, i_blue, i_red, i_green; graph = gdImageCreate(xsize, ysize); /* the first color allocated will be the background color. */ i_blank = gdImageColorAllocate(graph,c_blank); i_light = gdImageColorAllocate(graph,c_light); i_dark = gdImageColorAllocate(graph,c_dark); gdImageInterlace(graph, 1); i_black = gdImageColorAllocate(graph,c_black); i_white = gdImageColorAllocate(graph,c_white); i_red = gdImageColorAllocate(graph,c_red); i_green = gdImageColorAllocate(graph,c_green); i_blue = gdImageColorAllocate(graph,c_blue); /* draw the image border */ gdImageLine(graph,0,0,xsize-1,0,i_light); gdImageLine(graph,1,1,xsize-2,1,i_light); gdImageLine(graph,0,0,0,ysize-1,i_light); gdImageLine(graph,1,1,1,ysize-2,i_light); gdImageLine(graph,xsize-1,0,xsize-1,ysize-1,i_dark); gdImageLine(graph,0,ysize-1,xsize-1,ysize-1,i_dark); gdImageLine(graph,xsize-2,1,xsize-2,ysize-2,i_dark); gdImageLine(graph,1,ysize-2,xsize-2,ysize-2,i_dark); { /* date the graph */ struct tm *newtime; time_t aclock; time( &aclock ); /* Get time in seconds */ newtime = localtime( &aclock ); /* Convert time to struct */ /* tm form */ gdImageString(graph, gdFontSmall,gdFontSmall->w,3,asctime( newtime ),i_dark); }; /*i_col = find_color(graph, colortable[pcurrententry->color]); gdImageFilledRectangle(graph, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->coords[2], pcurrententry->coords[3],i_col2); } gdImageString(graph, gdFontSmall, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->str, i_col ); gdImageStringUp(graph, gdFontSmall, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->str, i_col ); gdImagePtr brush_2pix; brush_2pix = gdImageCreate(2,2); gdImageColorAllocate( brush_2pix, r(colortable[colorratetable[pcurrententry->rate]]), g(colortable[colorratetable[pcurrententry->rate]]), b(colortable[colorratetable[pcurrententry->rate]]) ); gdImageSetBrush(graph, brush_2pix); i_col = gdBrushed; gdImageLine(graph, x, y, x2, y2,i_col); gdImageDestroy(brush_2pix);*/ /* draw axes and graphs */ { int w = gdFontSmall->w, h = gdFontSmall->h, i, j, incrx, incry, maxio; char str[20]; #define AXESX (distcount + 1) #define AXESY 6 maxio = 0; for ( i = 0; i < distcount; i ++ ) { if (maxio < dist[i][0]) maxio = dist[i][0]; if (maxio < dist[i][1]) maxio = dist[i][1]; } incry = (ysize-(h*2)-(w*7))/(AXESY-1); incrx = (xsize-(w*9))/(AXESX-1); j = 100; for ( i = h*2; i <= ((h*2) + (incry * (AXESY-1))) ; i+= incry ) { gdImageLine(graph,w*7,i,w*7 + (incrx*(AXESX-1)),i,i_black); /* horizontal */ sprintf ( str, "%3u%%", j ); j-= 100/(AXESY-1); gdImageString(graph, gdFontSmall,w,i-h/2,str,i_black ); } j = 0; for ( i = w*7; i <= ((w*7) + (incrx * (AXESX-1))) ; i+= incrx ) { gdImageLine(graph,i,h*2,i,h*2 + (incry*(AXESY-1)),i_black); /* vertical */ sprintf ( str, "%3u%%", j ); j+= (100/(AXESX-1)); gdImageStringUp(graph, gdFontSmall, i - w/2, ysize - h, str, i_black ); } for ( i = 0; i < distcount; i ++ ) { int x1, x2, y1, y2, mrgx; mrgx = incrx/5; x1 = (w*7) + (i*incrx) + mrgx; x2 = x1 + (incrx/3); y2 = h*2 + (incry*(AXESY-1)) - 2; y1 = y2 - (((ysize-(h*2)-(w*7) - 2) * dist[i][0]) / rounds); /* printf ( "x1 %u, y1 %u, x2 %u, y2 %u.\n", x1, y1, x2, y2 );*/ gdImageFilledRectangle(graph, x1, y1, x2, y2,i_green); gdImageRectangle(graph, x1, y1, x2, y2,i_dark); x2 = (w*7) + ((i+1)*incrx) - mrgx; x1 = x2 - (incrx/3); y2 = h*2 + (incry*(AXESY-1)) - 2; y1 = y2 - (((ysize-(h*2)-(w*7) - 2) * dist[i][1]) / rounds); /* printf ( "x1 %u, y1 %u, x2 %u, y2 %u.\n", x1, y1, x2, y2 );*/ gdImageFilledRectangle(graph, x1, y1, x2, y2,i_blue); gdImageRectangle(graph, x1, y1, x2, y2,i_dark); } } gdImageGif(graph, gif); gdImageDestroy(graph); }
/* draw the gif file */ void draw_distrib_gif ( FILE * score, FILE * gif ) { #define c_blank 245,245,245 /* base colors */ #define c_light 194,194,194 #define c_dark 100,100,100 #define c_black 0,0,0 #define c_white 255,255,0 #define c_blue 0,0,255 #define c_red 255,0,0 #define c_green 0,255,0 gdImagePtr graph; int i_light,i_dark,i_blank, i_black, i_white, i_blue, i_red, i_green; int color[4000][2]; graph = gdImageCreate(xsize, ysize); /* the first color allocated will be the background color. */ i_blank = gdImageColorAllocate(graph,c_blank); i_light = gdImageColorAllocate(graph,c_light); i_dark = gdImageColorAllocate(graph,c_dark); gdImageInterlace(graph, 1); i_black = gdImageColorAllocate(graph,c_black); i_white = gdImageColorAllocate(graph,c_white); i_red = gdImageColorAllocate(graph,c_red); i_green = gdImageColorAllocate(graph,c_green); i_blue = gdImageColorAllocate(graph,c_blue); { int i; for (i = 0; i <= distcount; i++ ) { color[distcount - i - 1][0] = gdImageColorAllocate(graph, (255*i)/distcount, 255, (255*i)/distcount); } for (i = 0; i <= distcount; i++ ) { color[distcount - i - 1][1] = gdImageColorAllocate(graph, (255*i)/distcount, (255*i)/distcount, 255); } } /* draw the image border */ gdImageLine(graph,0,0,xsize-1,0,i_light); gdImageLine(graph,1,1,xsize-2,1,i_light); gdImageLine(graph,0,0,0,ysize-1,i_light); gdImageLine(graph,1,1,1,ysize-2,i_light); gdImageLine(graph,xsize-1,0,xsize-1,ysize-1,i_dark); gdImageLine(graph,0,ysize-1,xsize-1,ysize-1,i_dark); gdImageLine(graph,xsize-2,1,xsize-2,ysize-2,i_dark); gdImageLine(graph,1,ysize-2,xsize-2,ysize-2,i_dark); { /* date the graph */ struct tm *newtime; time_t aclock; time( &aclock ); /* Get time in seconds */ newtime = localtime( &aclock ); /* Convert time to struct */ /* tm form */ gdImageString(graph, gdFontSmall,gdFontSmall->w,3,asctime( newtime ),i_dark); }; /*i_col = find_color(graph, colortable[pcurrententry->color]); gdImageFilledRectangle(graph, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->coords[2], pcurrententry->coords[3],i_col2); } gdImageString(graph, gdFontSmall, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->str, i_col ); gdImageStringUp(graph, gdFontSmall, pcurrententry->coords[0], pcurrententry->coords[1], pcurrententry->str, i_col ); gdImagePtr brush_2pix; brush_2pix = gdImageCreate(2,2); gdImageColorAllocate( brush_2pix, r(colortable[colorratetable[pcurrententry->rate]]), g(colortable[colorratetable[pcurrententry->rate]]), b(colortable[colorratetable[pcurrententry->rate]]) ); gdImageSetBrush(graph, brush_2pix); i_col = gdBrushed; gdImageLine(graph, x, y, x2, y2,i_col); gdImageDestroy(brush_2pix);*/ /* draw axes and graphs */ { int w = gdFontSmall->w, h = gdFontSmall->h, i, j, k, incrx, incry; char str[4000]; int nbaxesx = (rate + 1); int nbaxesy = 6; int textx = 7; int texty = 15; incry = (ysize-(h*2)-(w*texty))/(nbaxesy-1); incrx = (xsize-(w*(textx+2)))/(nbaxesx-1); j = 100; for ( i = h*2; i <= ((h*2) + (incry * (nbaxesy-1))) ; i+= incry ) { gdImageLine(graph,w*textx,i,w*textx + (incrx*(nbaxesx-1)),i,i_black); /* horizontal */ sprintf ( str, "%3u%%", j ); j-= 100/(nbaxesy-1); gdImageString(graph, gdFontSmall,w,i-h/2,str,i_black ); } j = 0; for ( i = w*7; i <= ((w*7) + (incrx * (nbaxesx-1))) ; i+= incrx ) { /*gdImageLine(graph,i,h*2,i,h*2 + (incry*(AXESY-1)),i_black);*/ /* vertical */ /*sprintf ( str, "%3u%%", j ); j+= (100/(AXESX-1)); gdImageStringUp(graph, gdFontSmall, i - w/2, ysize - h, str, i_black );*/ } for ( i = 0; i < rate; i ++ ) { char *name,*ptr; int tin=0, tout=0; int x1, x2, y1, y2, mrgx; if ( fscanf ( score, "%s", str ) == EOF ) break; /*printf ( "%s\n", str );*/ name = str; if ((ptr = strtok( str, ":")) == NULL) continue; /*printf ( "%s:", name );*/ for ( j = 0; j < distcount; j++ ) { dist[j][0] = dist[j][0] = 0; if ((ptr = strtok( NULL, "/,")) == NULL) continue; dist[j][0] = atoi(ptr); tin += dist[j][0]; if ((ptr = strtok( NULL, "/,")) == NULL) continue; dist[j][1] = atoi(ptr); tout += dist[j][1]; /*printf ( "%u/%u,",dist[j][0],dist[j][1] );*/ } /*printf ( "\n" );*/ /* draw label and graphs */ mrgx = incrx/5; x1 = (w*textx) + (i*incrx) + (incrx/2) - (h/2); y1 = ysize-h; gdImageStringUp( graph, gdFontSmall, x1, y1, str, i_black ); for (k = 0; k < 2; k ++ ) { y1 = ysize-((texty-4) * w); if ( k == 0 ) { x1 = (w*textx) + (i*incrx) + mrgx; x2 = x1 + (incrx/3); gdImageStringUp( graph, gdFontSmall, x1, y1, "in", i_black ); } else { x2 = (w*textx) + ((i+1)*incrx) - mrgx; x1 = x2 - (incrx/3); gdImageStringUp( graph, gdFontSmall, x1, y1, "out", i_black ); } y2 = h*2 + (incry*(nbaxesy-1)); for ( j = distcount-1; j >=0 ; j-- ) { y1 = y2 - (((ysize-(h*2)-(w*texty) - 2) * dist[j][k]) / tin); if (j == 0) { /* 'correct' cumulative error */ y1 = h*2; } gdImageFilledRectangle(graph, x1, y1, x2, y2,color[j][k]); gdImageRectangle(graph, x1, y1, x2, y2,i_black); y2 = y1; } } /* int x1, x2, y1, y2, mrgx; mrgx = incrx/5; x1 = (w*7) + (i*incrx) + mrgx; x2 = x1 + (incrx/3); y2 = h*2 + (incry*(AXESY-1)) - 2; y1 = y2 - (((ysize-(h*2)-(w*7) - 2) * dist[i][0]) / rounds); gdImageFilledRectangle(graph, x1, y1, x2, y2,i_green); gdImageRectangle(graph, x1, y1, x2, y2,i_dark); x2 = (w*7) + ((i+1)*incrx) - mrgx; x1 = x2 - (incrx/3); y2 = h*2 + (incry*(AXESY-1)) - 2; y1 = y2 - (((ysize-(h*2)-(w*7) - 2) * dist[i][1]) / rounds); gdImageFilledRectangle(graph, x1, y1, x2, y2,i_blue); gdImageRectangle(graph, x1, y1, x2, y2,i_dark); */ } } gdImageGif(graph, gif); gdImageDestroy(graph); }
void graphicsGdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, const char *s, int color) { WITH_UCHARP_FROM_CONST_CHARP(dst, s, gdImageStringUp(im, f, x, y, dst, color), graphicsGdImageStringUp); }