~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/percept/c_src/egd_image.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-07 15:07:37 UTC
  • mfrom: (1.2.1 upstream) (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090507150737-i4yb5elwinm7r0hc
Tags: 1:13.b-dfsg1-1
* Removed another bunch of non-free RFCs from original tarball
  (closes: #527053).
* Fixed build-dependencies list by adding missing comma. This requires
  libsctp-dev again. Also, added libsctp1 dependency to erlang-base and
  erlang-base-hipe packages because the shared library is loaded via
  dlopen now and cannot be added using dh_slibdeps (closes: #526682).
* Weakened dependency of erlang-webtool on erlang-observer to recommends
  to avoid circular dependencies (closes: #526627).
* Added solaris-i386 to HiPE enabled architectures.
* Made script sources in /usr/lib/erlang/erts-*/bin directory executable,
  which is more convenient if a user wants to create a target Erlang system.
* Shortened extended description line for erlang-dev package to make it
  fit 80x25 terminals.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ``The contents of this file are subject to the Erlang Public License,
2
 
 * Version 1.1, (the "License"); you may not use this file except in
3
 
 * compliance with the License. You should have received a copy of the
4
 
 * Erlang Public License along with this software. If not, it can be
5
 
 * retrieved via the world wide web at http://www.erlang.org/.
6
 
 * 
7
 
 * Software distributed under the License is distributed on an "AS IS"
8
 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
 
 * the License for the specific language governing rights and limitations
10
 
 * under the License.
11
 
 * 
12
 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
 
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
 
 * AB. All Rights Reserved.''
15
 
 * 
16
 
 *     $Id$
17
 
 */
18
 
#include <gd.h>
19
 
#include <gdfontt.h>
20
 
#include <gdfonts.h>
21
 
#include <gdfontmb.h>
22
 
#include <gdfontl.h>
23
 
#include <gdfontg.h>
24
 
 
25
 
#include <erl_driver.h>
26
 
 
27
 
#include <math.h>
28
 
#include "egd_image.h"
29
 
#include "egd_coding.h"
30
 
 
31
 
int egd_options_to_gd_arc_style(int options);
32
 
 
33
 
void send_result(egd_data *d, unsigned int result) {
34
 
    unsigned char buffer[4];
35
 
    driver_output(d->port, (char *)encode((char *)buffer, result), 4);
36
 
}
37
 
 
38
 
void image_create(egd_data *d, char *buffer, int l){
39
 
    int w,h;
40
 
    w = decode(&buffer);
41
 
    h = decode(&buffer);
42
 
    d->im = gdImageCreateTrueColor(w, h);
43
 
    send_result(d, 0);
44
 
}
45
 
 
46
 
void image_destroy(egd_data *d)  {
47
 
    if (d != NULL && d->im != NULL) gdImageDestroy(d->im);      
48
 
}
49
 
 
50
 
/* X, Y, Color */
51
 
void image_pixel(egd_data *d, char *buffer, int l) {
52
 
    int x = decode(&buffer);
53
 
    int y = decode(&buffer);
54
 
    int c = decode_color(&buffer);
55
 
    gdImageSetPixel(d->im, x, y, c); 
56
 
    send_result(d, 0);
57
 
}
58
 
 
59
 
/* X1, Y1, X2, Y2, Color */
60
 
void image_line(egd_data *d, char *buffer, int l) {
61
 
    int x1 = decode(&buffer);
62
 
    int y1 = decode(&buffer);
63
 
    int x2 = decode(&buffer);
64
 
    int y2 = decode(&buffer);
65
 
    int c = decode_color(&buffer);
66
 
    gdImageLine(d->im, x1, y1, x2, y2, c);
67
 
    send_result(d, 0);
68
 
}
69
 
 
70
 
/* r, g, b */
71
 
void image_color(egd_data *d, char *buffer, int l) {
72
 
    int r = decode(&buffer);
73
 
    int g = decode(&buffer);
74
 
    int b = decode(&buffer);
75
 
    int color = gdImageColorAllocate(d->im, r,g,b);
76
 
    send_result(d, color);
77
 
}
78
 
 
79
 
void image_rectangle(egd_data *d, char *buffer, int l) {
80
 
    int x1 = decode(&buffer);
81
 
    int y1 = decode(&buffer);
82
 
    int x2 = decode(&buffer);
83
 
    int y2 = decode(&buffer);
84
 
    int c = decode_color(&buffer);
85
 
    gdImageRectangle(d->im, x1, y1, x2, y2, c);
86
 
    send_result(d, 0);
87
 
}
88
 
 
89
 
void image_filled_rectangle(egd_data *d, char *buffer, int l) {
90
 
    int x1 = decode(&buffer);
91
 
    int y1 = decode(&buffer);
92
 
    int x2 = decode(&buffer);
93
 
    int y2 = decode(&buffer);
94
 
    int c = decode_color(&buffer);
95
 
    gdImageFilledRectangle(d->im, x1, y1, x2, y2, c);
96
 
    send_result(d, 0);
97
 
}
98
 
 
99
 
/* color, points */
100
 
void image_polygon(egd_data *d, char *buffer, int l) {
101
 
    int color = decode_color(&buffer);
102
 
    int n = decode(&buffer);
103
 
    gdPoint *pts = image_alloc_points(buffer, n);
104
 
    
105
 
    gdImagePolygon(d->im, pts, n, color);
106
 
    image_free_points(pts);
107
 
    send_result(d, 0);
108
 
}
109
 
 
110
 
void image_filled_polygon(egd_data *d, char *buffer, int l) {
111
 
    int color = decode_color(&buffer);
112
 
    int n = decode(&buffer);
113
 
    gdPoint *pts = image_alloc_points(buffer, n);
114
 
    
115
 
    gdImageFilledPolygon(d->im, pts, n, color);
116
 
    image_free_points(pts);
117
 
 
118
 
    send_result(d, 0);
119
 
}
120
 
 
121
 
/* cx, cy, w, h, s, e. color*/
122
 
void image_arc(egd_data *d, char *buffer, int l) {
123
 
    int cx = decode(&buffer);
124
 
    int cy = decode(&buffer);
125
 
    int w = decode(&buffer);
126
 
    int h = decode(&buffer);
127
 
    int s = decode(&buffer);
128
 
    int e = decode(&buffer);
129
 
    int c = decode_color(&buffer);
130
 
    gdImageArc(d->im, cx, cy, w, h, s, e, c);
131
 
    send_result(d, 0);
132
 
}
133
 
 
134
 
 
135
 
/* cx, cy, w, h, s, e. color, style_options */
136
 
void image_filled_arc(egd_data *d, char *buffer, int l) {
137
 
    int cx = decode(&buffer);
138
 
    int cy = decode(&buffer);
139
 
    int w = decode(&buffer);
140
 
    int h = decode(&buffer);
141
 
    int s = decode(&buffer);
142
 
    int e = decode(&buffer);
143
 
    int c = decode_color(&buffer);
144
 
    int options = decode(&buffer);
145
 
    int style = egd_options_to_gd_arc_style(options);
146
 
    gdImageFilledArc(d->im, cx, cy, w, h, s, e, c, style);
147
 
    send_result(d, 0);
148
 
}
149
 
 
150
 
 
151
 
void image_filled_ellipse(egd_data *d, char *buffer , int l) {
152
 
    int cx = decode(&buffer);
153
 
    int cy = decode(&buffer);
154
 
    int w = decode(&buffer);
155
 
    int h = decode(&buffer);
156
 
    int c = decode_color(&buffer);
157
 
    gdImageFilledEllipse(d->im, cx, cy, w, h, c);
158
 
    send_result(d, 0);
159
 
}
160
 
 
161
 
/* Texting */
162
 
 
163
 
 
164
 
void image_text(egd_data *d, char *buffer, int l) {
165
 
    int x = decode(&buffer);
166
 
    int y = decode(&buffer);
167
 
    int size = decode(&buffer);
168
 
    int c = decode_color(&buffer);
169
 
    int str_len = decode(&buffer);
170
 
    unsigned char *str = (unsigned char*)image_alloc_string((char*)buffer, str_len);
171
 
    gdImageString(d->im, image_font(size), x,y, str, c);
172
 
    image_free_string((char *)str);
173
 
    send_result(d, 0);
174
 
}
175
 
 
176
 
void image_text_up(egd_data *d, char *buffer, int l) {
177
 
    int x = decode(&buffer);
178
 
    int y = decode(&buffer);
179
 
    int size = decode(&buffer);
180
 
    int c = decode_color(&buffer);
181
 
    int str_len = decode(&buffer);
182
 
    unsigned char *str = (unsigned char*)image_alloc_string((char *)buffer, str_len);
183
 
    gdImageStringUp(d->im, image_font(size), x,y, str, c);
184
 
    image_free_string((char *)str);
185
 
    send_result(d, 0);
186
 
}
187
 
 
188
 
void image_font_size(egd_data *d, char *buffer, int l) {
189
 
    int size = decode(&buffer);
190
 
    unsigned char output[8];
191
 
    gdFontPtr font = image_font(size);
192
 
    int w = font->w;
193
 
    int h = font->h;
194
 
    /* dangerous */
195
 
    encode((char*)output, w);
196
 
    encode((char*)output + 4, h);
197
 
    driver_output(d->port, output, 8);
198
 
}
199
 
 
200
 
void image_fill(egd_data *d, char *buffer, int l) {
201
 
    int x = decode(&buffer);
202
 
    int y = decode(&buffer);
203
 
    int c = decode_color(&buffer);
204
 
    gdImageFill(d->im, x, y, c);
205
 
    send_result(d, 0);
206
 
}
207
 
 
208
 
/* rotate and resample */
209
 
 
210
 
/* void image_rotate
211
 
 * In: 
212
 
 *      egd_data *d, image pointer, port data
213
 
 *      char *buffer, incoming data from erlang
214
 
 *      int l, length of data
215
 
 * Abstract:
216
 
 *      The buffer only contains the angle of rotation.
217
 
 */
218
 
 
219
 
void image_rotate(egd_data *d, char *buffer, int l){
220
 
    int angle = decode(&buffer);
221
 
    float alpha = angle * .0174532925f;
222
 
    int w = fabs((d->im->sx)*cos(alpha)) + fabs((d->im->sy)*sin(alpha));
223
 
    int h = fabs((d->im->sx)*sin(alpha)) + fabs((d->im->sy)*cos(alpha));
224
 
    gdImagePtr im = gdImageCreateTrueColor(w,h);
225
 
    gdImageCopyRotated(im, d->im, w/2.0, h/2.0, 0,0, d->im->sx, d->im->sy, angle);
226
 
    gdImageDestroy(d->im);
227
 
    d->im = im;
228
 
    send_result(d, 0);
229
 
}
230
 
 
231
 
void image_resample(egd_data *d, char *buffer, int l){
232
 
    int new_w = decode(&buffer);
233
 
    int new_h = decode(&buffer);
234
 
    gdImagePtr im = gdImageCreateTrueColor(new_w, new_h);
235
 
    gdImageCopyResampled(im, d->im, 0, 0, 0, 0, new_w, new_h, d->im->sx, d->im->sy);
236
 
    gdImageDestroy(d->im);
237
 
    d->im = im;
238
 
    send_result(d, 0);
239
 
}
240
 
 
241
 
 
242
 
/* Fetch images */
243
 
 
244
 
void image_gif(egd_data *d, char *buffer, int l){
245
 
    int size = 0;
246
 
    void *gif = NULL; 
247
 
    
248
 
    if ( (gif = gdImageGifPtr(d->im, &size)) == NULL) {
249
 
        fprintf(stderr, "Gif fetch error\n");
250
 
        send_result(d, 1);
251
 
        return;
252
 
    } 
253
 
        
254
 
    driver_output(d->port, (char*)gif, size);
255
 
    gdFree(gif);
256
 
}
257
 
 
258
 
void image_jpeg(egd_data *d, char *buffer, int l){
259
 
    int size = 0;
260
 
    void *jpeg = NULL;
261
 
    int quality = decode(&buffer);
262
 
 
263
 
    if ( (jpeg = gdImageJpegPtr(d->im, &size, quality)) == NULL) {
264
 
        fprintf(stderr, "Jpeg fetch error\n");
265
 
        send_result(d, 1);
266
 
        return;
267
 
    } 
268
 
 
269
 
    driver_output(d->port, (char*)jpeg, size);
270
 
    gdFree(jpeg);
271
 
}
272
 
 
273
 
void image_png(egd_data *d, char *buffer, int l){
274
 
    int size = 0;
275
 
    void *png = NULL;
276
 
 
277
 
    if ( (png = gdImagePngPtr(d->im, &size)) == NULL) {
278
 
        fprintf(stderr, "Png fetch error\n");
279
 
        send_result(d, 1);
280
 
        return;
281
 
    }
282
 
    driver_output(d->port, (char*)png, size);
283
 
    gdFree(png);
284
 
}
285
 
 
286
 
 
287
 
/* Helpers */
288
 
 
289
 
gdFontPtr image_font(int size) {
290
 
    if (size == 1) {
291
 
        return gdFontGetTiny();
292
 
    } else if (size == 2) {
293
 
        return gdFontGetSmall();
294
 
    } else if (size == 3) {
295
 
        return gdFontGetMediumBold();
296
 
    } else if (size == 4) {
297
 
        return gdFontGetLarge();
298
 
    } else if (size == 5) {
299
 
        return gdFontGetGiant();
300
 
    }
301
 
    return gdFontGetSmall();
302
 
}
303
 
 
304
 
int egd_options_to_gd_arc_style(int options) {
305
 
    int style = 0;
306
 
 
307
 
    if (options & EGD_ARC_STYLE_ARC) style |= gdArc;
308
 
    if (options & EGD_ARC_STYLE_CHORD) style |= gdChord;
309
 
    if (options & EGD_ARC_STYLE_NO_FILL) style |= gdNoFill;
310
 
    if (options & EGD_ARC_STYLE_EDGED) style |= gdEdged;
311
 
 
312
 
    return style;
313
 
}
314
 
/* Allocators */
315
 
 
316
 
gdPoint *image_alloc_points(char *buffer, int n) {
317
 
    int i;
318
 
    gdPoint *pts = (gdPoint*)driver_alloc(sizeof(gdPoint)*n);
319
 
 
320
 
    for (i = 0; i < n; i++) {
321
 
        pts[i].x = decode(&buffer);
322
 
        pts[i].y = decode(&buffer);
323
 
    }
324
 
    return pts;
325
 
}
326
 
 
327
 
void image_free_points(gdPoint *pts) {
328
 
    driver_free(pts);
329
 
}
330
 
 
331
 
char *image_alloc_string(char *buffer, int n) {
332
 
    int i;
333
 
    char *str = (char *)driver_alloc(sizeof(char)*(n + 1));
334
 
    for (i = 0; i < n; i++) {
335
 
        str[i] = (char)decode(&buffer);
336
 
    }
337
 
    str[n] = '\0';
338
 
    return str;
339
 
}
340
 
 
341
 
void image_free_string(char *str) {
342
 
    driver_free(str);
343
 
}