summaryrefslogtreecommitdiff
path: root/graphic.c
blob: 76f4c65311648682bb02bcb3b03048d4e1bc931f (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*----------------------------------------------------------------------*/
/* graphic.c --- xcircuit routines handling rendered graphic elements	*/
/* Copyright (c) 2005  Tim Edwards, MultiGiG, Inc.			*/
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/*	written by Tim Edwards, 7/11/05					*/
/*----------------------------------------------------------------------*/

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

#ifndef _MSC_VER
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#endif

#ifdef TCL_WRAPPER
#include <tk.h>
#endif

/*----------------------------------------------------------------------*/
/* Local includes							*/
/*----------------------------------------------------------------------*/

#include "xcircuit.h"
#include "colordefs.h"

/*----------------------------------------------------------------------*/
/* Function prototype declarations					*/
/*----------------------------------------------------------------------*/
#include "prototypes.h"

/*----------------------------------------------------------------------*/
/* Global Variable definitions						*/
/*----------------------------------------------------------------------*/

extern Display  *dpy;
extern Globaldata xobjs;
extern XCWindowData *areawin;
extern colorindex *colorlist;
extern int number_colors;

/*----------------------------------------------------------------------*/
/* Recursive search for graphic images in an object.			*/
/* Updates list "glist" when any image is found in an object or its	*/
/* descendents.								*/
/*----------------------------------------------------------------------*/

void count_graphics(objectptr thisobj, short *glist)
{
   genericptr *ge;
   graphicptr gp;
   Imagedata *iptr;
   int i;

   for (ge = thisobj->plist; ge < thisobj->plist + thisobj->parts; ge++) {
      if (IS_GRAPHIC(*ge)) {
	 gp = TOGRAPHIC(ge);
         for (i = 0; i < xobjs.images; i++) {
	    iptr = xobjs.imagelist + i;
            if (iptr->image == gp->source) {
	       glist[i]++;
	    }
	 }
      }
      else if (IS_OBJINST(*ge)) {
	 count_graphics(TOOBJINST(ge)->thisobject, glist);
      }
   }
}

/*----------------------------------------------------------------------*/
/* Given a list of pages, return a list of indices into the graphics	*/
/* buffer area of each graphic used on any of the indicated pages.	*/
/* The returned list is allocated and it is the responsibility of the	*/
/* calling routine to free it.						*/
/*----------------------------------------------------------------------*/

short *collect_graphics(short *pagelist)
{
   short *glist;
   int i;

   glist = (short *)malloc(xobjs.images * sizeof(short));

   for (i = 0; i < xobjs.images; i++) glist[i] = 0;

   for (i = 0; i < xobjs.pages; i++)
      if (pagelist[i] > 0)
	 count_graphics(xobjs.pagelist[i]->pageinst->thisobject, glist);
	
   return glist;
}

/*----------------------------------------------------------------------*/
/* Generate the target view of the indicated graphic image, combining	*/
/* the image's scale and rotation and the zoom factor of the current	*/
/* view.								*/
/*									*/
/* If the graphic is at the wrong scale or rotation but is not redrawn	*/
/* because it is outside the screen viewing area, return FALSE. 	*/
/* Otherwise, return TRUE.						*/
/*----------------------------------------------------------------------*/

#ifndef HAVE_CAIRO
Boolean transform_graphic(graphicptr gp)
{
    int width, height, twidth, theight;
    float scale, tscale, rotation, crot;
    double cosr, sinr;
    int x, y, c, s, hw, hh, thw, thh, xorig, yorig, xc, yc;
    int screen = DefaultScreen(dpy);
    static GC cmgc = (GC)NULL;

    tscale = UTopScale();
    scale = gp->scale * tscale;
    rotation = gp->rotation + UTopRotation();

    if (rotation >= 360.0) rotation -= 360.0;
    else if (rotation < 0.0) rotation += 360.0;

    /* Check if the top-level rotation and scale match the	*/
    /* saved target image.  If so, then we're done.		*/
    if ((rotation == gp->trot) && (scale == gp->tscale)) return TRUE;

    cosr = cos(RADFAC * rotation);
    sinr = sin(RADFAC * rotation);
    c = (int)(8192 * cosr / scale);
    s = (int)(8192 * sinr / scale);

    /* Determine the necessary width and height of the pixmap	*/
    /* that fits the rotated and scaled image.			*/

    crot = rotation;
    if (crot > 90.0 && crot < 180.0) crot = 180.0 - crot;
    if (crot > 270.0 && crot < 360.0) crot = 360.0 - crot;
    cosr = cos(RADFAC * crot);
    sinr = sin(RADFAC * crot);
    width = gp->source->width * scale;
    height = gp->source->height * scale;

    twidth = (int)(fabs(width * cosr + height * sinr));
    theight = (int)(fabs(width * sinr + height * cosr));
    if (twidth & 1) twidth++;
    if (theight & 1) theight++;

    /* Check whether this instance is going to be off-screen,	*/
    /* to avoid excessive computation.				*/

    UTopOffset(&xc, &yc);
    xc += (int)((float)gp->position.x * tscale);
    yc = areawin->height - yc;
    yc += (int)((float)gp->position.y * tscale);

    if (xc - (twidth >> 1) > areawin->width) return FALSE;
    if (xc + (twidth >> 1) < 0) return FALSE;
    if (yc - (theight >> 1) > areawin->height) return FALSE;
    if (yc + (theight >> 1) < 0) return FALSE;

    /* Generate the new target image */
    if (gp->target != NULL) {
       if (gp->target->data != NULL) {
	  /* Free data first, because we used our own malloc() */
	  free(gp->target->data);
	  gp->target->data = NULL;
       }
       XDestroyImage(gp->target);
    }
    if (gp->clipmask != (Pixmap)NULL) XFreePixmap(dpy, gp->clipmask);

    gp->target = XCreateImage(dpy, DefaultVisual(dpy, screen),
			DefaultDepth(dpy, screen), ZPixmap,
			0, 0, twidth, theight, 8, 0);
    gp->target->data = (char *)malloc(theight * gp->target->bytes_per_line);

    if (gp->target->data == (char *)NULL) {
       XDestroyImage(gp->target);
       gp->target = (XImage *)NULL;
       gp->clipmask = (Pixmap)NULL;
       return FALSE;
    }

    if (rotation != 0.0) {
       gp->clipmask = XCreatePixmap(dpy, areawin->window, twidth, theight, 1);
       if (cmgc == (GC)NULL) {
          XGCValues values;
          values.foreground = 0;
          values.background = 0;
          cmgc = XCreateGC(dpy, gp->clipmask, GCForeground | GCBackground, &values);
       }
       XSetForeground(dpy, cmgc, 1);
       XFillRectangle(dpy, gp->clipmask, cmgc, 0, 0, twidth, theight);
       XSetForeground(dpy, cmgc, 0);
    }
    else
       gp->clipmask = (Pixmap)NULL;

    hh = gp->source->height >> 1;
    hw = gp->source->width >> 1;
    thh = theight >> 1;
    thw = twidth >> 1;
    for (y = -thh; y < thh; y++) {
	for (x = -thw; x < thw; x++) {
	    xorig = ((x * c + y * s) >> 13) + hw;
	    yorig = ((-x * s + y * c) >> 13) + hh;

	    if ((xorig >= 0) && (yorig >= 0) &&
			(xorig < gp->source->width) && (yorig < gp->source->height))
	       XPutPixel(gp->target, x + thw, y + thh,
			XGetPixel(gp->source, xorig, yorig));
	    else if (gp->clipmask)
	       XDrawPoint(dpy, gp->clipmask, cmgc, x + thw, y + thh);
	}
    }
    gp->tscale = scale;
    gp->trot = rotation;
    return TRUE;
}
#endif /* HAVE_CAIRO */

/*----------------------------------------------------------------------*/
/* Draw a graphic image by copying from the image to the window.	*/
/* Image is centered on the center point of the graphic image.		*/
/*----------------------------------------------------------------------*/

#ifndef HAVE_CAIRO
void UDrawGraphic(graphicptr gp)
{
    XPoint ppt;

    /* transform to current scale and rotation, if necessary */
    if (transform_graphic(gp) == FALSE) return;  /* Graphic off-screen */

    /* transform to current position */
    UTransformbyCTM(DCTM, &(gp->position), &ppt, 1);

    /* user_to_window(gp->position, &ppt); */

    ppt.x -= (gp->target->width >> 1);
    ppt.y -= (gp->target->height >> 1);

    if (gp->clipmask != (Pixmap)NULL) {
       if (areawin->clipped > 0) {
	  /* Clipmask areawin->clipmask already applied to window. */
	  /* Modify existing clipmask with the graphic's.	   */
	  XSetFunction(dpy, areawin->cmgc, GXand);
	  XCopyArea(dpy, gp->clipmask, areawin->clipmask, areawin->cmgc,
		0, 0, gp->target->width, gp->target->height, ppt.x, ppt.y);
          XSetClipMask(dpy, areawin->gc, areawin->clipmask);
	  XSetFunction(dpy, areawin->cmgc, GXcopy);
       }
       else {
          XSetClipOrigin(dpy, areawin->gc, ppt.x, ppt.y);
          XSetClipMask(dpy, areawin->gc, gp->clipmask);
       }
    }

    XPutImage(dpy, areawin->window, areawin->gc,
		gp->target, 0, 0, ppt.x, ppt.y, gp->target->width,
		gp->target->height);

    if (gp->clipmask != (Pixmap)NULL) {
       if (areawin->clipped <= 0) {
	  XSetClipMask(dpy, areawin->gc, None);
          XSetClipOrigin(dpy, areawin->gc, 0, 0);
       }
    }
}
#endif /* HAVE_CAIRO */

/*----------------------------------------------------------------------*/
/* Allocate space for a new graphic source image of size width x height	*/
/*----------------------------------------------------------------------*/

Imagedata *addnewimage(char *name, int width, int height)
{
   Imagedata *iptr;
   int screen = DefaultScreen(dpy);

   /* Create the image and store in the global list of images */

   xobjs.images++;
   if (xobjs.imagelist)
      xobjs.imagelist = (Imagedata *)realloc(xobjs.imagelist,
		xobjs.images * sizeof(Imagedata));
   else
      xobjs.imagelist = (Imagedata *)malloc(sizeof(Imagedata));
    
   /* Save the image source in a file */
   iptr = xobjs.imagelist + xobjs.images - 1;
   if (name)
      iptr->filename = strdup(name);
   else
      iptr->filename = NULL;	/* must be filled in later! */
   iptr->refcount = 0;		/* no calls yet */
   iptr->image = xcImageCreate(width, height);

   return iptr;
}

/*----------------------------------------------------------------------*/
/* Create a new graphic image from a PPM file, and position it at the	*/
/* indicated (px, py) coordinate in user space.				*/
/*									*/
/* This should be expanded to incorporate more PPM formats.  Also, it	*/
/* needs to be combined with the render.c routines to transform		*/
/* PostScript graphics into an internal XImage for faster rendering.	*/
/*----------------------------------------------------------------------*/

graphicptr new_graphic(objinstptr destinst, char *filename, int px, int py)
{
    graphicptr *gp;
    objectptr destobject;
    objinstptr locdestinst;
    Imagedata *iptr;
    FILE *fg;
    int nr, width, height, imax, x, y, i; /* nf, (jdk) */
    char id[5], c, buf[128];

    locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
    destobject = locdestinst->thisobject;

    /* Check the existing list of images.  If there is a match,	*/
    /* re-use the source; don't load the file again.		*/

    for (i = 0; i < xobjs.images; i++) {
       iptr = xobjs.imagelist + i;
       if (!strcmp(iptr->filename, filename)) {
	  break;
       }
    }
    if (i == xobjs.images) {

       fg = fopen(filename, "r");
       if (fg == NULL) return NULL;

       /* This ONLY handles binary ppm files with max data = 255 */

       while (1) {
          nr = fscanf(fg, " %s", buf);
	  if (nr <= 0) return NULL;
	  if (buf[0] != '#') {
	     if (sscanf(buf, "%s", id) <= 0)
		return NULL;
	     break;
	  }
	  else fgets(buf, 127, fg);
       }
       if ((nr <= 0) || strncmp(id, "P6", 2)) return NULL;

       while (1) {
          nr = fscanf(fg, " %s", buf);
	  if (nr <= 0) return NULL;
	  if (buf[0] != '#') {
	     if (sscanf(buf, "%d", &width) <= 0)
		return NULL;
	     break;
	  }
	  else fgets(buf, 127, fg);
       }
       if (width <= 0) return NULL;

       while (1) {
          nr = fscanf(fg, " %s", buf);
	  if (nr <= 0) return NULL;
	  if (buf[0] != '#') {
	     if (sscanf(buf, "%d", &height) <= 0)
		return NULL;
	     break;
	  }
	  else fgets(buf, 127, fg);
       }
       if (height <= 0) return NULL;

       while (1) {
          nr = fscanf(fg, " %s", buf);
	  if (nr <= 0) return NULL;
	  if (buf[0] != '#') {
	     if (sscanf(buf, "%d", &imax) <= 0)
		return NULL;
	     break;
	  }
	  else fgets(buf, 127, fg);
       }
       if (imax != 255) return NULL;

       while (1) {
          fread(&c, 1, 1, fg);
          if (c == '\n') break;
          else if (c == '\0') return NULL;
       }

       iptr = addnewimage(filename, width, height);

       /* Read the image data from the PPM file */
       for (y = 0; y < height; y++)
          for (x = 0; x < width; x++) {
	     u_char r, g, b;
	     fread(&r, 1, 1, fg);
	     fread(&g, 1, 1, fg);
	     fread(&b, 1, 1, fg);
	     xcImagePutPixel(iptr->image, x, y, r, g, b);
          }
    }

    iptr->refcount++;
    NEW_GRAPHIC(gp, destobject);

    (*gp)->scale = 1.0;
    (*gp)->position.x = px;
    (*gp)->position.y = py;
    (*gp)->rotation = 0.0;
    (*gp)->color = DEFAULTCOLOR;
    (*gp)->passed = NULL;
    (*gp)->source = iptr->image;
#ifndef HAVE_CAIRO
    (*gp)->target = NULL;
    (*gp)->trot = 0.0;
    (*gp)->tscale = 0;
    (*gp)->clipmask = (Pixmap)NULL;
#endif /* HAVE_CAIRO */

    calcbboxvalues(locdestinst, (genericptr *)gp);
    updatepagebounds(destobject);
    incr_changes(destobject);

    register_for_undo(XCF_Graphic, UNDO_DONE, areawin->topinstance, *gp);

    return *gp;
}

/*----------------------------------------------------------------------*/
/* Create a gradient field graphic					*/
/* For now, gradient field is linear white-to-black size 100x100.	*/
/*----------------------------------------------------------------------*/

graphicptr gradient_field(objinstptr destinst, int px, int py, int c1, int c2)
{
    graphicptr *gp;
    objectptr destobject;
    objinstptr locdestinst;
    Imagedata *iptr;
    int width, height, imax, x, y, i;
    int r, g, b, rd, gd, bd;
    char id[11];

    locdestinst = (destinst == NULL) ? areawin->topinstance : destinst;
    destobject = locdestinst->thisobject;

    if (c1 < 0) c1 = 0;
    if (c1 >= number_colors) c1 = 1;
    if (c2 < 0) c2 = 0;
    if (c2 >= number_colors) c2 = 1;

    /* Create name "gradientXX"	*/

    y = 0;
    for (i = 0; i < xobjs.images; i++) {
       iptr = xobjs.imagelist + i;
       if (!strncmp(iptr->filename, "gradient", 8)) {
	  if (sscanf(iptr->filename + 8, "%2d", &x) == 1)
	     if (x >= y)
		y = x + 1;
       }
    }
    sprintf(id, "gradient%02d", y);

    width = height = 100;	/* Fixed size, at least for now */

    iptr = addnewimage(id, width, height);

    r = (colorlist[c1].color.red >> 8);
    g = (colorlist[c1].color.green >> 8);
    b = (colorlist[c1].color.blue >> 8);

    rd = (colorlist[c2].color.red >> 8) - r;
    gd = (colorlist[c2].color.green >> 8) - g;
    bd = (colorlist[c2].color.blue >> 8) - b;

    for (y = 0; y < height; y++)
      for (x = 0; x < width; x++) {
	 xcImagePutPixel(iptr->image, x, y,
	       r + ((y * rd) / (height - 1)),
	       g + ((y * gd) / (height - 1)),
	       b + ((y * bd) / (height - 1)));
      }

    iptr->refcount++;
    NEW_GRAPHIC(gp, destobject);

    (*gp)->scale = 1.0;
    (*gp)->position.x = px;
    (*gp)->position.y = py;
    (*gp)->rotation = 0.0;
    (*gp)->color = DEFAULTCOLOR;
    (*gp)->passed = NULL;
    (*gp)->source = iptr->image;
#ifndef HAVE_CAIRO
    (*gp)->target = NULL;
    (*gp)->trot = 0.0;
    (*gp)->tscale = 0;
    (*gp)->clipmask = (Pixmap)NULL;
#endif /* HAVE_CAIRO */

    calcbboxvalues(locdestinst, (genericptr *)gp);
    updatepagebounds(destobject);
    incr_changes(destobject);

    register_for_undo(XCF_Graphic, UNDO_DONE, areawin->topinstance, *gp);

    return *gp;
}


/*----------------------------------------------------------------------*/
/* Free memory associated with the XImage structure for a graphic.	*/
/*----------------------------------------------------------------------*/

void freeimage(xcImage *source)
{
   int i, j;
   Imagedata *iptr;

   for (i = 0; i < xobjs.images; i++) {
      iptr = xobjs.imagelist + i;
      if (iptr->image == source) {
	 iptr->refcount--;
	 if (iptr->refcount <= 0) {
	    xcImageDestroy(iptr->image);
	    free(iptr->filename);

	    /* Remove this from the list of images */

	    for (j = i; j < xobjs.images - 1; j++)
	       *(xobjs.imagelist + j) = *(xobjs.imagelist + j + 1);
	    xobjs.images--;
	 }
	 break;
      }
   }
}

/*----------------------------------------------------------------------*/
/* Free memory allocated by a graphicptr structure.			*/
/*----------------------------------------------------------------------*/

void freegraphic(graphicptr gp)
{
#ifndef HAVE_CAIRO
   if (gp->target != NULL) {
      if (gp->target->data != NULL) {
	 /* Free data first, because we used our own malloc() */
	 free(gp->target->data);
	 gp->target->data = NULL;
      }
      XDestroyImage(gp->target);
   }
   if (gp->clipmask != (Pixmap)NULL) XFreePixmap(dpy, gp->clipmask);
#endif /* HAVE_CAIRO */
   freeimage(gp->source);
}

/*----------------------------------------------------------------------*/
/* A light wrapper around XImage, to a generalized xcImage              */
/*----------------------------------------------------------------------*/

#ifndef HAVE_CAIRO
xcImage *xcImageCreate(int width, int height)
{
   int screen = DefaultScreen(dpy);
   xcImage *img = XCreateImage(dpy, DefaultVisual(dpy, screen),
	 DefaultDepth(dpy, screen), ZPixmap, 0, NULL, width, height, 8, 0);
   img->data = (char *) malloc(height * img->bytes_per_line);
   return img;
}

void xcImageDestroy(xcImage *img)
{
   free(img->data);
   img->data = NULL;
   XDestroyImage(img);
}

int xcImageGetWidth(xcImage *img)
{
   return img->width;
}

int xcImageGetHeight(xcImage *img)
{
   return img->height;
}

void xcImagePutPixel(xcImage *img, int x, int y, u_char r, u_char g, u_char b)
{
   union {
      u_char b[4];
      u_long i;
   } pixel;
   pixel.b[3] = 0;
   pixel.b[2] = r;
   pixel.b[1] = g;
   pixel.b[0] = b;
   XPutPixel(img, x, y, pixel.i);
}

void xcImageGetPixel(xcImage *img, int x, int y, u_char *r, u_char *g,
      u_char *b)
{
   union {
      u_char b[4];
      u_long i;
   } pixel;
   pixel.i = XGetPixel(img, x, y);
   *r = pixel.b[2];
   *g = pixel.b[1];
   *b = pixel.b[0];
}
#endif /* HAVE_CAIRO */