summaryrefslogtreecommitdiff
path: root/src/AlienImage/AlienImage_SGIRGBAlienData.cxx
blob: c8401f56a9e331da2d3cd3b0e9996cc0d8c365da (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
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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
// Modified	27/12/97 : FMN ; PERF: OPTIMISATION LOADER (LOPTIM)
// Modified	08/12/98 : FMN ; SGI : Changement option de compilation
//

#include <Aspect_ColorRampColorMap.hxx>
#include <Image_PseudoColorImage.hxx>
#include <AlienImage_SGIRGBFileHeader.hxx>
#include <AlienImage_SGIRGBAlienData.ixx>
#include <Standard.hxx>

#define TRACE

#include <stdio.h>
#if !defined(_IOERR)
# define       _IOERR          0040    /* I/O error from system */
#endif
#if !defined(_IOREAD)
# define       _IOREAD         0001    /* currently reading */
#endif
#if !defined(_IOWRT)
# define       _IOWRT          0002    /* currently writing */
#endif
#if !defined(_IORW)
# define       _IORW           0200    /* opened for reading and writing */
#endif
 
#ifdef TRACE
static int Verbose = 0 ;
#endif

#define LOPTIM
#ifndef LOPTIM
static Handle(Aspect_ColorRampColorMap) GrayScaleColorMap = 
	new Aspect_ColorRampColorMap( 0,256, Quantity_Color(1.,1.,1.,Quantity_TOC_RGB));
#else 
static Handle(Aspect_ColorRampColorMap)& _GrayScaleColorMap() {
static Handle(Aspect_ColorRampColorMap) GrayScaleColorMap =
      new Aspect_ColorRampColorMap( 0,256, Quantity_Color(1.,1.,1.,Quantity_TOC_RGB));
return GrayScaleColorMap;
}
#define GrayScaleColorMap _GrayScaleColorMap()
#endif // LOPTIM
  

//============================================================================
//==== C Class Method.
//============================================================================
static Standard_Boolean iopen(	OSD_File &File,
					AlienImage_SGIRGBFileHeader *,
					char ,
					unsigned int ,
					unsigned int ,
					unsigned int ,
					unsigned int ,
					unsigned int  );
static void		iclose(   	OSD_File &File,AlienImage_SGIRGBFileHeader *);
int 		iflush(   	OSD_File &File,AlienImage_SGIRGBFileHeader *);
static void 		isetname( 	AlienImage_SGIRGBFileHeader *, const char * );
#ifdef DEB
static void 		isetcolormap(	AlienImage_SGIRGBFileHeader *, int );
#endif
static int 		putrow(		OSD_File &File,
				AlienImage_SGIRGBFileHeader *,
				unsigned short	*,
				unsigned int , 
				unsigned int );
static int 		getrow(		OSD_File &File,
				AlienImage_SGIRGBFileHeader *,
				unsigned short	*,
				unsigned int , 
				unsigned int );


//------------------------------------------------------------------------------
//		Private Method
//------------------------------------------------------------------------------

#define DataSize() (myHeader.ysize*myHeader.xsize*sizeof(short))

Handle(Image_ColorImage) AlienImage_SGIRGBAlienData::ToColorImage() const

{ Handle(Image_ColorImage) ret_image = 
	new Image_ColorImage( 	0,0,
				(Standard_Integer)myHeader.xsize,
				(Standard_Integer)myHeader.ysize ) ;
  unsigned short *rbuf, *gbuf, *bbuf ;
  Aspect_ColorPixel CPixel ;
  Quantity_Color    acolor ;
  Standard_Real r,g,b ;
  unsigned short x,y ;
  Standard_Integer LowX = ret_image->LowerX() ;
  Standard_Integer LowY = ret_image->LowerY() ;

  rbuf = ( unsigned short * ) myRedData ;
  gbuf = ( unsigned short * ) myGreenData ;
  bbuf = ( unsigned short * ) myBlueData ;

  for(y=0; y<myHeader.ysize; y++) {
	for(x=0; x<myHeader.xsize; x++) {
		r = Standard_Real( rbuf[x] ) ;
		r /= 255. ;
		g = Standard_Real( gbuf[x] ) ;
		g /= 255. ;
		b = Standard_Real( bbuf[x] ) ;
		b /= 255. ;
		acolor.SetValues( r,g,b, Quantity_TOC_RGB ) ;
		CPixel.SetValue ( acolor ) ;

         	ret_image->SetPixel( LowX+x , LowY+y, CPixel ) ;
	}
        rbuf += myHeader.xsize ;
        gbuf += myHeader.xsize ;
        bbuf += myHeader.xsize ;
  }

  return( ret_image ) ;
}

Handle_Image_PseudoColorImage AlienImage_SGIRGBAlienData::ToPseudoColorImage() 
								const

{ Handle(Image_PseudoColorImage) ret_image = 
	new Image_PseudoColorImage( 	0,0,
					(Standard_Integer)myHeader.xsize,
					(Standard_Integer)myHeader.ysize,
					GrayScaleColorMap ) ;

  unsigned short *rbuf ;
  Aspect_IndexPixel CPixel ;
  unsigned short x,y ;
  Standard_Integer LowX = ret_image->LowerX() ;
  Standard_Integer LowY = ret_image->LowerY() ;

  rbuf = ( unsigned short * ) myRedData ;

  for(y=0; y<myHeader.ysize; y++) {
	for(x=0; x<myHeader.xsize; x++) {
		CPixel.SetValue ( Standard_Integer( rbuf[x] ) ) ;

         	ret_image->SetPixel( LowX+x , LowY+y, CPixel ) ;
	}
        rbuf += myHeader.xsize ;
  }

  return ret_image ;

}

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


//============================================================================
//==== C Class Method.
static unsigned short 	*ibufalloc(AlienImage_SGIRGBFileHeader *);
#ifdef DEB
static unsigned int 	reverse(unsigned int ) ;
#endif
static void 		cvtshorts( unsigned short [], int );
static void 		cvtlongs( int [], int  ) ;
static void 		cvtimage(int [] );
static int 		img_seek( 	OSD_File &File,
					AlienImage_SGIRGBFileHeader *,
				  	unsigned int , unsigned int  );
static int 		img_badrow(	AlienImage_SGIRGBFileHeader *, 
					int , int  );
static int 		img_write( 	OSD_File &File,
					AlienImage_SGIRGBFileHeader *,
					char *,	int  );
static int 		img_read( 	OSD_File &File,
					AlienImage_SGIRGBFileHeader *,
					char *,	int  );
static int 		img_optseek(	OSD_File &File,
					AlienImage_SGIRGBFileHeader *,
					unsigned int );
static int 		img_getrowsize( AlienImage_SGIRGBFileHeader * );
static void 		img_setrowsize( AlienImage_SGIRGBFileHeader *,
					int , int , int  );
static int 		img_rle_compact(	unsigned short *,
						int ,
						unsigned short *,
						int , int );
static void 		img_rle_expand(	unsigned short *,
					int ,
					unsigned short *,
					int  );

void i_errhdlr(const char *fmt)
{
		printf("%s",fmt);
		return;
}

/*
 *	iclose and iflush -
 *
 */

static void iclose( OSD_File& File, AlienImage_SGIRGBFileHeader *image)
{
    int tablesize;

    iflush( File, image);
    img_optseek( File, image, 0);
    if (image->flags&_IOWRT) {
	if(image->dorev)
	    cvtimage((int *)image);
	if (	img_write( File, image,
			(char *)image,sizeof(AlienImage_SGIRGBFileHeader)) != 	
		sizeof(AlienImage_SGIRGBFileHeader)) {
	    i_errhdlr("iclose: error on write of image header\n");
	    return ;
	}
	if(image->dorev)
	    cvtimage((int *)image);
	if(ISRLE(image->type)) {
	    img_optseek( File, image, 512);
	    tablesize = image->ysize*image->zsize*sizeof(int);
	    if(image->dorev)
		cvtlongs((int *)image->rowstart,tablesize);
	    if (img_write( File, image,(char *)image->rowstart,tablesize) != tablesize) {
		i_errhdlr("iclose: error on write of rowstart\n");
		return ;
	    }
	    if(image->dorev)
		cvtlongs((int *)image->rowsize,tablesize);
	    if (img_write( File, image,(char *)image->rowsize,tablesize) != tablesize) {
		i_errhdlr("iclose: error on write of rowsize\n");
		return ;
	    }
	}
    }
    if(image->base) {
	free(image->base);
	image->base = 0;
    }
    if(image->tmpbuf) {
	free(image->tmpbuf);
	image->tmpbuf = 0;
    }
    if(ISRLE(image->type)) {
	free(image->rowstart);
	image->rowstart = 0;
	free(image->rowsize);
	image->rowsize = 0;
    }

//    return(close(image->file));
}

int iflush( OSD_File& File, AlienImage_SGIRGBFileHeader *image)
{
    unsigned short *base;

    if ( (image->flags&_IOWRT)
     && (base=image->base)!=NULL && (image->ptr-base)>0) {
	    if (putrow(File,image, base, image->y,image->z)!=image->xsize) {
		    image->flags |= _IOERR;
		    return(EOF);
	    }
    }
    return 0 ;
}
/*
 *	isetname and isetcolormap -
 *
 */

static void isetname( AlienImage_SGIRGBFileHeader *image, const char *name )
{
    strncpy(image->name,name,80);
}

// Unused :
#ifdef DEB
static void isetcolormap(AlienImage_SGIRGBFileHeader *image, int colormap)
{
    image->colormap = colormap;
}
#endif
/*
 *	iopen - 
 *
 */
static Standard_Boolean iopen( 	OSD_File &File,
				AlienImage_SGIRGBFileHeader *image,
				char mode,
				unsigned int type,
				unsigned int dim,
				unsigned int xsize,
				unsigned int ysize,
				unsigned int zsize )
{
	
	int tablesize;
	register int i, max;

	Standard_Integer bblcount ;

	int calsize	= sizeof( AlienImage_SGIRGBFileHeader ) ;
	if (mode=='w') { //------------------WRITE------------------
		image->imagic = IMAGIC;
		image->type = (unsigned short)type;
		image->xsize = (unsigned short)xsize;
		image->ysize = 1;
		image->zsize = 1;
		if (dim>1)
		    image->ysize = (unsigned short)ysize;
		if (dim>2)
		    image->zsize = (unsigned short)zsize;
		if(image->zsize == 1) {
		    image->dim = 2;
		    if(image->ysize == 1)
			image->dim = 1;
		} else {
		    image->dim = 3;
		}
		image->min = 10000000;
		image->max = 0;
		isetname(image,"no name"); 
		image->wastebytes = 0;
		image->dorev = 0;

		File.Write( ( Standard_Address ) image, calsize ) ;
  		if ( File.Failed() ) {
			// ERROR
		    	i_errhdlr("iopen: error on write of image header\n");
			return( Standard_False ) ;
  		}
	} else {//--------------------------READ------------------------
		File.Read( ( Standard_Address& )image, calsize, bblcount ) ;
  		if ( File.Failed() || 
		     ( bblcount != calsize ) ) {
#ifdef TRACE
			// ERROR
		    	i_errhdlr("iopen: error on read of image header\n");
#endif
			return( Standard_False ) ;
  		}

		if( ((image->imagic>>8) | ((image->imagic&0xff)<<8)) 
							     == IMAGIC ) {
		    image->dorev = 1;
		    cvtimage((int *)image);
		} else
		    image->dorev = 0;
		if (image->imagic != IMAGIC) {
#ifdef TRACE
			i_errhdlr("iopen: bad magic in image file \n");
#endif
		    return Standard_False ;
		}
	}
	if (mode != 'r')
	    image->flags = _IOWRT;
	else
	    image->flags = _IOREAD;
	if(ISRLE(image->type)) {
	    tablesize = image->ysize*image->zsize*sizeof(int);
	    image->rowstart = (unsigned int *)malloc(tablesize);
	    image->rowsize = (int *)malloc(tablesize);
	    if( image->rowstart == 0 || image->rowsize == 0 ) {
		i_errhdlr("iopen: error on table alloc\n");
		return Standard_False ;
	    }
	    image->rleend = 512+2*tablesize;
	    if (mode=='w') {
		max = image->ysize*image->zsize;
		for(i=0; i<max; i++) {
		    image->rowstart[i] = 0;
		    image->rowsize[i] = -1;
		}
	    } else {
		tablesize = image->ysize*image->zsize*sizeof(int);
		File.Seek( 512, OSD_FromBeginning ) ;
		File.Read( (Standard_Address&) image->rowstart,tablesize, bblcount);
  		if ( File.Failed() || ( bblcount != tablesize ) ) {
			// ERROR
		    	i_errhdlr("iopen: error on read of rowstart\n");
			return( Standard_False ) ;
  		}

		if(image->dorev)
		    cvtlongs((int *)image->rowstart,tablesize);
		File.Read( (Standard_Address&) image->rowsize,tablesize, bblcount);
  		if ( File.Failed() || ( bblcount != tablesize ) ) {
			// ERROR
		    	i_errhdlr("iopen: error on read of rowsize\n");
			return( Standard_False ) ;
  		}

		if(image->dorev)
		    cvtlongs((int *)image->rowsize,tablesize);
	    }
	}
	image->cnt = 0;
	image->ptr = 0;
	image->base = 0;
	if( (image->tmpbuf = ibufalloc(image)) == 0 ) {	
	    i_errhdlr("iopen: error on tmpbuf alloc\n");
	    return Standard_False ;
	}
	image->x = image->y = image->z = 0;
	image->file = 0;
	image->offset = 512;			/* set up for img_optseek */

	File.Seek( 512, OSD_FromBeginning ) ;

	return(Standard_True);
}


static unsigned short *ibufalloc(AlienImage_SGIRGBFileHeader *image)
{
    return (unsigned short *)malloc(IBUFSIZE(image->xsize));
}

// Unused :
#ifdef DEB
static unsigned int reverse(unsigned int lwrd) 
{
    return ((lwrd>>24) 		| 
	   (lwrd>>8 & 0xff00) 	| 
	   (lwrd<<8 & 0xff0000) | 
	   (lwrd<<24) 		);
}
#endif

static void cvtshorts( unsigned short buffer[], int n)
{
    register short i;
    register int nshorts = n>>1;
    register unsigned short swrd;

    for(i=0; i<nshorts; i++) {
	swrd = *buffer;
	*buffer++ = (swrd>>8) | (swrd<<8);
    }
}

static void cvtlongs( int buffer[], int n ) 
{
    register short i;
    register int nlongs = n>>2;
    register unsigned int lwrd;

    for(i=0; i<nlongs; i++) {
	lwrd = buffer[i];
	buffer[i] =     ((lwrd>>24) 		| 
	   		(lwrd>>8 & 0xff00) 	| 
	   		(lwrd<<8 & 0xff0000) 	| 
	   		(lwrd<<24) 		);
    }
}

static void cvtimage(int buffer[] )
{
    cvtshorts((unsigned short *)buffer,12);
    cvtlongs(buffer+3,12);
    cvtlongs(buffer+26,4);
}

/*
 *	img_seek, img_write, img_read, img_optseek -
 *
 *
 */

static int img_seek( 	OSD_File &File,
			AlienImage_SGIRGBFileHeader *image,
			unsigned int y, unsigned int z )
{
    if(img_badrow(image,y,z)) {
	i_errhdlr("img_seek: row number out of range\n");
	return EOF;
    }
    image->x = 0;
    image->y = (unsigned short)y;
    image->z = (unsigned short)z;
    if(ISVERBATIM(image->type)) {
	switch(image->dim) {
	    case 1:
		return img_optseek(File,image, 512);
	    case 2: 
		return img_optseek(File,image,
			512+(y*image->xsize)*BPP(image->type));
	    case 3: 
		return img_optseek(File,image,
		    512+(y*image->xsize+z*image->xsize*image->ysize)*
							BPP(image->type));
	    default:
		i_errhdlr("img_seek: weird dim\n");
		break;
	}
    } else if(ISRLE(image->type)) {
	switch(image->dim) {
	    case 1:
		return img_optseek(File, image, image->rowstart[0]);
	    case 2: 
		return img_optseek(File, image, image->rowstart[y]);
	    case 3: 
		return img_optseek(File, image, 
			image->rowstart[y+z*image->ysize]);
	    default:
		i_errhdlr("img_seek: weird dim\n");
		break;
	}
    } else 
	i_errhdlr("img_seek: weird image type\n");
 return -1;
}

static int img_badrow(AlienImage_SGIRGBFileHeader *image, int y, int z )
{
    if(y>=(int)image->ysize || z>=(int)image->zsize)
	return 1;
//    else
    return 0;
}

static int img_write( OSD_File &File,
			AlienImage_SGIRGBFileHeader *image,char *buffer,
			int bblcount )
{
    int retval;

    File.Write( (Standard_Address) buffer,bblcount ) ;

    if ( File.Failed() ) retval = 0 ;
    else 		 retval = bblcount ;

    if(retval == bblcount) 
	image->offset += bblcount;
    else
	image->offset = (unsigned int ) -1;
    return retval;
}

static int img_read( OSD_File &File,
			AlienImage_SGIRGBFileHeader *image, char *buffer,
				int bblcount )
{
    int retval;
    File.Read( (Standard_Address&)buffer,bblcount, retval ) ;
    if(retval == bblcount && !File.Failed() ) 
	image->offset += bblcount;
    else
	image->offset = (unsigned int ) -1;
    return retval;
}

static int img_optseek(	OSD_File &File,
			AlienImage_SGIRGBFileHeader *image,
			unsigned int offset)
{

    if(image->offset != offset) {
       image->offset = offset;
       	File.Seek( offset, OSD_FromBeginning ) ;

	return( offset ) ;
       //return lseek(image->file,offset,0);
   }
   return offset;
}

/*
 *	img_getrowsize, img_setrowsize, img_rle_compact, img_rle_expand -
 *
 *
 */

static int img_getrowsize( AlienImage_SGIRGBFileHeader *image )
{
    switch(image->dim) {
	case 1:
	    return image->rowsize[0];
	case 2:
	    return image->rowsize[image->y];
	case 3:
	    return image->rowsize[image->y+image->z*image->ysize];
    }
 return -1;
}

static void img_setrowsize( 	AlienImage_SGIRGBFileHeader *image,
				int cnt, int y, int z )
{
    int *sizeptr=NULL;

    if(img_badrow(image,y,z)) 
	return;
    switch(image->dim) {
	case 1:
	    sizeptr = &image->rowsize[0];
	    image->rowstart[0] = image->rleend;
	    break;
	case 2:
	    sizeptr = &image->rowsize[y];
	    image->rowstart[y] = image->rleend;
	    break;
	case 3:
	    sizeptr = &image->rowsize[y+z*image->ysize];
	    image->rowstart[y+z*image->ysize] = image->rleend;
    }	
    if(*sizeptr != -1) 
	image->wastebytes += *sizeptr;
    *sizeptr = cnt;
    image->rleend += cnt;
}

#define docompact 							\
	while(iptr<ibufend) {						\
	    sptr = iptr;						\
	    iptr += 2;							\
	    while((iptr<ibufend)&&((iptr[-2]!=iptr[-1])||(iptr[-1]!=iptr[0])))\
		iptr++;							\
	    iptr -= 2;							\
	    bblcount = iptr-sptr;					\
	    while(bblcount) {						\
		todo = bblcount>126 ? 126:bblcount; 			\
		bblcount -= todo;					\
		*optr++ = 0x80|todo;					\
		while(todo--)						\
		    *optr++ = (unsigned char)*sptr++;			\
	    }								\
	    sptr = iptr;						\
	    cc = *iptr++;						\
	    while( (iptr<ibufend) && (*iptr == cc) )			\
		iptr++;							\
	    bblcount = iptr-sptr;					\
	    while(bblcount) {						\
		todo = bblcount>126 ? 126:bblcount; 			\
		bblcount -= todo;					\
		*optr++ = (unsigned char)todo;				\
		*optr++ = (unsigned char)cc;				\
	    }								\
	}								\
	*optr++ = 0;

static int img_rle_compact(	unsigned short *expbuf,
				int ibpp,
				unsigned short *rlebuf,
				int obpp, int cnt)
{
    if(ibpp == 1 && obpp == 1) {
	register unsigned char *iptr = (unsigned char *)expbuf;
	register unsigned char *ibufend = iptr+cnt;
	register unsigned char *sptr;
	register unsigned char *optr = (unsigned char *)rlebuf;
	register short todo, cc;
	register int bblcount;

	docompact;
	return (int)(optr - (unsigned char *)rlebuf);
    } else if(ibpp == 1 && obpp == 2) {
	register unsigned char *iptr = (unsigned char *)expbuf;
	register unsigned char *ibufend = iptr+cnt;
	register unsigned char *sptr;
	register unsigned short *optr = rlebuf;
	register short todo, cc;
	register int bblcount;

	docompact;
	return optr - rlebuf;
    } else if(ibpp == 2 && obpp == 1) {
	register unsigned short *iptr = expbuf;
	register unsigned short *ibufend = iptr+cnt;
	register unsigned short *sptr;
	register unsigned char *optr = (unsigned char *)rlebuf;
	register short todo, cc;
	register int bblcount;


	docompact;
	return (int)(optr - (unsigned char *)rlebuf);
    } else if(ibpp == 2 && obpp == 2) {
	register unsigned short *iptr = expbuf;
	register unsigned short *ibufend = iptr+cnt;
	register unsigned short *sptr;
	register unsigned short *optr = rlebuf;
	register short todo, cc;
	register int bblcount;

	docompact;
	return optr - rlebuf;
    } else  {
	i_errhdlr("rle_compact: bad bpp\n");
	return 0;
    }
}

#define doexpand				\
	while(1) {				\
	    pixel = *iptr++;			\
	    if ( (bblcount = (pixel & 0x7f)) == 0)	\
		return;				\
	    if(pixel & 0x80) {			\
	       while(bblcount--)		\
		    *optr++ = (unsigned char)*iptr++;		\
	    } else {				\
	       pixel = *iptr++;			\
	       while(bblcount--)		\
		    *optr++ = (unsigned char)pixel; \
	    }					\
	}

static void img_rle_expand(	unsigned short *rlebuf,
				int ibpp,
				unsigned short *expbuf,
				int obpp )

{
    if(ibpp == 1 && obpp == 1) {
	register unsigned char *iptr = (unsigned char *)rlebuf;
	register unsigned char *optr = (unsigned char *)expbuf;
	register unsigned short pixel,bblcount;

	doexpand;
    } else if(ibpp == 1 && obpp == 2) {
	register unsigned char *iptr = (unsigned char *)rlebuf;
	register unsigned short *optr = expbuf;
	register unsigned short pixel,bblcount;

	doexpand;
    } else if(ibpp == 2 && obpp == 1) {
	register unsigned short *iptr = rlebuf;
	register unsigned char  *optr = (unsigned char *)expbuf;
	register unsigned short pixel,bblcount;

	doexpand;
    } else if(ibpp == 2 && obpp == 2) {
	register unsigned short *iptr = rlebuf;
	register unsigned short *optr = expbuf;
	register unsigned short pixel,bblcount;

	doexpand;
    } else 
	i_errhdlr("rle_expand: bad bpp\n");
}
/*
 *	putrow, getrow -
 *
 */

static int putrow(	OSD_File& File,
		AlienImage_SGIRGBFileHeader	*image,
		unsigned short	*buffer,
		unsigned int y, unsigned int z )
{
    register unsigned short 	*sptr;
    register unsigned char      *cptr;
    register unsigned int x;
    register unsigned int min, max;
    register int cnt;

    if( !(image->flags & (_IORW|_IOWRT)) )
	return -1;
    if(image->dim<3)
	z = 0;
    if(image->dim<2)
	y = 0;
    if(ISVERBATIM(image->type)) {
	switch(BPP(image->type)) {
	    case 1: 
		min = image->min;
		max = image->max;
		cptr = (unsigned char *)image->tmpbuf;
		sptr = buffer;
		for(x=image->xsize; x--;) { 
		    *cptr = (unsigned char )*sptr++;
		    if (*cptr > max) max = *cptr;
		    if (*cptr < min) min = *cptr;
		    cptr++;
		}
		image->min = min;
		image->max = max;
		img_seek( File, image,y,z);
		cnt = image->xsize;
		if (img_write( File, image,(char *)image->tmpbuf,cnt) != cnt) 
		    return -1;
		else
		    return cnt;
		/* NOTREACHED */

	    case 2: 
		min = image->min;
		max = image->max;
		sptr = buffer;
		for(x=image->xsize; x--;) { 
		    if (*sptr > max) max = *sptr;
		    if (*sptr < min) min = *sptr;
		    sptr++;
		}
		image->min = min;
		image->max = max;
		img_seek( File, image,y,z);
		cnt = image->xsize<<1;
		if(image->dorev)	
		    cvtshorts(buffer,cnt);
		if (img_write( File, image,(char *)buffer,cnt) != cnt) {
		    if(image->dorev)	
			cvtshorts(buffer,cnt);
		    return -1;
		} else {
		    if(image->dorev)	
			cvtshorts(buffer,cnt);
		    return image->xsize;
		}
		/* NOTREACHED */

	    default:
		i_errhdlr("putrow: weird bpp\n");
	}
    } else if(ISRLE(image->type)) {
	switch(BPP(image->type)) {
	    case 1: 
		min = image->min;
		max = image->max;
		sptr = buffer;
		for(x=image->xsize; x--;) { 
		    if (*sptr > max) max = *sptr;
		    if (*sptr < min) min = *sptr;
		    sptr++;
		}
		image->min = min;
		image->max = max;
		cnt = img_rle_compact(buffer,2,image->tmpbuf,1,image->xsize);
		img_setrowsize(image,cnt,y,z);
		img_seek( File, image,y,z);
		if (img_write( File, image,(char *)image->tmpbuf,cnt) != cnt) 
		    return -1;
		else
		    return image->xsize;
		/* NOTREACHED */

	    case 2: 
		min = image->min;
		max = image->max;
		sptr = buffer;
		for(x=image->xsize; x--;) { 
		    if (*sptr > max) max = *sptr;
		    if (*sptr < min) min = *sptr;
		    sptr++;
		}
		image->min = min;
		image->max = max;
		cnt = img_rle_compact(buffer,2,image->tmpbuf,2,image->xsize);
		cnt <<= 1;
		img_setrowsize(image,cnt,y,z);
		img_seek( File, image,y,z);
		if(image->dorev)
		    cvtshorts(image->tmpbuf,cnt);
		if (img_write( File, image,(char *)image->tmpbuf,cnt) != cnt) {
		    if(image->dorev)
			cvtshorts(image->tmpbuf,cnt);
		    return -1;
		} else {
		    if(image->dorev)
			cvtshorts(image->tmpbuf,cnt);
		    return image->xsize;
		}
		/* NOTREACHED */

	    default:
		i_errhdlr("putrow: weird bpp\n");
	}
    } else 
	i_errhdlr("putrow: weird image type\n");
return -1;
}

static int getrow(	OSD_File& File,
		AlienImage_SGIRGBFileHeader *image,
		unsigned short *buffer,
		unsigned int y, unsigned int z)
{
    register short i;
    register unsigned char *cptr;
    register unsigned short *sptr;
    register int cnt; 

    if( !(image->flags & (_IORW|_IOREAD)) )
	return -1;
    if(image->dim<3)
	z = 0;
    if(image->dim<2)
	y = 0;
    img_seek( File, image, y, z);
    if(ISVERBATIM(image->type)) {
	switch(BPP(image->type)) {
	    case 1: 
		if (img_read( File, image,(char *)image->tmpbuf,image->xsize) 
							    != image->xsize) 
		    return -1;
		else {
		    cptr = (unsigned char *)image->tmpbuf;
		    sptr = buffer;
		    for(i=image->xsize; i--;)
			*sptr++ = *cptr++;
		}
		return image->xsize;
		/* NOTREACHED */

	    case 2: 
		cnt = image->xsize<<1; 
		if (img_read( File, image,(char *)buffer,cnt) != cnt)
		    return -1;
		else {
		    if(image->dorev)
			cvtshorts(buffer,cnt);
		    return image->xsize;
		}
		/* NOTREACHED */

	    default:
		i_errhdlr("getrow: weird bpp\n");
		break;
	}
    } else if(ISRLE(image->type)) {
	switch(BPP(image->type)) {
	    case 1: 
		if( (cnt = img_getrowsize(image)) == -1 )
		    return -1;
		if( img_read( File, image,(char *)image->tmpbuf,cnt) != cnt )
		    return -1;
		else {
		    img_rle_expand(image->tmpbuf,1,buffer,2);
		    return image->xsize;
		}
		/* NOTREACHED */

	    case 2: 
		if( (cnt = img_getrowsize(image)) == -1 )
		    return -1;
		if( cnt != img_read( File, image,(char *)image->tmpbuf,cnt) )
		    return -1;
		else {
		    if(image->dorev)
			cvtshorts(image->tmpbuf,cnt);
		    img_rle_expand(image->tmpbuf,2,buffer,2);
		    return image->xsize;
		}
		/* NOTREACHED */

	    default:
		i_errhdlr("getrow: weird bpp\n");
		break;
	}
    } else 
	i_errhdlr("getrow: weird image type\n");
return -1;
}

#ifdef PIX
/*
 *	ifilbuf -
 *
 */
static int 	ifilbuf(  	AlienImage_SGIRGBFileHeader *);

static int ifilbuf( AlienImage_SGIRGBFileHeader *image )
{
	int size;

	if ((image->flags&_IOREAD) == 0)
		return(EOF);
	if (image->base==NULL) {
		size = IBUFSIZE(image->xsize);
		if ((image->base = ibufalloc(image)) == NULL) {
			i_errhdlr("can't alloc image buffer\n");
			return EOF;
		}
	}
	image->cnt = getrow( File, image,image->base,image->y,image->z);
	image->ptr = image->base;
	if (--image->cnt < 0) {
		if (image->cnt == -1) {
			image->flags |= _IOEOF;
			if (image->flags & _IORW)
				image->flags &= ~_IOREAD;
		} else
			image->flags |= _IOERR;
		image->cnt = 0;
		return -1;
	}
	if(++image->y >= (short)image->ysize) {
	    image->y = 0;
	    if(++image->z >= (short)image->zsize) {
		image->z = image->zsize-1;
		image->flags |= _IOEOF;
		return -1;
	    }
	}
	return *image->ptr++ & 0xffff;
}
/*
 *	iflsbuf -
 *
 *
 */
static int 	iflsbuf(  	OSD_File&,
				AlienImage_SGIRGBFileHeader *,unsigned int  );

static int iflsbuf( OSD_File& File,
		    AlienImage_SGIRGBFileHeader *image,unsigned int c )
{
	register unsigned short *base;
	register n, rn;
	int size;

	if ((image->flags&_IOWRT)==0)
		return(EOF);
	if ((base=image->base)==NULL) {
		size = IBUFSIZE(image->xsize);
		if ((image->base=base=ibufalloc(image)) == NULL) {
			i_errhdlr("flsbuf: error on buf alloc\n");
			return EOF;
		}
		rn = n = 0;
	} else if ((rn = n = image->ptr - base) > 0)  {
			n = putrow( File, image,base,image->y,image->z);
			if(++image->y >= ( short )image->ysize) {
			    image->y = 0;
			    if(++image->z >= ( short )image->zsize) {
				image->z = image->zsize-1;
				image->flags |= _IOEOF;
				return -1;
			    }
			}
 	}
	image->cnt = image->xsize-1;
	*base++ = c;
	image->ptr = base;
	if (rn != n) {
		image->flags |= _IOERR;
		return(EOF);
	}
	return(c);
}
/*
 *	getpix and putpix -
 *
 *
 */

#undef getpix
#undef putpix

int getpix( AlienImage_SGIRGBFileHeader *image )
{
    if(--(image)->cnt>=0)
    	return *(image)->ptr++;
    else
	return ifilbuf(image);
}

int putpix( AlienImage_SGIRGBFileHeader *image,
		unsigned int pix )
{
    if(--(image)->cnt>=0)
        return *(image)->ptr++ = pix;
    else
	return iflsbuf( File, (image,pix);
}
#endif
//------------------------------------------------------------------------------
//		Public Method
//------------------------------------------------------------------------------

AlienImage_SGIRGBAlienData::AlienImage_SGIRGBAlienData()

{ 
  myRedData = myGreenData = myBlueData = NULL ;
}

void AlienImage_SGIRGBAlienData::SetName( const TCollection_AsciiString& aName)

{ myName = aName + TCollection_AsciiString( "\0" ) ;
}

const TCollection_AsciiString&  AlienImage_SGIRGBAlienData::Name() const

{ return ( myName ) ; }

void AlienImage_SGIRGBAlienData::Clear()

{ myName.Clear() ;

  if ( myRedData ) {
	//Free all allocated memory
	Standard::Free( myRedData) ;
	myRedData   = NULL ;
  }
  if ( myGreenData ) {
	//Free all allocated memory
	Standard::Free( myGreenData) ;
	myRedData   = NULL ;
  }
  if ( myBlueData ) {
	//Free all allocated memory
	Standard::Free( myBlueData) ;
	myRedData   = NULL ;
  }

  myHeader.xsize = myHeader.ysize = myHeader.zsize = 0 ;
}


Standard_Boolean AlienImage_SGIRGBAlienData::Read( OSD_File& file )

{  Standard_Boolean Success = iopen(	file,
					&myHeader,
					'r' ,
					0 ,
					0 ,
					0 ,
					0 ,
					0 );

  unsigned short *rbuf, *gbuf, *bbuf ;
  unsigned short y ;

  if ( Success ) {
#ifdef TRACE
  if ( Verbose ) {
    /* print a little info about the image */
    printf("Image x and y size in pixels: %d %d\n",
			myHeader.xsize,myHeader.ysize);
    printf("Image zsize in channels: %d\n",myHeader.zsize);
    printf("Image pixel min and max: %d %d\n",myHeader.min,myHeader.max);
  }
#endif

    myName = TCollection_AsciiString( myHeader.name ) ;

    /* allocate buffers for image data */
    if ( DataSize() ) {
	myRedData = Standard::Allocate( DataSize() ) ;
    }

    if(myHeader.zsize >= 3 && DataSize()) {/*if the image has alpha zsize is 4*/
	myGreenData = Standard::Allocate( DataSize() ) ;
	myBlueData = Standard::Allocate( DataSize() ) ;
    }

    rbuf = ( unsigned short * ) myRedData ;
    gbuf = ( unsigned short * ) myGreenData ;
    bbuf = ( unsigned short * ) myBlueData ;

    /* check to see if the image is B/W or RGB */
    if(myHeader.zsize == 1) {
#ifdef TRACE
  if ( Verbose ) printf("This is a black and write image\n");
#endif
        for(y=0; y<myHeader.ysize; y++) {
            getrow(file, &myHeader,rbuf,y,0); rbuf += myHeader.xsize ;
        }
    } else if(myHeader.zsize >= 3) {  /* if the image has alpha zsize is 4 */
#ifdef TRACE
   if ( Verbose )       printf("This is a rgb image\n");
#endif
        for(y=0; y<myHeader.ysize; y++) {
            	getrow(file, &myHeader,rbuf,myHeader.ysize-1-y,0); 
		rbuf += myHeader.xsize ;
            	getrow(file, &myHeader,gbuf,myHeader.ysize-1-y,1); 
		gbuf += myHeader.xsize ;
            	getrow(file, &myHeader,bbuf,myHeader.ysize-1-y,2); 
		bbuf += myHeader.xsize ;
        }
    }
  }

  return Success ;

  
}

Standard_Boolean AlienImage_SGIRGBAlienData::Write( OSD_File& file ) const

{ unsigned short *rbuf, *gbuf, *bbuf ;
  unsigned short y;
  unsigned int xsize,ysize,zsize ;
  
  AlienImage_SGIRGBFileHeader WritedHeader ;

  xsize = myHeader.xsize ;
  ysize = myHeader.ysize ;
  zsize = myHeader.zsize ;

  Standard_Boolean Success = iopen(	file,
					&WritedHeader,
					'w' ,
				 	RLE(1),
					3,
					xsize,
					ysize,
					zsize);

  if ( Success ) {
#ifdef TRACE
  if ( Verbose ) {
    /* print a little info about the image */
    printf("Image x and y size in pixels: %d %d\n",
			WritedHeader.xsize,WritedHeader.ysize);
    printf("Image zsize in channels: %d\n",WritedHeader.zsize);
    printf("Image pixel min and max: %d %d\n",
			WritedHeader.min,WritedHeader.max);
  }
#endif

    isetname( &WritedHeader, (Standard_PCharacter)myName.ToCString() ) ;

    rbuf = ( unsigned short * ) myRedData ;
    gbuf = ( unsigned short * ) myGreenData ;
    bbuf = ( unsigned short * ) myBlueData ;

    /* check to see if the image is B/W or RGB */
    if(WritedHeader.zsize == 1) {
        for(y=0; y<WritedHeader.ysize; y++) {
            	putrow(file, &WritedHeader,rbuf,WritedHeader.ysize-1-y,0); 
		rbuf += WritedHeader.xsize ;
        }
    } else if(WritedHeader.zsize >= 3) { /* if the image has alpha zsize is 4 */
        for(y=0; y<WritedHeader.ysize; y++) {
            	putrow(file, &WritedHeader,rbuf,WritedHeader.ysize-1-y,0); 
		rbuf += WritedHeader.xsize ;
            	putrow(file, &WritedHeader,gbuf,WritedHeader.ysize-1-y,1); 
		gbuf += WritedHeader.xsize ;
            	putrow(file, &WritedHeader,bbuf,WritedHeader.ysize-1-y,2); 
		bbuf += WritedHeader.xsize ;
        }
    }
  }

  iclose( file, &WritedHeader ) ;

  return Success ;

}

Handle_Image_Image AlienImage_SGIRGBAlienData::ToImage() const

{   if ( myHeader.zsize == 1 ) {
#ifdef TRACE
  if ( Verbose ) printf("This is a gray scale image\n");
#endif
	return( ToPseudoColorImage() ) ;
    }
    else if ( myHeader.zsize >= 3 ) {
#ifdef TRACE
  if ( Verbose ) printf("This is a rgb image\n");
#endif
	return( ToColorImage() ) ;
    }
    else {
  	 Standard_TypeMismatch_Raise_if( Standard_True,
	 "Attempt to convert a SGIRGBAlienData to a unknown Image_Image type");

	return( NULL ) ;
    }
}


void AlienImage_SGIRGBAlienData::FromImage(const Handle(Image_Image)& anImage )

{ unsigned short x,y ;
  unsigned short *rbuf, *gbuf, *bbuf ;
  Standard_Real r,g,b ;
  Standard_Integer LowX = anImage->LowerX() ;
  Standard_Integer LowY = anImage->LowerY() ;

  myHeader.xsize = (unsigned short)anImage->Width();
  myHeader.ysize = (unsigned short)anImage->Height();
  myHeader.zsize = 3;

  /* allocate buffers for image data */
  if ( DataSize() ) {
	myRedData   = Standard::Allocate( DataSize() ) ;
	myGreenData = Standard::Allocate( DataSize() ) ;
	myBlueData  = Standard::Allocate( DataSize() ) ;
  }

  rbuf = ( unsigned short * ) myRedData ;
  gbuf = ( unsigned short * ) myGreenData ;
  bbuf = ( unsigned short * ) myBlueData ;

  for(y=0; y<myHeader.ysize; y++) {
	for(x=0; x<myHeader.xsize; x++, rbuf++, gbuf++, bbuf++) {
	  (anImage->PixelColor( LowX+x,LowY+y )).Values(r,g,b,Quantity_TOC_RGB);
	  *rbuf = (unsigned short) (r*255.+0.5) ;
	  *gbuf = (unsigned short) (g*255.+0.5) ;
	  *bbuf = (unsigned short) (b*255.+0.5) ;
	}
  }
}