Image-Imlib2

 view release on metacpan or  search on metacpan

lib/Image/Imlib2.xs  view on Meta::CPAN

	Image::Imlib2	image
	int	r
	int 	g
	int 	b
	int 	a

	PROTOTYPE: $$$$$

        CODE:
	{
		imlib_context_set_image(image);

		imlib_context_set_color(r, g, b, a);
	}


void
Imlib2_draw_point(image, x, y)
	Image::Imlib2	image
	int	x
	int 	y

	PROTOTYPE: $$$

        CODE:
	{
		imlib_context_set_image(image);

		imlib_image_draw_pixel(x, y, 0);
	}


void
Imlib2_draw_line(image, x1, y1, x2, y2)
	Image::Imlib2	image
	int	x1
	int 	y1
	int 	x2
	int 	y2

	PROTOTYPE: $$$$$

        CODE:
	{
		imlib_context_set_image(image);

		imlib_image_draw_line(x1, y1, x2, y2, 0);
	}

void
Imlib2_query_pixel(image, x, y)
	Image::Imlib2	image
	int 	x
	int 	y

	PROTOTYPE: $$

	PREINIT:
		Imlib_Color color_return;

   PPCODE:
		imlib_context_set_image(image);
		
		imlib_image_query_pixel(x, y, &color_return);
        XPUSHs(sv_2mortal(newSViv(color_return.red)));
        XPUSHs(sv_2mortal(newSViv(color_return.green)));
        XPUSHs(sv_2mortal(newSViv(color_return.blue)));
        XPUSHs(sv_2mortal(newSViv(color_return.alpha)));

void
Imlib2_autocrop_dimensions(image)
	Image::Imlib2	image

	PROTOTYPE: $$

	PREINIT:
		Imlib_Color c, bg, tl, tr, bl, br;
                int width, height;
                int cx = 0;
                int cy = 0;
                int cw, ch;
                int x1, y1, x2, y2;
                int i;
                bool abort;

        PPCODE:
		imlib_context_set_image(image);
		width = imlib_image_get_width();
		height = imlib_image_get_height();
                cw = width;
                ch = height;

                /* guess the background colour
                   algorithm from gimp's autocrop.c, originally pinched from
                   pnmcrop: first see if three corners are equal, then if two are equal,
                   otherwise give up */
		imlib_image_query_pixel(0, 0, &tl);
		imlib_image_query_pixel(width - 1, 0, &tr);
		imlib_image_query_pixel(0, height - 1, &bl);
		imlib_image_query_pixel(width -1 , height - 1, &br);

                if (colours_equal(tr, bl) && colours_equal(tr, br)) {
                   bg = tr;
                } else if (colours_equal(tl, bl) && colours_equal(tl, br)) {
                   bg = tl;
                } else if (colours_equal(tl, tr) && colours_equal(tl, br)) {
                   bg = tl;
                } else if (colours_equal(tl, tr) && colours_equal(tl, bl)) {
                   bg = tl;
                } else if (colours_equal(tl, tr) || colours_equal(tl, bl) || colours_equal(tl, br)) {
                   bg = tl;
                } else if (colours_equal(tr, bl) || colours_equal(tr, bl)) {
                   bg = tr;
                } else if (colours_equal(br, bl)) {
                   bg = br;
                } else {
                   /* all different? give up */
                  XPUSHs(sv_2mortal(newSViv(cx)));
                  XPUSHs(sv_2mortal(newSViv(cy)));
                  XPUSHs(sv_2mortal(newSViv(cw)));
                  XPUSHs(sv_2mortal(newSViv(ch)));
                  return;
                }

                /* warn ("Have background colour: %i, %i, %i", bg.red, bg.green, bg.blue); */

                /* check how many of the bottom lines are uniform */
                abort = FALSE;
                for (y2 = height - 1; y2 >= 0 && !abort; y2--) {
                  for (i = 0; i < width && !abort; i++) {
                    imlib_image_query_pixel(i, y2, &c);
                    abort = !colours_equal (c, bg);
                  }
                }

                /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */

                if (y2 == -1) {
                  /* plain colour */
                  XPUSHs(sv_2mortal(newSViv(cx)));
                  XPUSHs(sv_2mortal(newSViv(cy)));
                  XPUSHs(sv_2mortal(newSViv(cw)));
                  XPUSHs(sv_2mortal(newSViv(ch)));
				  return;
                }

lib/Image/Imlib2.xs  view on Meta::CPAN

                /* the coordinates are now the first rows which DON'T match
                * the colour - crop instead to one row larger:
                */
                if (y1 > 0) --y1;
                if (y2 < height-1) ++y2;

                /* check how many of the left lines are uniform */
                abort = FALSE;
                for (x1 = 0; !abort; x1++) {
                  for (i = y1; i < y2 && !abort; i++) {
                    imlib_image_query_pixel(x1, i, &c);
                    abort = !colours_equal (c, bg);
                  }
                }

                /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */

                /* check how many of the right lines are uniform */
                abort = FALSE;
                for (x2 = width - 1; !abort; x2--) {
                  for (i = y1; i < y2 && !abort; i++) {
                    imlib_image_query_pixel(x2, i, &c);
                    abort = !colours_equal (c, bg);
                  }
                }

                x2 += 1; /* to make x2 - x1 == width */

                /* the coordinates are now the first columns which DON'T match
                 * the color - crop instead to one column larger:
                 */
                if (x1 > 0) --x1;
                if (x2 < width-1) ++x2;

                /* warn("x1 %i, y1 %i, x2 %i, y2 %i", x1, y1, x2, y2); */
                
                cx = x1;
                cy = y1;
                cw = x2 - x1;
                ch = y2 - y1;
  
                XPUSHs(sv_2mortal(newSViv(cx)));
                XPUSHs(sv_2mortal(newSViv(cy)));
                XPUSHs(sv_2mortal(newSViv(cw)));
                XPUSHs(sv_2mortal(newSViv(ch)));

void
Imlib2_find_colour(image)
	Image::Imlib2	image

	PROTOTYPE: $$

	PREINIT:
		Imlib_Color c;
                int r, g, b, a;
                int width, height;
                int x = 0;
                int y = 0;
                bool abort;

        PPCODE:
		imlib_context_set_image(image);
		width = imlib_image_get_width();
		height = imlib_image_get_height();
                imlib_context_get_color(&r, &g, &b, &a);
//                warn("pr = %i, pg = %i, pb = %i", r, g, b);

                abort = FALSE;
                for (y = 0; y < height && !abort; y++) {
                  for (x = 0; x < width && !abort; x++) {
                    imlib_image_query_pixel(x, y, &c);
                    abort = c.red == r && c.green == g && c.blue == b;
                    }
                }                                 

                if (abort) {
                  XPUSHs(sv_2mortal(newSViv(x)));
                  XPUSHs(sv_2mortal(newSViv(y)));
                } else {
                  XPUSHs(newSV(0));
                  XPUSHs(newSV(0));
                }

void
Imlib2_fill(image, x, y, newimage=NULL)
	Image::Imlib2	image
        Image::Imlib2   newimage
	int	x
	int 	y

	PROTOTYPE: $$$$;$

	PREINIT:
		Imlib_Color c;
                int r, g, b, a;
                int or, og, ob, oa;
                int width, height, px, py, west, east;
                AV* coords;
                SV* sv;
                int length;
                bool abort;

        PPCODE:
		imlib_context_set_image(image);
		width = imlib_image_get_width();
		height = imlib_image_get_height();

                imlib_image_query_pixel(x, y, &c);
                or = c.red; og = c.green; ob = c.blue;

                imlib_context_get_color(&r, &g, &b, &a);
//                warn("pr = %i, pg = %i, pb = %i", r, g, b);

                coords = newAV();
                av_push(coords, newSViv(x));
                av_push(coords, newSViv(y));

                while (av_len(coords) != -1) {

                      length = av_len(coords);
//                      warn("length %i", length);
                
                      sv = av_shift(coords);
                      x = SvIVX(sv);
                      sv_free(sv);
                      sv = av_shift(coords);
                      y = SvIVX(sv);
                      sv_free(sv);
                      imlib_image_query_pixel(x, y, &c);

                      if ((c.red == or && c.green == og && c.blue == ob)) {

                      if (newimage != NULL) {
                         imlib_context_set_image(newimage);
                         imlib_context_set_color(r, g, b, a);
                         imlib_image_draw_pixel(x, y, 0);                         
                         imlib_context_set_image(image);                         
                      }
                      imlib_image_draw_pixel(x, y, 0);

                      west = x;
                      east = x;

                      abort = FALSE;
                      while (!abort) {
                          west -= 1;
                          imlib_image_query_pixel(west, y, &c);
                          abort = (west == 0
                            || !(c.red == or && c.green == og && c.blue == ob)
                          );
                      }
                      
                      abort = FALSE;
                      while (!abort) {
                          east += 1;
                          imlib_image_query_pixel(east, y, &c);
                          abort = (east == width
                            || !(c.red == or && c.green == og && c.blue == ob)
                          );
                      }
//                      warn("  %i-%i, %i", west, east, y);

                      for (px = west; px <= east; px++) {

lib/Image/Imlib2.xs  view on Meta::CPAN

	int 	h

	PROTOTYPE: $$$$$

        CODE:
	{
		imlib_context_set_image(image);

		imlib_image_fill_ellipse(x, y, w, h);
	}




void
Imlib2_add_font_path(image, directory)
	Image::Imlib2	image
	char * directory

	PROTOTYPE: $$

        CODE:
	{
		imlib_context_set_image(image);

		imlib_add_path_to_font_path(directory);
	}


void
Imlib2_load_font(image, fontname)
	Image::Imlib2	image
	char * fontname

	PROTOTYPE: $$

        CODE:
	{
		Imlib_Font font;

		imlib_context_set_image(image);

		font = imlib_load_font(fontname);
		imlib_context_set_font(font);
	}


void
Imlib2_get_text_size(image, text, direction=IMLIB_TEXT_TO_RIGHT, angle=0)
	Image::Imlib2	image
	char * 	text
        int direction
        double  angle

        PROTOTYPE: $$

	PREINIT:
		int text_w;
		int text_h;

        PPCODE:
		imlib_context_set_image(image);
                imlib_context_set_direction(direction);
                imlib_context_set_angle(angle);

		imlib_get_text_size(text, &text_w, &text_h);

		XPUSHs(sv_2mortal(newSViv(text_w)));
		XPUSHs(sv_2mortal(newSViv(text_h)));


void
Imlib2_draw_text(image, x, y, text, direction=IMLIB_TEXT_TO_RIGHT, angle=0)
	Image::Imlib2	image
	int	x
	int 	y
	char * 	text
        int direction
        double  angle

	PROTOTYPE: $$$$;$$

        CODE:
	{
		imlib_context_set_image(image);
                imlib_context_set_direction(direction);
                imlib_context_set_angle(angle);

		imlib_text_draw(x, y, text);
	}


Image::Imlib2
Imlib2_crop(image, x, y, w, h)
	Image::Imlib2	image
	int x
	int y
	int w
	int h

	PROTOTYPE: $$$$$

        CODE:
	{
		Imlib_Image cropped;

		imlib_context_set_image(image);

		cropped = imlib_create_cropped_image(x, y, w, h);
		RETVAL = cropped;
	}
        OUTPUT:
	        RETVAL



void
Imlib2_blend(image, source, alpha, x, y, w, h, d_x, d_y, d_w, d_h)
	Image::Imlib2	image
	Image::Imlib2	source
	int alpha



( run in 1.565 second using v1.01-cache-2.11-cpan-71847e10f99 )