summaryrefslogtreecommitdiff
path: root/cd/src/sim/sim_other.c
blob: 0954406ce3be00408e001e5cefa597723b21fce5 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/** \file
 * \brief Simulation that is independent of the Simulation Base Driver
 *
 * See Copyright Notice in cd.h
 */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <memory.h>

#include "cd.h"
#include "cd_private.h"


void cdSimMark(cdCanvas* canvas, int x, int y)
{
  int oldinteriorstyle = canvas->interior_style;
  int oldlinestyle = canvas->line_style;
  int oldlinewidth = canvas->line_width;
  int size = canvas->mark_size;
  int half_size = size/2;
  int bottom = y-half_size;
  int top    = y+half_size;
  int left   = x-half_size;
  int right  = x+half_size;

  if (canvas->interior_style != CD_SOLID &&
      (canvas->mark_type == CD_CIRCLE ||
       canvas->mark_type == CD_BOX ||
       canvas->mark_type == CD_DIAMOND))
    cdCanvasInteriorStyle(canvas, CD_SOLID);

  if (canvas->line_style != CD_CONTINUOUS &&
      (canvas->mark_type == CD_STAR ||
       canvas->mark_type == CD_PLUS ||
       canvas->mark_type == CD_X ||
       canvas->mark_type == CD_HOLLOW_BOX ||
       canvas->mark_type == CD_HOLLOW_CIRCLE ||
       canvas->mark_type == CD_HOLLOW_DIAMOND))
    cdCanvasLineStyle(canvas, CD_CONTINUOUS);

  if (canvas->line_width != 1 &&
      (canvas->mark_type == CD_STAR ||
       canvas->mark_type == CD_PLUS ||
       canvas->mark_type == CD_X ||
       canvas->mark_type == CD_HOLLOW_BOX ||
       canvas->mark_type == CD_HOLLOW_CIRCLE ||
       canvas->mark_type == CD_HOLLOW_DIAMOND))
    cdCanvasLineWidth(canvas, 1);

  switch (canvas->mark_type)
  {
  case CD_STAR:
    canvas->cxLine(canvas->ctxcanvas, left, bottom, right, top);
    canvas->cxLine(canvas->ctxcanvas, left, top, right, bottom);
    /* continue */
  case CD_PLUS:
    canvas->cxLine(canvas->ctxcanvas, left, y, right, y);
    canvas->cxLine(canvas->ctxcanvas, x, bottom, x, top);
    break;
  case CD_HOLLOW_CIRCLE:
    canvas->cxArc(canvas->ctxcanvas, x, y, size, size, 0, 360);
    break;
  case CD_HOLLOW_BOX:
    canvas->cxRect(canvas->ctxcanvas, left, right, bottom, top);
    break;
  case CD_HOLLOW_DIAMOND:
    canvas->cxLine(canvas->ctxcanvas, left, y, x, top);
    canvas->cxLine(canvas->ctxcanvas, x, top, right, y);
    canvas->cxLine(canvas->ctxcanvas, right, y, x, bottom);
    canvas->cxLine(canvas->ctxcanvas, x, bottom, left, y);
    break;
  case CD_X:
    canvas->cxLine(canvas->ctxcanvas, left, bottom, right, top);
    canvas->cxLine(canvas->ctxcanvas, left, top, right, bottom);
    break;
  case CD_CIRCLE:
    canvas->cxSector(canvas->ctxcanvas, x, y, size, size, 0, 360);
    break;
  case CD_BOX:
    canvas->cxBox(canvas->ctxcanvas, left, right, bottom, top);
    break;
  case CD_DIAMOND:
    {
      cdPoint poly[5];
      poly[0].x = left;
      poly[0].y = y;
      poly[1].x = x;
      poly[1].y = top;
      poly[2].x = right;
      poly[2].y = y;
      poly[3].x = x;
      poly[3].y = bottom;
      canvas->cxPoly(canvas->ctxcanvas, CD_FILL, poly, 4);
    }
    break;
  }

  if (canvas->interior_style != oldinteriorstyle &&
      (canvas->mark_type == CD_CIRCLE ||
       canvas->mark_type == CD_BOX ||
       canvas->mark_type == CD_DIAMOND))
    cdCanvasInteriorStyle(canvas, oldinteriorstyle);

  if (canvas->line_style != oldlinestyle &&
      (canvas->mark_type == CD_STAR ||
       canvas->mark_type == CD_PLUS ||
       canvas->mark_type == CD_X ||
       canvas->mark_type == CD_HOLLOW_BOX ||
       canvas->mark_type == CD_HOLLOW_CIRCLE ||
       canvas->mark_type == CD_HOLLOW_DIAMOND))
    cdCanvasLineStyle(canvas, oldlinestyle);

  if (canvas->line_width != oldlinewidth &&
      (canvas->mark_type == CD_STAR ||
       canvas->mark_type == CD_PLUS ||
       canvas->mark_type == CD_X ||
       canvas->mark_type == CD_HOLLOW_BOX ||
       canvas->mark_type == CD_HOLLOW_CIRCLE ||
       canvas->mark_type == CD_HOLLOW_DIAMOND))
    cdCanvasLineWidth(canvas, oldlinewidth);
}

/* Setup Bezier coefficient array once for each control polygon. 
 */
static void BezierForm(const cdPoint* p, cdfPoint* c)
{
  int k; 
  static int choose[4] = {1, 3, 3, 1};
  for (k = 0; k < 4; k++) 
  {
    c[k].x = p[k].x * choose[k];
    c[k].y = p[k].y * choose[k];
  }
}

static void fBezierForm(const cdfPoint* p, cdfPoint* c)
{
  int k; 
  static int choose[4] = {1, 3, 3, 1};
  for (k = 0; k < 4; k++) 
  {
    c[k].x = p[k].x * choose[k];
    c[k].y = p[k].y * choose[k];
  }
}

/* Return Point pt(t), t <= 0 <= 1 from C. 
 * BezierForm must be called once for any given control polygon.
 */
static void BezierCurve(const cdfPoint* c, cdfPoint *pt, double t)
{                  
  int k;
  double t1, tt, u;
  cdfPoint b[4];

  u = t;

  b[0].x = c[0].x;
  b[0].y = c[0].y;
  for(k = 1; k < 4; k++) 
  {
    b[k].x = c[k].x * u;
    b[k].y = c[k].y * u;
    u =u*t;
  }

  pt->x = b[3].x;  
  pt->y = b[3].y;
  t1 = 1-t;          
  tt = t1;
  for(k = 2; k >= 0; k--) 
  {
    pt->x += b[k].x * tt;
    pt->y += b[k].y * tt;
    tt =tt*t1;
  }
}

static int BezierNumSegments(cdCanvas* canvas, const cdPoint* p)
{
  int i, K, dx, dy, d,
    xmax = p[0].x, 
    ymax = p[0].y, 
    xmin = p[0].x, 
    ymin = p[0].y;

  for (i = 1; i < 4; i++)
  {
    if (p[i].x > xmax)
      xmax = p[i].x;
    if (p[i].y > ymax)
      ymax = p[i].y;
    if (p[i].x < xmin)
      xmin = p[i].x;
    if (p[i].y < ymin)
      ymin = p[i].y;
  }

  if (canvas->use_matrix)
  {
    cdMatrixTransformPoint(canvas->matrix, xmin, ymin, &xmin, &ymin);
    cdMatrixTransformPoint(canvas->matrix, xmax, ymax, &xmax, &ymax);
  }

  /* diagonal of the bouding box */
  dx = (xmax-xmin);
  dy = (ymax-ymin);
  d = (int)(sqrt(dx*dx + dy*dy));
  K = d / 8;
  if (K < 8) K = 8;
  return K;
}

static int fBezierNumSegments(cdCanvas* canvas, const cdfPoint* p)
{
  int i, K, d;
  double dx, dy,
    xmax = p[0].x, 
    ymax = p[0].y, 
    xmin = p[0].x, 
    ymin = p[0].y;

  for (i = 1; i < 4; i++)
  {
    if (p[i].x > xmax)
      xmax = p[i].x;
    if (p[i].y > ymax)
      ymax = p[i].y;
    if (p[i].x < xmin)
      xmin = p[i].x;
    if (p[i].y < ymin)
      ymin = p[i].y;
  }

  /* diagonal of the bouding box */
  dx = (xmax-xmin);
  dy = (ymax-ymin);
  d = (int)(sqrt(dx*dx + dy*dy));
  K = d / 8;
  if (K < 8) K = 8;
  return K;
}

/* from sim.h */
void simfLineThin(cdCanvas* canvas, double x1, double y1, double x2, double y2, int *last_xi_a, int *last_yi_a, int *last_xi_b, int *last_yi_b);

/* Quick and Simple Bezier Curve Drawing --- Robert D. Miller
 * Graphics GEMS V  */
void cdSimPolyBezier(cdCanvas* canvas, const cdPoint* points, int n)
{
  int i = 0, k, K, poly_max = 0;
  cdfPoint pt, prev_pt;
  cdfPoint bezier_control[4];
  cdPoint* poly = NULL;
  int use_poly = 0,
      last_xi_a = -65535, 
      last_yi_a = -65535, 
      last_xi_b = -65535, 
      last_yi_b = -65535;

  /* Use special floating point anti-alias line draw when
     line_width==1, and NOT using cdlineSIM. */
  if (canvas->line_width > 1 || canvas->cxLine != cdlineSIM)
    use_poly = 1;

  n--; /* first n is 4 */
  while (n >= 3)
  {
    BezierForm(points+i, bezier_control);
    K = BezierNumSegments(canvas, points+i);

    if (use_poly && poly_max < K+1)
    {
      poly = realloc(poly, sizeof(cdPoint)*(K+1)); /* K+1 points */
      if (!poly) return;
      poly_max = K+1;
    }

    /* first segment */
    BezierCurve(bezier_control, &pt, 0);
    if (use_poly)
    {
      poly[0].x = _cdRound(pt.x);
      poly[0].y = _cdRound(pt.y);
    }
    else
      prev_pt = pt;

    for(k = 1; k < K+1; k++) 
    {
      BezierCurve(bezier_control, &pt, (double)k/(double)K);

      if (use_poly)
      {
        poly[k].x = _cdRound(pt.x);
        poly[k].y = _cdRound(pt.y);
      }
      else
      {
        int old_use_matrix = canvas->use_matrix;
        double x1 = prev_pt.x,
               y1 = prev_pt.y,
               x2 = pt.x,
               y2 = pt.y;

        if (canvas->use_matrix && !canvas->invert_yaxis)
        {
          cdfMatrixTransformPoint(canvas->matrix, x1, y1, &x1, &y1);
          cdfMatrixTransformPoint(canvas->matrix, x2, y2, &x2, &y2);
        }

        /* must disable transformation here, because line simulation use cxPixel */
        canvas->use_matrix = 0;

        simfLineThin(canvas, x1, y1, x2, y2, &last_xi_a, &last_yi_a, &last_xi_b, &last_yi_b);

        canvas->use_matrix = old_use_matrix;
        prev_pt = pt;
      }
    }

    if (use_poly)
      canvas->cxPoly(canvas->ctxcanvas, CD_OPEN_LINES, poly, k);

    n -= 3; i += 3;
  }

  if (poly) free(poly);
}

void cdfSimPolyBezier(cdCanvas* canvas, const cdfPoint* points, int n)
{
  int i = 0, k, K, poly_max = 0;
  cdfPoint pt;
  cdfPoint bezier_control[4];
  cdfPoint* poly = NULL;

  n--; /* first n is 4 */
  while (n >= 3)
  {
    fBezierForm(points+i, bezier_control);
    K = fBezierNumSegments(canvas, points+i);

    if (poly_max < K+1)
    {
      poly = realloc(poly, sizeof(cdfPoint)*(K+1)); /* K+1 points */
      if (!poly) return;
      poly_max = K+1;
    }

    /* first segment */
    BezierCurve(bezier_control, &pt, 0);
    poly[0].x = _cdRound(pt.x);
    poly[0].y = _cdRound(pt.y);

    for(k = 1; k < K+1; k++) 
    {
      BezierCurve(bezier_control, &pt, (double)k/(double)K);

      poly[k].x = _cdRound(pt.x);
      poly[k].y = _cdRound(pt.y);
    }

    canvas->cxFPoly(canvas->ctxcanvas, CD_OPEN_LINES, poly, k);
    n -= 3; i += 3;
  }

  if (poly) free(poly);
}

void cdSimPutImageRectRGBA(cdCanvas* canvas, int iw, int ih, const unsigned char *r, const unsigned char *g, const unsigned char *b, const unsigned char *a, int x, int y, int w, int h, int xmin, int xmax, int ymin, int ymax)
{
  int size, i, j, dst, src, *fx, *fy, rw, rh;
  unsigned char *ar, *ag, *ab, al;

  size = w * h;
  ar = (unsigned char*)malloc(size*3);
  if (!ar) return;
  ag = ar + size;
  ab = ag + size;

  canvas->cxGetImageRGB(canvas->ctxcanvas, ar, ag, ab, x, y, w, h);

  rw = xmax-xmin+1;
  rh = ymax-ymin+1;

  fx = cdGetZoomTable(w, rw, xmin);
  fy = cdGetZoomTable(h, rh, ymin);

  for (j = 0; j < h; j++)
  {
    for (i = 0; i < w; i++)
    {
      dst = j * w + i;
      src = fy[j] * iw + fx[i];
      al = a[src];
      ar[dst] = CD_ALPHA_BLEND(r[src], ar[dst], al);
      ag[dst] = CD_ALPHA_BLEND(g[src], ag[dst], al);
      ab[dst] = CD_ALPHA_BLEND(b[src], ab[dst], al);
    }
  }

  canvas->cxPutImageRectRGB(canvas->ctxcanvas, w, h, ar, ag, ab, x, y, w, h, 0, 0, 0, 0);

  free(ar);

  free(fx);
  free(fy);
}