File: picture.c

package info (click to toggle)
ncbi-tools6 6.1.20120620-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 241,636 kB
  • sloc: ansic: 1,431,708; cpp: 6,248; pascal: 3,949; xml: 3,390; sh: 3,090; perl: 1,077; csh: 488; makefile: 449; ruby: 93; lisp: 81
file content (1146 lines) | stat: -rw-r--r-- 32,814 bytes parent folder | download | duplicates (11)
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/*   picture.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*            National Center for Biotechnology Information (NCBI)
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government do not place any restriction on its use or reproduction.
*  We would, however, appreciate having the NCBI and the author cited in
*  any work or product based on this material
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
* ===========================================================================
*
* File Name:  picture.c
*
* Author:  Jonathan Kans, Alex Smirnov, Jill Shermer
*
* Version Creation Date:   10/23/92
*
* $Revision: 6.7 $
*
* File Description: 
*
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
*
* $Log: picture.c,v $
* Revision 6.7  2004/02/17 19:22:54  johnson
* make segment ID consistly unsigned
*
* Revision 6.6  2002/08/07 18:13:42  kans
* G/SetPrimitiveIDs, itemID is Uint4
*
* Revision 6.5  2002/03/07 15:53:45  kans
* added SetSegmentVisibleFlag to set/clear the visible flag before attaching to a viewer
*
* Revision 6.4  1999/10/13 17:45:46  kans
* added entityID, itemID, and itemtype to primitive internal structure, added Get and Set functions
*
* Revision 6.3  1999/10/04 17:16:33  kans
* include ncbidraw.h instead of vibrant.h, a couple Nlm_ prefixes
*
* Revision 6.2  1998/06/30 00:32:02  vakatov
* Nlm_AddAttribute():  pass "color" as constant
*
* Revision 6.1  1997/11/26 21:30:04  vakatov
* Fixed errors and warnings issued by C and C++ (GNU and Sun) compilers
*
* Revision 6.0  1997/08/25 18:56:22  madden
* Revision changed to 6.0
*
* Revision 5.2  1997/07/02 19:40:32  vakatov
* Fixed typo:  #DBUG --> #_DEBUG in DeletePrim()
*
* Revision 5.1  1997/07/02 18:35:51  vakatov
* Nlm_DeletePrim() redesigned/rewritten to guarantee prim. list consistency
*
* Revision 5.0  1996/05/28 13:45:08  ostell
* Set to revision 5.0
*
 * Revision 4.4  1996/04/01  16:07:10  hogue
 * Cast MemCpy to (void *)
 *
 * Revision 4.3  1996/03/18  20:42:05  vakatov
 * ChangeAddPrimOrder(...) function allows one to specify the order of adding
 * primitives(or segments) to a picture
 *
 * Revision 4.2  1995/12/14  17:29:30  kans
 * moved SearchSegment from viewer.c to picture.c, made extern
 *
 * Revision 4.1  1995/11/20  20:36:29  kans
 * line segments of circle now drawn with visible penwidth on Mac (AS)
 *
 * Revision 4.0  1995/07/26  13:51:04  ostell
 * force revision to 4.0
 *
 * Revision 1.30  1995/05/17  15:15:14  kans
 * added Log line
 *
*
* ==========================================================================
*/

#ifndef _NCBIDRAW_
#include <ncbidraw.h>
#endif

#ifndef _PICTURE_
#include <picture.h>
#endif

#ifndef _PICTUREP_
#include <picturep.h>
#endif

#ifndef _MAPPINGP_
#include <mappingp.h>
#endif

#ifndef _DRAWINGP_
#include <drawingp.h>
#endif

/*****************************************************************************
*
*   EXTERNAL VARIABLES
*
*****************************************************************************/

Uint1 BLACK_COLOR [3] = {0, 0, 0};
Uint1 WHITE_COLOR [3] = {255, 255, 255};
Uint1 RED_COLOR  [3] = {255, 0, 0};
Uint1 GREEN_COLOR [3] = {0, 255, 0};
Uint1 BLUE_COLOR [3] = {0, 0, 255};
Uint1 YELLOW_COLOR [3] = {255, 255, 0};
Uint1 CYAN_COLOR [3] = {0, 255, 255};
Uint1 MAGENTA_COLOR [3] = {255, 0, 255};

/*****************************************************************************
*
*   STATIC VARIABLES
*
*****************************************************************************/

static AttPData defAttPData = 
{ {0,0,0}, SOLID_LINE, SOLID_SHADING, STD_PEN_WIDTH, COPY_MODE };

/*****************************************************************************
*
*   CreatePicture (void)
*       Creates a Picture Record (same as Segment Record except for base.code)
*
*****************************************************************************/

SegmenT Nlm_CreatePicture (void)

{
  PicPPtr  pic;

  pic = (PicPPtr) MemNew (sizeof (PicPRec));
  if (pic != NULL) {
    pic->base.next = NULL;
    pic->base.prev = NULL;
    pic->base.code = PICTURE;
    pic->seg.box.left = INT4_MAX;
    pic->seg.box.top = INT4_MIN;
    pic->seg.box.right = INT4_MIN;
    pic->seg.box.bottom = INT4_MAX;
    pic->seg.head = NULL;
    pic->seg.tail = NULL;
    pic->seg.parent = NULL;
    pic->seg.visible = TRUE;
    pic->seg.maxscale = 0;
    pic->seg.penwidth = STD_PEN_WIDTH;
    pic->seg.highlight = PLAIN_SEGMENT;
    pic->seg.segID = 0;
    pic->attLast = defAttPData;
  }
  return (SegmenT) pic;
}

/*****************************************************************************
*
*   DeletePicture (picture)
*       Removes any previously existing segments, then frees picture node
*
*****************************************************************************/

SegmenT Nlm_DeletePicture (SegmenT picture)

{
  PicPPtr  pic;

  pic = NULL;
  if (picture != NULL) {
    pic = (PicPPtr) picture;
    if (pic->base.code == PICTURE) {
      ResetSegment ((SegmenT) pic);
      pic = (PicPPtr) MemFree(pic);
    } else {
      Message (MSG_ERROR, "DeletePicture argument not a picture");
    }
  }
  return (SegmenT) pic;
}

/*****************************************************************************
*
*   DeleteSegment (segment)
*       Removes any previously existing segments, then frees segment node
*
*****************************************************************************/

SegmenT Nlm_DeleteSegment (SegmenT segment)

{
  SegPPtr  seg;

  seg = NULL;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      ResetSegment ((SegmenT) seg);
      seg = (SegPPtr) MemFree(seg);
    } else {
      Message (MSG_ERROR, "DeleteSegment argument not a segment or picture");
    }
  }
  return (SegmenT) seg;
}

/*****************************************************************************
*
*   CreatePrimitive (parent, code, size)
*       Common routine creates any primitive type, then adds it to the
*       parent segment's child list, updating segment pointers
*
*****************************************************************************/

static Nlm_enumPrimAddOrder actualAddPrimOrder = ADD_TO_TAIL;
static Nlm_enumPrimAddOrder tempAddPrimOrder   = WHAT_ORDER;

Nlm_enumPrimAddOrder Nlm_ChangeAddPrimOrder(Nlm_enumPrimAddOrder prim_add_order)
{
  Nlm_enumPrimAddOrder prev_order = (tempAddPrimOrder == WHAT_ORDER) ?
    actualAddPrimOrder : tempAddPrimOrder;

  switch ( prim_add_order )
    {
    case WHAT_ORDER:
      break;
    case ADD_TO_TAIL:
    case ADD_TO_HEAD:
      actualAddPrimOrder = prim_add_order;
      tempAddPrimOrder   = WHAT_ORDER;
      break;
    case ADD_TO_TAIL_ONCE:
    case ADD_TO_HEAD_ONCE:
      tempAddPrimOrder = prim_add_order;
      break;
    }

  return prev_order;
}

static BasePPtr CreatePrimitive (SegmenT parent, Int1 code, size_t size)

{
  BasePPtr  prim;
  SegPPtr   prnt;

  prim = NULL;
  if (parent != NULL) {
    prnt = (SegPPtr) parent;
    if (prnt->base.code == SEGMENT || prnt->base.code == PICTURE) {
      prim = (BasePPtr) MemNew (size);
      if (prim != NULL) {
	Nlm_enumPrimAddOrder order = Nlm_ChangeAddPrimOrder( WHAT_ORDER );
	tempAddPrimOrder = WHAT_ORDER;

	prim->prev = NULL;
        prim->next = NULL;
        prim->code = code;
        if (prnt->seg.head != NULL && prnt->seg.tail != NULL) {
	  if (order == ADD_TO_HEAD  ||  order == ADD_TO_HEAD_ONCE)
	    {
	      prnt->seg.head->prev = prim;
	      prim->next = prnt->seg.head;
	      prnt->seg.head = prim;
	    }
	  else
	    {
	      prnt->seg.tail->next = prim;
	      prim->prev = prnt->seg.tail;
	      prnt->seg.tail = prim;
	    }
        } else if (prnt->seg.head == NULL && prnt->seg.tail == NULL) {
          prnt->seg.head = prim;
          prnt->seg.tail = prim;
        } else {
          Message (MSG_ERROR, "CreatePrimitive list integrity problem");
        }
      }
    } else {
      Message (MSG_ERROR, "CreatePrimitive parent not a segment or picture");
    }
  }
  return prim;
}

/*****************************************************************************
*
*   LinkSegment (parent, child)
*       Links existing segment into parent
*
*****************************************************************************/

void Nlm_LinkSegment (SegmenT parent, SegmenT child)

{
  BasePPtr  prev;
  BasePPtr  prim;
  SegPPtr   prnt;
  SegPPtr   seg;

   if (parent != NULL && child != NULL) {
    prnt = (SegPPtr) parent;
    if (prnt->base.code == SEGMENT || prnt->base.code == PICTURE) {
      prim = (BasePPtr) child;
      seg = (SegPPtr) prim;
      if (prim->code == SEGMENT) {
        if (seg->seg.parent == NULL) {
          seg->seg.parent = (BasePPtr) parent;
          if (prnt->seg.head != NULL && prnt->seg.tail != NULL) {
            prev = prnt->seg.tail;
            prev->next = prim;
            prim->prev = prev;
            prnt->seg.tail = prim;
          } else if (prnt->seg.head == NULL && prnt->seg.tail == NULL) {
            prnt->seg.head = prim;
            prnt->seg.tail = prim;
            prim->prev = NULL;
          } else {
            Message (MSG_ERROR, "LinkSegment list integrity problem");
          }
        } else {
          Message (MSG_ERROR, "LinkSegment child already linked");
        }
      } else {
        Message (MSG_ERROR, "LinkSegment child not a segment");
      }
    } else {
      Message (MSG_ERROR, "LinkSegment parent not a segment or picture");
    }
  }
}

/*****************************************************************************
*
*   UnlinkSegment (child)
*       Unlinks existing segment from parent
*
*****************************************************************************/

void Nlm_UnlinkSegment (SegmenT child)

{
  BasePPtr  item;
  BasePPtr  last;
  BasePPtr  next;
  BasePPtr  prim;
  SegPPtr   prnt;
  SegPPtr   seg;

  if (child != NULL) {
    prim = (BasePPtr) child;
    if (prim->code == SEGMENT) {
      seg = (SegPPtr) prim;
      prnt = (SegPPtr) seg->seg.parent;
      if (prnt != NULL) {
        last = NULL;
        item = prnt->seg.head;
        while (item != NULL) {
          if (item == prim) {
            seg->seg.parent = NULL;
            next = item->next;
            if (last != NULL) {
              last->next = next;
            }
            if ( next != NULL ) {
              next->prev = last;
            }
            item->next = NULL;
            item = NULL;
            if (prnt->seg.head == prim) {
              prnt->seg.head = next;
            }
            if (prnt->seg.tail == prim) {
              prnt->seg.tail = last;
            }
          } else {
            last = item;
            item = item->next;
          }
        }
      }
    } else {
      Message (MSG_ERROR, "UnlinkSegment child not a segment");
    }
  }
}

/*****************************************************************************
*
*   CreateSegment (parent, segID, maxScale)
*       Creates a segment primitive that can have its own list of children
*
*****************************************************************************/

SegmenT Nlm_CreateSegment (SegmenT parent, Uint2 segID, Int4 maxScale)
{
  BasePPtr  prim;
  SegPPtr   prnt;
  SegPPtr   seg;

  if (parent != NULL) {
    seg = (SegPPtr) CreatePrimitive (parent, SEGMENT, sizeof (SegPRec));
  } else {
    prim = (BasePPtr) MemNew (sizeof (SegPRec));
    if (prim != NULL) {
      prim->next = NULL;
      prim->prev = NULL;
      prim->code = SEGMENT;
    }
    seg = (SegPPtr) prim;
  }
  if (seg != NULL) {
    seg->seg.box.left = INT4_MAX;
    seg->seg.box.top = INT4_MIN;
    seg->seg.box.right = INT4_MIN;
    seg->seg.box.bottom = INT4_MAX;
    seg->seg.head = NULL;
    seg->seg.tail = NULL;
    seg->seg.parent = (BasePPtr) parent;
    seg->seg.visible = TRUE;
    seg->seg.maxscale = maxScale;
    prnt = (SegPPtr) parent;
    seg->seg.penwidth = STD_PEN_WIDTH;
    seg->attLast = defAttPData;
    if (prnt != NULL) {
      if (prnt->base.code == SEGMENT || prnt->base.code == PICTURE) {
        seg->seg.penwidth = prnt->seg.penwidth;
        seg->attLast = prnt->attLast;
      }
    }
    seg->seg.highlight = PLAIN_SEGMENT;
    seg->seg.segID = segID;
  }
  return (SegmenT) seg;
}

/*****************************************************************************
*
*   ResetSegment (segment)
*       Cleaves and deletes the segment's child list (recursively),
*       freeing item specific data, and leaving the segment itself
*       intact for repopulation
*
*****************************************************************************/

SegmenT Nlm_ResetSegment (SegmenT segment)

{
  BasePPtr  item;
  BasePPtr  next;
  SegPPtr   seg;

  seg = NULL;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      item = seg->seg.head;
      while (item != NULL) {
        next = item->next;
        switch (item->code) {
          case PICTURE :
          case SEGMENT :
            ResetSegment ((SegmenT) item);
            break;
          default :
            CleanupPrimitive (item);
            break;
        }
        MemFree (item);
        item = next;
      }
      seg->seg.box.left = INT4_MAX;
      seg->seg.box.top = INT4_MIN;
      seg->seg.box.right = INT4_MIN;
      seg->seg.box.bottom = INT4_MAX;
      seg->seg.head = NULL;
      seg->seg.tail = NULL;
      seg->seg.penwidth = STD_PEN_WIDTH;
      seg->seg.highlight = PLAIN_SEGMENT;
      seg->attLast = defAttPData;
    } else {
      Message (MSG_ERROR, "ResetSegment argument not a segment or picture");
    }
  }
  return (SegmenT) seg;
}

/*****************************************************************************
*
*   ParentSegment (segment)
*       Returns the parent of a segment
*
*****************************************************************************/

SegmenT Nlm_ParentSegment (SegmenT segment)

{
  SegmenT  parent;
  SegPPtr  seg;

  parent = NULL;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      parent = (SegmenT) seg->seg.parent;
      if (parent != NULL) {
        seg = (SegPPtr) parent;
        if (seg->base.code != SEGMENT && seg->base.code != PICTURE) {
          Message (MSG_ERROR, "ParentSegment parent not a segment or picture");
        }
      }
    } else {
      Message (MSG_ERROR, "ParentSegment argument not a segment or picture");
    }
  }
  return parent;
}

/*****************************************************************************
*
*   SegmentID (segment)
*       Returns the segment ID of a segment
*
*****************************************************************************/

Uint2 Nlm_SegmentID (SegmenT segment)

{
  SegPPtr  seg;
  Uint2     segID;

  segID = 0;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      segID = seg->seg.segID;
    } else {
      Message (MSG_ERROR, "SegmentID argument not a segment or picture");
    }
  }
  return segID;
}

/*****************************************************************************
*
*   SegmentVisible (segment)
*       Returns the visibility of a segment
*
*****************************************************************************/

Boolean Nlm_SegmentVisible (SegmenT segment)

{
  SegPPtr  seg;
  Boolean  vis;

  vis = FALSE;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      vis = seg->seg.visible;
    } else {
      Message (MSG_ERROR, "SegmentVisible argument not a segment or picture");
    }
  }
  return vis;
}

/*****************************************************************************
*
*   Nlm_SetSegmentVisibleFlag (segment, visible)
*       Sets the visibility of a segment
*
*****************************************************************************/

void Nlm_SetSegmentVisibleFlag (SegmenT segment, Boolean visible)

{
  SegPPtr  seg;

  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      seg->seg.visible = visible;
    } else {
      Message (MSG_ERROR, "Nlm_SetSegmentVisibleFlag argument not a segment or picture");
    }
  }
}

/*****************************************************************************
*
*   SegmentStyle (segment)
*       Returns the highlight style of a segment
*
*****************************************************************************/

Int1 Nlm_SegmentStyle (SegmenT segment)

{
  Int1     highlight;
  SegPPtr  seg;

  highlight = PLAIN_SEGMENT;
  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      highlight = seg->seg.highlight;
    } else {
      Message (MSG_ERROR, "SegmentStyle argument not a segment or picture");
    }
  }
  return highlight;
}

/*****************************************************************************
*
*   SegmentBox (segment, box)
*       Returns the bounding box of a segment
*
*****************************************************************************/

void Nlm_SegmentBox (SegmenT segment, BoxPtr box)

{
  SegPPtr  seg;

  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      if (box != NULL) {
        *box = seg->seg.box;
      }
    } else {
      Message (MSG_ERROR, "SegmentBox argument not a segment or picture");
    }
  }
}

/*****************************************************************************
*
*   GetPrimitive (segment, primCt)
*       Gets a primitive by index within a segment
*
*****************************************************************************/

PrimitivE Nlm_GetPrimitive (SegmenT segment, Uint2 primCt)

{
  BasePPtr  item;
  SegPPtr   seg;

  item = NULL;
  if (segment != NULL && primCt > 0) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      item = seg->seg.head;
      while (item != NULL && primCt > 1) {
        item = item->next;
        primCt--;
      }
    } else {
      Message (MSG_ERROR, "FindPrimitive argument not a segment or picture");
    }
  }
  return (PrimitivE) item;
}

void Nlm_SetPrimitiveIDs (PrimitivE prim, Uint2 entityID, Uint4 itemID,
                          Uint2 itemtype, Uint2 primID)

{
  GenPPtr   gpp;
  BasePPtr  item;

  item = (BasePPtr) prim;
  if (item != NULL && item->code == GENERIC) {
    gpp = (GenPPtr) item;
    gpp->entityID = entityID;
    gpp->itemID = itemID;
    gpp->itemtype = itemtype;
    gpp->primID = primID;
  }
}

void Nlm_GetPrimitiveIDs (PrimitivE prim, Uint2Ptr entityIDPtr, Uint4Ptr itemIDPtr,
                          Uint2Ptr itemtypePtr, Uint2Ptr primIDPtr)

{
  GenPPtr   gpp;
  BasePPtr  item;
  item = (BasePPtr) prim;
  if (item != NULL && item->code == GENERIC) {
    gpp = (GenPPtr) item;
    if (entityIDPtr != NULL) {
      *entityIDPtr = gpp->entityID;
    }
    if (itemIDPtr != NULL) {
      *itemIDPtr = gpp->itemID;
    }
    if (itemtypePtr != NULL) {
      *itemtypePtr = gpp->itemtype;
    }
    if (primIDPtr != NULL) {
      *primIDPtr = gpp->primID;
    }
  }
}

/*****************************************************************************
*
*   AdjustParent (parent, left, top, right, bottom)
*       Recalculates segment boundaries, propagates up parent list
*
*****************************************************************************/

static void AdjustParent (SegmenT parent, Int4 left, Int4 top, Int4 right, Int4 bottom)

{
  SegPPtr  seg;

  if (parent != NULL) {
    seg = (SegPPtr) parent;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      if (seg->seg.box.left > left) {
        seg->seg.box.left = left;
      }
      if (seg->seg.box.top < top) {
        seg->seg.box.top = top;
      }
      if (seg->seg.box.right < right) {
        seg->seg.box.right = right;
      }
      if (seg->seg.box.bottom > bottom) {
        seg->seg.box.bottom = bottom;
      }
      if ( seg->base.code == SEGMENT ) {
        AdjustParent ((SegmenT) seg->seg.parent, left, top, right, bottom);
      }
    } else {
      Message (MSG_ERROR, "AdjustParent argument not a segment or picture");
    }
  }
}

/*****************************************************************************
*
*   AddAttribute (parent, flags, color, linestyle, shading, penwidth, mode)
*       Changes current attribute settings (applied only to lower levels)
*
*****************************************************************************/

void Nlm_AddAttribute(SegmenT parent, Uint1 flags, const Uint1* color,
                      Int1 linestyle, Int1 shading, Int1 penwidth, Int1 mode)

{
  AttPPtr  att;
  SegPPtr  prnt;

  prnt = (SegPPtr) parent;
  if (prnt != NULL) {
    prnt->seg.penwidth = penwidth;
    att = &(prnt->attLast);
    if (linestyle >= NO_LINE_STYLE && linestyle <= DASHED_LINE) {
      if (shading >= NO_SHADING && shading <= THICK_NWSE_SHADING) {
        if ( flags & COLOR_ATT ) {
          if (color != NULL) {
            att->color [0] = color [0];
            att->color [1] = color [1];
            att->color [2] = color [2];
          } else {
            att->color [0] = BLACK_COLOR [0];
            att->color [1] = BLACK_COLOR [1];
            att->color [2] = BLACK_COLOR [2];
          }
        }
        if ( flags & STYLE_ATT ) att->linestyle = linestyle;
        if ( flags & SHADING_ATT ) att->shading = shading;
        if ( flags & WIDTH_ATT ) {
          att->penwidth = MIN (penwidth, PEN_MAX);
          if ( att->penwidth <= 0 ) att->penwidth = 1;
        }
        if ( flags & MODE_ATT ) att->mode = mode;
      } else {
        Message (MSG_ERROR, "AddAttribute shading out of range");
      }
    } else {
      Message (MSG_ERROR, "AddAttribute line style out of range");
    }
  } else {
    Message (MSG_ERROR, "AddAttribute parent is NULL");
  }
}

/*****************************************************************************
*
*   AddPrimitive (pdp, parent, primID, pextra, extrasize )
*
*****************************************************************************/

PrimitivE Nlm_AddPrimitive (PrimDefPtr pdp, SegmenT parent, Uint2 primID, 
                        VoidPtr pextra, Int2 extrasize )
{
  GenPPtr  gpp = NULL;
  BoxInfo  pLimits;

  if (pdp == NULL) {
    Message (MSG_ERROR, "Primitive definition ptr is NULL");
  } else {
    gpp = (GenPPtr) CreatePrimitive (parent, GENERIC, 
                    sizeof (GenPRec) + MAX(0,extrasize-sizeof(double)));
    if (gpp != NULL) {
      gpp->primID = primID;
      gpp->pdp = pdp;
      if (pextra != NULL && extrasize > 0 ) {
        MemCpy ((void *)&gpp->data, (void *)pextra, (size_t)extrasize);
      }
      gpp->highlight = PLAIN_SEGMENT;
      gpp->att = ((SegPPtr)parent)->attLast;
      TryGetPrimitiveLimits ((BasePPtr)gpp, 1, 1, &pLimits );
      AdjustParent (parent,
                    pLimits.left, pLimits.top,
                    pLimits.right, pLimits.bottom);
    }
  }
  return (PrimitivE) gpp;
}

static void RecalSegment (SegPPtr seg, Int4 scaleX, Int4 scaleY )

{
  BasePPtr item;
  BoxInfo  pLimits;

  if (seg != NULL) {
    seg->seg.box.left = INT4_MAX;
    seg->seg.box.top = INT4_MIN;
    seg->seg.box.right = INT4_MIN;
    seg->seg.box.bottom = INT4_MAX;
    item = seg->seg.head;
    while (item != NULL) {
      switch (item->code) {
        case PICTURE :
        case SEGMENT :
          RecalSegment ((SegPPtr)item, scaleX, scaleY);
          break;
        case GENERIC :
          TryGetPrimitiveLimits ( item, scaleX, scaleY, &pLimits );
          AdjustParent ((SegmenT)seg,
                        pLimits.left, pLimits.top,
                        pLimits.right, pLimits.bottom);
      }
      item = item->next;
    }
  }
}

/*****************************************************************************
*
*   RecalculateSegment (segment, scaleX, scaleY)
*       Explores the segment's child list (recursively), recalculating segment
*       boundaries and scaled margins, propagating back up parent list
*
*****************************************************************************/

void Nlm_RecalculateSegment (SegmenT segment, Int4 scaleX, Int4 scaleY )

{
  SegPPtr     seg;

  if (segment != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      RecalSegment (seg, scaleX, scaleY);
    } else {
      Message (MSG_ERROR, "RecalculateSegment argument not a segment or picture");
    }
  }
}

/*****************************************************************************
*
*   OffsetSegment (segment, deltaX, deltaY)
*
*****************************************************************************/

void Nlm_OffsetSegment (SegmenT segment, Int4 deltaX, Int4 deltaY)

{
  BasePPtr  item;
  SegPPtr   seg;

  if (segment != NULL) {
    item = (BasePPtr)segment;
    switch (item->code) {
      case PICTURE :
      case SEGMENT :
        seg = (SegPPtr) segment;
        seg->seg.box.left += deltaX;
        seg->seg.box.top += deltaY;
        seg->seg.box.right += deltaX;
        seg->seg.box.bottom += deltaY;
        item = seg->seg.head;
        while (item != NULL) {
          switch (item->code) {
            case PICTURE :
            case SEGMENT :
              Nlm_OffsetSegment ((SegmenT)item, deltaX, deltaY );
              break;
            case GENERIC :
              TryOffsetPrimitive (item, deltaX, deltaY);
          }
          item = item->next;
        }

    }
  }
}

/*****************************************************************************
*
*   ExploreSegment (segment, userdata, callback)
*       Explores the segment's child list (recursively)
*
*****************************************************************************/

static Boolean ExploreSegmentProc (SegmenT segment, VoidPtr userdata, SegmentExploreProc callback)

{
  Boolean   goOn;
  GenPPtr   gpp;
  BasePPtr  item;
  BasePPtr  next;
  Uint2     primCt;
  SegPPtr   seg;

  goOn = FALSE;
  if (segment != NULL && callback != NULL) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      goOn = callback (segment, NULL, seg->seg.segID, 0, 0, userdata);
      item = seg->seg.head;
      primCt = 0;
      while (item != NULL && goOn) {
        primCt++;
        next = item->next;
        switch (item->code) {
          case PICTURE :
          case SEGMENT :
            goOn = ExploreSegmentProc ((SegmenT) item, userdata, callback);
            break;
          case GENERIC :
            gpp = (GenPPtr) item;
            goOn = callback (segment, (PrimitivE) item, seg->seg.segID,
                             gpp->primID, primCt, userdata);
            break;
          default :
            break;
        }
        item = next;
      }
    } else {
      Message (MSG_ERROR, "ExploreSegment argument not a segment or picture");
    }
  }
  return goOn;
}

void Nlm_ExploreSegment (SegmenT segment, VoidPtr userdata, SegmentExploreProc callback)

{
  ExploreSegmentProc (segment, userdata, callback);
}

/*****************************************************************************
*
*   SearchSegment (segment, pt, pnt, scale, prID, prCt)
*       Returns the deepest segment that contains the pnt in world coordinates
*
*****************************************************************************/

SegmenT SearchSegment (SegmenT segment, ScalePtr scalePtr, PrimitivE PNTR prPtr )
{
  SegmenT   foundSeg = NULL;
  BasePPtr  item;
  SegPPtr   seg;
  ScaleInfo scale;

  if (segment != NULL && scalePtr != NULL ) {
    seg = (SegPPtr) segment;
    if (seg->base.code == SEGMENT || seg->base.code == PICTURE) {
      scale = *scalePtr;
      if (seg->seg.maxscale == 0 || 
          seg->seg.maxscale >= MAX (scale.scaleX, scale.scaleY)) {
        if ( (seg->seg.box.left <= scale.worldWindow.right) &&
             (seg->seg.box.right >= scale.worldWindow.left) &&
             (seg->seg.box.top >= scale.worldWindow.bottom) &&
             (seg->seg.box.bottom <= scale.worldWindow.top) ){
          if (seg->seg.visible) {
            item = seg->seg.tail;
            while (item != NULL && foundSeg == NULL) {
              switch (item->code) {
                case GENERIC :
                  if (PrimitiveIsCloseToPoint (item, &scale)) {
                    foundSeg = (SegmenT) seg;
                    *prPtr = (PrimitivE)item;
                  }
                  break;
                case SEGMENT :
                  foundSeg = SearchSegment ((SegmenT)item, &scale, prPtr);
                  break;
                default : 
                  Message (MSG_ERROR, "FindSegment child is a picture");
              }
              item = item->prev;
            }
          }
        }
      }
    } else {
      Message (MSG_ERROR, "SearchSegment argument not a segment or picture");
    }
  }
  return foundSeg;
}

/*****************************************************************************
*
*  ChangePrimAttribute (prim, flags, color, linestyle, shading, penwidth, mode)
*
*****************************************************************************/

void Nlm_ChangePrimAttribute (PrimitivE prim, Uint1 flags, Uint1Ptr color, 
       Int1 linestyle, Int1 shading, Int1 penwidth, Int1 mode) 
{
  GenPPtr  gpp = (GenPPtr)prim;
  AttPPtr  att;

  if ( gpp ) {
    att = &(gpp->att);
    if (linestyle >= NO_LINE_STYLE && linestyle <= DASHED_LINE) {
      if (shading >= NO_SHADING && shading <= THICK_NWSE_SHADING) {
        if ( flags & COLOR_ATT ) {
          if (color != NULL) {
            att->color [0] = color [0];
            att->color [1] = color [1];
            att->color [2] = color [2];
          } else {
            att->color [0] = BLACK_COLOR [0];
            att->color [1] = BLACK_COLOR [1];
            att->color [2] = BLACK_COLOR [2];
          }
        }
        if ( flags & STYLE_ATT ) att->linestyle = linestyle;
        if ( flags & SHADING_ATT ) att->shading = shading;
        if ( flags & WIDTH_ATT ) att->penwidth = MIN (penwidth, PEN_MAX);
        if ( flags & MODE_ATT ) att->mode = mode;
      } else {
        Message (MSG_ERROR, "ChangePrimAttribute shading out of range");
      }
    } else {
      Message (MSG_ERROR, "ChangePrimAttribute line style out of range");
    }
  } else {
    Message (MSG_ERROR, "ChangePrimAttribute primitive is NULL");
  }
}

/*****************************************************************************
*
*   OffsetPrim ( prim, deltaX, deltaY)
*
*****************************************************************************/

void Nlm_OffsetPrim(PrimitivE prim, Int4 deltaX, Int4 deltaY)
{
  BasePPtr item = (BasePPtr)prim;

  if (item  &&  item->code == GENERIC)
    TryOffsetPrimitive(item, deltaX, deltaY);
}


/*****************************************************************************
*
*   DeletePrim ( prim )
*
*****************************************************************************/

void Nlm_DeletePrim(Nlm_SegmenT parent, Nlm_PrimitivE prim)
{
  BasePPtr      item = (BasePPtr)prim;
  Nlm_SegPData *seg  = &((SegPPtr)parent)->seg;

  if ( !item )
    return;

#ifdef _DEBUG
  {{
    BasePPtr temp = seg->head;
    for ( ;  temp  &&  temp != item;  temp = temp->next);
    ASSERT ( temp == item );
  }}
#endif

  if (item == seg->head)
    seg->head = item->next;
  if (item == seg->tail)
    seg->tail = item->prev;

  if ( item->prev )
    item->prev->next = item->next;
  if ( item->next )
    item->next->prev = item->prev;

  CleanupPrimitive( item );
  MemFree( item );

  /* It would be nice to call Nlm_RecalculateSegment() here, but:
   *  1) we don't know the scaling factors;
   *  2) it can be rather time-consuming if a lot of primitives get
   *     deleted at once.
   *  Ergo:  that's up to the user to recalc the seg. Take care...
   */
}