1 // Copyright (c) 1998-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 // Modifications: PLOTNIKOV Eugeny at July 1998 (BUC60286)
21 // include windows.h first to have all definitions available
25 #include <WNT_ImageManager.ixx>
27 #include <WNT_Image.hxx>
28 #include <WNT_GraphicDevice.hxx>
30 #include <WNT_Bitmap.h>
34 #define PWND ( ( WNT_WindowPtr )myWindow )
36 HBITMAP LoadImageFromFile ( Handle( WNT_GraphicDevice )&, char*, HDC = NULL );
37 int SaveWindowToFile (
38 Handle( WNT_GraphicDevice )&, HWND, char*, int, int, int, int
40 int SaveBitmapToFile (
41 Handle( WNT_GraphicDevice )&, HBITMAP, char*, int, int, int, int
43 void SetOutputFormat ( WNT_TypeOfImage );
45 //***************************** Constructor ******************************//
47 WNT_ImageManager :: WNT_ImageManager ( const WNT_WindowPtr& aWindow ) {
54 //******************************* Destroy ********************************//
56 void WNT_ImageManager :: Destroy () {
59 } // end WNT_ImageManager :: Destroy
61 //******************************* SetFormat ******************************//
63 void WNT_ImageManager :: SetFormat ( const WNT_TypeOfImage aFormat ) {
66 SetOutputFormat ( aFormat );
68 } // end WNT_ImageManager :: SetFormat
70 //********************************** Load ********************************//
72 Standard_Integer WNT_ImageManager :: Load ( const Standard_CString aFileName ) {
74 Handle( WNT_Image ) image;
75 Standard_Integer i, iHCode, len, retVal = 0;
78 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
79 DownCast ( PWND -> GraphicDevice () );
81 iHCode = StringHashCode ( aFileName );
83 if ( myLastIndex && myLastImage -> myHashCode == iHCode ) return myLastIndex;
85 for ( i = 1; i <= myTrash.Length (); ++i )
87 if ( myTrash.Value ( i ) -> myHashCode == iHCode ) {
89 myLastImage = myTrash.Value ( i );
96 len = myImages.Length ();
98 for ( i = 1; i <= len; ++i )
100 if ( myImages.Value ( i ) -> myHashCode == iHCode ) {
102 myLastImage = myImages.Value ( i );
109 hBmp = LoadImageFromFile ( gDev,(Standard_PCharacter) aFileName );
113 myLastImage = new WNT_Image ( hBmp, iHCode );
114 myImages.Append ( myLastImage );
115 retVal = myImages.Length ();
119 return myLastIndex = retVal;
121 } // end WNT_ImageManager :: Load
123 //********************************** Save ********************************//
125 Standard_Boolean WNT_ImageManager :: Save (
126 const Standard_CString aFileName,
127 const Standard_Integer aX,
128 const Standard_Integer aY,
129 const Standard_Integer aWidth,
130 const Standard_Integer aHeight
133 Standard_Boolean retVal;
135 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
136 DownCast ( PWND -> GraphicDevice () );
138 retVal = SaveWindowToFile (
139 gDev, ( HWND )( PWND -> HWindow () ),
140 (Standard_PCharacter) aFileName, aX, aY, aWidth, aHeight
145 } // end WNT_ImageManager :: Save
147 //****************************** SaveBuffer ******************************//
149 Standard_Boolean WNT_ImageManager :: SaveBuffer (
150 const Standard_CString aFileName,
151 const Standard_Integer aX,
152 const Standard_Integer aY,
153 const Standard_Integer aWidth,
154 const Standard_Integer aHeight
157 Standard_Boolean retVal;
158 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
159 DownCast ( PWND -> GraphicDevice () );
161 retVal = SaveBitmapToFile (
162 gDev, ( HBITMAP )( PWND -> HPixmap () ),
163 (Standard_PCharacter)aFileName, aX, aY, aWidth, aHeight
168 } // end WNT_ImageManager :: SaveBuffer
170 //********************************** Draw ********************************//
172 void WNT_ImageManager :: Draw (
173 const Standard_Integer anIndex,
174 const Standard_Integer Xc,
175 const Standard_Integer Yc,
176 const Standard_Integer aWidth,
177 const Standard_Integer aHeight,
178 const Standard_Real anAngle
181 HDC hDC, hDCmemSrc, hDCmemDst = 0;
182 HPALETTE hPal, hOldPal;
183 Standard_Integer iw, ih;
185 if ( myLastIndex != anIndex ) {
187 myLastIndex = anIndex;
188 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
192 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
193 DownCast ( PWND -> GraphicDevice () );
195 Dim ( anIndex, iw, ih );
197 hDC = GetDC ( ( HWND )( PWND -> HWindow () ) );
199 if ( gDev -> IsPaletteDevice () ) {
201 hOldPal = SelectPalette (
202 hDC, hPal = ( HPALETTE )( gDev -> HPalette () ), FALSE
205 if ( RealizePalette ( hDC ) )
207 UpdateColors ( hDC );
211 if ( PWND -> DoubleBuffer () ) {
213 hDCmemDst = CreateCompatibleDC ( hDC );
214 SelectBitmap ( hDCmemDst, PWND -> HPixmap () );
220 hDCmemSrc = CreateCompatibleDC ( hDC );
221 SetStretchBltMode ( hDCmemDst, COLORONCOLOR );
224 hDCmemSrc, ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp
227 if ( aWidth == iw && aHeight == ih && anAngle == 0.0F )
230 hDCmemDst, Xc - aWidth / 2 - ( aWidth & 1 ),
231 Yc - aHeight / 2, aWidth, aHeight,
232 hDCmemSrc, 0, 0, SRCCOPY
235 else if ( anAngle == 0.0F )
238 hDCmemDst, Xc - aWidth / 2,
239 Yc - aHeight / 2 + ( aHeight & 1 ), aWidth, aHeight,
240 hDCmemSrc, 0, 0, iw, ih, SRCCOPY
248 double sinVal, cosVal, angle;
250 SetGraphicsMode ( hDCmemDst, GM_ADVANCED );
252 angle = ( double )anAngle * ( M_PI / 180. );
253 cosVal = Cos ( angle );
254 sinVal = Sin ( angle );
256 pts[ 0 ].x = Xc - aWidth / 2;
257 pts[ 0 ].y = Yc - aHeight / 2 + ( aHeight & 1 );
259 pts[ 1 ].x = Xc + aWidth / 2;
260 pts[ 1 ].y = pts[ 0 ].y;
262 pts[ 2 ].x = pts[ 0 ].x;
263 pts[ 2 ].y = Yc + aHeight / 2 + ( aHeight & 1 );
265 pivot.x = ( pts[ 1 ].x + pts[ 2 ].x ) / 2;
266 pivot.y = ( pts[ 1 ].y + pts[ 2 ].y ) / 2;
268 x.eM11 = 1.0F; x.eM12 = 0.0F;
269 x.eM21 = 0.0F; x.eM22 = 1.0F;
270 x.eDx = ( float )-pivot.x;
271 x.eDy = ( float )-pivot.y;
272 ModifyWorldTransform ( hDCmemDst, &x, MWT_RIGHTMULTIPLY );
274 x.eM11 = ( float )cosVal; x.eM12 = ( float )-sinVal;
275 x.eM21 = ( float )sinVal; x.eM22 = ( float ) cosVal;
278 ModifyWorldTransform ( hDCmemDst, &x, MWT_RIGHTMULTIPLY );
280 x.eM11 = 1.0F; x.eM12 = 0.0F;
281 x.eM21 = 0.0F; x.eM22 = 1.0F;
282 x.eDx = ( float )pivot.x;
283 x.eDy = ( float )pivot.y;
284 ModifyWorldTransform ( hDCmemDst, &x, MWT_RIGHTMULTIPLY );
286 PlgBlt ( hDCmemDst, pts, hDCmemSrc, 0, 0, iw, ih, NULL, 0, 0 );
290 DeleteDC ( hDCmemSrc );
292 if ( gDev -> IsPaletteDevice () )
294 SelectPalette ( hDC, hOldPal, FALSE );
296 if ( PWND -> DoubleBuffer () )
298 DeleteDC ( hDCmemDst );
300 ReleaseDC ( ( HWND )( PWND -> HWindow () ), hDC );
302 } // end WNT_ImageManager :: Draw
304 //********************************** Scale *******************************//
306 Aspect_Handle WNT_ImageManager :: Scale (
307 const Standard_Integer anIndex,
308 const Standard_Real aScaleX,
309 const Standard_Real aScaleY,
310 const Standard_Boolean aReplace
313 Standard_Integer iw, ih, iNw, iNh;
314 HDC hDCmemSrc, hDCmemDst, hDC;
315 HPALETTE hOldPal = NULL;
316 HBITMAP hBitmap, hOldBitmap, hBmp;
318 if ( myLastIndex != anIndex ) {
320 myLastIndex = anIndex;
321 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
325 Handle( WNT_GraphicDevice ) gDev = Handle( WNT_GraphicDevice ) ::
326 DownCast ( PWND -> GraphicDevice () );
328 Dim ( anIndex, iw, ih );
330 iNw = ( Standard_Integer )( iw * aScaleX );
331 iNh = ( Standard_Integer )( ih * aScaleY );
333 hDC = GetDC ( NULL );
335 if ( gDev -> IsPaletteDevice () )
337 hOldPal = SelectPalette ( hDC, ( HPALETTE )( gDev -> HPalette () ), FALSE );
339 hDCmemSrc = CreateCompatibleDC ( hDC );
340 hDCmemDst = CreateCompatibleDC ( hDC );
342 SetStretchBltMode ( hDCmemDst, COLORONCOLOR );
344 hBitmap = CreateCompatibleBitmap ( hDC, iNw, iNh );
348 hBmp = ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp;
349 hOldBitmap = SelectBitmap ( hDCmemSrc, hBmp );
350 SelectBitmap ( hDCmemDst, hBitmap );
353 hDCmemDst, 0, 0, iNw, iNh,
354 hDCmemSrc, 0, 0, iw, ih, SRCCOPY
357 SelectBitmap ( hDCmemSrc, hOldBitmap );
361 DeleteObject ( hBmp );
362 ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp = hBitmap;
368 DeleteDC ( hDCmemDst );
369 DeleteDC ( hDCmemSrc );
371 if ( hOldPal != NULL ) SelectPalette ( hDC, hOldPal, FALSE );
373 ReleaseDC ( NULL, hDC );
375 return ( Aspect_Handle )hBitmap;
377 } // end WNT_ImageManager :: Scale
379 //******************************** HashCode ******************************//
381 Standard_Integer WNT_ImageManager :: StringHashCode (
382 const Standard_CString aString
385 Standard_Integer i, n, aHashCode = 0;
394 n = (Standard_Integer) strlen ( aString );
401 u.intPtr[ n - 1 ] = 0;
402 strcpy ( u.charPtr, aString );
407 strncpy ( u.charPtr, aString, 80 );
411 for ( i = 0; i < n; ++i )
413 aHashCode = aHashCode ^ u.intPtr[ i ];
417 return Abs ( aHashCode ) + 1;
419 } // end WNT_ImageManager :: HashCode
421 //**************************** ImageHandle *******************************//
423 Aspect_Handle WNT_ImageManager :: ImageHandle (
424 const Standard_Integer anIndex
427 if ( myLastIndex == anIndex ) return ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp;
429 myLastIndex = anIndex;
430 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
432 return ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp;
434 } // end WNT_ImageManager :: ImageHandle
436 //******************************** Dim ***********************************//
438 void WNT_ImageManager :: Dim (
439 const Standard_Integer anIndex,
440 Standard_Integer& aWidth,
441 Standard_Integer& aHeight
446 if ( myLastIndex != anIndex ) {
448 myLastIndex = anIndex;
449 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
454 ( ( PWNT_Bitmap )myLastImage -> myImage ) -> hBmp,
455 sizeof ( BITMAP ), ( LPVOID )&bmp
458 aWidth = bmp.bmWidth;
459 aHeight = bmp.bmHeight;
461 } // WNT_ImageManager :: Dim
463 //****************************** HashCode ********************************//
465 Standard_Integer WNT_ImageManager :: HashCode (
466 const Standard_Integer anIndex
469 if ( myLastIndex == anIndex ) return myLastImage -> myHashCode;
471 myLastIndex = anIndex;
472 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
474 return myLastImage -> myHashCode;
476 } // end WNT_ImageManager :: HashCode
478 //****************************** Index ***********************************//
480 Standard_Integer WNT_ImageManager :: Index (
481 const Standard_Integer aHashCode
484 Standard_Integer retVal = 0;
486 if ( myLastImage -> myHashCode == aHashCode ) return myLastIndex;
488 for ( int i = 1; i <= myImages.Length (); ++i )
490 if ( myImages.Value ( i ) -> myHashCode == aHashCode ) {
492 myLastImage = myImages.Value ( retVal = myLastIndex = i );
499 for ( int i = 1; i <= myTrash.Length (); ++i )
501 if ( myTrash.Value ( i ) -> myHashCode == aHashCode ) {
503 myLastImage = myImages.Value ( i );
504 retVal = myLastIndex = -i;
511 } // end WNT_ImageManager :: Index
513 //****************************** Delete **********************************//
515 void WNT_ImageManager :: Delete ( const Standard_Integer anIndex ) {
517 myImages.Remove ( anIndex );
519 if ( myLastIndex == anIndex ) myLastIndex = 0;
521 } // end WNT_ImageManager :: Delete
523 //****************************** Delete **********************************//
525 void WNT_ImageManager :: Discard ( const Standard_Integer anIndex ) {
529 int len = myTrash.Length ();
531 if ( len == TRASH_SIZE ) myTrash.Remove ( len );
533 myTrash.Prepend ( myImages.Value ( anIndex ) );
535 myImages.Remove ( anIndex );
537 if ( myLastIndex == anIndex ) myLastIndex = 0;
541 } // end WNT_ImageManager :: Delete
543 //******************************** Size **********************************//
545 Standard_Integer WNT_ImageManager :: Size () const {
547 return myImages.Length ();
549 } // end WNT_ImageManager :: Size
551 //******************************** Add ***********************************//
553 void WNT_ImageManager :: Add ( const Handle_WNT_Image& anImage ) {
555 myImages.Append ( myLastImage = anImage );
557 myLastIndex = myImages.Length ();
559 } // end WNT_ImageManager :: Add
561 //******************************** Image *********************************//
563 Handle( WNT_Image ) WNT_ImageManager :: Image (
564 const Standard_Integer anIndex
567 if ( myLastIndex == anIndex ) return myLastImage;
569 myLastIndex = anIndex;
570 myLastImage = anIndex < 0 ? myTrash.Value ( -anIndex ) : myImages.Value ( anIndex );
574 } // end WNT_ImageManager :: Image
576 //******************************** Open **********************************//
578 Standard_Integer WNT_ImageManager :: Open (
579 const Aspect_Handle aDC,
580 const Standard_Integer aWidth,
581 const Standard_Integer aHeight,
582 const Standard_Integer aHashCode
585 HDC hdc = ( HDC )aDC;
586 HBITMAP hBmp = CreateCompatibleBitmap ( hdc, aWidth, aHeight );
588 myImages.Append ( myLastImage = new WNT_Image ( hBmp, aHashCode ) );
590 return myLastIndex = myImages.Length ();
592 } // end WNT_ImageMagager :: Open
594 //************************************************************************//