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
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
                                Pike module: 
 
                                    image 
 
                       Pontus Hagland law@infovav.se 
                         Per Hedbor per@infovav.se 
                        David K├ągedal kg@infovav.se 
---------------------------------------------------------------------------- 
This package adds two Pike progams: 
 
   * "precompiled/image" and 
   * "precompiled/font". 
 
---------------------------------------------------------------------------- 
 
methods in precompiled/image: 
 
Methods resulting in a new object: 
object clone( [int xsize,int ysize [,int r,int g,int b] ] ); 
 
object copy( [int x1,int y1,int x2,int y2 [,int r,int g,int b] ] ); 
object autocrop( [int border_width [,int left,int right,int top,int bottom] 
[,int r,int g,int b] ] ); 
 
object gray(); 
object color(int r,int g,int b); 
object invert(); 
 
object mirrorx(void); 
object mirrory(void); 
object rotate_cw(void); 
object rotate_ccw(void); 
object threshold([int r,int g,int b]); 
object apply_matrix(array(array(int)) matrix,[int r,int g,int b[,int div]]); 
 
object scale(float factor); 
object scale(float factorx,float factory); 
object scale(int newx|0,int newy|0); 
 
Methods operating on current object: 
string toppm(void); 
string|object fromppm(string s); 
string togif( [int r,inr g,int b] ); 
 
object paste(object img [,int x,int y]) 
object paste_alpha(object img, int alpha [,int x, int y]); 
object paste_mask(object img, object alpha_mask [,int x,int y]); 
 
object setcolor(int r,int g,int b); 
object setpixel(int x,int y [,int r,int g,int b] ); 
object line(int x1,int y1,int x2,int y2 [,int r,int g,int b] ); 
object box(int x1,int y1,int x2,int y2 [,int r,int g,int b] ); 
object circle(int x,int y,int radx,int rady [,int r,int b,int g] ); 
object tuned_box(int x1,int y1,int x2,int y2,array(array(int)) corner_rgb); 
 
Information giving methods: 
object xsize(); 
object ysize(); 
---------------------------------------------------------------------------- 
 
METHOD 
     object apply_matrix(array(array(int)) matrix,[int r,int g,int b[,int 
     div]]); 
DESCRIPTION 
     This method applies a matrix on the image. Each surrounding pixel is 
     multiplied with the value of the matrix element in that point, these 
     values are added and divided by the total sum of the matrix values (and 
     the div argument) and stored on the pixel (eventually added to the 
     r,g,b argument given as 'mean' value). 
 
     It is possible to use a matrix of RGB groups (ie an array of three 
     integers) instead of the simple values, this making it possible to 
     apply different matrices on red, green and blue channel. 
RETURN VALUE 
     the new object 
EXAMPLE 
     A 'blur' operation (3x3, gaussian): 
 
     blurred=image->apply_matrix( ({ ({1,2,1}), ({2,3,2}), ({1,2,1}) }) ); 
 
     A 'Emboss' operation (3x3): 
 
     emossed=image->apply_matrix(({ ({0,1,8}), ({-1,0,1}), ({-8,-1,0}) }), 128,128,128, 15 ); 
 
     Here i'm using 128,128,128 (gray) as a mean, because i get negative 
     values. 
     A division by 15 is good to give 'normal' edges. 
BUGS 
     not known 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object autocrop( [int border_width [,int left,int right,int top,int 
     bottom] [,int r,int g,int b] ] ); 
DESCRIPTION 
     Crops away unneccesary borders from the image. The border argument is 
     to define the new thickness of the surrounding border and the r,g,b is 
     the newly created border color. 
 
     The left, right, ... arguments is used to tell which edges should be 
     autocropped. 
RETURN VALUE 
     the new object 
EXAMPLE 
 
     cropped=image->autocrop(); 
 
BUGS 
     now known 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object box(int x1,int y1,int x2,int y2 [,int r,int g,int b] ); 
DESCRIPTION 
     Draw a box of the default or specified color. 
RETURN VALUE 
     the image object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object circle(int x,int y,int radx,int rady [,int r,int b,int g] ); 
DESCRIPTION 
     Draw a circle. The coordinates given are the center of the image and 
     the radius in x (horisontal) and y (vertical), this making it possible 
     to draw an ellipse too. :-) 
RETURN VALUE 
     the image object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object clone( [int xsize,int ysize [,int r,int g,int b] ] ); 
DESCRIPTION 
     make a new object and return it 
        o no arguments -> old image is copied 
        o size is given -> old image is copied cropped 
        o color is given -> new default color 
RETURN VALUE 
     the new object 
SEE ALSO 
     copy, clear 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object color(int r,int g,int b); 
DESCRIPTION 
     Apply a color filter on the image. 
RETURN VALUE 
     the new object 
EXAMPLE 
 
     cyan=image->color(64,255,192); 
 
     This function is most usable on a image that has been grayed first. 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object copy( [int x1,int y1,int x2,int y2 [,int r,int g,int b] ] ); 
DESCRIPTION 
     Make a copy, or a copy of a part of the image. It is possible to copy 
     more then the image, to extend the image, this area is filled with the 
     current (or given) color. 
RETURN VALUE 
     the new image object 
EXAMPLE 
 
     copy=image->copy(); 
 
     copy=image->copy(-10,-10,image->xsize()+9,image->ysize()+9); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     string|object fromppm(string s); 
DESCRIPTION 
     Import a ppm image. 
RETURN VALUE 
     0 (object) upon success, else the error message (string). 
EXAMPLE 
 
     image=clone( (program)"precompiled/image" ); 
     image->fromppm(read_bytes("my_image.ppm",0,10000000)); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object gray([int r,int g,int b]); 
DESCRIPTION 
     Make this image gray (each r,g,b gets the same value). 
     If a color is given, that specifies the amount of r, g, and b that is 
     used to compute the gray level. Default is 87,127,41. 
RETURN VALUE 
     the new object 
EXAMPLE 
 
     gray=image->gray() 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object invert(); 
DESCRIPTION 
     Invert the image. 
RETURN VALUE 
     the new object 
EXAMPLE 
 
     inverted=image->invert() 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object line(int x1,int y1,int x2,int y2 [,int r,int g,int b] ); 
DESCRIPTION 
     Draw a line from x1,y1 to x2,y2. 
RETURN VALUE 
     the image object 
EXAMPLE 
 
     image->line(17,100,42,1000); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object mirrorx(void); 
     object mirrory(void); 
DESCRIPTION 
     Mirrors the image, horisontally or vertically. 
RETURN VALUE 
     the new image object 
EXAMPLE 
 
     mirrored=image->mirrorx(); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object rotate_cw(void); 
     object rotate_ccw(void); 
DESCRIPTION 
     Rotate the image, clockwise or counterclockwise, 90 degrees. 
     This operation is very fast compared to rotating any angle. 
RETURN VALUE 
     the new image object 
EXAMPLE 
 
     snurr=image->rotate_cw(); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object paste(object img [,int x,int y]) 
     object paste_alpha(object img, int alpha [,int x, int y]); 
     object paste_mask(object img, object alpha_mask [,int x,int y]); 
DESCRIPTION 
     Paste an image on this image. Use the specified alpha channel value or 
     the second specified image as an alpha channel. 
     The first argument is the image that will be pasted. 
RETURN VALUE 
     the image object this function doesn't return anything 
EXAMPLE 
 
     image->paste(other_smaller_image,17,42); 
 
     image->paste_mask(other_image,alpha_channel_image); 
 
     Paste a dog on a landscape: 
 
     landscape->paste(dog,dog_alpha_channel,xpos,ypos); 
 
     Write some text: 
 
     text=font->write("some text"); 
     foreground=text->clear(255,255,255); // white 
     background->paste(foreground,text,xpos,ypos); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object scale(float factor); (1 
     object scale(float factorx,float factory); (2 
     object scale(int newx|0,int newy|0); (3 
DESCRIPTION 
     Scale this image. 
       1. scale the image with a (line scale) factor 
       2. scale the image with different factors on x and y 
       3. scale the image to a new size 
          with newx or newy set to zero, just scale the image to fit the x 
          or y size and keep proportions. 
RETURN VALUE 
     the new object this function doesn't return anything 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
METHOD 
     object setcolor(int r,int g,int b); 
DESCRIPTION 
     set the default color used for drawing lines, etc 
RETURN VALUE 
     the image object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
METHOD 
     object setpixel(int x,int y [,int r,int g,int b] ); 
DESCRIPTION 
     set the color of the specified pixel 
RETURN VALUE 
     the image object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object threshold([int r,int g,int b]); 
DESCRIPTION 
     make image black-and-white using the given value as the threshold 
RETURN VALUE 
     the new object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     string togif( [int r,inr g,int b] ); 
DESCRIPTION 
     export gif 
     if the color are given, this is the transparent color 
RETURN VALUE 
     the gifimage as a string 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     string toppm(object); 
DESCRIPTION 
     export ppm 
RETURN VALUE 
     the ppm image as a string 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object tuned_box(int x1,int y1,int x2,int y2,array(array(int)) 
     corner_rgb); 
DESCRIPTION 
     draw a box with the specified corner colours, and shade the colors 
     between 
RETURN VALUE 
     the image object 
EXAMPLE 
 
     image->tuned_box(0,0,img->xsize()-1,img->ysize()-1, 
                     ({({0,0,64}),({16,16,128}), 
                       ({16,16,128}),({192,160,128})})); 
 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object xsize(); 
     object ysize(); 
DESCRIPTION 
RETURN VALUE 
     Gives the x- or the y-size (horisontal or vertical size) of the image. 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
methods in precompiled/font: 
 
int load(string file_name); 
object write(string line, ...); 
---------------------------------------------------------------------------- 
 
METHOD 
     int load(string file_name); 
DESCRIPTION 
     load this font object with the font from the specified file 
RETURN VALUE 
     true on success 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
METHOD 
     object write(string line, ...); 
DESCRIPTION 
     make a new image object from the specified text, each argument 
     representing a line 
RETURN VALUE 
     the new image object 
EXAMPLE 
BUGS 
 
---------------------------------------------------------------------------- 
 
Example program: 
 
(pike) 
 
int main() 
{ 
   object txt,o,shad,font; 
   int i; 
 
   txt = 
      (font=clone((program)"/precompiled/font")) 
      ->load("/usr/local/lib/pike/fonts/64/helvetica_bold_r") 
      ->write("The Image Module") 
      ->autocrop(20,0,0,0); 
 
   shad=txt->mirrory()->scale(1.0,0.3)->color(64,64,64); 
 
   o=clone((program)"/precompiled/image", 
           txt->xsize(),txt->ysize(), 0,0,100) 
        ->tuned_box(0,0,txt->xsize(),txt->ysize(), 
                    ({({0,0,0}),({0,0,0}), 
                      ({0,0,255}),({128,128,0})})); 
 
   o->setcolor(255,255,255,200); 
   for (i=0; i<30; i++) 
      if (random(2)) 
         o->line(random(o->xsize()),o->ysize()-10-random(20+i*3), 
                 o->xsize()-1-random(30),o->ysize()-1); 
      else 
         o->line(random(o->xsize()),o->ysize()-10-random(20+i), 
                 random(30),o->ysize()-1); 
 
   for (i=0; i<10; i++) 
      o->box(random(o->xsize()),random(o->ysize()), 
             random(o->xsize()),random(o->ysize()), 
             random(256),random(256),random(256),220); 
 
   o -> paste_mask(txt->clear(0,255,0), 
                   shad,0,(int)(font->baseline()*0.7)+shad->ysize()-10) 
     -> paste_mask(txt->clear(255,255,0), 
                 txt->apply_matrix(({({1,2,1}),({2,4,2}),({1,2,1})})) 
                 ->apply_matrix(({({1,2,1}),({2,4,2}),({1,2,1})})) 
                 ->modify_by_intensity(1,0,0, 0,255,255,255,255,255)) 
     -> paste_mask(txt->clone() 
                   ->tuned_box(0,0,txt->xsize()-1,txt->ysize()-1, 
                               ({({128,128,128}),({64,128,0}), 
                                 ({64,128,0}),({255,255,0})})), 
                   txt); 
   write(o->togif_fs()); 
   return 0; 
}