1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include "gdimg.h"
int GDImg::defaultBg=0xFFFFFF;
void GDImg::init(int w, int h, const char* fn, int bg_rgb) {
fout=NULL;
imgH=h;
imgW=w;
img = gdImageCreate(imgW, imgH);
currentColor=-1;
if (fn!=NULL) setFile(fn);
bgColor=color(bg_rgb);
gdImageColorTransparent(img, -1);
}
void GDImg::setBg(int rgb) {
gdImageColorDeallocate(img, bgColor);
bgColor=this->color(rgb);
}
GDImg::~GDImg() {
gdImageDestroy(img);
if (fout!=NULL && fout!=stdout) fclose(fout);
}
int GDImg::color(byte r, byte g, byte b) {
return gdImageColorAllocate(img,(int)r,(int)g,(int)b);
}
void GDImg::line(int x1, int y1, int x2, int y2, int color) {
if (color==-1) color=currentColor;
gdImageLine(img,x1,y1,x2,y2,color);
}
void GDImg::rectangle(int x1, int y1, int x2, int y2, int color) {
if (color==-1) color=currentColor;
gdImageRectangle(img,x1,y1,x2,y2,color);
}
void GDImg::fillRectangle(int x1, int y1, int x2, int y2, int color) {
if (color==-1) color=currentColor;
gdImageFilledRectangle(img,x1,y1,x2,y2,color);
}
void GDImg::fillPolygon(gdPointPtr points, int ptotal, int color) {
if (color==-1) color=currentColor;
gdImageFilledPolygon(img, points,ptotal,color);
}
void GDImg::setTransparent(int cidx) {
gdImageColorTransparent(img,cidx);
}
void GDImg::setFile(const char* fname) {
if (fout!=NULL && fout!=stdout) fclose(fout);
if (fname[0]=='-' && fname[1]==0) {
fout=stdout;
}
else {
fout=fopen(fname, "wb");
if (fout==NULL) GError("Error: cannot open file %s for writing!\n",fname);
}
}
void GDImg::setFile(FILE* f) {
if (fout!=NULL && fout!=stdout) fclose(fout);
fout=f;
}
void GDImg::write(const char* fname) {
if (fname==NULL && fout==NULL)
GError("Error at GDImg::writeGIF() - no destination file given!\n");
if (fname!=NULL) setFile(fname);
gdImageGif(img,fout);
}
|