13
13
#include <X11/Xutil.h>
14
14
#include <sys/types.h>
15
15
#include <sys/times.h>
16
-
17
- #define MAX_ITERATIONS 16
16
+ #include <unistd.h>
18
17
19
18
char * title = "xmandelbrot" ;
20
19
20
+ struct color {
21
+ unsigned char r , g , b ;
22
+ };
23
+
24
+ struct color colors_1bit [2 ];
25
+ struct color colors_2bit [4 ];
26
+ struct color colors_4bit [16 ];
27
+ struct color colors_8bit [256 ];
28
+
21
29
void get_window_size (display , window , height , width )
22
30
Display * display ;
23
31
Window window ;
@@ -30,16 +38,21 @@ void get_window_size(display, window, height, width)
30
38
XGetGeometry (display , window , & root , & x , & y , width , height , & border , & depth );
31
39
}
32
40
33
- unsigned long lookup_color (display , colormap , color_name , color )
41
+ unsigned long lookup_color (display , colormap , color )
34
42
Display * display ;
35
43
Colormap colormap ;
36
- char * color_name ;
37
- XColor * color ;
44
+ struct color color ;
38
45
{
39
- XColor exact , screen ;
40
- if (!XAllocNamedColor (display , colormap , color_name , & exact , & screen ))
46
+ XColor screen ;
47
+
48
+ /* Convert our 8-bit color space into a 16-bit color space */
49
+ screen .red = (color .r << 8 ) | color .r ;
50
+ screen .green = (color .g << 8 ) | color .g ;
51
+ screen .blue = (color .b << 8 ) | color .b ;
52
+
53
+ if (!XAllocColor (display , colormap , & screen ))
41
54
{
42
- printf ("Color Not Found: %s \n" , color_name );
55
+ printf ("Color Not Found: rgb:%02x/%02x/%02x \n" , color . r , color . g , color . b );
43
56
return 0 ;
44
57
}
45
58
return screen .pixel ;
@@ -75,15 +88,15 @@ void calculate(display, window, gc, palette, palette_size)
75
88
76
89
for (row = 0 ; row < lines ; row ++ )
77
90
{
78
- // printf("Row: %u\n", row);
91
+ // printf("Row: %u\n", row);q
79
92
y0 = (row * 2.1 / height ) - 1 ;
80
93
for (col = 0 ; col < cols ; col ++ )
81
94
{
82
95
x0 = (col * 3.5 / width ) - 2.5 ;
83
96
x = 0.0 ;
84
97
y = 0.0 ;
85
98
iteration = 0 ;
86
- while ( ((x * x ) + (y * y ) <= 4 ) && (iteration < MAX_ITERATIONS ) )
99
+ while ( ((x * x ) + (y * y ) <= 4 ) && (iteration < palette_size ) )
87
100
{
88
101
xtemp = (x * x ) - (y * y ) + x0 ;
89
102
y = (2 * x * y ) + y0 ;
@@ -104,10 +117,129 @@ void calculate(display, window, gc, palette, palette_size)
104
117
105
118
}
106
119
120
+ struct color color (r , g , b )
121
+ unsigned char r , g , b ;
122
+ {
123
+ struct color c ;
124
+ c .r = r ;
125
+ c .g = g ;
126
+ c .b = b ;
127
+ return c ;
128
+ }
129
+
130
+ void initialize_colors ()
131
+ {
132
+ int steps [16 ];
133
+ int idx , i , j , v , r , g , b ;
134
+
135
+ /* Macintosh System 7 1-bit palette (Black, White) */
136
+ colors_1bit [0 ] = color (0x00 , 0x00 , 0x00 );
137
+ colors_1bit [1 ] = color (0xff , 0xff , 0xff );
138
+
139
+ /* Macintosh System 7 2-bit palette */
140
+ colors_2bit [0 ] = color (0x00 , 0x00 , 0x00 );
141
+ colors_2bit [1 ] = color (0x44 , 0x44 , 0x44 );
142
+ colors_2bit [2 ] = color (0xbb , 0xbb , 0xbb );
143
+ colors_2bit [3 ] = color (0xff , 0xff , 0xff );
144
+
145
+ /* Macintosh System 7 4-bit palette (16 colors, XLib rgb format) */
146
+ colors_4bit [0 ] = color (0x00 , 0x00 , 0x00 );
147
+ colors_4bit [1 ] = color (0x88 , 0x00 , 0x00 );
148
+ colors_4bit [2 ] = color (0x00 , 0x88 , 0x00 );
149
+ colors_4bit [3 ] = color (0x00 , 0x00 , 0x88 );
150
+ colors_4bit [4 ] = color (0x88 , 0x88 , 0x00 );
151
+ colors_4bit [5 ] = color (0x00 , 0x88 , 0x88 );
152
+ colors_4bit [6 ] = color (0x88 , 0x00 , 0x88 );
153
+ colors_4bit [7 ] = color (0x44 , 0x44 , 0x44 );
154
+ colors_4bit [8 ] = color (0xbb , 0xbb , 0xbb );
155
+ colors_4bit [9 ] = color (0xff , 0x88 , 0x88 );
156
+ colors_4bit [10 ] = color (0x88 , 0xff , 0x88 );
157
+ colors_4bit [11 ] = color (0x88 , 0x88 , 0xff );
158
+ colors_4bit [12 ] = color (0xff , 0xff , 0x88 );
159
+ colors_4bit [13 ] = color (0x88 , 0xff , 0xff );
160
+ colors_4bit [14 ] = color (0xff , 0x88 , 0xff );
161
+ colors_4bit [15 ] = color (0xff , 0xff , 0xff );
162
+
163
+ /* Macintosh 8-bit system palette (from lospec.com, 256 colors) */
164
+ idx = 0 ;
165
+
166
+ steps [0 ] = 0x00 ;
167
+ steps [1 ] = 0x0b ;
168
+ steps [2 ] = 0x22 ;
169
+ steps [3 ] = 0x44 ;
170
+ steps [4 ] = 0x55 ;
171
+ steps [5 ] = 0x77 ;
172
+ steps [6 ] = 0x88 ;
173
+ steps [7 ] = 0xaa ;
174
+ steps [8 ] = 0xbb ;
175
+ steps [9 ] = 0xdd ;
176
+ steps [10 ] = 0xee ;
177
+ steps [11 ] = 0x33 ;
178
+ steps [12 ] = 0x66 ;
179
+ steps [13 ] = 0x99 ;
180
+ steps [14 ] = 0xcc ;
181
+ steps [15 ] = 0xff ;
182
+
183
+ /* grayscale */
184
+ for (i = 0 ; i < 11 ; i ++ )
185
+ {
186
+ v = steps [i ];
187
+ colors_8bit [idx ++ ] = color (v , v , v );
188
+ }
189
+
190
+ /* 10 blue, 10 green, 10 red */
191
+ for (j = 0 ; j < 3 ; j ++ )
192
+ {
193
+ for (i = 1 ; i < 11 ; i ++ )
194
+ {
195
+ v = steps [i ];
196
+ switch (j )
197
+ {
198
+ case 0 : colors_8bit [idx ++ ] = color (0x00 , 0x00 , v ); break ; /* blue */
199
+ case 1 : colors_8bit [idx ++ ] = color (0x00 , v , 0x00 ); break ; /* green */
200
+ case 2 : colors_8bit [idx ++ ] = color (v , 0x00 , 0x00 ); break ; /* red */
201
+ }
202
+ }
203
+ }
204
+
205
+ /* the rest */
206
+ for (r = 10 ; r < 16 ; r ++ )
207
+ {
208
+ for (g = 10 ; g < 16 ; g ++ )
209
+ {
210
+ for (b = 10 ; b < 16 ; b ++ )
211
+ {
212
+ if (r == 10 && g == 10 && b == 10 ) continue ; /* skip black */
213
+ colors_8bit [idx ++ ] = color (steps [r == 10 ? 0 : r ], steps [g == 10 ? 0 : g ], steps [b == 10 ? 0 : b ]);
214
+ }
215
+ }
216
+ }
217
+
218
+ printf ("Initialized %u colors.\n" , idx );
219
+
220
+ }
221
+
222
+ void create_palette (display , colormap , palette , colors , size )
223
+ Display * display ;
224
+ Colormap colormap ;
225
+ unsigned long * palette ;
226
+ struct color * colors ;
227
+ size_t size ;
228
+ {
229
+ size_t i ;
230
+ for (i = 0 ; i < size ; i ++ )
231
+ {
232
+ palette [i ] = lookup_color (display , colormap , colors [i ]);
233
+ }
234
+ }
235
+
107
236
int main (argc ,argv )
108
237
int argc ;
109
238
char * * argv ;
110
239
{
240
+
241
+ /* X11 variables */
242
+
111
243
Display * display ;
112
244
Window window ;
113
245
@@ -123,40 +255,52 @@ int main(argc,argv)
123
255
int i ;
124
256
char text [10 ];
125
257
int done ;
126
-
258
+ unsigned int width , height , lastWidth , lastHeight ;
259
+ struct color * colors = colors_8bit ;
127
260
Colormap colormap ;
128
261
129
- unsigned long palette [16 ];
130
- char * color_names [16 ];
131
- color_names [0 ] = "Black" ;
132
- color_names [1 ] = "DarkBlue" ;
133
- color_names [2 ] = "DarkGreen" ;
134
- color_names [3 ] = "DarkCyan" ;
135
- color_names [4 ] = "DarkRed" ;
136
- color_names [5 ] = "DarkMagenta" ;
137
- color_names [6 ] = "Brown" ;
138
- color_names [7 ] = "LightGray" ;
139
- color_names [8 ] = "Gray" ;
140
- color_names [9 ] = "Blue" ;
141
- color_names [10 ] = "Green" ;
142
- color_names [11 ] = "Cyan" ;
143
- color_names [12 ] = "Red" ;
144
- color_names [13 ] = "Magenta" ;
145
- color_names [14 ] = "Yellow" ;
146
- color_names [15 ] = "White" ;
262
+ unsigned long palette [256 ];
263
+
264
+ int max_iterations = 256 ;
265
+ if (argc > 1 )
266
+ {
267
+ max_iterations = atoi (argv [1 ]);
268
+ if (max_iterations < 1 || max_iterations > 256 )
269
+ {
270
+ fprintf (stderr , "Usage: %s [max_iterations (1-256)]\n" , argv [0 ]);
271
+ exit (1 );
272
+ }
273
+ }
274
+ initialize_colors ();
147
275
148
276
/* setup display/screen */
149
277
display = XOpenDisplay ("" );
150
-
278
+ if (!display ) {
279
+ fprintf (stderr , "Error: Unable to open display\n" );
280
+ exit (1 );
281
+ }
282
+
151
283
screen = DefaultScreen (display );
152
284
153
285
colormap = DefaultColormap (display , screen );
154
286
155
- for ( i = 0 ; i < 16 ; i ++ )
287
+ if ( max_iterations < 1 || max_iterations > 256 )
156
288
{
157
- palette [i ] = lookup_color (display , colormap , color_names [i ]);
289
+ fprintf (stderr , "Error: max_iterations must be between 1 and 256\n" );
290
+ exit (1 );
158
291
}
159
-
292
+
293
+ if (max_iterations <= 2 )
294
+ colors = colors_1bit ;
295
+ else if (max_iterations <= 4 )
296
+ colors = colors_2bit ;
297
+ else if (max_iterations <= 16 )
298
+ colors = colors_4bit ;
299
+ else
300
+ colors = colors_8bit ;
301
+
302
+ create_palette (display , colormap , palette , colors , max_iterations );
303
+
160
304
/* drawing contexts for an window */
161
305
bg = BlackPixel (display , screen );
162
306
fg = WhitePixel (display , screen );
@@ -197,9 +341,17 @@ int main(argc,argv)
197
341
198
342
switch (event .type ){
199
343
case Expose :
200
- /* Window was showed. */
201
- if (event .xexpose .count == 0 )
202
- calculate (display , window , gc , palette , 16 );
344
+ /* Window was shown. */
345
+ if (event .xexpose .count == 0 )
346
+ {
347
+ get_window_size (display , window , & height , & width );
348
+ if (width != lastWidth || height != lastHeight )
349
+ {
350
+ calculate (display , window , gc , palette , max_iterations );
351
+ }
352
+ lastWidth = width ;
353
+ lastHeight = height ;
354
+ }
203
355
break ;
204
356
case MappingNotify :
205
357
/* Modifier key was up/down. */
0 commit comments