Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/libart_lgpl/test_gradient.c
blob: 65e4a2ec920ac7aced634b247b92df2091ef35c6 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* test_gradient.c */

#include <math.h>
#include <gtk/gtk.h>
#include "art_point.h"
#include "art_misc.h"
#include "art_affine.h"
#include "art_svp.h"
#include "art_svp_vpath.h"
#include "art_rgb.h"
#include "art_rgb_svp.h"
#include "art_render.h"
#include "art_render_gradient.h"

GtkWidget *drawing_area;
static art_u8 *rgbdata = NULL;
int width, height;

double pos_x[2], pos_y[2];
int pos_nr = 0;

double a = 1/32.0;
double b = -1/33.00000;
double c = -1/3.0;
ArtGradientSpread spread = ART_GRADIENT_REPEAT;

ArtVpath *
randstar (int n)
{
  ArtVpath *vec;
  int i;
  double r, th;

  vec = art_new (ArtVpath, n + 2);
  for (i = 0; i < n; i++)
    {
      vec[i].code = i ? ART_LINETO : ART_MOVETO;
      r = rand () * (250.0 / RAND_MAX);
      th = i * 2 * M_PI / n;
      vec[i].x = 250 + r * cos (th);
      vec[i].y = 250 - r * sin (th);
    }
  vec[i].code = ART_LINETO;
  vec[i].x = vec[0].x;
  vec[i].y = vec[0].y;
  i++;
  vec[i].code = ART_END;
  vec[i].x = 0;
  vec[i].y = 0;
  return vec;
}

ArtVpath *
rect (void)
{
  ArtVpath *vec;
  int i;
  double r, th;

#define START 0
  
  vec = art_new (ArtVpath, 6);
  vec[0].code = ART_MOVETO;
  vec[0].x = START;
  vec[0].y = START; 
  vec[1].code = ART_LINETO;
  vec[1].x = START;
  vec[1].y = 512-START; 
  vec[2].code = ART_LINETO;
  vec[2].x = 512-START;
  vec[2].y = 512-START; 
  vec[3].code = ART_LINETO;
  vec[3].x = 512-START;
  vec[3].y = START; 
  vec[4].code = ART_LINETO;
  vec[4].x = START;
  vec[4].y = START; 
  vec[5].code = ART_END;
  vec[5].x = 0;
  vec[5].y = 0; 
  
  return vec;
}


static void
draw_test()
{
  static ArtVpath *vpath = NULL;
  static ArtSVP *svp = NULL;
  ArtRender *render;
  ArtPixMaxDepth color[3] = {0x0000, 0x0000, 0x8000 };
  ArtGradientLinear gradient;
  ArtGradientStop stops[3] = {
    { 0.02, { 0x7fff, 0x0000, 0x0000, 0x7fff }},
    { 0.5, { 0x0000, 0x0000, 0x0000, 0x1000 }},
    { 0.98, { 0x0000, 0x7fff, 0x0000, 0x7fff }}
  };

  if (!vpath) {
    vpath = rect ();
    svp = art_svp_from_vpath (vpath);
  }

  gradient.a = a;
  gradient.b = b;
  gradient.c = c;
  gradient.spread = spread;
  
  gradient.n_stops = sizeof(stops) / sizeof(stops[0]);
  gradient.stops = stops;
  
  render = art_render_new (0, 0,
			   width, height,
			   rgbdata, width * 3,
			   3, 8, ART_ALPHA_NONE,
			   NULL);
  art_render_clear_rgb (render, 0xfff0c0);
  art_render_svp (render, svp);
  art_render_gradient_linear (render, &gradient, ART_FILTER_NEAREST);
  //  art_render_image_solid (render, color);
  art_render_invoke (render);
}

/* Create a new backing pixmap of the appropriate size */
static gint configure_event( GtkWidget         *widget,
                             GdkEventConfigure *event )
{
  if (rgbdata)
    free(rgbdata);

  rgbdata = malloc(3*widget->allocation.width*widget->allocation.height);
  width = widget->allocation.width;
  height = widget->allocation.height;

  draw_test();
  
  return TRUE;
}

/* Redraw the screen from the backing pixmap */
static gint expose_event( GtkWidget      *widget,
                          GdkEventExpose *event )
{
  static GdkGC *copy_gc = NULL;

  if (copy_gc == NULL) {
    copy_gc = gdk_gc_new(widget->window);
  }

  gdk_draw_rgb_image(widget->window,
		     copy_gc,
		     event->area.x, event->area.y,
		     event->area.width, event->area.height,
		     GDK_RGB_DITHER_NONE,
		     rgbdata + event->area.x*3+event->area.y*3*width,
		     width*3 );

  return FALSE;
}

static gint button_press_event( GtkWidget      *widget,
                                GdkEventButton *event )
{
  static GdkGC *copy_gc = NULL;
  int x, y;
  
  x = event->x;
  y = event->y;

  pos_x[pos_nr] = (double) x;
  pos_y[pos_nr] = (double) y;

  pos_nr = (pos_nr+1) % 2;

  draw_test();

  if (copy_gc == NULL) {
    copy_gc = gdk_gc_new(widget->window);
  }

  gdk_draw_rgb_image(widget->window,
		     copy_gc,
		     0, 0,
		     width, height,
		     GDK_RGB_DITHER_NONE,
		     rgbdata, width*3 );

  return TRUE;
}

static gint motion_notify_event( GtkWidget *widget,
                                 GdkEventMotion *event )
{
  static GdkGC *copy_gc = NULL;
  int x, y;
  GdkModifierType state;

  if (event->is_hint) {
    gdk_window_get_pointer (event->window, &x, &y, &state);
  } else {
    x = event->x;
    y = event->y;
    state = event->state;
  }

  if (state & GDK_BUTTON1_MASK && rgbdata != NULL) {
    
    pos_x[(pos_nr+1) % 2] = (double) x;
    pos_y[(pos_nr+1) % 2] = (double) y;
    
    draw_test();
    
    if (copy_gc == NULL) {
      copy_gc = gdk_gc_new(widget->window);
    }
    
    gdk_draw_rgb_image(widget->window,
		       copy_gc,
		       0, 0,
		       width, height,
		       GDK_RGB_DITHER_NONE,
		       rgbdata, width*3 );
  }
  
  return TRUE;
}

void quit ()
{
  gtk_exit (0);
}

static void
change_gradient (GtkEntry *entry,
		 double *ptr)
{
  double d;
  d = g_ascii_strtod (gtk_entry_get_text (entry), NULL);
  if (d != 0.0)
    *ptr = 1.0/d;
  else
    *ptr = 0.0;
  draw_test();
  gtk_widget_queue_draw (drawing_area);
}

static void
change_spread (GtkEntry *entry,
	       int *ptr)
{
  double d;
  d = g_ascii_strtod (gtk_entry_get_text (entry), NULL);
  *ptr = (int)d;
  draw_test();
  gtk_widget_queue_draw (drawing_area);
}


int main( int   argc, 
          char *argv[] )
{
  GtkWidget *window;
  GtkWidget *vbox;
  char buf[G_ASCII_DTOSTR_BUF_SIZE];

  GtkWidget *button;
  GtkWidget *entry;

  gtk_init (&argc, &argv);

  gdk_rgb_init();

  pos_x[0] = 100;
  pos_y[0] = 100;
  pos_x[1] = 300;
  pos_y[1] = 300;
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (window, "Test Input");

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC (quit), NULL);

  /* Create the drawing area */

  drawing_area = gtk_drawing_area_new ();
  gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 512, 512);
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);

  gtk_widget_show (drawing_area);

  /* Signals used to handle backing pixmap */

  gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
		      (GtkSignalFunc) expose_event, NULL);
  gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
		      (GtkSignalFunc) configure_event, NULL);

  /* Event signals */

  gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
		      (GtkSignalFunc) motion_notify_event, NULL);
  gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
		      (GtkSignalFunc) button_press_event, NULL);

  gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_POINTER_MOTION_MASK
			 | GDK_POINTER_MOTION_HINT_MASK);

  
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

  gtk_signal_connect (GTK_OBJECT (entry), "activate",
		      GTK_SIGNAL_FUNC (change_gradient),
		      &a);
  gtk_entry_set_text  (GTK_ENTRY (entry),
		       g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE,
				       1.0/a));
  gtk_widget_show (entry);

  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

  gtk_signal_connect (GTK_OBJECT (entry), "activate",
		      GTK_SIGNAL_FUNC (change_gradient),
		      &b);
  gtk_entry_set_text  (GTK_ENTRY (entry),
		       g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE,
				       1.0/b));
  gtk_widget_show (entry);

  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

  gtk_signal_connect (GTK_OBJECT (entry), "activate",
		      GTK_SIGNAL_FUNC (change_gradient),
		      &c);
  gtk_entry_set_text  (GTK_ENTRY (entry),
		       g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE,
				       1.0/c));
  gtk_widget_show (entry);

  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

  gtk_signal_connect (GTK_OBJECT (entry), "activate",
		      GTK_SIGNAL_FUNC (change_spread),
		      &spread);
  gtk_entry_set_text  (GTK_ENTRY (entry),
		       g_strdup_printf ("%d", spread));
  gtk_widget_show (entry);

  
  /* .. And a quit button */
  button = gtk_button_new_with_label ("Quit");
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
			     GTK_SIGNAL_FUNC (gtk_widget_destroy),
			     GTK_OBJECT (window));
  gtk_widget_show (button);

  gtk_widget_show (window);

  gtk_main ();

  return 0;
}