ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/gdimg.cpp
Revision: 264
Committed: Mon Aug 27 18:21:11 2012 UTC (7 years, 1 month ago) by gpertea
File size: 2102 byte(s)
Log Message:
minor refactoring etc

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