[q-lang-cvs] q/modules/magick/examples Makefile.am,NONE,1.1 magicktest.q,NONE,1.1 mozilla.png,NONE,1
Brought to you by:
agraef
From: <ag...@us...> - 2003-12-16 19:11:19
|
Update of /cvsroot/q-lang/q/modules/magick/examples In directory sc8-pr-cvs1:/tmp/cvs-serv655/modules/magick/examples Added Files: Makefile.am magicktest.q mozilla.png sprite.q Log Message: Added magick module --- NEW FILE: Makefile.am --- ## Process this file with automake to generate a GNU Makefile examplesdir = $(pkgdatadir)/examples/magick if GGI examples_DATA = *.q *.png endif EXTRA_DIST = *.q *.png --- NEW FILE: magicktest.q --- import ggi, magick; /* show NAME INFO: show an image (designated by a valid ImageMagick source specification NAME and an INFO structure, see magick.q for details) in a GGI visual. Returns the created visual. */ show NAME INFO = display IMG where IMG:Image = read_image NAME INFO; /* The simple case: our image is opaque. In this case we just render it on a screen visual. */ display IMG = ggi_put_box VIS (0,0) (W,H) PIXELS || ggi_flush VIS || VIS where (W,H|_) = image_info IMG, VIS = ggi_open "x:-keepcursor", () = ggi_set_mode VIS (sprintf "%dx%d" (W,H)), PIXELS = get_image_pixels IMG (0,0) (W,H) if is_opaque_image IMG; /* The complicated case: our image has transparency. In this case we draw the image over a background. To these ends, we first render the image on a memory visual and crossblit that to the target visual on the screen. */ def BACK = magick_pixel "gray"; display IMG = ggi_put_box MEM (0,0) (W,H) PIXELS || /* we need to also set the alpha values since these aren't transferred by ggi_put_box */ ggi_put_alpha_box MEM (0,0) (W,H) ALPHA || ggi_set_background VIS BACK || ggi_clear VIS || ggi_cross_blit MEM (0,0) (W,H) VIS (0,0) || ggi_flush VIS || VIS where (W,H|_) = image_info IMG, VIS = ggi_open "x:-keepcursor", () = ggi_set_mode VIS (sprintf "%dx%d.A8" (W,H)), () = ggi_set_alpha_flags VIS (ggi_get_alpha_flags VIS or GGI_BUF_PUTGET), (_,_,_,_,_,_,_,_,D,S) = sscanf (ggi_get_mode VIS) "%dx%d.V%dx%d.F%d.D%dx%d.[%c%d/%d]", MEM = ggi_open "memory", () = ggi_set_mode MEM (sprintf "%dx%d.[C%d/%d].A8" (W,H,D,S)), () = ggi_set_alpha_flags MEM (ggi_get_alpha_flags MEM or GGI_BUF_PUTGET), PIXELS = get_image_pixels IMG (0,0) (W,H), ALPHA = get_image_alpha IMG (0,0) (W,H) otherwise; --- NEW FILE: mozilla.png --- (This appears to be a binary file; contents omitted.) --- NEW FILE: sprite.q --- /* sprite.q: This is a tiny test program demonstrating how to move an image with transparency over a background image. 12-12-03 AG */ /* To use, evaluate `sprite', click left in the GGI window to show the sprite at the current pointer position, move the pointer to move it around, click right to hide the sprite again, etc. Press the middle mouse button to terminate the input loop at any time. */ import ggi, magick; /* globals */ def /* read in the background and the sprite image */ BACK_IMG = read_image "logo:" (), SPRITE_IMG = read_image "mozilla.png" (), /* determine the dimensions of the background image and render it inside a GGI display visual */ (W,H|_) = image_info BACK_IMG, VIS = ggi_open "x:-keepcursor", _ = ggi_set_mode VIS (sprintf "%dx%d.A8" (W,H)) || ggi_set_flags VIS GGI_FLAG_ASYNC || ggi_put_box VIS (0,0) (W,H) (get_image_pixels BACK_IMG (0,0) (W,H)) || ggi_flush VIS, /* get the colorspace of the display visual */ (_,_,_,_,_,_,_,_,D,S) = sscanf (ggi_get_mode VIS) "%dx%d.V%dx%d.F%d.D%dx%d.[%c%d/%d]", /* dimensions of the sprite image */ (W,H|_) = image_info SPRITE_IMG, /* create a memory visual as back storage */ BACK = ggi_open "memory", /* set the same colorspace as on the display visual, in order to avoid colorspace conversions (note that we need no alpha buffer here, since we only have to store the pixels of the background image) */ _ = ggi_set_mode BACK (sprintf "%dx%d.[C%d/%d]" (W,H,D,S)), /* render the sprite image in another memory visual with alpha buffer, which will be crossblitted to the display */ SPRITE = ggi_open "memory", _ = ggi_set_mode SPRITE (sprintf "%dx%d.[C%d/%d].A8" (W,H,D,S)) || /* make sure GGI_BUF_PUTGET is set to enable full alpha blending */ ggi_set_alpha_flags SPRITE (ggi_get_alpha_flags SPRITE or GGI_BUF_PUTGET) || ggi_put_box SPRITE (0,0) (W,H) (get_image_pixels SPRITE_IMG (0,0) (W,H)) || ggi_put_alpha_box SPRITE (0,0) (W,H) (get_image_alpha SPRITE_IMG (0,0) (W,H)); /* save background pixels in the BACK visual */ save_back (X,Y) = ggi_put_box BACK (0,0) (W,H) (ggi_get_box VIS (X,Y) (W,H)); /* restore background from the BACK visual */ restore_back (X,Y) = ggi_put_box VIS (X,Y) (W,H) (ggi_get_box BACK (0,0) (W,H)); /* draw the sprite (crossblit from SPRITE visual) */ draw_sprite (X,Y) = ggi_cross_blit SPRITE (0,0) (W,H) VIS (X,Y); /* show, hide and move the sprite on the display */ show_sprite (X,Y) = save_back (X,Y) || draw_sprite (X,Y) || ggi_flush VIS; hide_sprite (X,Y) = restore_back (X,Y) || ggi_flush VIS; move_sprite (X,Y) (X1,Y1) = restore_back (X,Y) || save_back (X1,Y1) || draw_sprite (X1,Y1) || ggi_flush VIS; /* the main function */ sprite = loop () (); /* the input loop; take appropriate actions on mouse events */ loop STATE POS = process STATE POS (ggi_read VIS GGI_EM_ALL); // exit when middle button pressed process STATE _ (ggi_event TYPE _ _ _ _ PARAM) = cleanup STATE if (TYPE = GGI_EV_PTRBUTTONPRESS) and then (PARAM = 3); // clean up at exit cleanup (X,Y) = hide_sprite (X-W2,Y-H2); cleanup _ = () otherwise; // use these offsets to center the sprite at the cursor def W2 = W div 2, H2 = H div 2; // show sprite when left button pressed process () (X,Y) (ggi_event TYPE _ _ _ _ PARAM) = show_sprite (X-W2,Y-H2) || loop (X,Y) () if (TYPE = GGI_EV_PTRBUTTONPRESS) and then (PARAM = 1); // hide sprite when right button pressed process (X,Y) _ (ggi_event TYPE _ _ _ _ PARAM) = hide_sprite (X-W2,Y-H2) || loop () (X,Y) if (TYPE = GGI_EV_PTRBUTTONPRESS) and then (PARAM = 2); // move sprite when pointer is moved process (X,Y) _ (ggi_event TYPE _ _ _ _ (X1,Y1|_)) = move_sprite (X-W2,Y-H2) (X1-W2,Y1-H2) || loop (X1,Y1) () if TYPE = GGI_EV_PTRABSOLUTE; // track the pointer position when the sprite is off process () _ (ggi_event TYPE _ _ _ _ (X,Y|_)) = loop () (X,Y) if TYPE = GGI_EV_PTRABSOLUTE; // default action: do nothing process STATE POS _ = loop STATE POS otherwise; |