Merge branch 'pu'
[jungerl.git] / lib / gd1_drv / c_src / my-gd1.c
blob76c479a10d04c80c5234b29afe37c5808290a1ce
2 #include <stdio.h>
3 #include <errno.h>
5 #include <erl_driver_tk.h>
6 #include <gd.h>
7 #include <gdfontt.h>
8 #include <gdfonts.h>
9 #include <gdfontmb.h>
10 #include <gdfontl.h>
11 #include <gdfontg.h>
12 #include <my-gd1.h>
13 #include <gd1_drv.h>
15 gdImagePtr gdImageCreateFromXpm(FILE *); /* gd.h doesn't have a prototype */
17 int
18 my_gdImagePng(gdImagePtr im, char *file)
20 FILE *fp;
22 if ((fp = fopen(file, "wb")) == NULL)
23 return errno;
24 gdImagePng(im, fp);
25 if (fclose(fp) == EOF)
26 return errno;
27 else
28 return 0;
31 int
32 my_gdImageJpeg(gdImagePtr im, char *file, int quality)
34 FILE *fp;
36 if ((fp = fopen(file, "wb")) == NULL)
37 return errno;
38 gdImageJpeg(im, fp, quality);
39 if (fclose(fp) == EOF)
40 return errno;
41 else
42 return 0;
45 gdImagePtr
46 my_gdImageCreateFromJpeg(char *file, int *status)
48 FILE *fp;
49 gdImagePtr im;
51 if ((fp = fopen(file, "rb")) == NULL) {
52 *status = errno;
53 return NULL;
55 if ((im = gdImageCreateFromJpeg(fp)) == NULL) {
56 *status = -1;
57 return NULL;
59 if (fclose(fp) == EOF) {
60 *status = errno;
61 return NULL;
62 } else {
63 return im;
67 gdImagePtr
68 my_gdImageCreateFromPng(char *file, int *status)
70 FILE *fp;
71 gdImagePtr im;
73 if ((fp = fopen(file, "rb")) == NULL) {
74 *status = errno;
75 return NULL;
77 if ((im = gdImageCreateFromPng(fp)) == NULL) {
78 *status = -1;
79 return NULL;
81 if (fclose(fp) == EOF) {
82 *status = errno;
83 return NULL;
84 } else {
85 return im;
89 gdImagePtr
90 my_gdImageCreateFromGd(char *file, int *status)
92 FILE *fp;
93 gdImagePtr im;
95 if ((fp = fopen(file, "rb")) == NULL) {
96 *status = errno;
97 return NULL;
99 if ((im = gdImageCreateFromGd(fp)) == NULL) {
100 *status = -1;
101 return NULL;
103 if (fclose(fp) == EOF) {
104 *status = errno;
105 return NULL;
106 } else {
107 return im;
111 gdImagePtr
112 my_gdImageCreateFromGd2(char *file, int *status)
114 FILE *fp;
115 gdImagePtr im;
117 if ((fp = fopen(file, "rb")) == NULL) {
118 *status = errno;
119 return NULL;
121 if ((im = gdImageCreateFromGd2(fp)) == NULL) {
122 *status = -1;
123 return NULL;
125 if (fclose(fp) == EOF) {
126 *status = errno;
127 return NULL;
128 } else {
129 return im;
133 gdImagePtr
134 my_gdImageCreateFromGd2Part(char *file, int srcX, int srcY, int w, int h,
135 int *status)
137 FILE *fp;
138 gdImagePtr im;
140 if ((fp = fopen(file, "rb")) == NULL) {
141 *status = errno;
142 return NULL;
144 if ((im = gdImageCreateFromGd2Part(fp, srcX, srcY, w, h)) == NULL) {
145 *status = -1;
146 return NULL;
148 if (fclose(fp) == EOF) {
149 *status = errno;
150 return NULL;
151 } else {
152 return im;
156 gdImagePtr
157 my_gdImageCreateFromXpm(char *file, int *status)
159 FILE *fp;
160 gdImagePtr im;
162 if ((fp = fopen(file, "rb")) == NULL) {
163 *status = errno;
164 return NULL;
166 if ((im = gdImageCreateFromXpm(fp)) == NULL) {
167 *status = -1;
168 return NULL;
170 if (fclose(fp) == EOF) {
171 *status = errno;
172 return NULL;
173 } else {
174 return im;
179 my_gdImageWBMP(gdImagePtr im, int fg, char *file)
181 FILE *fp;
183 if ((fp = fopen(file, "wb")) == NULL)
184 return errno;
185 gdImageWBMP(im, fg, fp);
186 if (fclose(fp) == EOF)
187 return errno;
188 else
189 return 0;
193 my_gdImageGd(gdImagePtr im, char *file)
195 FILE *fp;
197 if ((fp = fopen(file, "wb")) == NULL)
198 return errno;
199 gdImageGd(im, fp);
200 if (fclose(fp) == EOF)
201 return errno;
202 else
203 return 0;
207 my_gdImageGd2(gdImagePtr im, char *file, int chunkSize, int fmt)
209 FILE *fp;
211 if ((fp = fopen(file, "wb")) == NULL)
212 return errno;
213 gdImageGd2(im, fp, chunkSize, fmt);
214 if (fclose(fp) == EOF)
215 return errno;
216 else
217 return 0;
221 my_gdImagePolygon(gdImagePtr im, char *vertices, int pointsTotal, int color)
223 gdPoint v[MAX_POLYGON_VERTICES];
224 int i;
226 if (pointsTotal > MAX_POLYGON_VERTICES) {
227 return -1;
229 for (i = 0; i < pointsTotal; i++) {
230 v[i].x = get_int32(vertices);
231 vertices += 4;
232 v[i].y = get_int32(vertices);
233 vertices += 4;
235 gdImagePolygon(im, v, pointsTotal, color);
236 return 0;
240 my_gdImageFilledPolygon(gdImagePtr im, char *vertices, int pointsTotal, int color)
242 gdPoint v[MAX_POLYGON_VERTICES];
243 int i;
245 if (pointsTotal > MAX_POLYGON_VERTICES) {
246 return -1;
248 for (i = 0; i < pointsTotal; i++) {
249 v[i].x = get_int32(vertices);
250 vertices += 4;
251 v[i].y = get_int32(vertices);
252 vertices += 4;
254 gdImageFilledPolygon(im, v, pointsTotal, color);
255 return 0;
259 my_gdImageSetStyle(gdImagePtr im, char *style, int styleLength)
261 int v[MAX_STYLE_LENGTH];
262 int i;
264 if (styleLength > MAX_STYLE_LENGTH) {
265 return -1;
267 for (i = 0; i < styleLength; i++) {
268 v[i] = get_int32(style);
269 style += 4;
271 gdImageSetStyle(im, v, styleLength);
272 return 0;
275 gdFontPtr
276 my_GetFontPtr(int font_idx)
278 switch (font_idx) {
279 case FONT_TINY: return gdFontTiny;
280 case FONT_SMALL: return gdFontSmall;
281 case FONT_MEDIUMBOLD: return gdFontMediumBold;
282 case FONT_LARGE: return gdFontLarge;
283 case FONT_GIANT: return gdFontGiant;
284 default: return NULL;
286 /* NOTREACHED */