0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / AlienImage / AlienImage_X11XWDAlienData.cxx
1 #define PRO9517 //GG_010997
2 //              1) Le swap ne fonctionne pas correctement sur DEC
3 //              car X11XColor.pixel est int et non long (::Read(),::Write()).
4 //              2) Initialiser la table des couleurs avant d'ecrire
5 //              le fichier temporaire (::Write())
6
7 #define K4      //GG_110398
8 //              Ne pas initialiser una AsciiString avec '\0' dans le
9 //              constructeur de AlienImage_X11XWDAlienData sinon RAISE !
10
11 #define TEST    //GG_140699
12 //              Check file extension, must be ".xwd".
13
14 #ifndef WNT
15 # include <X11/Xlib.h>
16 #endif  // WNT
17 #include <Aspect_GenericColorMap.hxx>
18 #include <Image_PseudoColorImage.hxx>
19 #include <AlienImage_MemoryOperations.hxx>
20 #include <AlienImage_X11XColor.hxx>
21 #include <AlienImage_X11XWDAlienData.ixx>
22 #include <Aspect_ColorMapEntry.hxx>
23 #include <Standard.hxx>
24
25 #ifdef TRACE
26 static int Verbose = 0 ;
27 #endif
28
29
30
31 AlienImage_X11XWDAlienData::AlienImage_X11XWDAlienData()
32
33 { myData   = NULL ;
34   myColors = NULL ;
35 }
36
37 void AlienImage_X11XWDAlienData::SetName( const TCollection_AsciiString& aName)
38
39 { myName = aName + TCollection_AsciiString( "\0" ) ;
40   myHeader.header_size = sizeof( myHeader ) + myName.Length() ;
41 }
42
43 const TCollection_AsciiString&  AlienImage_X11XWDAlienData::Name() const
44
45 { return ( myName ) ; }
46
47 void AlienImage_X11XWDAlienData::Clear()
48
49 { Standard_Integer size ;
50
51   myHeader.header_size  = sizeof( myHeader ) ; 
52                                 /* Size of the entire file header (bytes).*/
53   myHeader.file_version = 0 ;   /* XWD_FILE_VERSION */
54   myHeader.pixmap_format= 0;    /* Pixmap format */
55   myHeader.pixmap_depth = 0 ;   /* Pixmap depth */
56   myHeader.pixmap_width = 0 ;   /* Pixmap width */
57   myHeader.pixmap_height= 0 ;   /* Pixmap height */
58   myHeader.xoffset      = 0 ;   /* Bitmap x offset */
59   myHeader.byte_order   = 0;    /* MSBFirst, LSBFirst */
60   myHeader.bitmap_unit  = 0 ;   /* Bitmap unit */
61   myHeader.bitmap_bit_order = 0;        /* MSBFirst, LSBFirst */
62   myHeader.bitmap_pad   = 0 ;   /* Bitmap scanline pad */
63   myHeader.bits_per_pixel = 0 ; /* Bits per pixel */
64   myHeader.bytes_per_line =0 ;  /* Bytes per scanline */
65   myHeader.visual_class = 0 ;   /* Class of colormap */
66   myHeader.red_mask     = 0 ;   /* Z red mask */
67   myHeader.green_mask   = 0 ;   /* Z green mask */
68   myHeader.blue_mask    = 0 ;   /* Z blue mask */
69   myHeader.bits_per_rgb = 0 ;   /* Log2 of distinct color values */
70   myHeader.colormap_entries = 0 ;       /* Number of entries in colormap */
71   myHeader.ncolors      = 0 ;   /* Number of Color structures */
72   myHeader.window_width = 0 ;   /* Window width */
73   myHeader.window_height= 0 ;   /* Window height */
74   myHeader.window_x     = 0 ;   /* Window upper left X coordinate */
75   myHeader.window_y     = 0 ;   /* Window upper left Y coordinate */
76   myHeader.window_bdrwidth =0 ; /* Window border width */
77   
78   myName.Clear() ;
79
80   if ( myData ) {
81         //Free all allocated memory
82         Standard::Free(myData) ;
83         myData   = NULL ;
84   }
85
86   if ( myColors ) {
87         size = ( Standard_Integer ) 
88                         myHeader.ncolors * sizeof( AlienImage_X11XColor ) ;
89         //Free all allocated memory
90         Standard::Free(myColors);
91         myColors = NULL ;
92   }
93
94 }
95
96 Standard_Boolean AlienImage_X11XWDAlienData::Write( OSD_File& file ) const
97
98 { Standard_Integer  size, i ;
99   unsigned long swaptest = 1;
100   AlienImage_X11XWDFileHeader TheHeader = myHeader ;
101
102   if ( myData == NULL )         return( Standard_False ) ;
103   if ( TheHeader.ncolors && 
104        myColors == NULL )       return( Standard_False ) ;
105
106   
107   // Write out TheHeader information
108
109   size = ( Standard_Integer ) TheHeader.header_size - sizeof( TheHeader ) ;
110
111   if ( size ) {
112         // Add '\0' at the end of name
113         TheHeader.header_size++ ;
114   }
115
116   if (*(char *) &swaptest) {
117     AlienImage_X11XWDFileHeader SwapHeader = TheHeader ;
118
119     AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &SwapHeader, 
120                                            sizeof(SwapHeader));
121     file.Write( &SwapHeader ,  sizeof( SwapHeader ) ) ;
122   }
123   else {
124     const Standard_Address pHeader = ( Standard_Address ) &TheHeader ;
125
126     file.Write( pHeader,  sizeof( TheHeader ) ) ;
127   }
128
129   if ( file.Failed() ) {
130         // ERROR
131         file.Seek( 0, OSD_FromBeginning ) ;
132         return( Standard_False ) ;
133   }
134
135   size = ( Standard_Integer ) TheHeader.header_size - sizeof( TheHeader ) ;
136
137   if ( size ) {
138         char end = '\0' ;
139         Standard_Address pend = Standard_Address( &end ) ;
140
141         file.Write( myName, myName.Length() ) ;
142
143         file.Write( pend, 1 ) ;
144   }
145
146   if ( file.Failed() ) {
147         // ERROR
148         file.Seek( 0, OSD_FromBeginning ) ;
149         return( Standard_False ) ;
150   }
151
152   // write out the color map buffer
153
154   if ( TheHeader.ncolors ) {
155         size = ( Standard_Integer ) TheHeader.ncolors 
156                                         * sizeof( AlienImage_X11XColor ) ;
157
158         if (*(char *) &swaptest) {
159           Standard_Address palloc =  
160                         Standard::Allocate( size ) ;
161 #ifdef PRO9517
162           const AlienImage_X11XColor *p = ( AlienImage_X11XColor * ) myColors ;
163           AlienImage_X11XColor *pp = ( AlienImage_X11XColor * ) palloc ;
164
165           for (i = 0 ; (unsigned int ) i < TheHeader.ncolors; i++,p++,pp++) {
166             pp->pixel = p->pixel;
167             pp->red = p->red;
168             pp->green = p->green;
169             pp->blue = p->blue;
170             pp->flags = p->flags;
171             AlienImage_MemoryOperations::SwapLong (
172                         (Standard_Address) &(pp->pixel), sizeof(int));
173             AlienImage_MemoryOperations::SwapShort(
174                         (Standard_Address) &(pp->red)  , 3 * sizeof(short));
175           }
176 #else
177           const AlienImage_X11XColor *p = ( AlienImage_X11XColor * ) palloc ;
178
179           for (i = 0 ; i < TheHeader.ncolors; i++,p++) {
180                   AlienImage_MemoryOperations::SwapLong (
181                         (Standard_Address) &(p->pixel), sizeof(long));
182                   AlienImage_MemoryOperations::SwapShort(
183                         (Standard_Address) &(p->red)  , 3 * sizeof(short));
184           }
185 #endif
186                 
187           file.Write( palloc, size ) ;
188
189           //Free all allocated memory
190           Standard::Free(palloc) ;
191         }
192         else {
193                 file.Write( myColors, size ) ;
194         }
195
196         if ( file.Failed() ) {
197                 // ERROR
198                 file.Seek( 0, OSD_FromBeginning ) ;
199                 return( Standard_False ) ;
200         }
201
202   }
203
204   if ( DataSize() ) {
205         file.Write( myData, DataSize() ) ;
206
207         if ( file.Failed() ) {
208                 // ERROR
209                 file.Seek( 0, OSD_FromBeginning ) ;
210                 return( Standard_False ) ;
211         }
212
213   }
214
215   return( Standard_True ) ;
216   
217 }
218
219
220 Standard_Boolean AlienImage_X11XWDAlienData::Read( OSD_File& file )
221
222 { Standard_Integer bblcount, i, size ;
223   unsigned long swaptest = 1;
224   Standard_Address pheader = ( Standard_Address ) &myHeader ;
225
226 #ifdef TEST
227   OSD_Path path; file.Path(path); 
228   TCollection_AsciiString ext = path.Extension(); ext.LowerCase();
229   if( ext != ".xwd" ) {
230     TCollection_AsciiString sysname; path.SystemName(sysname);
231 #ifdef TRACE
232     cout << " *** AlienImage_X11XWDAlienData::Read('" << sysname << "'). must have an '.xwd' extension" << endl;
233 #endif
234     return Standard_False;
235   }
236 #endif
237
238   // Read in myHeader information
239
240   file.Read( pheader, sizeof( myHeader ), bblcount ) ;
241
242   if ( file.Failed() || ( bblcount != sizeof( myHeader ) ) ) {
243         // ERROR
244         file.Seek( 0, OSD_FromBeginning ) ;
245         return( Standard_False ) ;
246   }
247
248   if (*(char *) &swaptest) 
249     AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &myHeader, 
250                                            sizeof(myHeader));
251
252   // check to see if the dump file is in the proper format */
253   if (myHeader.file_version != XWD_FILE_VERSION) {
254         // ERROR "XWD file format version mismatch."
255
256         if (*(char *) &swaptest) 
257           AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &myHeader,
258                                                   sizeof(myHeader));
259
260         if (myHeader.file_version != XWD_FILE_VERSION) {
261                 // ERROR "XWD file format version mismatch."
262                 file.Seek( 0, OSD_FromBeginning ) ;
263                 return( Standard_False ) ;
264         }
265         else {
266                 // Data come from a swaped computer ??
267                 swaptest = 0 ;
268         }
269   }
270
271   if (myHeader.header_size < sizeof(myHeader)) {
272         // ERROR "XWD header size is too small."
273         file.Seek( 0, OSD_FromBeginning ) ;
274         return( Standard_False ) ;
275   }
276
277 #ifdef TRACE
278   if ( Verbose ) cout << myHeader << endl << flush ;
279 #endif
280
281   // read in window name
282
283   size = ( Standard_Integer ) myHeader.header_size - sizeof( myHeader ) ;
284
285   if ( size > 0 ) {
286 #ifdef K4
287     TCollection_AsciiString name( bblcount ) ;
288 #else
289     TCollection_AsciiString name( bblcount , '\0') ;
290 #endif
291
292     file.Read( name, size ) ; bblcount = name.Length() ;
293 #ifdef WNT
294     --size;
295 #endif  // WNT
296
297     if ( file.Failed() || ( bblcount != size ) ) {
298         // ERROR
299         file.Seek( 0, OSD_FromBeginning ) ;
300         return( Standard_False ) ;
301     }
302
303     myName = name ;
304
305 #ifdef TRACE
306     if ( Verbose ) cout << myName << endl << flush ;
307 #endif
308
309   }
310
311   // read in the color map buffer
312
313   if ( myHeader.ncolors ) {
314         size = ( Standard_Integer ) myHeader.ncolors 
315                         * sizeof( AlienImage_X11XColor ) ;
316
317         myColors = Standard::Allocate( size ) ;
318
319         file.Read( myColors, size, bblcount ) ;
320
321         if ( file.Failed() || ( bblcount != size ) ) {
322                 // ERROR
323                 file.Seek( 0, OSD_FromBeginning ) ;
324                 return( Standard_False ) ;
325         }
326
327         if ( *(char *) &swaptest ) {
328                 AlienImage_X11XColor *p  
329                                 = ( AlienImage_X11XColor * )myColors ;
330                 for (i = 0 ; (unsigned int ) i < myHeader.ncolors; i++,p++) {
331 #ifdef PRO9517
332                   AlienImage_MemoryOperations::SwapLong (
333                         (Standard_Address) &(p->pixel), sizeof(int));
334 #else
335                   AlienImage_MemoryOperations::SwapLong (
336                         (Standard_Address) &(p->pixel), sizeof(long));
337 #endif
338                   AlienImage_MemoryOperations::SwapShort(
339                         (Standard_Address) &(p->red)  , 3 * sizeof(short));
340                 }
341         }
342
343 #ifdef TRACE
344         if ( Verbose )  { 
345           AlienImage_X11XColor *p = ( AlienImage_X11XColor * )myColors;
346
347           for (i = 0 ; i < myHeader.ncolors; i++,p++) {
348                 cout << *p << endl << flush ;
349           }
350
351         }
352 #endif
353   }
354
355   if ( DataSize() ) {
356         myData = Standard::Allocate( DataSize() ) ;
357
358         file.Read( myData, DataSize(), bblcount ) ;
359
360         if ( file.Failed() || ( bblcount != DataSize() ) ) {
361                 // ERROR
362                 file.Seek( 0, OSD_FromBeginning ) ;
363                 return( Standard_False ) ;
364         }
365   }
366
367   return( Standard_True ) ;
368   
369 }
370
371 Handle_Image_Image AlienImage_X11XWDAlienData::ToImage() const
372
373 {   if ( myHeader.pixmap_depth <= 8 && 
374        myHeader.ncolors &&
375        myHeader.pixmap_format == ZPixmap) {
376         return( ToPseudoColorImage() ) ;
377     }
378     else if ( myHeader.visual_class  == TrueColor && 
379               myHeader.pixmap_format == ZPixmap) {
380         return( ToColorImage() ) ;
381     }
382     else {
383          Standard_TypeMismatch_Raise_if( Standard_True,
384          "Attempt to convert a X11XWDAlienData to a unknown Image_Image type");
385
386         return( NULL ) ;
387     }
388 }
389
390 void AlienImage_X11XWDAlienData::FromImage( const Handle_Image_Image& anImage )
391
392 { if ( anImage->Type() == Image_TOI_PseudoColorImage ) {
393         Handle(Image_PseudoColorImage) aPImage =
394                 Handle(Image_PseudoColorImage)::DownCast(anImage) ;
395
396         FromPseudoColorImage( aPImage ) ;
397   }
398   else if ( anImage->Type() == Image_TOI_ColorImage )  {
399         Handle(Image_ColorImage) aCImage =
400                 Handle(Image_ColorImage)::DownCast(anImage) ;
401
402         FromColorImage( aCImage ) ;
403   }
404   else {
405          Standard_TypeMismatch_Raise_if( Standard_True,
406          "Attempt to convert a unknown Image_Image type to a X11XWDAlienData");
407   }
408 }
409
410 //------------------------------------------------------------------------------
411 //              Private Method
412 //------------------------------------------------------------------------------
413
414
415 void AlienImage_X11XWDAlienData::FromPseudoColorImage(
416                         const Handle_Image_PseudoColorImage& anImage)
417
418 { Standard_Integer size, i ;
419
420   /* Size of the entire file header (bytes).*/
421   myHeader.header_size      = sizeof(myHeader) + myName.Length() ;
422   myHeader.file_version     = XWD_FILE_VERSION ;        /* XWD_FILE_VERSION */
423   myHeader.pixmap_format    = ZPixmap ;         /* Pixmap format */
424   myHeader.pixmap_depth     = 8 ;               /* Pixmap depth */
425   myHeader.pixmap_width     = anImage->Width() ;/* Pixmap width */
426   myHeader.pixmap_height    = anImage->Height() ;/* Pixmap height */
427   myHeader.xoffset          = 0 ;               /* Bitmap x offset */
428   myHeader.byte_order       = MSBFirst;         /* MSBFirst, LSBFirst */
429   myHeader.bitmap_unit      = 32 ;              /* Bitmap unit */
430   myHeader.bitmap_bit_order = MSBFirst;         /* MSBFirst, LSBFirst */
431   myHeader.bitmap_pad       = 32 ;              /* Bitmap scanline pad */
432   myHeader.bits_per_pixel   = 8 ;               /* Bits per pixel */
433   /* Bytes per scanline */
434   size = ( Standard_Integer ) ( anImage->Width() * myHeader.bits_per_pixel );
435
436   myHeader.bytes_per_line = size / myHeader.bitmap_unit ;
437   if ( size %  myHeader.bitmap_pad ) myHeader.bytes_per_line += 1 ;
438
439   myHeader.bytes_per_line *= ( myHeader.bitmap_unit / 8 ) ;
440
441   myHeader.visual_class     = PseudoColor ;     /* Class of colormap */
442   myHeader.red_mask         = 0 ;               /* Z red mask */
443   myHeader.green_mask       = 0 ;               /* Z green mask */
444   myHeader.blue_mask        = 0 ;               /* Z blue mask */
445   myHeader.bits_per_rgb     = 8 ;       /* Log2 of distinct color values */
446   myHeader.colormap_entries = 256 ;     /* Number of entries in colormap */
447   myHeader.ncolors          = (anImage->ColorMap())->Size() ;
448                                         /* Number of Color structures */
449   myHeader.window_width     = anImage->Width() ;        /* Window width */
450   myHeader.window_height    = anImage->Height() ;       /* Window height */
451   myHeader.window_x         = 0 ;       /* Window upper left X coordinate */
452   myHeader.window_y         = 0 ;       /* Window upper left Y coordinate */
453   myHeader.window_bdrwidth  = 0 ;       /* Window border width */
454
455   size = ( Standard_Integer ) myHeader.ncolors 
456                                 * sizeof( AlienImage_X11XColor ) ;
457
458   myColors = Standard::Allocate( size ) ;
459
460   AlienImage_X11XColor *p = ( AlienImage_X11XColor * )myColors ;
461   Aspect_ColorMapEntry  aCentry ;
462
463   for ( i = 1 ; (unsigned int ) i <= myHeader.ncolors ; i++, p++ ) {
464         p->pixel = 0 ;
465         p->red   = p->green = p->blue = 0 ;
466         p->flags = 0 ;
467
468         aCentry = (anImage->ColorMap())->Entry( i ) ;
469
470         if ( aCentry.IsAllocated() == Standard_True ) {
471                 p->flags = DoRed | DoGreen | DoBlue ;
472                 p->pixel = aCentry.Index() ;
473                 p->red   = ( unsigned short )
474                                 ( (aCentry.Color()).Red()   * 0xffff + 0.5 ) ;
475                 p->green = ( unsigned short )
476                                 ( (aCentry.Color()).Green() * 0xffff + 0.5 ) ;
477                 p->blue  = ( unsigned short )
478                                 ( (aCentry.Color()).Blue()  * 0xffff + 0.5 ) ;
479         }
480   }
481
482   if ( anImage->Size() ) {
483         Standard_Integer x, y ;
484         
485         myData = Standard::Allocate( DataSize() ) ;
486
487         for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
488           for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
489                 SetPixel( x, y, 
490                           ( anImage->Pixel( anImage->LowerX()+x , 
491                                             anImage->LowerY()+y )).Value() ) ;
492           }
493         }
494
495   }
496 }
497
498 void AlienImage_X11XWDAlienData::FromColorImage(
499                         const Handle_Image_ColorImage& anImage)
500
501 { long int size ;;
502
503   /* Size of the entire file header (bytes).*/
504   myHeader.header_size      = sizeof(myHeader) + myName.Length() ;
505   myHeader.file_version     = XWD_FILE_VERSION ;        /* XWD_FILE_VERSION */
506   myHeader.pixmap_format    = ZPixmap ;         /* Pixmap format */
507   myHeader.pixmap_depth     = 24 ;              /* Pixmap depth */
508   myHeader.pixmap_width     = anImage->Width() ;/* Pixmap width */
509   myHeader.pixmap_height    = anImage->Height() ;/* Pixmap height */
510   myHeader.xoffset          = 0 ;               /* Bitmap x offset */
511   myHeader.byte_order       = MSBFirst;         /* MSBFirst, LSBFirst */
512   myHeader.bitmap_unit      = 32 ;              /* Bitmap unit */
513   myHeader.bitmap_bit_order = MSBFirst;         /* MSBFirst, LSBFirst */
514   myHeader.bitmap_pad       = 32 ;              /* Bitmap scanline pad */
515   myHeader.bits_per_pixel   = 32 ;              /* Bits per pixel */
516   /* Bytes per scanline */
517   size = anImage->Width() * myHeader.bits_per_pixel ;
518
519   myHeader.bytes_per_line = size / myHeader.bitmap_unit ;
520   if ( size %  myHeader.bitmap_pad ) myHeader.bytes_per_line += 1 ;
521
522   myHeader.bytes_per_line *= ( myHeader.bitmap_unit / 8 ) ;
523
524   myHeader.visual_class     = TrueColor ;       /* Class of colormap */
525   myHeader.red_mask         = 0xff ;            /* Z red mask */
526   myHeader.green_mask       = 0xff00 ;          /* Z green mask */
527   myHeader.blue_mask        = 0xff0000 ;        /* Z blue mask */
528   myHeader.bits_per_rgb     = 8 ;       /* Log2 of distinct color values */
529   myHeader.colormap_entries = 256 ;     /* Number of entries in colormap */
530   myHeader.ncolors          = 0 ;       /* Number of Color structures */
531   myHeader.window_width     = anImage->Width() ;        /* Window width */
532   myHeader.window_height    = anImage->Height() ;       /* Window height */
533   myHeader.window_x         = 0 ;       /* Window upper left X coordinate */
534   myHeader.window_y         = 0 ;       /* Window upper left Y coordinate */
535   myHeader.window_bdrwidth  = 0 ;       /* Window border width */
536
537
538   myColors = NULL ;
539
540   if ( anImage->Size() ) {
541         Standard_Integer x, y, pix, c ;
542         const Standard_Integer rs = RedShift() ;
543         const Standard_Integer gs = GreenShift() ;
544         const Standard_Integer bs = BlueShift() ;
545         const Standard_Integer ColorRange = 
546                         Standard_Integer( ( 1 << myHeader.bits_per_rgb ) - 1 );
547         Quantity_Color col ;
548         
549         myData = Standard::Allocate( DataSize() ) ;
550
551         for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
552           for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
553                 col = anImage->Pixel( anImage->LowerX()+x , 
554                                         anImage->LowerY()+y ).Value() ;
555
556                 pix = 0 ;
557
558                 c = ( Standard_Integer ) ( col.Red() * ColorRange + 0.5 );
559                 c = ( Standard_Integer ) (( c << rs ) & myHeader.red_mask) ;
560                 pix |= c ;
561                 
562                 c = ( Standard_Integer ) ( col.Green() * ColorRange + 0.5 );
563                 c = ( Standard_Integer ) (( c << gs ) & myHeader.green_mask) ;
564                 pix |= c ;
565                 
566                 c = ( Standard_Integer ) ( col.Blue() * ColorRange + 0.5 );
567                 c = ( Standard_Integer ) (( c << bs ) & myHeader.blue_mask) ;
568                 pix |= c ;
569                 
570                 SetPixel( x, y, pix ) ;
571           }
572         }
573
574   }
575 }
576
577 Handle_Image_ColorImage AlienImage_X11XWDAlienData::ToColorImage() const
578
579 { Aspect_ColorPixel CPixel ;
580   Quantity_Color    acolor ;
581   Handle(Image_ColorImage) ret_image = NULL ;
582   Standard_Integer pix,x,y ;
583   Standard_Real r,g,b, maxcol ;
584
585   if ( myHeader.visual_class  == TrueColor && 
586        myHeader.pixmap_format == ZPixmap) {
587     ret_image = new Image_ColorImage( 0,0,
588                         (Standard_Integer)myHeader.pixmap_width,
589                         (Standard_Integer)myHeader.pixmap_height ) ;
590
591     maxcol = ( 1 << myHeader.bits_per_rgb ) - 1 ;
592
593     for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
594       for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
595         pix = Pixel( x, y ) ;
596
597         r = ( ( pix & myHeader.red_mask )   >> RedShift() )    / maxcol ;
598         g = ( ( pix & myHeader.green_mask ) >> GreenShift() )  / maxcol ;
599         b = ( ( pix & myHeader.blue_mask )  >> BlueShift() )   / maxcol ;
600         
601         acolor.SetValues( r,g,b, Quantity_TOC_RGB ) ;
602         CPixel.SetValue ( acolor ) ;
603
604         ret_image->SetPixel( ret_image->LowerX()+x , 
605                              ret_image->LowerY()+y, CPixel ) ;
606       }
607     }
608         
609   }
610
611   return( ret_image ) ;
612 }
613
614 Handle_Image_PseudoColorImage AlienImage_X11XWDAlienData::ToPseudoColorImage() 
615                                                                 const
616
617 { Standard_Real r,g,b ;
618   Standard_Integer x, y ;
619   const Standard_Real XRange = Standard_Real( Standard_Integer(0xffff) );
620   const Standard_Integer newColorSize = 
621        Standard_Integer(myHeader.colormap_entries*sizeof(AlienImage_X11XColor));
622   Handle(Image_PseudoColorImage) ret_image = NULL ;
623
624   if ( myHeader.pixmap_depth <= 8 && 
625        myHeader.ncolors &&
626        myHeader.pixmap_format == ZPixmap) {     
627 //    unsigned long int i, j, ncol ;
628     unsigned long int i, ncol ;
629     Aspect_ColorMapEntry Centry ;
630     Quantity_Color       color ;
631     AlienImage_X11XColor *p ;
632     AlienImage_X11XColor *newColor ;
633     Standard_Address     palloc ;
634     Aspect_IndexPixel IPixel ;
635
636     palloc = Standard::Allocate( newColorSize ) ;
637
638     newColor = ( AlienImage_X11XColor * ) palloc ;
639
640     p = ( AlienImage_X11XColor * )myColors;
641
642     for ( i = 0 ; i < myHeader.ncolors ; i++, p++ )     newColor[p->pixel] = *p;
643     for ( i = 0 ; i < myHeader.colormap_entries ; i++ ) newColor[i].flags  = 0 ;
644
645     for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
646         for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
647                 newColor[ Pixel( x, y ) ].flags = DoRed | DoGreen | DoBlue ;
648         }
649     }
650
651     for ( i = ncol = 0 ; i < myHeader.colormap_entries ; i++ ) {
652         if ( newColor[i].flags ) ncol++ ;
653     }
654
655     Handle(Aspect_GenericColorMap) colormap = 
656         new Aspect_GenericColorMap();
657
658     for ( i = 0 ; i < myHeader.colormap_entries ; i++ ) {
659         if ( newColor[i].flags ) {
660                 r = ( Standard_Real ) newColor[i].red   / XRange ;
661                 g = ( Standard_Real ) newColor[i].green / XRange ;
662                 b = ( Standard_Real ) newColor[i].blue  / XRange ;
663                 color.SetValues( r,g,b, Quantity_TOC_RGB );
664                 Centry.SetValue( Standard_Integer(newColor[i].pixel), color ) ;
665                 colormap->AddEntry( Centry ) ;
666         }
667     }
668
669     ret_image = new Image_PseudoColorImage( 0,0,
670                                    Standard_Integer(myHeader.pixmap_width),
671                                    Standard_Integer(myHeader.pixmap_height),
672                                    colormap ) ;
673
674     for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
675       for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
676         IPixel.SetValue( Pixel( x, y ) ) ;
677         ret_image->SetPixel( ret_image->LowerX()+x , 
678                              ret_image->LowerY()+y, IPixel ) ;
679       }
680     }
681         
682
683     //Free all allocated memory
684     Standard::Free(palloc);
685   }
686
687   return ret_image ;
688
689 }
690
691 Standard_Integer AlienImage_X11XWDAlienData::DataSize() const
692
693
694     if ( myHeader.pixmap_format != ZPixmap)
695       return( myHeader.bytes_per_line * myHeader.pixmap_height 
696                                       * myHeader.pixmap_depth );
697
698     return( myHeader.bytes_per_line * myHeader.pixmap_height);
699 }
700
701 void AlienImage_X11XWDAlienData::SetPixel( 
702         const Standard_Integer x, 
703         const Standard_Integer y, 
704         const Standard_Integer value )
705
706 { unsigned char *p ;
707   unsigned long int pixel_size ;
708  
709   Standard_OutOfRange_Raise_if(
710         ( x < 0 || (unsigned int ) x >= myHeader.pixmap_width ||
711           y < 0 || (unsigned int ) y >= myHeader.pixmap_height ),
712          "Index out of range in X11XWDAlienData::Pixel");
713
714   pixel_size = ( unsigned long int ) myHeader.bytes_per_line / 
715                                      myHeader.pixmap_width;
716  
717   p  = ( unsigned char * ) myData ;
718   p += y * myHeader.bytes_per_line + x * pixel_size ;
719  
720   if ( pixel_size == 1 ) {
721         *p = ( unsigned char ) value ;
722   }
723   else if ( pixel_size == 2 ) {
724         *( ( unsigned short int * ) p ) = ( unsigned short int ) value ;
725   }
726   else if ( pixel_size == 4 ) {
727         *( ( unsigned long int * ) p )  = ( unsigned long int ) value ;
728   }
729 }
730
731 Standard_Integer AlienImage_X11XWDAlienData::Pixel( 
732         const Standard_Integer x, const Standard_Integer y ) const
733
734 { unsigned char *p ;
735   unsigned long int  pixel_size ;
736   unsigned long int pix ;
737   Standard_Integer ret ;
738
739   Standard_OutOfRange_Raise_if(
740         ( x < 0 || (unsigned int ) x >= myHeader.pixmap_width ||
741           y < 0 || (unsigned int ) y >= myHeader.pixmap_height ),
742          "Index out of range in X11XWDAlienData::Pixel");
743
744   pixel_size = ( unsigned long int ) myHeader.bytes_per_line / 
745                                      myHeader.pixmap_width;
746  
747   p  = ( unsigned char * ) myData ;
748   p  += y * myHeader.bytes_per_line + x * pixel_size ;
749  
750   if ( pixel_size == 1 ) {
751         pix = ( unsigned long int ) *p ;
752   }
753   else if ( pixel_size == 2 ) {
754         pix = *( ( unsigned short int * ) p ) ;
755   }
756   else {
757         pix = *( ( unsigned long int * ) p ) ;
758   }
759
760   ret = Standard_Integer ( pix );
761  
762   return( ret ) ;
763  
764 }
765
766 Standard_Integer AlienImage_X11XWDAlienData::RedShift() const
767
768 { Standard_Integer shift = 0 ;
769
770   Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
771           "Attempt to get RedShift from a non TrueColor X11XWDImage" ) ;
772
773   if ( ( myHeader.red_mask >> myHeader.bits_per_rgb ) == 0 ) {
774         shift = 0 ;
775   }
776   else if ( ( myHeader.red_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
777         shift = Standard_Integer( myHeader.bits_per_rgb ) ;
778   }
779   else {
780         shift = Standard_Integer( 2 * myHeader.bits_per_rgb );
781   }
782   return shift ;
783 }
784
785 Standard_Integer AlienImage_X11XWDAlienData::GreenShift() const
786
787 { Standard_Integer shift = 0 ;
788
789   Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
790           "Attempt to get GreenShift from a non TrueColor X11XWDImage" ) ;
791
792   if ( ( myHeader.green_mask >> myHeader.bits_per_rgb ) == 0 ) {
793         shift = 0 ;
794   }
795   else if ( ( myHeader.green_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
796         shift = Standard_Integer( myHeader.bits_per_rgb ) ;
797   }
798   else {
799         shift = Standard_Integer( 2 * myHeader.bits_per_rgb ) ;
800   }
801   return shift ;
802 }
803
804 Standard_Integer AlienImage_X11XWDAlienData::BlueShift() const 
805
806 { Standard_Integer shift = 0 ;
807
808   Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
809           "Attempt to get BlueShift from a non TrueColor X11XWDImage" ) ;
810
811   if ( ( myHeader.blue_mask >> myHeader.bits_per_rgb ) == 0 ) {
812         shift = 0 ;
813   }
814   else if ( ( myHeader.blue_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
815         shift = Standard_Integer( myHeader.bits_per_rgb ) ;
816   }
817   else {
818         shift = Standard_Integer( 2 * myHeader.bits_per_rgb ) ;
819   }
820
821   return shift ;
822 }
823