File: gdimg.cpp

package info (click to toggle)
libgclib 0.11.10%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 972 kB
  • sloc: cpp: 18,275; makefile: 58; sh: 21
file content (80 lines) | stat: -rw-r--r-- 2,102 bytes parent folder | download | duplicates (3)
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);
 //allocate white background color by default
 bgColor=color(bg_rgb);
 gdImageColorTransparent(img, -1); //default is non-transparent white background
}

void GDImg::setBg(int rgb) {
    //if (rgb!=defaultBg) bgColor=color(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) {
  //cidx must be the color index of a color allocated previously for img!
  gdImageColorTransparent(img,cidx);
}

void GDImg::setFile(const char* fname) {
  if (fout!=NULL && fout!=stdout) fclose(fout);
  if (fname[0]=='-' && fname[1]==0) {
    //special "-" file name means stdout
    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);
}