summaryrefslogtreecommitdiffstats
path: root/bltGrMarkerBitmap.C
blob: cf008b945cc4bd151512e178c0ada1c556049c62 (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
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*
 *	Copyright 1993-2004 George A Howlett.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining
 *	a copy of this software and associated documentation files (the
 *	"Software"), to deal in the Software without restriction, including
 *	without limitation the rights to use, copy, modify, merge, publish,
 *	distribute, sublicense, and/or sell copies of the Software, and to
 *	permit persons to whom the Software is furnished to do so, subject to
 *	the following conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

extern "C" {
#include "bltGraph.h"
#include "bltBitmap.h"
};

#include "bltGrMarkerBitmap.h"

#define GETBITMAP(b) (((b)->destBitmap == None) ? (b)->srcBitmap : (b)->destBitmap)

static Tk_OptionSpec optionSpecs[] = {
  {TK_OPTION_ANCHOR, "-anchor", "anchor", "Anchor", 
   "center", -1, Tk_Offset(BitmapMarker, anchor), 0, NULL, 0},
  {TK_OPTION_COLOR, "-background", "background", "Background",
   NULL, -1, Tk_Offset(BitmapMarker, fillColor), TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_SYNONYM, "-bg", NULL, NULL, NULL, -1, 0, 0, "-background", 0},
  {TK_OPTION_CUSTOM, "-bindtags", "bindTags", "BindTags", 
   "Bitmap all", -1, Tk_Offset(BitmapMarker, obj.tags), 
   TK_OPTION_NULL_OK, &listObjOption, 0},
  {TK_OPTION_BITMAP, "-bitmap", "bitmap", "Bitmap", 
   NULL, -1, Tk_Offset(BitmapMarker, srcBitmap), TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_CUSTOM, "-coords", "coords", "Coords",
   NULL, -1, Tk_Offset(BitmapMarker, worldPts), 
   TK_OPTION_NULL_OK, &coordsObjOption, 0},
  {TK_OPTION_STRING, "-element", "element", "Element", 
   NULL, -1, Tk_Offset(BitmapMarker, elemName), TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_SYNONYM, "-fg", NULL, NULL, NULL, -1, 0, 0, "-foreground", 0},
  {TK_OPTION_SYNONYM, "-fill", NULL, NULL, NULL, -1, 0, 0, "-background", 0},
  {TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
   STD_NORMAL_FOREGROUND, -1, Tk_Offset(BitmapMarker, outlineColor), 
   TK_OPTION_NULL_OK, NULL, 0},
  {TK_OPTION_BOOLEAN, "-hide", "hide", "Hide", 
   "no", -1, Tk_Offset(BitmapMarker, hide), 0, NULL, 0},
  {TK_OPTION_CUSTOM, "-mapx", "mapX", "MapX",
   "x", -1, Tk_Offset(BitmapMarker, axes.x), 0, &xAxisObjOption, 0},
  {TK_OPTION_CUSTOM, "-mapy", "mapY", "MapY", 
   "y", -1, Tk_Offset(BitmapMarker, axes.y), 0, &yAxisObjOption, 0},
  {TK_OPTION_SYNONYM, "-outline", NULL, NULL, NULL, -1, 0, 0, "-foreground", 0},
  //  {TK_OPTION_DOUBLE, "-rotate", "rotate", "Rotate", 
  //   "0", -1, Tk_Offset(BitmapMarker, reqAngle), 0, NULL, 0},
  {TK_OPTION_STRING_TABLE, "-state", "state", "State", 
   "normal", -1, Tk_Offset(BitmapMarker, state), 0, &stateObjOption, 0},
  {TK_OPTION_BOOLEAN, "-under", "under", "Under",
   "no", -1, Tk_Offset(BitmapMarker, drawUnder), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-xoffset", "xOffset", "XOffset",
   "0", -1, Tk_Offset(BitmapMarker, xOffset), 0, NULL, 0},
  {TK_OPTION_PIXELS, "-yoffset", "yOffset", "YOffset",
   "0", -1, Tk_Offset(BitmapMarker, yOffset), 0, NULL, 0},
  {TK_OPTION_END, NULL, NULL, NULL, NULL, -1, 0, 0, NULL, 0}
};

MarkerCreateProc Blt_CreateBitmapProc;
static MarkerConfigProc ConfigureBitmapProc;
static MarkerDrawProc DrawBitmapProc;
static MarkerFreeProc FreeBitmapProc;
static MarkerMapProc MapBitmapProc;
static MarkerPointProc PointInBitmapProc;
static MarkerPostscriptProc BitmapToPostscriptProc;
static MarkerRegionProc RegionInBitmapProc;

static MarkerClass bitmapMarkerClass = {
  optionSpecs,
  ConfigureBitmapProc,
  DrawBitmapProc,
  FreeBitmapProc,
  MapBitmapProc,
  PointInBitmapProc,
  RegionInBitmapProc,
  BitmapToPostscriptProc,
};

Marker* Blt_CreateBitmapProc(Graph* graphPtr)
{
  BitmapMarker* bmPtr = (BitmapMarker*)calloc(1, sizeof(BitmapMarker));
  bmPtr->classPtr = &bitmapMarkerClass;
  bmPtr->optionTable = Tk_CreateOptionTable(graphPtr->interp, optionSpecs);

  return (Marker*)bmPtr;
}

static int ConfigureBitmapProc(Marker *markerPtr)
{
  Graph* graphPtr = markerPtr->obj.graphPtr;
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;
  GC newGC;
  XGCValues gcValues;
  unsigned long gcMask;

  if (bmPtr->srcBitmap == None)
    return TCL_OK;

  bmPtr->angle = fmod(bmPtr->reqAngle, 360.0);
  if (bmPtr->angle < 0.0)
    bmPtr->angle += 360.0;

  gcMask = 0;

  if (bmPtr->outlineColor) {
    gcMask |= GCForeground;
    gcValues.foreground = bmPtr->outlineColor->pixel;
  }

  if (bmPtr->fillColor) {
    /* Opaque bitmap: both foreground and background (fill) colors
     * are used. */
    gcValues.background = bmPtr->fillColor->pixel;
    gcMask |= GCBackground;
  } else {
    /* Transparent bitmap: set the clip mask to the current bitmap. */
    gcValues.clip_mask = bmPtr->srcBitmap;
    gcMask |= GCClipMask;
  }

  /* 
   * This is technically a shared GC, but we're going to set/change the clip
   * origin anyways before we draw the bitmap.  This relies on the fact that
   * no other client will be allocated this GC with the GCClipMask set to
   * this particular bitmap.
   */
  newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
  if (bmPtr->gc) {
    Tk_FreeGC(graphPtr->display, bmPtr->gc);
  }
  bmPtr->gc = newGC;

  /* Create the background GC containing the fill color. */

  if (bmPtr->fillColor) {
    gcValues.foreground = bmPtr->fillColor->pixel;
    newGC = Tk_GetGC(graphPtr->tkwin, gcMask, &gcValues);
    if (bmPtr->fillGC) {
      Tk_FreeGC(graphPtr->display, bmPtr->fillGC);
    }
    bmPtr->fillGC = newGC;
  }

  markerPtr->flags |= MAP_ITEM;
  if (markerPtr->drawUnder) {
    graphPtr->flags |= CACHE_DIRTY;
  }

  Blt_EventuallyRedrawGraph(graphPtr);
  return TCL_OK;
}

static void MapBitmapProc(Marker *markerPtr)
{
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;
  Region2d extents;
  Graph* graphPtr = markerPtr->obj.graphPtr;
  Point2d anchorPt;
  Point2d corner1, corner2;
  int destWidth, destHeight;
  int srcWidth, srcHeight;
  int i;

  if (bmPtr->srcBitmap == None)
    return;

  if (bmPtr->destBitmap != None) {
    Tk_FreePixmap(graphPtr->display, bmPtr->destBitmap);
    bmPtr->destBitmap = None;
  }
  /* 
   * Collect the coordinates.  The number of coordinates will determine the
   * calculations to be made.
   * 
   *	   x1 y1	A single pair of X-Y coordinates.  They represent
   *			the anchor position of the bitmap.  
   *
   *	x1 y1 x2 y2	Two pairs of X-Y coordinates.  They represent
   *			two opposite corners of a bounding rectangle. The
   *			bitmap is possibly rotated and scaled to fit into
   *			this box.
   *
   */   
  Tk_SizeOfBitmap(graphPtr->display, bmPtr->srcBitmap, &srcWidth, 
		  &srcHeight);
  corner1 = Blt_MapPoint(markerPtr->worldPts, &markerPtr->axes);
  if (markerPtr->nWorldPts > 1) {
    double hold;

    corner2 = Blt_MapPoint(markerPtr->worldPts + 1, &markerPtr->axes);
    /* Flip the corners if necessary */
    if (corner1.x > corner2.x) {
      hold = corner1.x, corner1.x = corner2.x, corner2.x = hold;
    }
    if (corner1.y > corner2.y) {
      hold = corner1.y, corner1.y = corner2.y, corner2.y = hold;
    }
  } else {
    corner2.x = corner1.x + srcWidth - 1;
    corner2.y = corner1.y + srcHeight - 1;
  }
  destWidth = (int)(corner2.x - corner1.x) + 1;
  destHeight = (int)(corner2.y - corner1.y) + 1;

  if (markerPtr->nWorldPts == 1) {
    anchorPt = Blt_AnchorPoint(corner1.x, corner1.y, (double)destWidth, 
			       (double)destHeight, bmPtr->anchor);
  }
  else
    anchorPt = corner1;

  anchorPt.x += markerPtr->xOffset;
  anchorPt.y += markerPtr->yOffset;

  /* Check if the bitmap sits at least partially in the plot area. */
  extents.left   = anchorPt.x;
  extents.top    = anchorPt.y;
  extents.right  = anchorPt.x + destWidth - 1;
  extents.bottom = anchorPt.y + destHeight - 1;
  markerPtr->clipped = Blt_BoxesDontOverlap(graphPtr, &extents);
  if (markerPtr->clipped) {
    return;				/* Bitmap is offscreen. Don't generate
					 * rotated or scaled bitmaps. */
  }

  /*  
   * Scale the bitmap if necessary. It's a little tricky because we only
   * want to scale what's visible on the screen, not the entire bitmap.
   */
  if ((bmPtr->angle != 0.0f) || (destWidth != srcWidth) || 
      (destHeight != srcHeight)) {
    int regionX, regionY, regionWidth, regionHeight; 
    double left, right, top, bottom;

    /* Ignore parts of the bitmap outside of the plot area. */
    left   = MAX(graphPtr->left, extents.left);
    right  = MIN(graphPtr->right, extents.right);
    top    = MAX(graphPtr->top, extents.top);
    bottom = MIN(graphPtr->bottom, extents.bottom);

    /* Determine the portion of the scaled bitmap to display. */
    regionX = regionY = 0;
    if (graphPtr->left > extents.left) {
      regionX = (int)(graphPtr->left - extents.left);
    }
    if (graphPtr->top > extents.top) {
      regionY = (int)(graphPtr->top - extents.top);
    }	    
    regionWidth = (int)(right - left) + 1;
    regionHeight = (int)(bottom - top) + 1;
	
    anchorPt.x = left;
    anchorPt.y = top;
    bmPtr->destBitmap = Blt_ScaleRotateBitmapArea(graphPtr->tkwin, bmPtr->srcBitmap, srcWidth, srcHeight, regionX, regionY, regionWidth, regionHeight, destWidth, destHeight, bmPtr->angle);
    bmPtr->destWidth = regionWidth;
    bmPtr->destHeight = regionHeight;
  } else {
    bmPtr->destWidth = srcWidth;
    bmPtr->destHeight = srcHeight;
    bmPtr->destBitmap = None;
  }
  bmPtr->anchorPt = anchorPt;
  {
    double xScale, yScale;
    double tx, ty;
    double rotWidth, rotHeight;
    Point2d polygon[5];
    int n;

    /* 
     * Compute a polygon to represent the background area of the bitmap.
     * This is needed for backgrounds of arbitrarily rotated bitmaps.  We
     * also use it to print a background in PostScript.
     */
    Blt_GetBoundingBox(srcWidth, srcHeight, bmPtr->angle, &rotWidth, 
		       &rotHeight, polygon);
    xScale = (double)destWidth / rotWidth;
    yScale = (double)destHeight / rotHeight;
	
    /* 
     * Adjust each point of the polygon. Both scale it to the new size and
     * translate it to the actual screen position of the bitmap.
     */
    tx = extents.left + destWidth * 0.5;
    ty = extents.top + destHeight * 0.5;
    for (i = 0; i < 4; i++) {
      polygon[i].x = (polygon[i].x * xScale) + tx;
      polygon[i].y = (polygon[i].y * yScale) + ty;
    }
    Blt_GraphExtents(graphPtr, &extents);
    n = Blt_PolyRectClip(&extents, polygon, 4, bmPtr->outline); 
    if (n < 3) { 
      memcpy(&bmPtr->outline, polygon, sizeof(Point2d) * 4);
      bmPtr->nOutlinePts = 4;
    } else {
      bmPtr->nOutlinePts = n;
    }
  }
}

static int PointInBitmapProc(Marker *markerPtr, Point2d *samplePtr)
{
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;

  if (bmPtr->srcBitmap == None)
    return 0;

  if (bmPtr->angle != 0.0f) {
    Point2d points[MAX_OUTLINE_POINTS];
    int i;

    /*  
     * Generate the bounding polygon (isolateral) for the bitmap and see
     * if the point is inside of it.
     */
    for (i = 0; i < bmPtr->nOutlinePts; i++) {
      points[i].x = bmPtr->outline[i].x + bmPtr->anchorPt.x;
      points[i].y = bmPtr->outline[i].y + bmPtr->anchorPt.y;
    }
    return Blt_PointInPolygon(samplePtr, points, bmPtr->nOutlinePts);
  }
  return ((samplePtr->x >= bmPtr->anchorPt.x) && 
	  (samplePtr->x < (bmPtr->anchorPt.x + bmPtr->destWidth)) &&
	  (samplePtr->y >= bmPtr->anchorPt.y) && 
	  (samplePtr->y < (bmPtr->anchorPt.y + bmPtr->destHeight)));
}

static int RegionInBitmapProc(Marker *markerPtr, Region2d *extsPtr, 
			      int enclosed)
{
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;

  if (markerPtr->nWorldPts < 1)
    return FALSE;

  if (bmPtr->angle != 0.0f) {
    Point2d points[MAX_OUTLINE_POINTS];
    int i;
	
    /*  
     * Generate the bounding polygon (isolateral) for the bitmap and see
     * if the point is inside of it.
     */
    for (i = 0; i < bmPtr->nOutlinePts; i++) {
      points[i].x = bmPtr->outline[i].x + bmPtr->anchorPt.x;
      points[i].y = bmPtr->outline[i].y + bmPtr->anchorPt.y;
    }
    return Blt_RegionInPolygon(extsPtr, points, bmPtr->nOutlinePts, 
			       enclosed);
  }
  if (enclosed) {
    return ((bmPtr->anchorPt.x >= extsPtr->left) &&
	    (bmPtr->anchorPt.y >= extsPtr->top) && 
	    ((bmPtr->anchorPt.x + bmPtr->destWidth) <= extsPtr->right) &&
	    ((bmPtr->anchorPt.y + bmPtr->destHeight) <= extsPtr->bottom));
  }
  return !((bmPtr->anchorPt.x >= extsPtr->right) ||
	   (bmPtr->anchorPt.y >= extsPtr->bottom) ||
	   ((bmPtr->anchorPt.x + bmPtr->destWidth) <= extsPtr->left) ||
	   ((bmPtr->anchorPt.y + bmPtr->destHeight) <= extsPtr->top));
}

static void DrawBitmapProc(Marker *markerPtr, Drawable drawable)
{
  Graph* graphPtr = markerPtr->obj.graphPtr;
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;
  Pixmap bitmap = GETBITMAP(bmPtr);
  if ((bitmap == None) || (bmPtr->destWidth < 1) || (bmPtr->destHeight < 1))
    return;

  double rangle = fmod(bmPtr->angle, 90.0);
  if ((bmPtr->fillColor == NULL) || (rangle != 0.0)) {

    /* 
     * If the bitmap is rotated and a filled background is required, then
     * a filled polygon is drawn before the bitmap.
     */
    if (bmPtr->fillColor) {
      int i;
      XPoint polygon[MAX_OUTLINE_POINTS];

      for (i = 0; i < bmPtr->nOutlinePts; i++) {
	polygon[i].x = (short int)bmPtr->outline[i].x;
	polygon[i].y = (short int)bmPtr->outline[i].y;
      }
      XFillPolygon(graphPtr->display, drawable, bmPtr->fillGC,
		   polygon, bmPtr->nOutlinePts, Convex, CoordModeOrigin);
    }
    XSetClipMask(graphPtr->display, bmPtr->gc, bitmap);
    XSetClipOrigin(graphPtr->display, bmPtr->gc, (int)bmPtr->anchorPt.x, 
		   (int)bmPtr->anchorPt.y);
  } else {
    XSetClipMask(graphPtr->display, bmPtr->gc, None);
    XSetClipOrigin(graphPtr->display, bmPtr->gc, 0, 0);
  }
  XCopyPlane(graphPtr->display, bitmap, drawable, bmPtr->gc, 0, 0,
	     bmPtr->destWidth, bmPtr->destHeight, (int)bmPtr->anchorPt.x, 
	     (int)bmPtr->anchorPt.y, 1);
}

static void BitmapToPostscriptProc(Marker *markerPtr, Blt_Ps ps)
{
  Graph* graphPtr = markerPtr->obj.graphPtr;
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;
  Pixmap bitmap = GETBITMAP(bmPtr);
  if ((bitmap == None) || (bmPtr->destWidth < 1) || (bmPtr->destHeight < 1))
    return;

  if (bmPtr->fillColor) {
    Blt_Ps_XSetBackground(ps, bmPtr->fillColor);
    Blt_Ps_XFillPolygon(ps, bmPtr->outline, 4);
  }
  Blt_Ps_XSetForeground(ps, bmPtr->outlineColor);

  Blt_Ps_Format(ps,
		"  gsave\n    %g %g translate\n    %d %d scale\n", 
		bmPtr->anchorPt.x, bmPtr->anchorPt.y + bmPtr->destHeight, 
		bmPtr->destWidth, -bmPtr->destHeight);
  Blt_Ps_Format(ps, "    %d %d true [%d 0 0 %d 0 %d] {",
		bmPtr->destWidth, bmPtr->destHeight, bmPtr->destWidth, 
		-bmPtr->destHeight, bmPtr->destHeight);
  Blt_Ps_XSetBitmapData(ps, graphPtr->display, bitmap,
			bmPtr->destWidth, bmPtr->destHeight);
  Blt_Ps_VarAppend(ps, 
		   "    } imagemask\n",
		   "grestore\n", (char*)NULL);
}

static void FreeBitmapProc(Marker *markerPtr)
{
  BitmapMarker* bmPtr = (BitmapMarker*)markerPtr;
  Graph* graphPtr = markerPtr->obj.graphPtr;

  if (bmPtr->gc)
    Tk_FreeGC(graphPtr->display, bmPtr->gc);

  if (bmPtr->fillGC)
    Tk_FreeGC(graphPtr->display, bmPtr->fillGC);

  if (bmPtr->destBitmap != None)
    Tk_FreePixmap(graphPtr->display, bmPtr->destBitmap);
}