1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 
5 #include "ppport.h"
6 
7 #include <Imlib2.h>
8 #include <stdio.h>
9 #include <string.h>
10 
11 typedef Imlib_Image Image__Imlib2;
12 typedef ImlibPolygon Image__Imlib2__Polygon;
13 typedef Imlib_Color_Range Image__Imlib2__ColorRange;
14 
colours_equal(Imlib_Color col1,Imlib_Color col2)15 bool colours_equal(Imlib_Color col1, Imlib_Color col2) {
16   return col1.red   == col2.red   &&
17          col1.green == col2.green &&
18          col1.blue  == col2.blue;
19 }
20 
21 static double
TEXT_TO_RIGHT(void)22 TEXT_TO_RIGHT(void)
23 {
24    return IMLIB_TEXT_TO_RIGHT;
25 }
26 
27 static double
TEXT_TO_LEFT(void)28 TEXT_TO_LEFT(void)
29 {
30    return IMLIB_TEXT_TO_LEFT;
31 }
32 
33 static double
TEXT_TO_UP(void)34 TEXT_TO_UP(void)
35 {
36    return IMLIB_TEXT_TO_UP;
37 }
38 
39 static double
TEXT_TO_DOWN(void)40 TEXT_TO_DOWN(void)
41 {
42    return IMLIB_TEXT_TO_DOWN;
43 }
44 
45 static double
TEXT_TO_ANGLE(void)46 TEXT_TO_ANGLE(void)
47 {
48    return IMLIB_TEXT_TO_ANGLE;
49 }
50 
51 MODULE = Image::Imlib2          PACKAGE = Image::Imlib2
52 
53 double
54 TEXT_TO_RIGHT()
55 
56 double
57 TEXT_TO_LEFT()
58 
59 double
60 TEXT_TO_UP()
61 
62 double
63 TEXT_TO_DOWN()
64 
65 double
66 TEXT_TO_ANGLE()
67 
68 MODULE = Image::Imlib2		PACKAGE = Image::Imlib2		PREFIX= Imlib2_
69 
70 Image::Imlib2
71 Imlib2_new(packname="Image::Imlib2", x=256, y=256)
72         char * packname
73 	int x
74 	int y
75 
76 	PROTOTYPE: $;$$
77 
78         CODE:
79 	{
80 		Imlib_Image image;
81 
82 		image = imlib_create_image(x, y);
83 
84 		imlib_context_set_image(image);
85 		imlib_image_set_has_alpha(1);
86 
87 		RETVAL = image;
88 	}
89         OUTPUT:
90 	        RETVAL
91 
92 Image::Imlib2
93 Imlib2__new_using_data(packname="Image::Imlib2", x=256, y=256, data)
94         char * packname
95 	int x
96 	int y
97         DATA32 * data
98 
99 	PROTOTYPE: $;$$$
100 
101         CODE:
102 	{
103 		Imlib_Image image;
104 
105 		image = imlib_create_image_using_copied_data(x, y, data);
106 
107 		imlib_context_set_image(image);
108 		imlib_image_set_has_alpha(1);
109 
110 		RETVAL = image;
111 	}
112         OUTPUT:
113 	        RETVAL
114 
115 
116 char
117 Imlib2_will_blend(packname="Image::Imlib2", ...)
118         char * packname
119 
120         PREINIT:
121         char   value;
122 
123         PROTOTYPE: $;$
124 
125         CODE:
126 	{
127 		if (items > 1) {
128 		  value =  SvTRUE(ST(1))?1:0;
129 		  imlib_context_set_blend(value);
130 		}
131 
132 		RETVAL = imlib_context_get_blend();
133 	}
134 
135         OUTPUT:
136                 RETVAL
137 
138 
139 
140 
141 void
Imlib2_DESTROY(image)142 Imlib2_DESTROY(image)
143 	Image::Imlib2	image
144 
145 	PROTOTYPE: $
146 
147 	CODE:
148 	{
149 		imlib_context_set_image(image);
150 
151 		imlib_free_image();
152 	}
153 
154 
155 
156 Image::Imlib2
157 Imlib2_load(packname="Image::Imlib2", filename)
158         char * packname
159 	char * filename
160 
161 	PROTOTYPE: $$
162 
163         CODE:
164 	{
165 		Imlib_Image image;
166                 Imlib_Load_Error err;
167 
168                 image = imlib_load_image_with_error_return (filename, &err);
169                 if (err == IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST) {
170                   Perl_croak(aTHX_ "Image::Imlib2 load error: File does not exist");
171                 }
172 
173                 if (err == IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY) {
174                   Perl_croak(aTHX_ "Image::Imlib2 load error: File is directory");
175                 }
176 
177                 if (err == IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ) {
178                   Perl_croak(aTHX_ "Image::Imlib2 load error: Permission denied");
179                 }
180 
181                 if (err == IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT) {
182                   Perl_croak(aTHX_ "Image::Imlib2 load error: No loader for file format");
183                 }
184 		RETVAL = image;
185 	}
186         OUTPUT:
187 	        RETVAL
188 
189 
190 void
Imlib2_save(image,filename)191 Imlib2_save(image, filename)
192 	Image::Imlib2	image
193 	char * filename
194 
195 	PROTOTYPE: $$
196 
197         CODE:
198 	{
199                 Imlib_Load_Error err;
200 
201 		imlib_context_set_image(image);
202 		imlib_save_image_with_error_return(filename, &err);
203 
204                 if (err != IMLIB_LOAD_ERROR_NONE) {
205                   Perl_croak(aTHX_ "Image::Imlib2 save error: Unknown error");
206                 }
207 	}
208 
209 
210 
211 
212 int
Imlib2_get_width(image)213 Imlib2_get_width(image)
214 	Image::Imlib2	image
215 
216         PROTOTYPE: $
217 
218         CODE:
219 	{
220 		imlib_context_set_image(image);
221 
222 		RETVAL = imlib_image_get_width();
223 	}
224 
225         OUTPUT:
226                 RETVAL
227 
228 
229 int
Imlib2_width(image)230 Imlib2_width(image)
231 	Image::Imlib2	image
232 
233         PROTOTYPE: $
234 
235         CODE:
236 	{
237 		imlib_context_set_image(image);
238 
239 		RETVAL = imlib_image_get_width();
240 	}
241 
242         OUTPUT:
243                 RETVAL
244 
245 
246 int
Imlib2_get_height(image)247 Imlib2_get_height(image)
248 	Image::Imlib2	image
249 
250         PROTOTYPE: $
251 
252         CODE:
253 	{
254 		imlib_context_set_image(image);
255 
256 		RETVAL = imlib_image_get_height();
257 	}
258 
259         OUTPUT:
260                 RETVAL
261 
262 
263 int
Imlib2_height(image)264 Imlib2_height(image)
265 	Image::Imlib2	image
266 
267         PROTOTYPE: $
268 
269         CODE:
270 	{
271 		imlib_context_set_image(image);
272 
273 		RETVAL = imlib_image_get_height();
274 	}
275 
276         OUTPUT:
277                 RETVAL
278 
279 
280 void
Imlib2_set_color(image,r,g,b,a)281 Imlib2_set_color(image, r, g, b, a)
282 	Image::Imlib2	image
283 	int	r
284 	int 	g
285 	int 	b
286 	int 	a
287 
288 	PROTOTYPE: $$$$$
289 
290         CODE:
291 	{
292 		imlib_context_set_image(image);
293 
294 		imlib_context_set_color(r, g, b, a);
295 	}
296 
297 
298 void
Imlib2_set_colour(image,r,g,b,a)299 Imlib2_set_colour(image, r, g, b, a)
300 	Image::Imlib2	image
301 	int	r
302 	int 	g
303 	int 	b
304 	int 	a
305 
306 	PROTOTYPE: $$$$$
307 
308         CODE:
309 	{
310 		imlib_context_set_image(image);
311 
312 		imlib_context_set_color(r, g, b, a);
313 	}
314 
315 
316 void
Imlib2_draw_point(image,x,y)317 Imlib2_draw_point(image, x, y)
318 	Image::Imlib2	image
319 	int	x
320 	int 	y
321 
322 	PROTOTYPE: $$$
323 
324         CODE:
325 	{
326 		imlib_context_set_image(image);
327 
328 		imlib_image_draw_pixel(x, y, 0);
329 	}
330 
331 
332 void
Imlib2_draw_line(image,x1,y1,x2,y2)333 Imlib2_draw_line(image, x1, y1, x2, y2)
334 	Image::Imlib2	image
335 	int	x1
336 	int 	y1
337 	int 	x2
338 	int 	y2
339 
340 	PROTOTYPE: $$$$$
341 
342         CODE:
343 	{
344 		imlib_context_set_image(image);
345 
346 		imlib_image_draw_line(x1, y1, x2, y2, 0);
347 	}
348 
349 void
350 Imlib2_query_pixel(image, x, y)
351 	Image::Imlib2	image
352 	int 	x
353 	int 	y
354 
355 	PROTOTYPE: $$
356 
357 	PREINIT:
358 		Imlib_Color color_return;
359 
360    PPCODE:
361 		imlib_context_set_image(image);
362 
363 		imlib_image_query_pixel(x, y, &color_return);
364         XPUSHs(sv_2mortal(newSViv(color_return.red)));
365         XPUSHs(sv_2mortal(newSViv(color_return.green)));
366         XPUSHs(sv_2mortal(newSViv(color_return.blue)));
367         XPUSHs(sv_2mortal(newSViv(color_return.alpha)));
368 
369 void
370 Imlib2_autocrop_dimensions(image)
371 	Image::Imlib2	image
372 
373 	PROTOTYPE: $$
374 
375 	PREINIT:
376 		Imlib_Color c, bg, tl, tr, bl, br;
377                 int width, height;
378                 int cx = 0;
379                 int cy = 0;
380                 int cw, ch;
381                 int x1, y1, x2, y2;
382                 int i;
383                 bool abort;
384 
385         PPCODE:
386 		imlib_context_set_image(image);
387 		width = imlib_image_get_width();
388 		height = imlib_image_get_height();
389                 cw = width;
390                 ch = height;
391 
392                 /* guess the background colour
393                    algorithm from gimp's autocrop.c, originally pinched from
394                    pnmcrop: first see if three corners are equal, then if two are equal,
395                    otherwise give up */
396 		imlib_image_query_pixel(0, 0, &tl);
397 		imlib_image_query_pixel(width - 1, 0, &tr);
398 		imlib_image_query_pixel(0, height - 1, &bl);
399 		imlib_image_query_pixel(width -1 , height - 1, &br);
400 
401                 if (colours_equal(tr, bl) && colours_equal(tr, br)) {
402                    bg = tr;
403                 } else if (colours_equal(tl, bl) && colours_equal(tl, br)) {
404                    bg = tl;
405                 } else if (colours_equal(tl, tr) && colours_equal(tl, br)) {
406                    bg = tl;
407                 } else if (colours_equal(tl, tr) && colours_equal(tl, bl)) {
408                    bg = tl;
409                 } else if (colours_equal(tl, tr) || colours_equal(tl, bl) || colours_equal(tl, br)) {
410                    bg = tl;
411                 } else if (colours_equal(tr, bl) || colours_equal(tr, bl)) {
412                    bg = tr;
413                 } else if (colours_equal(br, bl)) {
414                    bg = br;
415                 } else {
416                    /* all different? give up */
417                   XPUSHs(sv_2mortal(newSViv(cx)));
418                   XPUSHs(sv_2mortal(newSViv(cy)));
419                   XPUSHs(sv_2mortal(newSViv(cw)));
420                   XPUSHs(sv_2mortal(newSViv(ch)));
421                   return;
422                 }
423 
424                 /* warn ("Have background colour: %i, %i, %i", bg.red, bg.green, bg.blue); */
425 
426                 /* check how many of the bottom lines are uniform */
427                 abort = FALSE;
428                 for (y2 = height - 1; y2 >= 0 && !abort; y2--) {
429                   for (i = 0; i < width && !abort; i++) {
430                     imlib_image_query_pixel(i, y2, &c);
431                     abort = !colours_equal (c, bg);
432                   }
433                 }
434 
435                 /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */
436 
437                 if (y2 == -1) {
438                   /* plain colour */
439                   XPUSHs(sv_2mortal(newSViv(cx)));
440                   XPUSHs(sv_2mortal(newSViv(cy)));
441                   XPUSHs(sv_2mortal(newSViv(cw)));
442                   XPUSHs(sv_2mortal(newSViv(ch)));
443 				  return;
444                 }
445 
446 				/* since now we don't need to check for the upper boundary
447 				of the outer loops as there is at least one pixel of different colour */
448 
449                 /* check how many of the top lines are uniform */
450                 abort = FALSE;
451                 for (y1 = 0; !abort; y1++) {
452                   for (i = 0; i < width && !abort; i++) {
453                     imlib_image_query_pixel(i, y1, &c);
454                     abort = !colours_equal (c, bg);
455                     }
456                 }
457 
458                 y2 += 1; /* to make y2 - y1 == height */
459 
460                 /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */
461 
462                 /* the coordinates are now the first rows which DON'T match
463                 * the colour - crop instead to one row larger:
464                 */
465                 if (y1 > 0) --y1;
466                 if (y2 < height-1) ++y2;
467 
468                 /* check how many of the left lines are uniform */
469                 abort = FALSE;
470                 for (x1 = 0; !abort; x1++) {
471                   for (i = y1; i < y2 && !abort; i++) {
472                     imlib_image_query_pixel(x1, i, &c);
473                     abort = !colours_equal (c, bg);
474                   }
475                 }
476 
477                 /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */
478 
479                 /* check how many of the right lines are uniform */
480                 abort = FALSE;
481                 for (x2 = width - 1; !abort; x2--) {
482                   for (i = y1; i < y2 && !abort; i++) {
483                     imlib_image_query_pixel(x2, i, &c);
484                     abort = !colours_equal (c, bg);
485                   }
486                 }
487 
488                 x2 += 1; /* to make x2 - x1 == width */
489 
490                 /* the coordinates are now the first columns which DON'T match
491                  * the color - crop instead to one column larger:
492                  */
493                 if (x1 > 0) --x1;
494                 if (x2 < width-1) ++x2;
495 
496                 /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */
497 
498                 cx = x1;
499                 cy = y1;
500                 cw = x2 - x1;
501                 ch = y2 - y1;
502 
503                 XPUSHs(sv_2mortal(newSViv(cx)));
504                 XPUSHs(sv_2mortal(newSViv(cy)));
505                 XPUSHs(sv_2mortal(newSViv(cw)));
506                 XPUSHs(sv_2mortal(newSViv(ch)));
507 
508 void
509 Imlib2_find_colour(image)
510 	Image::Imlib2	image
511 
512 	PROTOTYPE: $$
513 
514 	PREINIT:
515 		Imlib_Color c;
516                 int r, g, b, a;
517                 int width, height;
518                 int x = 0;
519                 int y = 0;
520                 bool abort;
521 
522         PPCODE:
523 		imlib_context_set_image(image);
524 		width = imlib_image_get_width();
525 		height = imlib_image_get_height();
526                 imlib_context_get_color(&r, &g, &b, &a);
527 //                warn("pr = %i, pg = %i, pb = %i", r, g, b);
528 
529                 abort = FALSE;
530                 for (y = 0; y < height && !abort; y++) {
531                   for (x = 0; x < width && !abort; x++) {
532                     imlib_image_query_pixel(x, y, &c);
533                     abort = c.red == r && c.green == g && c.blue == b;
534                     }
535                 }
536 
537                 if (abort) {
538                   XPUSHs(sv_2mortal(newSViv(x)));
539                   XPUSHs(sv_2mortal(newSViv(y)));
540                 } else {
541                   XPUSHs(newSV(0));
542                   XPUSHs(newSV(0));
543                 }
544 
545 void
546 Imlib2_fill(image, x, y, newimage=NULL)
547 	Image::Imlib2	image
548         Image::Imlib2   newimage
549 	int	x
550 	int 	y
551 
552 	PROTOTYPE: $$$$;$
553 
554 	PREINIT:
555 		Imlib_Color c;
556                 int r, g, b, a;
557                 int or, og, ob, oa;
558                 int width, height, px, py, west, east;
559                 AV* coords;
560                 SV* sv;
561                 int length;
562                 bool abort;
563 
564         PPCODE:
565 		imlib_context_set_image(image);
566 		width = imlib_image_get_width();
567 		height = imlib_image_get_height();
568 
569                 imlib_image_query_pixel(x, y, &c);
570                 or = c.red; og = c.green; ob = c.blue;
571 
572                 imlib_context_get_color(&r, &g, &b, &a);
573 //                warn("pr = %i, pg = %i, pb = %i", r, g, b);
574 
575                 coords = newAV();
576                 av_push(coords, newSViv(x));
577                 av_push(coords, newSViv(y));
578 
579                 while (av_len(coords) != -1) {
580 
581                       length = av_len(coords);
582 //                      warn("length %i", length);
583 
584                       sv = av_shift(coords);
585                       x = SvIVX(sv);
586                       sv_free(sv);
587                       sv = av_shift(coords);
588                       y = SvIVX(sv);
589                       sv_free(sv);
590                       imlib_image_query_pixel(x, y, &c);
591 
592                       if ((c.red == or && c.green == og && c.blue == ob)) {
593 
594                       if (newimage != NULL) {
595                          imlib_context_set_image(newimage);
596                          imlib_context_set_color(r, g, b, a);
597                          imlib_image_draw_pixel(x, y, 0);
598                          imlib_context_set_image(image);
599                       }
600                       imlib_image_draw_pixel(x, y, 0);
601 
602                       west = x;
603                       east = x;
604 
605                       abort = FALSE;
606                       while (!abort) {
607                           west -= 1;
608                           imlib_image_query_pixel(west, y, &c);
609                           abort = (west == 0
610                             || !(c.red == or && c.green == og && c.blue == ob)
611                           );
612                       }
613 
614                       abort = FALSE;
615                       while (!abort) {
616                           east += 1;
617                           imlib_image_query_pixel(east, y, &c);
618                           abort = (east == width
619                             || !(c.red == or && c.green == og && c.blue == ob)
620                           );
621                       }
622 //                      warn("  %i-%i, %i", west, east, y);
623 
624                       for (px = west; px <= east; px++) {
625                           if (newimage != NULL) {
626                              imlib_context_set_image(newimage);
627                              imlib_image_draw_pixel(px, y, 0);
628                              imlib_context_set_image(image);
629                           }
630                           imlib_image_draw_pixel(px, y, 0);
631 
632                           py = y - 1;
633                           imlib_image_query_pixel(px, py, &c);
634                           if (py > 0
635                               && (c.red == or && c.green == og && c.blue == ob)
636                           ) {
637 //                                warn("  ^ %i, %i", px, py);
638                               av_push(coords, newSViv(px));
639                               av_push(coords, newSViv(py));
640                           }
641 
642                           py = y + 1;
643                           imlib_image_query_pixel(px, py, &c);
644                           if (py < height
645                               && (c.red == or && c.green == og && c.blue == ob)
646                           ) {
647 //                                warn("  v %i, %i", px, py);
648                               av_push(coords, newSViv(px));
649                               av_push(coords, newSViv(py));
650                           }
651                       }
652                       }
653                 }
654                 av_undef(coords);
655 
656 
657 void
Imlib2_draw_rectangle(image,x,y,w,h)658 Imlib2_draw_rectangle(image, x, y, w, h)
659 	Image::Imlib2	image
660 	int	x
661 	int 	y
662 	int 	w
663 	int 	h
664 
665 	PROTOTYPE: $$$$$
666 
667         CODE:
668 	{
669 		imlib_context_set_image(image);
670 
671 		imlib_image_draw_rectangle(x, y, w, h);
672 	}
673 
674 
675 void
Imlib2_fill_rectangle(image,x,y,w,h)676 Imlib2_fill_rectangle(image, x, y, w, h)
677 	Image::Imlib2	image
678 	int	x
679 	int 	y
680 	int 	w
681 	int 	h
682 
683 	PROTOTYPE: $$$$$
684 
685         CODE:
686 	{
687 		imlib_context_set_image(image);
688 
689 		imlib_image_fill_rectangle(x, y, w, h);
690 	}
691 
692 
693 void
Imlib2_draw_ellipse(image,x,y,w,h)694 Imlib2_draw_ellipse(image, x, y, w, h)
695 	Image::Imlib2	image
696 	int	x
697 	int 	y
698 	int 	w
699 	int 	h
700 
701 	PROTOTYPE: $$$$$
702 
703         CODE:
704 	{
705 		imlib_context_set_image(image);
706 
707 		imlib_image_draw_ellipse(x, y, w, h);
708 	}
709 
710 
711 void
Imlib2_fill_ellipse(image,x,y,w,h)712 Imlib2_fill_ellipse(image, x, y, w, h)
713 	Image::Imlib2	image
714 	int	x
715 	int 	y
716 	int 	w
717 	int 	h
718 
719 	PROTOTYPE: $$$$$
720 
721         CODE:
722 	{
723 		imlib_context_set_image(image);
724 
725 		imlib_image_fill_ellipse(x, y, w, h);
726 	}
727 
728 
729 
730 
731 void
Imlib2_add_font_path(image,directory)732 Imlib2_add_font_path(image, directory)
733 	Image::Imlib2	image
734 	char * directory
735 
736 	PROTOTYPE: $$
737 
738         CODE:
739 	{
740 		imlib_context_set_image(image);
741 
742 		imlib_add_path_to_font_path(directory);
743 	}
744 
745 
746 void
Imlib2_load_font(image,fontname)747 Imlib2_load_font(image, fontname)
748 	Image::Imlib2	image
749 	char * fontname
750 
751 	PROTOTYPE: $$
752 
753         CODE:
754 	{
755 		Imlib_Font font;
756 
757 		imlib_context_set_image(image);
758 
759 		font = imlib_load_font(fontname);
760 		imlib_context_set_font(font);
761 	}
762 
763 
764 void
765 Imlib2_get_text_size(image, text, direction=IMLIB_TEXT_TO_RIGHT, angle=0)
766 	Image::Imlib2	image
767 	char * 	text
768         int direction
769         double  angle
770 
771         PROTOTYPE: $$
772 
773 	PREINIT:
774 		int text_w;
775 		int text_h;
776 
777         PPCODE:
778 		imlib_context_set_image(image);
779                 imlib_context_set_direction(direction);
780                 imlib_context_set_angle(angle);
781 
782 		imlib_get_text_size(text, &text_w, &text_h);
783 
784 		XPUSHs(sv_2mortal(newSViv(text_w)));
785 		XPUSHs(sv_2mortal(newSViv(text_h)));
786 
787 
788 void
789 Imlib2_draw_text(image, x, y, text, direction=IMLIB_TEXT_TO_RIGHT, angle=0)
790 	Image::Imlib2	image
791 	int	x
792 	int 	y
793 	char * 	text
794         int direction
795         double  angle
796 
797 	PROTOTYPE: $$$$;$$
798 
799         CODE:
800 	{
801 		imlib_context_set_image(image);
802                 imlib_context_set_direction(direction);
803                 imlib_context_set_angle(angle);
804 
805 		imlib_text_draw(x, y, text);
806 	}
807 
808 
809 Image::Imlib2
Imlib2_crop(image,x,y,w,h)810 Imlib2_crop(image, x, y, w, h)
811 	Image::Imlib2	image
812 	int x
813 	int y
814 	int w
815 	int h
816 
817 	PROTOTYPE: $$$$$
818 
819         CODE:
820 	{
821 		Imlib_Image cropped;
822 
823 		imlib_context_set_image(image);
824 
825 		cropped = imlib_create_cropped_image(x, y, w, h);
826 		RETVAL = cropped;
827 	}
828         OUTPUT:
829 	        RETVAL
830 
831 
832 
833 void
Imlib2_blend(image,source,alpha,x,y,w,h,d_x,d_y,d_w,d_h)834 Imlib2_blend(image, source, alpha, x, y, w, h, d_x, d_y, d_w, d_h)
835 	Image::Imlib2	image
836 	Image::Imlib2	source
837 	int alpha
838 	int x
839 	int y
840 	int w
841 	int h
842 	int d_x
843 	int d_y
844 	int d_w
845 	int d_h
846 
847 	PROTOTYPE: $$$$$$$$$$$
848 
849         CODE:
850 	{
851 		imlib_context_set_image(image);
852 
853 		imlib_blend_image_onto_image(source, alpha, x, y, w, h, d_x, d_y, d_w, d_h);
854 	}
855 
856 
857 void
Imlib2_blur(image,radius)858 Imlib2_blur(image, radius)
859 	Image::Imlib2	image
860 	int	radius
861 
862 	PROTOTYPE: $$
863 
864         CODE:
865 	{
866 		imlib_context_set_image(image);
867 		imlib_image_blur(radius);
868 	}
869 
870 
871 void
Imlib2_sharpen(image,radius)872 Imlib2_sharpen(image, radius)
873 	Image::Imlib2	image
874 	int	radius
875 
876 	PROTOTYPE: $$
877 
878         CODE:
879 	{
880 		imlib_context_set_image(image);
881 		imlib_image_sharpen(radius);
882 	}
883 
884 
885 Image::Imlib2
Imlib2_clone(image)886 Imlib2_clone(image)
887 	Image::Imlib2	image
888 
889 	PROTOTYPE: $
890 
891         CODE:
892 	{
893 		Imlib_Image cloned;
894 
895 		imlib_context_set_image(image);
896 		cloned = imlib_clone_image();
897 
898 		RETVAL = cloned;
899 	}
900         OUTPUT:
901 	        RETVAL
902 
903 
904 void
Imlib2_draw_polygon(image,poly,closed)905 Imlib2_draw_polygon(image, poly, closed)
906         Image::Imlib2  image
907         Image::Imlib2::Polygon  poly
908         unsigned char closed
909 
910         PROTOTYPE: $$$
911 
912         CODE:
913         {
914 		imlib_context_set_image(image);
915 
916                 imlib_image_draw_polygon(poly,closed);
917         }
918 
919 void
Imlib2_fill_color_range_rectangle(image,cr,x,y,width,height,angle)920 Imlib2_fill_color_range_rectangle(image, cr, x, y, width, height, angle)
921         Image::Imlib2  image
922         Image::Imlib2::ColorRange cr
923         int x
924         int y
925         int width
926         int height
927         double angle
928 
929         PROTOTYPE: $$$$$$
930 
931         CODE:
932         {
933                 Imlib_Color_Range oldcr;
934 
935 		imlib_context_set_image(image);
936                 oldcr = imlib_context_get_color_range();
937                 imlib_context_set_color_range(cr);
938                 imlib_image_fill_color_range_rectangle(x,y,width,height,angle);
939                 imlib_context_set_color_range(oldcr);
940         }
941 
942 
943 void
Imlib2_image_orientate(image,steps)944 Imlib2_image_orientate(image, steps)
945 	Image::Imlib2	image
946 	int	steps
947 
948 	PROTOTYPE: $$
949 
950         CODE:
951 	{
952 		imlib_context_set_image(image);
953 
954 		imlib_image_orientate(steps);
955 	}
956 
957 void
Imlib2_image_set_format(image,format)958 Imlib2_image_set_format(image, format)
959   Image::Imlib2  image
960   char *   format
961   PROTOTYPE: $$
962         CODE:
963   {
964      imlib_context_set_image(image);
965      imlib_image_set_format(format);
966   }
967 
968 Image::Imlib2
Imlib2_create_scaled_image(image,dw,dh)969 Imlib2_create_scaled_image(image, dw, dh)
970         Image::Imlib2	image
971 	int dw
972 	int dh
973 
974 	PROTOTYPE: $$$
975 
976         CODE:
977 	{
978 		Imlib_Image dstimage;
979 		int sw, sh;
980 
981 		imlib_context_set_image(image);
982 		sw = imlib_image_get_width();
983 		sh = imlib_image_get_height();
984 
985 		if ( dw == 0 ) {
986 			dw = (int) (((double) dh * sw) / sh);
987 		}
988 		if ( dh == 0 ) {
989 			dh = (int) (((double) dw * sh) / sw);
990 		}
991 
992 		dstimage = imlib_create_cropped_scaled_image(0, 0, sw, sh, dw, dh);
993 
994 		RETVAL = dstimage;
995 	}
996         OUTPUT:
997 	        RETVAL
998 
Imlib2_set_quality(image,qual)999 Image::Imlib2 Imlib2_set_quality(image, qual)
1000         Image::Imlib2	image
1001 	int qual
1002 
1003 	PROTOTYPE: $$
1004 
1005         CODE:
1006 	{
1007 		imlib_context_set_image(image);
1008 		imlib_image_attach_data_value("quality",NULL,qual,NULL);
1009 	}
1010 
Imlib2_flip_horizontal(image)1011 Image::Imlib2 Imlib2_flip_horizontal(image)
1012         Image::Imlib2	image
1013 
1014 	PROTOTYPE: $
1015 
1016         CODE:
1017 	{
1018 		imlib_context_set_image(image);
1019 		imlib_image_flip_horizontal();
1020 	}
1021 
Imlib2_flip_vertical(image)1022 Image::Imlib2 Imlib2_flip_vertical(image)
1023         Image::Imlib2	image
1024 
1025 	PROTOTYPE: $
1026 
1027         CODE:
1028 	{
1029 		imlib_context_set_image(image);
1030 		imlib_image_flip_vertical();
1031 	}
1032 
Imlib2_flip_diagonal(image)1033 Image::Imlib2 Imlib2_flip_diagonal(image)
1034         Image::Imlib2	image
1035 
1036 	PROTOTYPE: $
1037 
1038         CODE:
1039 	{
1040 		imlib_context_set_image(image);
1041 		imlib_image_flip_diagonal();
1042 	}
1043 
1044 
1045 int
1046 Imlib2_has_alpha(image, ...)
1047 	Image::Imlib2	image
1048 
1049         PREINIT:
1050         char   value;
1051 
1052         PROTOTYPE: $;$
1053 
1054         CODE:
1055 	{
1056 		imlib_context_set_image(image);
1057 
1058 		if (items > 1) {
1059 		  value =  SvTRUE(ST(1))?1:0;
1060 		  imlib_image_set_has_alpha(value);
1061 		}
1062 
1063 		RETVAL = imlib_image_has_alpha();
1064 	}
1065 
1066         OUTPUT:
1067                 RETVAL
1068 
1069 void
1070 Imlib2_set_cache_size(packname="Image::Imlib2", size)
1071         char * packname
1072         int size
1073 
1074         PROTOTYPE: $$
1075 
1076         CODE:
1077 	{
1078                 imlib_set_cache_size(size);
1079 	}
1080 
1081 
1082 int
1083 Imlib2_get_cache_size(packname="Image::Imlib2")
1084         char * packname
1085 
1086         PROTOTYPE: $
1087 
1088         CODE:
1089 	{
1090                 RETVAL = imlib_get_cache_size();
1091 	}
1092 	OUTPUT:
1093 	        RETVAL
1094 
1095 void
Imlib2_set_changes_on_disk(image)1096 Imlib2_set_changes_on_disk(image)
1097 	Image::Imlib2	image
1098 
1099         PROTOTYPE: $
1100 
1101         CODE:
1102 	{
1103 		imlib_context_set_image(image);
1104                 imlib_image_set_changes_on_disk();
1105 	}
1106 
1107 
1108 Image::Imlib2
1109 Imlib2_create_transparent_image(source, alpha)
1110         Image::Imlib2 source
1111         int alpha
1112 
1113         PROTOTYPE: $$
1114 
1115 	PREINIT:
1116 		Imlib_Image destination;
1117 		Imlib_Color color_return;
1118                 int x, y, w, h;
1119 
1120         CODE:
1121         {
1122 		imlib_context_set_image(source);
1123 		w = imlib_image_get_width();
1124 		h = imlib_image_get_height();
1125 
1126                 destination = imlib_create_image(w, h);
1127 		imlib_context_set_image(destination);
1128 		imlib_image_set_has_alpha(1);
1129 
1130                 for (y = 0; y < h; y++) {
1131                   for (x = 0; x < w; x++)  {
1132            	    imlib_context_set_image(source);
1133                     imlib_image_query_pixel(x, y, &color_return);
1134                     imlib_context_set_color(color_return.red, color_return.green, color_return.blue, alpha);
1135 		    imlib_context_set_image(destination);
1136 		    imlib_image_draw_pixel(x, y, 0);
1137                   }
1138                 }
1139 		RETVAL = destination;
1140 	}
1141         OUTPUT:
1142 	        RETVAL
1143 
1144 
1145 Image::Imlib2
1146 Imlib2_create_blended_image(source1, source2, pc)
1147         Image::Imlib2 source1
1148         Image::Imlib2 source2
1149         int pc
1150 
1151         PROTOTYPE: $$
1152 
1153 	PREINIT:
1154 		Imlib_Image destination;
1155 		Imlib_Color color1, color2;
1156                 int x, y, w, h;
1157                 int npc;
1158 
1159         CODE:
1160         {
1161                 npc = 100 - pc;
1162 		imlib_context_set_image(source1);
1163 		w = imlib_image_get_width();
1164 		h = imlib_image_get_height();
1165 
1166                 destination = imlib_create_image(w, h);
1167 		imlib_context_set_image(destination);
1168 
1169                 for (y = 0; y < h; y++) {
1170                   for (x = 0; x < w; x++)  {
1171            	    imlib_context_set_image(source1);
1172                     imlib_image_query_pixel(x, y, &color1);
1173            	    imlib_context_set_image(source2);
1174                     imlib_image_query_pixel(x, y, &color2);
1175 		    imlib_context_set_image(destination);
1176                     imlib_context_set_color((color1.red * pc + color2.red * npc)/100, (color1.green * pc + color2.green * npc)/100, (color1.blue * pc + color2.blue * npc)/100, 255);
1177 		    imlib_image_draw_line(x, y, x, y, 0);
1178                   }
1179                 }
1180 		RETVAL = destination;
1181 	}
1182         OUTPUT:
1183 	        RETVAL
1184 
1185 Image::Imlib2
1186 Imlib2_create_rotated_image(source, angle)
1187 	Image::Imlib2 source
1188 	double angle
1189 	CODE:
1190 		imlib_context_set_image(source);
1191 		RETVAL = imlib_create_rotated_image(angle);
1192 	OUTPUT:
1193 		RETVAL
1194 
1195 MODULE = Image::Imlib2	PACKAGE = Image::Imlib2::Polygon	PREFIX= Imlib2_Polygon_
1196 
1197 Image::Imlib2::Polygon
1198 Imlib2_Polygon_new(packname="Image::Imlib2::Polygon")
1199         char * packname
1200 
1201 	PROTOTYPE: $
1202 
1203         CODE:
1204 	{
1205 		ImlibPolygon poly;
1206 
1207 		poly = imlib_polygon_new();
1208 		RETVAL = poly;
1209 	}
1210         OUTPUT:
1211 	        RETVAL
1212 
1213 
1214 void
Imlib2_Polygon_DESTROY(poly)1215 Imlib2_Polygon_DESTROY(poly)
1216         Image::Imlib2::Polygon  poly
1217 
1218         PROTOTYPE: $
1219 
1220         CODE:
1221         {
1222                 imlib_polygon_free(poly);
1223         }
1224 
1225 
1226 void
Imlib2_Polygon_add_point(poly,x,y)1227 Imlib2_Polygon_add_point(poly, x, y)
1228 	Image::Imlib2::Polygon	poly
1229 	int x
1230 	int y
1231 
1232 	PROTOTYPE: $$$
1233 
1234         CODE:
1235 	{
1236                 imlib_polygon_add_point(poly,x,y);
1237 	}
1238 
1239 
1240 void
Imlib2_Polygon_fill(poly)1241 Imlib2_Polygon_fill(poly)
1242         Image::Imlib2::Polygon  poly
1243 
1244         PROTOTYPE: $
1245 
1246         CODE:
1247         {
1248                 imlib_image_fill_polygon(poly);
1249         }
1250 
1251 
1252 MODULE = Image::Imlib2	PACKAGE = Image::Imlib2::ColorRange	PREFIX= Imlib2_ColorRange_
1253 
1254 Image::Imlib2::ColorRange
1255 Imlib2_ColorRange_new(packname="Image::Imlib2::ColorRange")
1256         char * packname
1257 
1258         PROTOTYPE: $
1259 
1260         CODE:
1261         {
1262                 Imlib_Color_Range cr;
1263 
1264                 cr = imlib_create_color_range();
1265                 RETVAL = cr;
1266         }
1267         OUTPUT:
1268                 RETVAL
1269 
1270 void
Imlib2_ColorRange_DESTROY(cr)1271 Imlib2_ColorRange_DESTROY(cr)
1272         Image::Imlib2::ColorRange cr
1273 
1274         PROTOTYPE: $
1275 
1276         CODE:
1277         {
1278                 Imlib_Color_Range oldcr;
1279                 oldcr = imlib_context_get_color_range();
1280                 imlib_context_set_color_range(cr);
1281                 imlib_free_color_range();
1282                 imlib_context_set_color_range(oldcr);
1283         }
1284 
1285 void
Imlib2_ColorRange_add_color(cr,d,r,g,b,a)1286 Imlib2_ColorRange_add_color(cr, d, r, g, b, a)
1287         Image::Imlib2::ColorRange cr
1288         int d
1289         int r
1290         int g
1291         int b
1292         int a
1293 
1294         PROTOTYPE: $$
1295 
1296         CODE:
1297         {
1298                 Imlib_Color_Range oldcr;
1299                 oldcr = imlib_context_get_color_range();
1300                 imlib_context_set_color_range(cr);
1301                 imlib_context_set_color(r,b,g,a);
1302                 imlib_add_color_to_color_range(d);
1303                 imlib_context_set_color_range(oldcr);
1304         }
1305 
1306