1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 #include "wxImageBrowserWdg.h"
29 //#include <gtmlib/math/mathdefs.h>
32 BEGIN_EVENT_TABLE( wxImageBrowserWdg, wxScrolledWindow )
33 EVT_LEFT_UP( wxImageBrowserWdg::OnMouseLeftClick )
34 EVT_RIGHT_UP( wxImageBrowserWdg::OnMouseRightClick )
35 EVT_SIZE( wxImageBrowserWdg::OnSize )
38 wxImageBrowserWdg::wxImageBrowserWdg(
49 : wxScrolledWindow( parent, id, pos, size, style, name )
56 _ima_list.RemoveAll( );
57 _ima_list.Create( _sx, _sy );
62 void wxImageBrowserWdg::SetVolume(
63 unsigned short*** volume,
74 unsigned char* data; //pointeur sur mon image
77 tmpI.Create( width, height );
81 _ima_list.RemoveAll( );
82 _ima_list.Create( _sx, _sy );
84 for( int k = 0; k < depth; k++ )
87 data = tmpI.GetData();
88 data += 3*width*height;
89 // unsigned short** voltemp = volume[k];
90 for ( int i = 0; i < width; i++ )
92 for ( int j = 0; j < height; j++ )
94 value = ( unsigned char )( 0x00ff*( volume[ k ][ i ][ height - j - 1] - min) / (max - min));
99 //memset( data, value, 3);
103 //image is set, add it to the list
104 //another image is created so allocation is not a problem
105 _ima_list.Add( wxBitmap( tmpI.Scale( _sx, _sy ) ) );
110 this->OnSize( wxSizeEvent( ) );
112 wxSizeEvent myevent(wxSize(-1, -1), this->GetId());
113 this->OnSize(myevent);
120 void wxImageBrowserWdg::SetScaleX( int sx )
126 void wxImageBrowserWdg::SetScaleY( int sy )
132 void wxImageBrowserWdg::SetGap( int gap )
138 int wxImageBrowserWdg::GetScaleX( )
144 int wxImageBrowserWdg::GetScaleY( )
150 int wxImageBrowserWdg::GetGap( )
156 int wxImageBrowserWdg::GetFirst( )
158 return( GTM_MIN( _first_selected, _last_selected ) );
162 int wxImageBrowserWdg::GetLast( )
164 return( GTM_MAX( _first_selected, _last_selected ) );
168 void wxImageBrowserWdg::GetSizeParameters( int* n, int* c, int* r )
170 wxSize sz = this->GetClientSize( );
172 *n = _ima_list.GetImageCount( );
173 *c = ( int )floor( ( double )( sz.GetWidth( ) ) / ( double )( _gap + _sx ) );
174 *c = ( *c <= 0 )? 1: *c;
175 *r = ( int )ceil( ( double )( *n ) / ( double )( *c ) );
179 void wxImageBrowserWdg::OnDraw( wxDC& dc )
181 int r, c, i, x, y, n, rt;
182 wxSize sz = this->GetClientSize( );
184 dc.SetTextForeground( wxColour( 255, 255, 0 ) );
185 dc.SetBrush( *wxTRANSPARENT_BRUSH );
187 this->GetSizeParameters( &n, &c, &r );
188 for( i = 0; i < n; i++ )
190 rt = ( int )floor( ( double )i / ( double )c );
191 x = ( _sx * ( i % c ) ) + ( ( ( i % c ) + 1 ) * _gap );
192 y = ( _sy * ( rt % r ) ) + ( ( ( rt % r ) + 1 ) * _gap );
193 _ima_list.Draw( i, dc, x, y, wxIMAGELIST_DRAW_NORMAL, true );
194 dc.DrawText( _nrs_list[ i ], x, y );
195 if( _first_selected == i && _last_selected != i )
197 dc.SetPen( *wxRED_PEN );
198 dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
201 else if( _first_selected != i && _last_selected == i )
203 dc.SetPen( *wxGREEN_PEN );
204 dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
207 else if( _first_selected == i && _last_selected == i )
209 dc.SetPen( *wxCYAN_PEN );
210 dc.DrawRectangle( x - ( _gap / 2 ), y - ( _gap / 2 ), _sx + _gap, _sy + _gap );
218 void wxImageBrowserWdg::OnSize( wxSizeEvent& event )
221 wxSize sz = this->GetClientSize( );
223 this->GetSizeParameters( &n, &c, &r );
226 this->SetScrollbars( _sx + _gap, _sy + ( _gap * 2 ), c, r );
230 int wxImageBrowserWdg::GetIndexClicked( wxMouseEvent& event )
233 int x, y, n, c, r, i;
234 wxClientDC dc( this );
235 this->PrepareDC( dc );
236 wxPoint pos = event.GetLogicalPosition( dc );
238 this->GetSizeParameters( &n, &c, &r );
240 // Image index calculation (Help!!, Mr. wizard!)
242 re = ( double )( pos.x ) / ( double )( _gap + _sx );
243 if( ( re - floor( re ) ) >= ( ( double )( _gap ) / ( double )( _gap + _sx ) ) ) x = ( int )floor( re );
244 x = ( x >= 0 && x < c )? x: -1;
246 re = ( double )( pos.y ) / ( double )( _gap + _sy );
247 if( ( re - floor( re ) ) >= ( ( double )( _gap ) / ( double )( _gap + _sy ) ) ) y = ( int )floor( re );
248 y = ( y >= 0 && y < r )? y: -1;
249 i = ( x != -1 && y != -1 )? x + ( y * c ): -1;
250 return( ( i < n )? i: -1 );
254 void wxImageBrowserWdg::OnMouseLeftClick( wxMouseEvent& event )
257 _first_selected = this->GetIndexClicked( event );
264 void wxImageBrowserWdg::OnMouseRightClick( wxMouseEvent& event )
267 _last_selected = this->GetIndexClicked( event );
275 void wxImageBrowserWdg::Add( const wxImage& ima, const wxString& nrs )
277 _ima_list.Add( wxBitmap( ima.Scale( _sx, _sy ) ) );
278 _nrs_list.Add( nrs );
279 this->OnSize( wxSizeEvent( ) );