summaryrefslogtreecommitdiff
path: root/tests/julia.c
blob: eec6de53086277bb6874c77675e5b17755a9bb5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#include <xcb/xcb.h>
#include <xcb/shm.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_image.h>
#define XCB_ALL_PLANES ~0

/* Needed for xcb_set_wm_protocols() */
#include <xcb/xcb_icccm.h>

#include "julia.h"

#define W_W 640
#define W_H 480

/* Parameters of the fractal */

/* double cr = -0.7927; */
/* double ci = 0.1609; */

/* double cr = 0.32; */
/* double ci = 0.043; */

/* double cr = -1.1380; */
/* double ci = -0.2403; */

/* double cr = -0.0986; */
/* double ci = -0.65186; */

/* double cr = -0.1225; */
/* double ci = 0.7449; */

double cr = -0.3380;
double ci = -0.6230;
double origin_x = -1.8;
double origin_y = -1.2;
double width = 3.6;
double height = 2.4;

/* Numbers of colors in the palette */
int cmax = 316;

static xcb_atom_t
get_atom (xcb_connection_t *connection, const char *atomName)
{
  if (atomName == NULL)
    return XCB_NONE;
  xcb_atom_t atom = XCB_NONE;
  xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection,
	xcb_intern_atom(connection, 0, strlen(atomName), atomName), NULL);
  if (reply)
    {
      atom = reply->atom;
      free(reply);
    }
  return atom;
}

void
palette_julia (Data *datap)
{
  xcb_alloc_color_reply_t *rep;
  int               i;

  datap->palette = (uint32_t *)malloc (sizeof (uint32_t) * cmax);
  
  for (i = 0 ; i < cmax ; i++)
    {
      if (i < 128)
	rep = xcb_alloc_color_reply (datap->conn,
				  xcb_alloc_color (datap->conn,
						 datap->cmap,
						 i<<9, 0, 0),
				  0);
      else if (i < 255)
	rep = xcb_alloc_color_reply (datap->conn,
				  xcb_alloc_color (datap->conn,
						 datap->cmap,
						 65535, (i-127)<<9, 0),
				  0);
      else
	rep = xcb_alloc_color_reply (datap->conn,
				  xcb_alloc_color (datap->conn,
						 datap->cmap,
						 65535, 65535, (i-255)<<10),
				  0);
      
      if (!rep)
	datap->palette[i] = 0;
      else
	datap->palette[i] = rep->pixel;
      free (rep);
    }
  
}

void
draw_julia (Data *datap)
{
  double    zr, zi, t;
  int       c;
  int       i, j;
  
  datap->image = xcb_image_get (datap->conn, datap->draw,
		       0, 0, W_W, W_H,
		       XCB_ALL_PLANES, datap->format);
  
  assert(datap->image);

  for (i = 0 ; i < datap->image->width ; i++)
    for (j = 0 ; j < datap->image->height ; j++)
      {
	zr = origin_x + width * (double)i / (double)datap->image->width;
	zi = origin_y + height * (double)j / (double)datap->image->height;
	c = 0;
	while ((zr*zr + zi*zi < 4.0) &&
	       (c < cmax-1))
	  {
	    t = zr;
	    zr = zr*zr - zi*zi + cr;
	    zi = 2.0*t*zi + ci;
	    c++;
	  }
	xcb_image_put_pixel (datap->image,
			  i,j,
			  datap->palette[c]);
      }

  xcb_image_put (datap->conn, datap->draw, datap->gc, datap->image,
	       0, 0, 0);
}

int
main (int argc, char *argv[])
{
  Data             data;
  xcb_screen_t       *screen;
  xcb_drawable_t      win;
  xcb_drawable_t      rect;
  xcb_gcontext_t      bgcolor;
  uint32_t           mask;
  uint32_t           valgc[2];
  uint32_t           valwin[3];
  xcb_rectangle_t     rect_coord = { 0, 0, W_W, W_H};
  int              screen_num;
  
  data.conn = xcb_connect (0, &screen_num);
  screen = xcb_aux_get_screen (data.conn, screen_num);
  data.depth = xcb_aux_get_depth (data.conn, screen);

  win = screen->root;

  data.gc = xcb_generate_id (data.conn);
  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  valgc[0] = screen->black_pixel;
  valgc[1] = 0; /* no graphics exposures */
  xcb_create_gc (data.conn, data.gc, win, mask, valgc);

  bgcolor = xcb_generate_id (data.conn);
  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  valgc[0] = screen->white_pixel;
  valgc[1] = 0; /* no graphics exposures */
  xcb_create_gc (data.conn, bgcolor, win, mask, valgc);

  data.draw = xcb_generate_id (data.conn);
  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
  valwin[0] = screen->white_pixel;
  valwin[1] = XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_EXPOSURE;
  valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
  xcb_create_window (data.conn, 0,
		   data.draw,
		   screen->root,
		   0, 0, W_W, W_H,
		   10,
		   XCB_WINDOW_CLASS_INPUT_OUTPUT,
		   screen->root_visual,
		   mask, valwin);
  xcb_map_window (data.conn, data.draw);

  rect = xcb_generate_id (data.conn);
  xcb_create_pixmap (data.conn, data.depth,
		   rect, data.draw,
		   W_W, W_H);
  xcb_poly_fill_rectangle(data.conn, rect, bgcolor, 1, &rect_coord);

  xcb_map_window (data.conn, data.draw);

  data.format = XCB_IMAGE_FORMAT_Z_PIXMAP;

  data.cmap = xcb_generate_id (data.conn);
  xcb_create_colormap (data.conn,
		     XCB_COLORMAP_ALLOC_NONE,
		     data.cmap,
		     data.draw,
		     screen->root_visual);

  palette_julia (&data);

  xcb_atom_t deleteWindowAtom = get_atom(data.conn, "WM_DELETE_WINDOW");
  /* Listen to X client messages in order to be able to pickup
     the "delete window" message that is generated for example
     when someone clicks the top-right X button within the window
     manager decoration (or when user hits ALT-F4). */
  xcb_set_wm_protocols (data.conn, data.draw, 1, &deleteWindowAtom);

  xcb_flush (data.conn); 

  bool finished = false;
  while (!finished)
    {
      xcb_generic_event_t *e;
      if (e = xcb_wait_for_event(data.conn))
	{
          switch (e->response_type & 0x7f)
	    {
	    case XCB_EXPOSE:
	      {
	        xcb_copy_area(data.conn, rect, data.draw, bgcolor,
				0, 0, 0, 0, W_W, W_H);
	        draw_julia (&data);
	        xcb_flush (data.conn);
	        break;
	      }
	    case XCB_CLIENT_MESSAGE:
	      {
	        if (((xcb_client_message_event_t *)e)->data.data32[0] == deleteWindowAtom)
	          {
	            finished = true;
	          }
	        break;
	      }
	    case XCB_BUTTON_PRESS:
	      {
	        finished = true;
	        break;
	      }
	  }
        free (e);
      }
    }

  if (data.palette)
    free (data.palette);
  if (data.image)
    xcb_image_destroy (data.image);
  xcb_disconnect (data.conn);

  return 0;
}