1 #include "config.h"
2
3 #include <string.h>
4 #include <stdlib.h>
5
6 /****h* libAfterImage/tutorials/ASFlip
7 * NAME
8 * ASFlip
9 * SYNOPSIS
10 * libAfterImage application for image rotation.
11 * DESCRIPTION
12 * New steps described in this tutorial are :
13 * ASFlip.1. Flip value.
14 * ASFlip.2. Rotating ASImage.
15 * SEE ALSO
16 * Tutorial 1: ASView - explanation of basic steps needed to use
17 * libAfterImage and some other simple things.
18 * Tutorial 2: ASScale - image scaling basics.
19 * Tutorial 3: ASTile - image tiling and tinting.
20 * Tutorial 4: ASMerge - scaling and blending of arbitrary number of
21 * images.
22 * Tutorial 5: ASGrad - drawing multipoint linear gradients.
23 * SOURCE
24 */
25
26 #include "../afterbase.h"
27 #include "../afterimage.h"
28 #include "common.h"
29
usage()30 void usage()
31 {
32 printf( "Usage: asflip [-h]|[[-f flip]|[-m vertical] "
33 "[-g geom] image]");
34 printf( "\nWhere: image - is image filename\n");
35 printf( " flip - rotation angle in degrees. "
36 "90, 180 and 270 degrees supported\n");
37 printf( " geom - source image is tiled using this geometry, "
38 "prior to rotation\n");
39 printf( " vertical - 1 - mirror image in vertical direction, "
40 "0 - horizontal\n");
41 }
42
main(int argc,char * argv[])43 int main(int argc, char* argv[])
44 {
45 Display *dpy = NULL;
46 ASVisual *asv ;
47 int screen = 0 , depth = 0 ;
48 char *image_file = "rose512.jpg" ;
49 int flip = FLIP_VERTICAL;
50 Bool vertical = False, mirror = False ;
51 int tile_x, tile_y, geom_flags = 0;
52 unsigned int tile_width, tile_height ;
53 ASImage *im = NULL;
54 ASImage *flipped_im = NULL ;
55
56 /* see ASView.1 : */
57 set_application_name( argv[0] );
58
59 if( argc > 1 )
60 {
61 int i = 1 ;
62 if( strcmp( argv[1], "-h" ) == 0 )
63 {
64 usage();
65 return 0;
66 }
67 for( i = 1 ; i < argc ; i++ )
68 {
69 if( argv[i][0] == '-' && i < argc-1 )
70 {
71 switch(argv[i][1])
72 {
73 case 'm' :
74 mirror = True;
75 vertical = atoi(argv[i+1]) ;
76 break ;
77 case 'f' : /* see ASFlip.1 */
78 mirror = False;
79 flip = atoi(argv[i+1])/90 ;
80 break ;
81 case 'g' : /* see ASTile.2 : */
82 geom_flags = XParseGeometry( argv[i+1],
83 &tile_x, &tile_y,
84 &tile_width,
85 &tile_height );
86 break ;
87 }
88 ++i ;
89 }else
90 image_file = argv[i] ;
91 }
92 }else
93 usage();
94
95 #ifndef X_DISPLAY_MISSING
96 dpy = XOpenDisplay(NULL);
97 _XA_WM_DELETE_WINDOW = XInternAtom( dpy, "WM_DELETE_WINDOW", False);
98 screen = DefaultScreen(dpy);
99 depth = DefaultDepth( dpy, screen );
100 #endif
101 /* see ASView.2 : */
102 im = file2ASImage( image_file, 0xFFFFFFFF, SCREEN_GAMMA, 0, getenv("IMAGE_PATH"), NULL );
103 if( im == NULL )
104 return 1;
105
106 /* Making sure tiling geometry is sane : */
107 if( !get_flags(geom_flags, XValue ) )
108 tile_x = 0 ;
109 if( !get_flags(geom_flags, YValue ) )
110 tile_y = 0 ;
111 if( !get_flags(geom_flags, WidthValue ) )
112 {
113 if( !mirror )
114 tile_width = (get_flags(flip,FLIP_VERTICAL))?
115 im->height:im->width ;
116 else
117 tile_width = im->width ;
118 }
119 if( !get_flags(geom_flags, HeightValue ) )
120 {
121 if( !mirror )
122 tile_height = (get_flags(flip,FLIP_VERTICAL))?
123 im->width:im->height;
124 else
125 tile_height = im->height ;
126 }
127 printf( "%s: tiling image \"%s\" to %dx%d%+d%+d and then "
128 "flipping it by %d degrees\n",
129 get_application_name(), image_file,
130 tile_width, tile_height,tile_x, tile_y, flip*90 );
131
132 /* see ASView.3 : */
133 asv = create_asvisual( dpy, screen, depth, NULL );
134
135 /* see ASFlip.2 : */
136 if( !mirror )
137 flipped_im = flip_asimage( asv, im,
138 tile_x, tile_y,
139 tile_width, tile_height,
140 flip,
141 ASA_ASImage, 0,
142 ASIMAGE_QUALITY_DEFAULT );
143 else
144 flipped_im = mirror_asimage(asv, im,
145 tile_x, tile_y,
146 tile_width, tile_height,
147 vertical,
148 ASA_ASImage, 0,
149 ASIMAGE_QUALITY_DEFAULT );
150 destroy_asimage( &im );
151
152 if( flipped_im )
153 {
154 #ifndef X_DISPLAY_MISSING
155 /* see ASView.4 : */
156 Window w = create_top_level_window( asv, DefaultRootWindow(dpy),
157 32, 32,
158 tile_width, tile_height,
159 1, 0, NULL,
160 "ASFlip", image_file );
161 if( w != None )
162 {
163 Pixmap p ;
164
165 XMapRaised (dpy, w);
166 /* see ASView.5 : */
167 p = asimage2pixmap( asv, DefaultRootWindow(dpy), flipped_im,
168 NULL, True );
169 destroy_asimage( &flipped_im );
170 /* see common.c: set_window_background_and_free() : */
171 p = set_window_background_and_free( w, p );
172 /* see common.c: wait_closedown() : */
173 }
174 wait_closedown(w);
175 dpy = NULL;
176 #else
177 /* writing result into the file */
178 ASImage2file( flipped_im, NULL, "asflip.jpg", ASIT_Jpeg, NULL );
179 destroy_asimage( &flipped_im );
180 #endif
181 }
182 return 0 ;
183 }
184 /**************/
185 /****f* libAfterImage/tutorials/ASFlip.1 [6.1]
186 * SYNOPSIS
187 * Step 1. Flip value.
188 * DESCRIPTION
189 * libAfterImage provides facility for rotating images in 90 degree
190 * increments - flipping essentially. Accordingly flip parameter could
191 * have 4 values - 0, -90, -180, -270 degrees.
192 * EXAMPLE
193 * flip = atoi(argv[2])/90;
194 * SEE ALSO
195 * flip
196 ********/
197 /****f* libAfterImage/tutorials/ASFlip.2 [6.2]
198 * SYNOPSIS
199 * Step 2. Flipping ASImage.
200 * DESCRIPTION
201 * Flipping can actually be combined with offset and tiling. Original
202 * image gets tiled to suplied rectangle, and then gets rotated to
203 * requested degree.
204 * EXAMPLE
205 * flipped_im = flip_asimage(asv, im,
206 * tile_x, tile_y,
207 * tile_width, tile_height,
208 * flip,
209 * ASA_XImage,0,
210 * ASIMAGE_QUALITY_DEFAULT );
211 * destroy_asimage( &im );
212 * NOTES
213 * As far as we need to render rotated image right away - we set to_xim
214 * parameter to True, so that image will be rotated into XImage. Right
215 * after rotation is done - we can destroy original image.
216 * SEE ALSO
217 * flip_asimage()
218 ********/
219