1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 #define PRO9517 //GG_010997
20 // 1) Le swap ne fonctionne pas correctement sur DEC
21 // car X11XColor.pixel est int et non long (::Read(),::Write()).
22 // 2) Initialiser la table des couleurs avant d'ecrire
23 // le fichier temporaire (::Write())
25 #define K4 //GG_110398
26 // Ne pas initialiser una AsciiString avec '\0' dans le
27 // constructeur de AlienImage_X11XWDAlienData sinon RAISE !
29 #define TEST //GG_140699
30 // Check file extension, must be ".xwd".
33 # include <X11/Xlib.h>
35 #include <Aspect_GenericColorMap.hxx>
36 #include <Image_PseudoColorImage.hxx>
37 #include <AlienImage_MemoryOperations.hxx>
38 #include <AlienImage_X11XColor.hxx>
39 #include <AlienImage_X11XWDAlienData.ixx>
40 #include <Aspect_ColorMapEntry.hxx>
41 #include <Standard.hxx>
44 static int Verbose = 0 ;
49 AlienImage_X11XWDAlienData::AlienImage_X11XWDAlienData()
55 void AlienImage_X11XWDAlienData::SetName( const TCollection_AsciiString& aName)
57 { myName = aName + TCollection_AsciiString( "\0" ) ;
58 myHeader.header_size = sizeof( myHeader ) + myName.Length() ;
61 const TCollection_AsciiString& AlienImage_X11XWDAlienData::Name() const
63 { return ( myName ) ; }
65 void AlienImage_X11XWDAlienData::Clear()
67 { Standard_Integer size ;
69 myHeader.header_size = sizeof( myHeader ) ;
70 /* Size of the entire file header (bytes).*/
71 myHeader.file_version = 0 ; /* XWD_FILE_VERSION */
72 myHeader.pixmap_format= 0; /* Pixmap format */
73 myHeader.pixmap_depth = 0 ; /* Pixmap depth */
74 myHeader.pixmap_width = 0 ; /* Pixmap width */
75 myHeader.pixmap_height= 0 ; /* Pixmap height */
76 myHeader.xoffset = 0 ; /* Bitmap x offset */
77 myHeader.byte_order = 0; /* MSBFirst, LSBFirst */
78 myHeader.bitmap_unit = 0 ; /* Bitmap unit */
79 myHeader.bitmap_bit_order = 0; /* MSBFirst, LSBFirst */
80 myHeader.bitmap_pad = 0 ; /* Bitmap scanline pad */
81 myHeader.bits_per_pixel = 0 ; /* Bits per pixel */
82 myHeader.bytes_per_line =0 ; /* Bytes per scanline */
83 myHeader.visual_class = 0 ; /* Class of colormap */
84 myHeader.red_mask = 0 ; /* Z red mask */
85 myHeader.green_mask = 0 ; /* Z green mask */
86 myHeader.blue_mask = 0 ; /* Z blue mask */
87 myHeader.bits_per_rgb = 0 ; /* Log2 of distinct color values */
88 myHeader.colormap_entries = 0 ; /* Number of entries in colormap */
89 myHeader.ncolors = 0 ; /* Number of Color structures */
90 myHeader.window_width = 0 ; /* Window width */
91 myHeader.window_height= 0 ; /* Window height */
92 myHeader.window_x = 0 ; /* Window upper left X coordinate */
93 myHeader.window_y = 0 ; /* Window upper left Y coordinate */
94 myHeader.window_bdrwidth =0 ; /* Window border width */
99 //Free all allocated memory
100 Standard::Free(myData) ;
105 size = ( Standard_Integer )
106 myHeader.ncolors * sizeof( AlienImage_X11XColor ) ;
107 //Free all allocated memory
108 Standard::Free(myColors);
114 Standard_Boolean AlienImage_X11XWDAlienData::Write( OSD_File& file ) const
116 { Standard_Integer size, i ;
117 unsigned long swaptest = 1;
118 AlienImage_X11XWDFileHeader TheHeader = myHeader ;
120 if ( myData == NULL ) return( Standard_False ) ;
121 if ( TheHeader.ncolors &&
122 myColors == NULL ) return( Standard_False ) ;
125 // Write out TheHeader information
127 size = ( Standard_Integer ) TheHeader.header_size - sizeof( TheHeader ) ;
130 // Add '\0' at the end of name
131 TheHeader.header_size++ ;
134 if (*(char *) &swaptest) {
135 AlienImage_X11XWDFileHeader SwapHeader = TheHeader ;
137 AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &SwapHeader,
139 file.Write( &SwapHeader , sizeof( SwapHeader ) ) ;
142 const Standard_Address pHeader = ( Standard_Address ) &TheHeader ;
144 file.Write( pHeader, sizeof( TheHeader ) ) ;
147 if ( file.Failed() ) {
149 file.Seek( 0, OSD_FromBeginning ) ;
150 return( Standard_False ) ;
153 size = ( Standard_Integer ) TheHeader.header_size - sizeof( TheHeader ) ;
157 Standard_Address pend = Standard_Address( &end ) ;
159 file.Write( myName, myName.Length() ) ;
161 file.Write( pend, 1 ) ;
164 if ( file.Failed() ) {
166 file.Seek( 0, OSD_FromBeginning ) ;
167 return( Standard_False ) ;
170 // write out the color map buffer
172 if ( TheHeader.ncolors ) {
173 size = ( Standard_Integer ) TheHeader.ncolors
174 * sizeof( AlienImage_X11XColor ) ;
176 if (*(char *) &swaptest) {
177 Standard_Address palloc =
178 Standard::Allocate( size ) ;
180 const AlienImage_X11XColor *p = ( AlienImage_X11XColor * ) myColors ;
181 AlienImage_X11XColor *pp = ( AlienImage_X11XColor * ) palloc ;
183 for (i = 0 ; (unsigned int ) i < TheHeader.ncolors; i++,p++,pp++) {
184 pp->pixel = p->pixel;
186 pp->green = p->green;
188 pp->flags = p->flags;
189 AlienImage_MemoryOperations::SwapLong (
190 (Standard_Address) &(pp->pixel), sizeof(int));
191 AlienImage_MemoryOperations::SwapShort(
192 (Standard_Address) &(pp->red) , 3 * sizeof(short));
195 const AlienImage_X11XColor *p = ( AlienImage_X11XColor * ) palloc ;
197 for (i = 0 ; i < TheHeader.ncolors; i++,p++) {
198 AlienImage_MemoryOperations::SwapLong (
199 (Standard_Address) &(p->pixel), sizeof(long));
200 AlienImage_MemoryOperations::SwapShort(
201 (Standard_Address) &(p->red) , 3 * sizeof(short));
205 file.Write( palloc, size ) ;
207 //Free all allocated memory
208 Standard::Free(palloc) ;
211 file.Write( myColors, size ) ;
214 if ( file.Failed() ) {
216 file.Seek( 0, OSD_FromBeginning ) ;
217 return( Standard_False ) ;
223 file.Write( myData, DataSize() ) ;
225 if ( file.Failed() ) {
227 file.Seek( 0, OSD_FromBeginning ) ;
228 return( Standard_False ) ;
233 return( Standard_True ) ;
238 Standard_Boolean AlienImage_X11XWDAlienData::Read( OSD_File& file )
240 { Standard_Integer bblcount, i, size ;
241 unsigned long swaptest = 1;
242 Standard_Address pheader = ( Standard_Address ) &myHeader ;
245 OSD_Path path; file.Path(path);
246 TCollection_AsciiString ext = path.Extension(); ext.LowerCase();
247 if( ext != ".xwd" ) {
248 TCollection_AsciiString sysname; path.SystemName(sysname);
250 cout << " *** AlienImage_X11XWDAlienData::Read('" << sysname << "'). must have an '.xwd' extension" << endl;
252 return Standard_False;
256 // Read in myHeader information
258 file.Read( pheader, sizeof( myHeader ), bblcount ) ;
260 if ( file.Failed() || ( bblcount != sizeof( myHeader ) ) ) {
262 file.Seek( 0, OSD_FromBeginning ) ;
263 return( Standard_False ) ;
266 if (*(char *) &swaptest)
267 AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &myHeader,
270 // check to see if the dump file is in the proper format */
271 if (myHeader.file_version != XWD_FILE_VERSION) {
272 // ERROR "XWD file format version mismatch."
274 if (*(char *) &swaptest)
275 AlienImage_MemoryOperations::SwapLong( ( Standard_Address ) &myHeader,
278 if (myHeader.file_version != XWD_FILE_VERSION) {
279 // ERROR "XWD file format version mismatch."
280 file.Seek( 0, OSD_FromBeginning ) ;
281 return( Standard_False ) ;
284 // Data come from a swaped computer ??
289 if (myHeader.header_size < sizeof(myHeader)) {
290 // ERROR "XWD header size is too small."
291 file.Seek( 0, OSD_FromBeginning ) ;
292 return( Standard_False ) ;
296 if ( Verbose ) cout << myHeader << endl << flush ;
299 // read in window name
301 size = ( Standard_Integer ) myHeader.header_size - sizeof( myHeader ) ;
305 TCollection_AsciiString name( bblcount ) ;
307 TCollection_AsciiString name( bblcount , '\0') ;
310 file.Read( name, size ) ; bblcount = name.Length() ;
315 if ( file.Failed() || ( bblcount != size ) ) {
317 file.Seek( 0, OSD_FromBeginning ) ;
318 return( Standard_False ) ;
324 if ( Verbose ) cout << myName << endl << flush ;
329 // read in the color map buffer
331 if ( myHeader.ncolors ) {
332 size = ( Standard_Integer ) myHeader.ncolors
333 * sizeof( AlienImage_X11XColor ) ;
335 myColors = Standard::Allocate( size ) ;
337 file.Read( myColors, size, bblcount ) ;
339 if ( file.Failed() || ( bblcount != size ) ) {
341 file.Seek( 0, OSD_FromBeginning ) ;
342 return( Standard_False ) ;
345 if ( *(char *) &swaptest ) {
346 AlienImage_X11XColor *p
347 = ( AlienImage_X11XColor * )myColors ;
348 for (i = 0 ; (unsigned int ) i < myHeader.ncolors; i++,p++) {
350 AlienImage_MemoryOperations::SwapLong (
351 (Standard_Address) &(p->pixel), sizeof(int));
353 AlienImage_MemoryOperations::SwapLong (
354 (Standard_Address) &(p->pixel), sizeof(long));
356 AlienImage_MemoryOperations::SwapShort(
357 (Standard_Address) &(p->red) , 3 * sizeof(short));
363 AlienImage_X11XColor *p = ( AlienImage_X11XColor * )myColors;
365 for (i = 0 ; i < myHeader.ncolors; i++,p++) {
366 cout << *p << endl << flush ;
374 myData = Standard::Allocate( DataSize() ) ;
376 file.Read( myData, DataSize(), bblcount ) ;
378 if ( file.Failed() || ( bblcount != DataSize() ) ) {
380 file.Seek( 0, OSD_FromBeginning ) ;
381 return( Standard_False ) ;
385 return( Standard_True ) ;
389 Handle_Image_Image AlienImage_X11XWDAlienData::ToImage() const
391 { if ( myHeader.pixmap_depth <= 8 &&
393 myHeader.pixmap_format == ZPixmap) {
394 return( ToPseudoColorImage() ) ;
396 else if ( myHeader.visual_class == TrueColor &&
397 myHeader.pixmap_format == ZPixmap) {
398 return( ToColorImage() ) ;
401 Standard_TypeMismatch_Raise_if( Standard_True,
402 "Attempt to convert a X11XWDAlienData to a unknown Image_Image type");
408 void AlienImage_X11XWDAlienData::FromImage( const Handle_Image_Image& anImage )
410 { if ( anImage->Type() == Image_TOI_PseudoColorImage ) {
411 Handle(Image_PseudoColorImage) aPImage =
412 Handle(Image_PseudoColorImage)::DownCast(anImage) ;
414 FromPseudoColorImage( aPImage ) ;
416 else if ( anImage->Type() == Image_TOI_ColorImage ) {
417 Handle(Image_ColorImage) aCImage =
418 Handle(Image_ColorImage)::DownCast(anImage) ;
420 FromColorImage( aCImage ) ;
423 Standard_TypeMismatch_Raise_if( Standard_True,
424 "Attempt to convert a unknown Image_Image type to a X11XWDAlienData");
428 //------------------------------------------------------------------------------
430 //------------------------------------------------------------------------------
433 void AlienImage_X11XWDAlienData::FromPseudoColorImage(
434 const Handle_Image_PseudoColorImage& anImage)
436 { Standard_Integer size, i ;
438 /* Size of the entire file header (bytes).*/
439 myHeader.header_size = sizeof(myHeader) + myName.Length() ;
440 myHeader.file_version = XWD_FILE_VERSION ; /* XWD_FILE_VERSION */
441 myHeader.pixmap_format = ZPixmap ; /* Pixmap format */
442 myHeader.pixmap_depth = 8 ; /* Pixmap depth */
443 myHeader.pixmap_width = anImage->Width() ;/* Pixmap width */
444 myHeader.pixmap_height = anImage->Height() ;/* Pixmap height */
445 myHeader.xoffset = 0 ; /* Bitmap x offset */
446 myHeader.byte_order = MSBFirst; /* MSBFirst, LSBFirst */
447 myHeader.bitmap_unit = 32 ; /* Bitmap unit */
448 myHeader.bitmap_bit_order = MSBFirst; /* MSBFirst, LSBFirst */
449 myHeader.bitmap_pad = 32 ; /* Bitmap scanline pad */
450 myHeader.bits_per_pixel = 8 ; /* Bits per pixel */
451 /* Bytes per scanline */
452 size = ( Standard_Integer ) ( anImage->Width() * myHeader.bits_per_pixel );
454 myHeader.bytes_per_line = size / myHeader.bitmap_unit ;
455 if ( size % myHeader.bitmap_pad ) myHeader.bytes_per_line += 1 ;
457 myHeader.bytes_per_line *= ( myHeader.bitmap_unit / 8 ) ;
459 myHeader.visual_class = PseudoColor ; /* Class of colormap */
460 myHeader.red_mask = 0 ; /* Z red mask */
461 myHeader.green_mask = 0 ; /* Z green mask */
462 myHeader.blue_mask = 0 ; /* Z blue mask */
463 myHeader.bits_per_rgb = 8 ; /* Log2 of distinct color values */
464 myHeader.colormap_entries = 256 ; /* Number of entries in colormap */
465 myHeader.ncolors = (anImage->ColorMap())->Size() ;
466 /* Number of Color structures */
467 myHeader.window_width = anImage->Width() ; /* Window width */
468 myHeader.window_height = anImage->Height() ; /* Window height */
469 myHeader.window_x = 0 ; /* Window upper left X coordinate */
470 myHeader.window_y = 0 ; /* Window upper left Y coordinate */
471 myHeader.window_bdrwidth = 0 ; /* Window border width */
473 size = ( Standard_Integer ) myHeader.ncolors
474 * sizeof( AlienImage_X11XColor ) ;
476 myColors = Standard::Allocate( size ) ;
478 AlienImage_X11XColor *p = ( AlienImage_X11XColor * )myColors ;
479 Aspect_ColorMapEntry aCentry ;
481 for ( i = 1 ; (unsigned int ) i <= myHeader.ncolors ; i++, p++ ) {
483 p->red = p->green = p->blue = 0 ;
486 aCentry = (anImage->ColorMap())->Entry( i ) ;
488 if ( aCentry.IsAllocated() == Standard_True ) {
489 p->flags = DoRed | DoGreen | DoBlue ;
490 p->pixel = aCentry.Index() ;
491 p->red = ( unsigned short )
492 ( (aCentry.Color()).Red() * 0xffff + 0.5 ) ;
493 p->green = ( unsigned short )
494 ( (aCentry.Color()).Green() * 0xffff + 0.5 ) ;
495 p->blue = ( unsigned short )
496 ( (aCentry.Color()).Blue() * 0xffff + 0.5 ) ;
500 if ( anImage->Size() ) {
501 Standard_Integer x, y ;
503 myData = Standard::Allocate( DataSize() ) ;
505 for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
506 for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
508 ( anImage->Pixel( anImage->LowerX()+x ,
509 anImage->LowerY()+y )).Value() ) ;
516 void AlienImage_X11XWDAlienData::FromColorImage(
517 const Handle_Image_ColorImage& anImage)
521 /* Size of the entire file header (bytes).*/
522 myHeader.header_size = sizeof(myHeader) + myName.Length() ;
523 myHeader.file_version = XWD_FILE_VERSION ; /* XWD_FILE_VERSION */
524 myHeader.pixmap_format = ZPixmap ; /* Pixmap format */
525 myHeader.pixmap_depth = 24 ; /* Pixmap depth */
526 myHeader.pixmap_width = anImage->Width() ;/* Pixmap width */
527 myHeader.pixmap_height = anImage->Height() ;/* Pixmap height */
528 myHeader.xoffset = 0 ; /* Bitmap x offset */
529 myHeader.byte_order = MSBFirst; /* MSBFirst, LSBFirst */
530 myHeader.bitmap_unit = 32 ; /* Bitmap unit */
531 myHeader.bitmap_bit_order = MSBFirst; /* MSBFirst, LSBFirst */
532 myHeader.bitmap_pad = 32 ; /* Bitmap scanline pad */
533 myHeader.bits_per_pixel = 32 ; /* Bits per pixel */
534 /* Bytes per scanline */
535 size = anImage->Width() * myHeader.bits_per_pixel ;
537 myHeader.bytes_per_line = size / myHeader.bitmap_unit ;
538 if ( size % myHeader.bitmap_pad ) myHeader.bytes_per_line += 1 ;
540 myHeader.bytes_per_line *= ( myHeader.bitmap_unit / 8 ) ;
542 myHeader.visual_class = TrueColor ; /* Class of colormap */
543 myHeader.red_mask = 0xff ; /* Z red mask */
544 myHeader.green_mask = 0xff00 ; /* Z green mask */
545 myHeader.blue_mask = 0xff0000 ; /* Z blue mask */
546 myHeader.bits_per_rgb = 8 ; /* Log2 of distinct color values */
547 myHeader.colormap_entries = 256 ; /* Number of entries in colormap */
548 myHeader.ncolors = 0 ; /* Number of Color structures */
549 myHeader.window_width = anImage->Width() ; /* Window width */
550 myHeader.window_height = anImage->Height() ; /* Window height */
551 myHeader.window_x = 0 ; /* Window upper left X coordinate */
552 myHeader.window_y = 0 ; /* Window upper left Y coordinate */
553 myHeader.window_bdrwidth = 0 ; /* Window border width */
558 if ( anImage->Size() ) {
559 Standard_Integer x, y, pix, c ;
560 const Standard_Integer rs = RedShift() ;
561 const Standard_Integer gs = GreenShift() ;
562 const Standard_Integer bs = BlueShift() ;
563 const Standard_Integer ColorRange =
564 Standard_Integer( ( 1 << myHeader.bits_per_rgb ) - 1 );
567 myData = Standard::Allocate( DataSize() ) ;
569 for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
570 for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
571 col = anImage->Pixel( anImage->LowerX()+x ,
572 anImage->LowerY()+y ).Value() ;
576 c = ( Standard_Integer ) ( col.Red() * ColorRange + 0.5 );
577 c = ( Standard_Integer ) (( c << rs ) & myHeader.red_mask) ;
580 c = ( Standard_Integer ) ( col.Green() * ColorRange + 0.5 );
581 c = ( Standard_Integer ) (( c << gs ) & myHeader.green_mask) ;
584 c = ( Standard_Integer ) ( col.Blue() * ColorRange + 0.5 );
585 c = ( Standard_Integer ) (( c << bs ) & myHeader.blue_mask) ;
588 SetPixel( x, y, pix ) ;
595 Handle_Image_ColorImage AlienImage_X11XWDAlienData::ToColorImage() const
597 { Aspect_ColorPixel CPixel ;
598 Quantity_Color acolor ;
599 Handle(Image_ColorImage) ret_image = NULL ;
600 Standard_Integer pix,x,y ;
601 Standard_Real r,g,b, maxcol ;
603 if ( myHeader.visual_class == TrueColor &&
604 myHeader.pixmap_format == ZPixmap) {
605 ret_image = new Image_ColorImage( 0,0,
606 (Standard_Integer)myHeader.pixmap_width,
607 (Standard_Integer)myHeader.pixmap_height ) ;
609 maxcol = ( 1 << myHeader.bits_per_rgb ) - 1 ;
611 for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
612 for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
613 pix = Pixel( x, y ) ;
615 r = ( ( pix & myHeader.red_mask ) >> RedShift() ) / maxcol ;
616 g = ( ( pix & myHeader.green_mask ) >> GreenShift() ) / maxcol ;
617 b = ( ( pix & myHeader.blue_mask ) >> BlueShift() ) / maxcol ;
619 acolor.SetValues( r,g,b, Quantity_TOC_RGB ) ;
620 CPixel.SetValue ( acolor ) ;
622 ret_image->SetPixel( ret_image->LowerX()+x ,
623 ret_image->LowerY()+y, CPixel ) ;
629 return( ret_image ) ;
632 Handle_Image_PseudoColorImage AlienImage_X11XWDAlienData::ToPseudoColorImage()
635 { Standard_Real r,g,b ;
636 Standard_Integer x, y ;
637 const Standard_Real XRange = Standard_Real( Standard_Integer(0xffff) );
638 const Standard_Integer newColorSize =
639 Standard_Integer(myHeader.colormap_entries*sizeof(AlienImage_X11XColor));
640 Handle(Image_PseudoColorImage) ret_image = NULL ;
642 if ( myHeader.pixmap_depth <= 8 &&
644 myHeader.pixmap_format == ZPixmap) {
645 // unsigned long int i, j, ncol ;
646 unsigned long int i, ncol ;
647 Aspect_ColorMapEntry Centry ;
648 Quantity_Color color ;
649 AlienImage_X11XColor *p ;
650 AlienImage_X11XColor *newColor ;
651 Standard_Address palloc ;
652 Aspect_IndexPixel IPixel ;
654 palloc = Standard::Allocate( newColorSize ) ;
656 newColor = ( AlienImage_X11XColor * ) palloc ;
658 p = ( AlienImage_X11XColor * )myColors;
660 for ( i = 0 ; i < myHeader.ncolors ; i++, p++ ) newColor[p->pixel] = *p;
661 for ( i = 0 ; i < myHeader.colormap_entries ; i++ ) newColor[i].flags = 0 ;
663 for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
664 for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
665 newColor[ Pixel( x, y ) ].flags = DoRed | DoGreen | DoBlue ;
669 for ( i = ncol = 0 ; i < myHeader.colormap_entries ; i++ ) {
670 if ( newColor[i].flags ) ncol++ ;
673 Handle(Aspect_GenericColorMap) colormap =
674 new Aspect_GenericColorMap();
676 for ( i = 0 ; i < myHeader.colormap_entries ; i++ ) {
677 if ( newColor[i].flags ) {
678 r = ( Standard_Real ) newColor[i].red / XRange ;
679 g = ( Standard_Real ) newColor[i].green / XRange ;
680 b = ( Standard_Real ) newColor[i].blue / XRange ;
681 color.SetValues( r,g,b, Quantity_TOC_RGB );
682 Centry.SetValue( Standard_Integer(newColor[i].pixel), color ) ;
683 colormap->AddEntry( Centry ) ;
687 ret_image = new Image_PseudoColorImage( 0,0,
688 Standard_Integer(myHeader.pixmap_width),
689 Standard_Integer(myHeader.pixmap_height),
692 for ( y = 0 ; (unsigned int ) y < myHeader.pixmap_height ; y++ ) {
693 for ( x = 0 ; (unsigned int ) x < myHeader.pixmap_width ; x++ ) {
694 IPixel.SetValue( Pixel( x, y ) ) ;
695 ret_image->SetPixel( ret_image->LowerX()+x ,
696 ret_image->LowerY()+y, IPixel ) ;
701 //Free all allocated memory
702 Standard::Free(palloc);
709 Standard_Integer AlienImage_X11XWDAlienData::DataSize() const
712 if ( myHeader.pixmap_format != ZPixmap)
713 return( myHeader.bytes_per_line * myHeader.pixmap_height
714 * myHeader.pixmap_depth );
716 return( myHeader.bytes_per_line * myHeader.pixmap_height);
719 void AlienImage_X11XWDAlienData::SetPixel(
720 const Standard_Integer x,
721 const Standard_Integer y,
722 const Standard_Integer value )
725 unsigned long int pixel_size ;
727 Standard_OutOfRange_Raise_if(
728 ( x < 0 || (unsigned int ) x >= myHeader.pixmap_width ||
729 y < 0 || (unsigned int ) y >= myHeader.pixmap_height ),
730 "Index out of range in X11XWDAlienData::Pixel");
732 pixel_size = ( unsigned long int ) myHeader.bytes_per_line /
733 myHeader.pixmap_width;
735 p = ( unsigned char * ) myData ;
736 p += y * myHeader.bytes_per_line + x * pixel_size ;
738 if ( pixel_size == 1 ) {
739 *p = ( unsigned char ) value ;
741 else if ( pixel_size == 2 ) {
742 *( ( unsigned short int * ) p ) = ( unsigned short int ) value ;
744 else if ( pixel_size == 4 ) {
745 *( ( unsigned long int * ) p ) = ( unsigned long int ) value ;
749 Standard_Integer AlienImage_X11XWDAlienData::Pixel(
750 const Standard_Integer x, const Standard_Integer y ) const
753 unsigned long int pixel_size ;
754 unsigned long int pix ;
755 Standard_Integer ret ;
757 Standard_OutOfRange_Raise_if(
758 ( x < 0 || (unsigned int ) x >= myHeader.pixmap_width ||
759 y < 0 || (unsigned int ) y >= myHeader.pixmap_height ),
760 "Index out of range in X11XWDAlienData::Pixel");
762 pixel_size = ( unsigned long int ) myHeader.bytes_per_line /
763 myHeader.pixmap_width;
765 p = ( unsigned char * ) myData ;
766 p += y * myHeader.bytes_per_line + x * pixel_size ;
768 if ( pixel_size == 1 ) {
769 pix = ( unsigned long int ) *p ;
771 else if ( pixel_size == 2 ) {
772 pix = *( ( unsigned short int * ) p ) ;
775 pix = *( ( unsigned long int * ) p ) ;
778 ret = Standard_Integer ( pix );
784 Standard_Integer AlienImage_X11XWDAlienData::RedShift() const
786 { Standard_Integer shift = 0 ;
788 Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
789 "Attempt to get RedShift from a non TrueColor X11XWDImage" ) ;
791 if ( ( myHeader.red_mask >> myHeader.bits_per_rgb ) == 0 ) {
794 else if ( ( myHeader.red_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
795 shift = Standard_Integer( myHeader.bits_per_rgb ) ;
798 shift = Standard_Integer( 2 * myHeader.bits_per_rgb );
803 Standard_Integer AlienImage_X11XWDAlienData::GreenShift() const
805 { Standard_Integer shift = 0 ;
807 Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
808 "Attempt to get GreenShift from a non TrueColor X11XWDImage" ) ;
810 if ( ( myHeader.green_mask >> myHeader.bits_per_rgb ) == 0 ) {
813 else if ( ( myHeader.green_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
814 shift = Standard_Integer( myHeader.bits_per_rgb ) ;
817 shift = Standard_Integer( 2 * myHeader.bits_per_rgb ) ;
822 Standard_Integer AlienImage_X11XWDAlienData::BlueShift() const
824 { Standard_Integer shift = 0 ;
826 Standard_TypeMismatch_Raise_if( myHeader.visual_class != TrueColor ,
827 "Attempt to get BlueShift from a non TrueColor X11XWDImage" ) ;
829 if ( ( myHeader.blue_mask >> myHeader.bits_per_rgb ) == 0 ) {
832 else if ( ( myHeader.blue_mask >> ( 2 * myHeader.bits_per_rgb ) ) == 0 ) {
833 shift = Standard_Integer( myHeader.bits_per_rgb ) ;
836 shift = Standard_Integer( 2 * myHeader.bits_per_rgb ) ;