1astile(1) AfterStep X11 window manager astile(1)
2
3
4
6 astile - demonstrates image tiling/cropping and tinting libAfterImâ
7 age/tutorials/ASTile
8
11 Simple program based on libAfterImage to tile and tint image.
12
14 All we want to do here is to get image filename, tint color and
15 desired geometry from the command line. We then load this image, and
16 proceed on to tiling it based on parameters. Tiling geometry
17 specifies rectangular shape on limitless plane on which original
18 image is tiled. While we are at tiling the image we also tint it to
19 specified color, or to some random value derived from the current
20 time in seconds elapsed since 1971.
21 We then display the result in simple window.
22 After that we would want to wait, until user closes our window.
23
24 In this tutorial we will only explain new steps, not described in
25 previous tutorial. New steps described in this tutorial are :
26 ASTile.1. Parsing ARGB32 tinting color.
27 ASTile.2. Parsing geometry spec.
28 ASTile.3. Tiling and tinting ASImage.
29
31 ASView - explanation of basic steps needed to use libAfterImage and
32 some other simple things.
33
35 Source :
36 #include "../afterbase.h"
37 #include "../afterimage.h"
38 #include "common.h"
39
40 void usage()
41 {
42 printf( "Usage: astile [-h]|[[-g geometry][-t tint_color] image]0);
43 printf( "Where: image - source image filename.0);
44 printf( " geometry - width and height of the resulting image,0);
45 printf( " and x, y of the origin of the tiling on "
46 "source image.0);
47 printf( " tint_color - color to tint image with.( defaults to "
48 "current time :)0);
49 }
50
51 int main(int argc, char* argv[])
52 {
53 Window w ;
54 ASVisual *asv ;
55 int screen = 0, depth = 0;
56 char *image_file = "rose512.jpg" ;
57 ARGB32 tint_color = time(NULL);
58 int tile_x, tile_y, geom_flags = 0;
59 unsigned int tile_width, tile_height ;
60 ASImage *im ;
61
62 /* see ASView.1 : */
63 set_application_name( argv[0] );
64
65 #ifndef X_DISPLAY_MISSING
66 /* parse_argb_color can only be used after display is open,
67 otherwise we are limited to colors defined as ARGB values : */
68 dpy = XOpenDisplay(NULL);
69 _XA_WM_DELETE_WINDOW = XInternAtom( dpy, "WM_DELETE_WINDOW", False);
70 screen = DefaultScreen(dpy);
71 depth = DefaultDepth( dpy, screen );
72 #endif
73
74 if( argc > 1 )
75 {
76 int i ;
77
78 if( strncmp( argv[1], "-h", 2 ) == 0 )
79 {
80 usage();
81 return 0;
82 }
83
84 for( i = 1 ; i < argc ; i++ )
85 {
86 if( argv[i][0] == '-' && i < argc-1 )
87 {
88 switch(argv[i][1])
89 {
90 case 't' : /* see ASTile.1 : */
91 if( parse_argb_color( argv[i+1], &tint_color ) ==
92 argv[i+1] )
93 show_warning( "unable to parse tint color - "
94 "default used: #%8.8X",
95 tint_color );
96 break ;
97 case 'g' : /* see ASTile.2 : */
98 geom_flags = XParseGeometry( argv[i+1],
99 &tile_x, &tile_y,
100 &tile_width,
101 &tile_height );
102 break ;
103 }
104 ++i ;
105 }else
106 image_file = argv[i] ;
107 }
108 }else
109 {
110 show_warning( "no image file or tint color specified - "
111 "defaults used:
112 image_file, tint_color );
113 usage();
114 }
115
116 /* see ASView.2 : */
117 im = file2ASImage( image_file, 0xFFFFFFFF, SCREEN_GAMMA, 0, getenv("IMAGE_PATH"), NULL );
118
119 /* Making sure tiling geometry is sane : */
120 if( !get_flags(geom_flags, XValue ) )
121 tile_x = im->width/2 ;
122 if( !get_flags(geom_flags, YValue ) )
123 tile_y = im->height/2 ;
124 if( !get_flags(geom_flags, WidthValue ) )
125 tile_width = im->width*2 ;
126 if( !get_flags(geom_flags, HeightValue ) )
127 tile_height = im->height*2;
128 printf( "%s: tiling image
129 "%dx%d%+d%+d tinting with #%8.8lX0,
130 get_application_name(), image_file, tile_width, tile_height,
131 tile_x, tile_y, tint_color );
132
133 if( im != NULL )
134 {
135 /* see ASView.3 : */
136 asv = create_asvisual( dpy, screen, depth, NULL );
137 w = None ;
138 #ifndef X_DISPLAY_MISSING
139 /* see ASView.4 : */
140 w = create_top_level_window( asv, DefaultRootWindow(dpy), 32, 32,
141 tile_width, tile_height, 1, 0, NULL,
142 "ASTile", image_file );
143 if( w != None )
144 {
145 Pixmap p ;
146 ASImage *tinted_im ;
147
148 XMapRaised (dpy, w);
149 /* see ASTile.3 : */
150 tinted_im = tile_asimage( asv, im, tile_x, tile_y,
151 tile_width, tile_height,
152 tint_color, ASA_XImage, 0,
153 ASIMAGE_QUALITY_TOP );
154 destroy_asimage( &im );
155 /* see ASView.5 : */
156 p = asimage2pixmap( asv, DefaultRootWindow(dpy), tinted_im,
157 NULL, True );
158 destroy_asimage( &tinted_im );
159 /* see common.c: set_window_background_and_free() : */
160 p = set_window_background_and_free( w, p );
161 }
162 /* see common.c: wait_closedown() : */
163 wait_closedown(w);
164 #else
165 {
166 ASImage *tinted_im ;
167 /* see ASTile.3 : */
168 tinted_im = tile_asimage( asv, im, tile_x, tile_y,
169 tile_width, tile_height,
170 tint_color, ASA_ASImage, 0,
171 ASIMAGE_QUALITY_TOP );
172 destroy_asimage( &im );
173 /* writing result into the file */
174 ASImage2file( tinted_im, NULL, "astile.jpg", ASIT_Jpeg, NULL );
175 destroy_asimage( &tinted_im );
176 }
177 #endif
178 }
179 return 0 ;
180 }
181
183 Step 1. Color parsing.
184
186 libAfterImage utilizes function provided by libAfterBase for color
187 parsing. In case libAfterBase is unavailable - libAfterImage
188 includes its own copy of that function. This function differs from
189 standard XParseColor in a way that it allows for parsing of alpha
190 channel in addition to red, green and blue. It autodetects if value
191 include alpha channel or not, using the following logic:
192 If number of hex digits in color spec is divisible by 4 and is not
193 equal to 12 then first digits are treated as alpha channel.
194 In case named color is specified or now apha channel is specified
195 alpha value of 0xFF will be used, marking this color as solid.
196
198 if( parse_argb_color( argv[i+1], &tint_color ) == argv[i+1] )
199 show_warning( "unable to parse tint color - default used: #%8.8X",
200 tint_color );
201
203 On success parse_argb_color returns pointer to the character
204 immidiately following color specification in original string.
205 Therefore test for returned value to be equal to original string will
206 can be used to detect error.
207
209 libAfterBase, parse_argb_color(), ARGB32
210
212 Step 2. Parsing the geometry.
213
215 Geometry can be specified in WIDTHxHEIGHT+X+Y format. Accordingly we
216 use standard X function to parse it: XParseGeometry. Returned flags
217 tell us what values has been specified. We only have to fill the rest
218 with some sensible defaults. Default x is width/2, y is height/2, and
219 default size is same as image's width.
220
222 geom_flags = XParseGeometry ( argv[i+1], &tile_x, &tile_y,
223 &tile_width, &tile_height );
224
226 ASScale.1
227
229 Step 3. Actuall tiling of the image.
230
232 Actuall tiling is quite simple - just call tile_asimage and it will
233 generate new ASImage containing tiled and tinted image. For the sake
234 of example we set quality to TOP, but normally GOOD quality is quite
235 sufficient, and is a default. Again, compression is set to 0 since we
236 do not intend to store image for long time. Even better we don't need
237 to store it at all - all we need is XImage, so we can transfer it to
238 the server easily. That is why to_xim argument is set to ASA_XImage.
239 As the result obtained ASImage will not have any data in its buffers,
240 but it will have ximage member set to point to valid XImage.
241 Subsequently we enjoy that convinience, by setting use_cached to True
242 in call to asimage2pixmap(). That ought to save us alot of processing.
243
244 Tinting works in both directions - it can increase intensity of the
245 color or decrease it. If any particular channel of the tint_color is
246 greater then 127 then intensity is increased, otherwise its decreased.
247
249 tinted_im = tile_asimage( asv, im, tile_x, tile_y,
250 tile_width, tile_height,
251 tint_color,
252 ASA_XImage, 0, ASIMAGE_QUALITY_TOP );
253 destroy_asimage( &im );
254
257 tile_asimage().
258
259
260
2613rd Berkeley Distribution AfterStep v.2.2.6 astile(1)