Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Image / Image_ColorImage.cxx
1 #include <Image_ColorImage.ixx>
2
3 #include <Image_ColorPixelDataMap.hxx>
4 #include <Image_DataMapIteratorOfColorPixelDataMap.hxx>
5 #include <TColStd_Array1OfInteger.hxx>
6 #include <Quantity_Array1OfColor.hxx>
7 #include <Aspect_ColorMapEntry.hxx>
8
9 #ifdef TRACE
10 static int Verbose = 0 ;
11 #endif
12
13 Image_ColorImage::Image_ColorImage (
14                         const Standard_Integer x,
15                         const Standard_Integer y,
16                         const Standard_Integer dx,
17                         const Standard_Integer dy) 
18         : Image_DColorImage( x, y, dx, dy, 
19                 Aspect_ColorPixel( Quantity_Color(0.,0.,0.,Quantity_TOC_RGB)) ) 
20
21 {
22 }
23
24 Image_ColorImage::Image_ColorImage (
25                         const Standard_Integer x,
26                         const Standard_Integer y,
27                         const Standard_Integer dx,
28                         const Standard_Integer dy,
29                         const Aspect_ColorPixel&  BackPixel ) 
30                         : Image_DColorImage( x, y, dx, dy, BackPixel ) 
31
32
33 }
34
35 Handle(Image_Image) Image_ColorImage::Dup() const {
36
37   Handle(Image_ColorImage) aImage = 
38                 new Image_ColorImage( LowerX(), LowerY(), Width(), Height(),
39                                         BackgroundPixel() ) ;
40
41   aImage->InternalDup( this ) ;
42
43   return aImage ;
44 }
45
46 Image_TypeOfImage Image_ColorImage::Type () const {
47
48   return Image_TOI_ColorImage ;
49
50 }
51
52 const Quantity_Color& Image_ColorImage::PixelColor( 
53                         const Standard_Integer x,
54                         const Standard_Integer y ) const 
55
56 { return( Pixel( x, y ).Value() ) ;
57 }
58
59 Handle(Aspect_GenericColorMap) Image_ColorImage::ChooseColorMap( 
60                         const Standard_Integer aSize ) const
61
62 { Handle(Aspect_GenericColorMap) TheColorMap = new Aspect_GenericColorMap() ;
63   Standard_Integer x,y,i,bblcount, UpX, UpY ;
64   Image_ColorPixelDataMap DataMap( 4000 ) ; // a Big DataMap !!!
65   Aspect_ColorPixel pix ;
66   
67   UpX = UpperX() ;
68   UpY = UpperY() ;
69
70   for ( y = LowerY() ; y <= UpY ; y++ ) {
71         for ( x = LowerX() ; x <= UpX ; x++ ) {
72                 pix = Pixel(x,y) ;
73                 if ( !DataMap.IsBound( pix ) ) DataMap.Bind( pix, 1 ) ;
74                 else                           DataMap( pix )++ ;
75         }
76   }
77
78 #ifdef TRACE
79   Standard_Integer CheckSum = 0 ;
80
81   if ( Verbose ) 
82     cout << "ChooseColorMap : Extent " << DataMap.Extent() << endl << flush ;
83 #endif
84
85   Image_DataMapIteratorOfColorPixelDataMap It( DataMap ) ;
86   TColStd_Array1OfInteger Tab( 1, aSize ) ; Tab.Init(0) ;
87   Quantity_Array1OfColor  Col( 1, aSize) ; Col.Init( 
88                                 Quantity_Color( 0.,0.,0., Quantity_TOC_RGB ) ) ;
89   Standard_Integer MaxOfTab = 0 ;
90   Standard_Integer MinOfTab = 0 ;
91
92   while ( It.More() ) {
93         bblcount = It.Value() ;
94
95 #ifdef TRACE
96   if ( Verbose ) {
97         cout << CheckSum << "," << It.Key() << "," << bblcount << endl ;
98
99         CheckSum += bblcount ;
100   }
101 #endif
102
103         if ( bblcount > MinOfTab ) {
104                 for ( i = 1 ; i <= aSize ; i++ ) {
105                         if ( Tab( i ) == MinOfTab ) {
106                                 Tab( i ) = bblcount ;
107                                 Col( i ) = It.Key().Value() ;
108                                 break ;
109                         }
110                 }
111
112                 for ( i = 1 ; i <= aSize ; i++ ) {
113                         MaxOfTab = Max( MaxOfTab, Tab( i ) ) ;
114                         MinOfTab = Min( MinOfTab, Tab( i ) ) ;
115                 }
116
117         }
118
119         It.Next() ;
120   }
121
122   Aspect_ColorMapEntry aEntry ;
123
124   for ( i = 1, bblcount = 0 ; i <= aSize ; i++ ) {
125         if ( Tab( i ) != 0 ) {
126                 aEntry.SetValue( bblcount++, Col( i ) ) ;
127                 TheColorMap->AddEntry( aEntry ) ;
128         }
129   }
130
131   cout << flush ;
132
133   return TheColorMap ;
134 }
135
136 #ifdef OLD
137 Handle(Standard_Transient) Image_ColorImage::ShallowCopy() const {
138         return DeepCopy() ;
139 }
140
141 Handle(Standard_Transient) Image_ColorImage::DeepCopy() const {
142
143   return Dup() ;
144 }
145 #endif