wxArt2D
canimage.cpp
1 /*! \file canvas/src/rectangle.cpp
2  \author Klaas Holwerda
3 
4  Copyright: 2000-2004 (c) Klaas Holwerda
5 
6  Licence: wxWidgets Licence
7 
8  RCS-ID: $Id: canimage.cpp,v 1.3 2009/07/17 16:03:34 titato Exp $
9 */
10 
11 #include "a2dprec.h"
12 
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16 
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20 
21 #include "wx/canvas/canobj.h"
22 #include "wx/canvas/rectangle.h"
23 #include "wx/canvas/canglob.h"
24 #include "wx/canvas/vpath.h"
25 #include "wx/canvas/drawer.h"
26 #include "wx/canvas/canvas.h"
27 #include "wx/canvas/canimage.h"
28 
29 IMPLEMENT_DYNAMIC_CLASS( a2dImage, a2dCanvasObject )
30 IMPLEMENT_DYNAMIC_CLASS( a2dRgbaImage, a2dRectMM )
31 
32 //----------------------------------------------------------------------------
33 // a2dImage
34 //----------------------------------------------------------------------------
35 
36 a2dPropertyIdDouble* a2dImage::PROPID_Width = NULL;
37 a2dPropertyIdDouble* a2dImage::PROPID_Height = NULL;
38 
39 INITIALIZE_PROPERTIES( a2dImage, a2dCanvasObject )
40 {
41  A2D_PROPID_GSI( a2dPropertyIdDouble, a2dImage, Width, 0 );
42  A2D_PROPID_GSI( a2dPropertyIdDouble, a2dImage, Height, 0 );
43  return true;
44 }
45 
47  : a2dCanvasObject()
48 {
49  m_width = 10;
50  m_height = 10;
51  m_OpacityFactor = 255;
52 
53  m_image = wxImage( 10, 10 );
54 
55  m_filename = wxT( "" );
56 
57  m_type = wxBITMAP_TYPE_PNG;
58 
59  m_drawPatternOnTop = false;
60 }
61 
62 a2dImage::a2dImage( const wxImage& image, double xc, double yc, double w, double h )
63  : a2dCanvasObject()
64 {
65  m_lworld.Translate( xc, yc );
66 
67  if ( w == 0 )
68  w = image.GetWidth();
69 
70  if ( h == 0 )
71  h = image.GetHeight();
72 
73  m_width = w;
74  m_height = h;
75  m_OpacityFactor = 255;
76 
77  m_image = image;
78 
79  m_filename = wxT( "" );
80 
81  m_type = wxBITMAP_TYPE_PNG;
82 
83  m_drawPatternOnTop = false;
84 }
85 
86 a2dImage::a2dImage( const wxString& imagefile, wxBitmapType type, double xc, double yc, double w, double h )
87  : a2dCanvasObject()
88 {
89  wxBitmap bitmap;
90 
91  m_type = type;
92  if ( !wxFileExists( imagefile ) )
93  {
94  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "could not open file %s image file" ), imagefile.c_str() );
95  m_image = wxImage( 100, 100 );
96  }
97  else
98  {
99  bitmap.LoadFile( imagefile, type );
100  m_image = bitmap.ConvertToImage();
101  }
102 
103  if ( w == 0 )
104  w = m_image.GetWidth();
105 
106  if ( h == 0 )
107  h = m_image.GetHeight();
108 
109  m_filename = imagefile;
110 
111  m_lworld.Translate( xc, yc );
112 
113  m_width = w;
114  m_height = h;
115  m_OpacityFactor = 255;
116 
117  m_drawPatternOnTop = false;
118 }
119 
120 a2dImage::a2dImage( a2dCanvasObject* torender, double xc, double yc, double w, double h, int imagew, int imageh )
121  : a2dCanvasObject()
122 {
123  m_lworld.Translate( xc, yc );
124 
125  m_width = w;
126  m_height = h;
127  m_OpacityFactor = 255;
128 
129  RenderObject( torender, imagew, imageh );
130 
131  m_filename = wxT( "" );
132 
133  m_type = wxBITMAP_TYPE_PNG;
134 
135  m_drawPatternOnTop = false;
136 }
137 
138 a2dImage::a2dImage( const a2dImage& other, CloneOptions options, a2dRefMap* refs )
139  : a2dCanvasObject( other, options, refs )
140 {
141  m_width = other.m_width;
142  m_height = other.m_height;
143  m_OpacityFactor = other.m_OpacityFactor;
144  m_image = other.m_image;
145  m_filename = other.m_filename;
146  m_type = other.m_type;
147  m_drawPatternOnTop = other.m_drawPatternOnTop;
148 }
149 
150 a2dImage::~a2dImage()
151 {
152 }
153 
154 a2dCanvasObjectList* a2dImage::GetAsRectangles( const wxColour& col1, const wxColour& col2, bool transform )
155 {
156  a2dAffineMatrix pworld;
157  if ( transform )
158  pworld = m_lworld;
159 
160  a2dCanvasObjectList* canpathlist = new a2dCanvasObjectList();
161 
162  unsigned char* source_data = m_image.GetData();
163  long w = m_image.GetWidth();
164  long h = m_image.GetHeight();
165 
166  double rectw = m_width / m_image.GetWidth();
167  double recth = m_height / m_image.GetHeight();
168 
169  for ( long y = 0; y < h; y++ )
170  {
171  long rowlenght = 0;
172  long startrow = 0;
173  for ( long x = 0; x < w; x++ )
174  {
175  unsigned char* pixel = source_data + ( y * w ) * 3 + x * 3;
176  unsigned char red = pixel[0] ;
177  unsigned char green = pixel[1] ;
178  unsigned char blue = pixel[2] ;
179  unsigned char alpha = 255 ;
180 
181  if ( red >= col1.Red() && red <= col2.Red() &&
182  green >= col1.Green() && green <= col2.Green() &&
183  blue >= col1.Blue() && blue <= col2.Blue()
184  )
185  {
186  if ( !rowlenght )
187  startrow = x;
188  rowlenght++;
189  }
190  else if ( rowlenght )
191  {
192  a2dRect* pixRect = new a2dRect( -m_width / 2.0 + startrow * rectw, m_height / 2.0 - y * recth, rectw * rowlenght, -recth );
193  pixRect->Transform( pworld );
194  pixRect->SetLayer( m_layer );
195  pixRect->SetContourWidth( GetContourWidth() );
196  pixRect->SetRoot( m_root, false );
197  canpathlist->push_back( pixRect );
198  rowlenght = 0;
199  }
200 
201  }
202  if ( rowlenght )
203  {
204  a2dRect* pixRect = new a2dRect( -m_width / 2.0 + startrow * rectw, m_height / 2.0 - y * recth, rectw * rowlenght, -recth );
205  pixRect->Transform( pworld );
206  pixRect->SetLayer( m_layer );
207  pixRect->SetContourWidth( GetContourWidth() );
208  pixRect->SetRoot( m_root, false );
209  canpathlist->push_back( pixRect );
210  rowlenght = 0;
211  }
212  }
213 
214  return canpathlist;
215 }
216 
217 void a2dImage::RenderObject( a2dCanvasObject* torender, int imagew, int imageh )
218 {
219 /* TODO
220  a2dSmrtPtr<a2dDrawing> doc = torender->GetRoot();
221  bool hasDoc = true;
222  if ( !doc )
223  {
224  doc = new a2dDrawing(); //will use default layers
225  doc->Append( torender );
226  hasDoc = false;
227  }
228 
229  a2dSmrtPtr<a2dCanvasView> drawer = new a2dCanvasView( imagew, imageh );
230  drawer->SetDocument( doc );
231  drawer->SetShowObject( torender );
232  doc->SetCanvasDocumentRecursive();
233  //set some mapping in order to be able to calculate boundingboxes
234  drawer->GetDrawer2D()->SetMappingWidthHeight( 0, 0, 1000, 1000 );
235  drawer->GetDrawer2D()->SetMappingWidthHeight( torender->GetUnTransformedBbox( a2dCANOBJ_BBOX_CHILDREN ) );
236  drawer->UpdateArea( 0, 0, imagew, imageh );
237 
238  m_image = drawer->GetDrawer2D()->GetBuffer().ConvertToImage();
239 
240  drawer->SetClosed();
241 
242  if ( !hasDoc )
243  {
244  torender->SetCanvasDocument( NULL );
245  }
246 */
247 }
248 
249 void a2dImage::SetFilename( const wxString filename, wxBitmapType type, bool doread )
250 {
251  m_filename = filename;
252  m_type = type;
253 
254  SetPending( true );
255 
256  if ( doread )
257  {
258  if ( !wxFileExists( m_filename ) )
259  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "image file %s not found" ), m_filename.c_str() );
260  wxCHECK_RET( m_image.LoadFile( m_filename, type ), wxT( "invalid image file" ) );
261  }
262 }
263 
264 
265 void a2dImage::SetDrawPatternOnTop( bool drawPatternOnTop )
266 {
267 
268  // change only if really neccessary
269  if ( m_drawPatternOnTop != drawPatternOnTop )
270  {
271  m_drawPatternOnTop = drawPatternOnTop;
272  SetPending( true );
273  }
274 }
275 
276 bool a2dImage::DoStartEdit( wxUint16 editmode, wxEditStyle editstyle )
277 {
278  if ( m_flags.m_editable )
279  {
280  PROPID_IncludeChildren->SetPropertyToObject( this, false );
281  PROPID_Allowrotation->SetPropertyToObject( this, true );
282  PROPID_Allowskew->SetPropertyToObject( this, false );
283 
284  return a2dCanvasObject::DoStartEdit( editmode, editstyle );
285  }
286 
287  return false;
288 }
289 
290 
292 {
293  return new a2dImage( *this, options, refs );
294 };
295 
296 a2dBoundingBox a2dImage::DoGetUnTransformedBbox( a2dBboxFlags WXUNUSED( flags ) ) const
297 {
298  a2dBoundingBox bbox;
299 
300  bbox.Expand( -m_width / 2, -m_height / 2 );
301  bbox.Expand( m_width / 2, m_height / 2 );
302  return bbox;
303 }
304 
305 void a2dImage::DoRender( a2dIterC& ic, OVERLAP WXUNUSED( clipparent ) )
306 {
307  // Draws rectangle / pattern on back if necessary
308  if ( !m_drawPatternOnTop )
309  {
310  if ( !GetStroke().IsSameAs( *a2dTRANSPARENT_STROKE ) || !GetFill().IsSameAs( *a2dTRANSPARENT_FILL ) )
311  {
312  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0, 0, m_width, m_height, 0 );
313  }
314  }
315 
316 
318  {
319  ic.GetDrawer2D()->DrawImage( m_image, 0, 0, m_width, m_height, m_OpacityFactor );
320  }
321  else
322  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0, 0 , m_width, m_height, 0 );
323 
324 
325  // Draws rectangle / pattern on top if necessary
326  if ( m_drawPatternOnTop )
327  {
328  if ( GetStroke() != *a2dTRANSPARENT_STROKE || GetFill() != *a2dTRANSPARENT_FILL )
329  {
330  //feature of shape, so why not use it.
331  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0, 0, m_width, m_height, 0 );
332  }
333  }
334  if ( m_flags.m_selected )
335  {
336  //layer pens for select layer are set already on higher levels
337  ic.GetDrawer2D()->DrawCenterRoundedRectangle( 0, 0 , m_width, m_height, 0 );
338  }
339 }
340 
341 bool a2dImage::DoIsHitWorld( a2dIterC& WXUNUSED( ic ), a2dHitEvent& hitEvent )
342 {
343  hitEvent.m_how = a2dHit::stock_fill;
344  return true;
345 }
346 
347 #if wxART2D_USE_CVGIO
348 void a2dImage::DoLoad( wxObject* parent, a2dIOHandlerXmlSerIn& parser, a2dXmlSer_flag xmlparts )
349 {
350  a2dCanvasObject::DoLoad( parent, parser, xmlparts );
351  if ( xmlparts == a2dXmlSer_attrib )
352  {
353  m_filename = parser.GetAttributeValue( wxT( "filename" ) );
354  m_width = parser.GetAttributeValueDouble( wxT( "width" ) );
355  m_height = parser.GetAttributeValueDouble( wxT( "height" ) );
356  m_type = ( wxBitmapType ) parser.GetAttributeValueLong( wxT( "type" ) );
357  m_drawPatternOnTop = parser.GetAttributeValueBool( wxT( "patternontop" ) );
358  m_OpacityFactor = parser.GetAttributeValueLong( wxT( "opacityfactor" ) );
359 
360  if ( !m_filename.IsEmpty() )
361  {
362  wxString fullfilepath = parser.GetFileName().GetPathWithSep() + m_filename;
363  if ( !::wxFileExists( fullfilepath ) )
364  {
365  fullfilepath = a2dGlobals->GetImagePathList().FindValidPath( m_filename );
366  if ( !::wxFileExists( fullfilepath ) )
367  {
368  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "could not open file %s for loading image" ), m_filename.c_str() );
369  return;
370  }
371  }
372  m_image.LoadFile( fullfilepath, m_type );
373  }
374  else
375  {
376  m_image = wxImage( ( int ) m_width, ( int ) m_height );
377  }
378  }
379  else
380  {
381  }
382 }
383 
384 void a2dImage::DoSave( wxObject* parent, a2dIOHandlerXmlSerOut& out, a2dXmlSer_flag xmlparts , a2dObjectList* towrite )
385 {
386  a2dCanvasObject::DoSave( parent, out, xmlparts, towrite );
387  if ( xmlparts == a2dXmlSer_attrib )
388  {
389  if ( m_filename.IsEmpty() )
390  {
391  m_image.SaveFile( GetName(), m_type );
392  out.WriteAttribute( wxT( "filename" ), GetName() );
393  }
394  else
395  {
396  wxString filename = a2dGlobals->GetImagePathList().FindValidPath( m_filename, false );
397  if ( filename.IsEmpty() )
398  {
399  wxString fullfilepath = out.GetFileName().GetPathWithSep() + m_filename;
400  m_image.SaveFile( fullfilepath, m_type );
401  }
402 
403  out.WriteAttribute( wxT( "filename" ), m_filename );
404  }
405  out.WriteAttribute( wxT( "width" ), m_width );
406  out.WriteAttribute( wxT( "height" ), m_height );
407  out.WriteAttribute( wxT( "type" ), ( wxInt32 ) m_type );
408  out.WriteAttribute( wxT( "patternontop" ), m_drawPatternOnTop );
409  out.WriteAttribute( wxT( "opacityfactor" ), ( wxUint8 ) m_OpacityFactor );
410  }
411  else
412  {
413  }
414 }
415 #endif //wxART2D_USE_CVGIO
416 
417 //----------------------------------------------------------------------------
418 // a2dRgbaImage
419 //----------------------------------------------------------------------------
420 
421 BEGIN_EVENT_TABLE( a2dRgbaImage, a2dRectMM )
422 END_EVENT_TABLE()
423 
425  : a2dRectMM()
426 {
427 }
428 
429 a2dRgbaImage::a2dRgbaImage( double x, double y, wxImage& image, wxUint8 OpacityFactor )
430  : a2dRectMM( x, y, image.GetWidth(), image.GetHeight() )
431 {
432  m_glimage = a2dImageRGBA( image, OpacityFactor );
433  m_OpacityFactor = OpacityFactor;
434  m_drawPatternOnTop = false;
435 }
436 
437 a2dRgbaImage::~a2dRgbaImage()
438 {
439 }
440 
441 a2dRgbaImage::a2dRgbaImage( const a2dRgbaImage& other, CloneOptions options, a2dRefMap* refs )
442  : a2dRectMM( other, options, refs )
443 {
444  m_glimage = other.m_glimage;
445  m_OpacityFactor = other.m_OpacityFactor;
446  m_flip = other.m_flip;
447  m_drawPatternOnTop = other.m_drawPatternOnTop;
448 }
449 
451 {
452  return new a2dRgbaImage( *this, options, refs );
453 };
454 
455 void a2dRgbaImage::DoRender( a2dIterC& ic, OVERLAP clipparent )
456 {
457  // Draws rectangle / pattern on back if necessary
458  if ( !m_drawPatternOnTop )
459  {
460  if ( !GetStroke().IsSameAs( *a2dTRANSPARENT_STROKE ) || !GetFill().IsSameAs( *a2dTRANSPARENT_FILL ) )
461  {
463  }
464  }
465 
467  {
468  ic.GetDrawer2D()->DrawImage( m_glimage, m_minx, m_miny, m_maxx - m_minx, m_maxy - m_miny, m_OpacityFactor );
469  }
470  else
472 
473 
474  // Draws rectangle / pattern on top if necessary
475  if ( m_drawPatternOnTop )
476  {
477  if ( GetStroke() != *a2dTRANSPARENT_STROKE || GetFill() != *a2dTRANSPARENT_FILL )
478  {
479  //feature of shape, so why not use it.
481  }
482  }
483  if ( m_flags.m_selected )
484  {
485  //layer pens for select layer are set already on higher levels
487  }
488 }
489 
490 #if wxART2D_USE_CVGIO
491 
492 void a2dRgbaImage::DoSave( wxObject* parent, a2dIOHandlerXmlSerOut& out, a2dXmlSer_flag xmlparts , a2dObjectList* towrite )
493 {
494  a2dRectMM::DoSave( parent, out, xmlparts, towrite );
495  if ( xmlparts == a2dXmlSer_attrib )
496  {
497  }
498  else
499  {
500  }
501 }
502 
503 void a2dRgbaImage::DoLoad( wxObject* parent, a2dIOHandlerXmlSerIn& parser, a2dXmlSer_flag xmlparts )
504 {
505  a2dRectMM::DoLoad( parent, parser, xmlparts );
506  if ( xmlparts == a2dXmlSer_attrib )
507  {
508  }
509  else
510  {
511  }
512 }
513 #endif //wxART2D_USE_CVGIO
514 
515 
516 IMPLEMENT_DYNAMIC_CLASS( a2dImageMM, a2dRectMM )
517 
518 //----------------------------------------------------------------------------
519 // a2dImageMM
520 //----------------------------------------------------------------------------
521 
523  : a2dRectMM( 0, 0, 10, 10 )
524 {
525  m_image = wxImage( 10, 10 );
526 
527  m_filename = wxT( "" );
528 
529  m_type = wxBITMAP_TYPE_PNG;
530 
531  m_drawPatternOnTop = false;
532 }
533 
534 a2dImageMM::a2dImageMM( double x, double y, wxImage& image, unsigned char alpha )
535  : a2dRectMM( x, y, image.GetWidth(), image.GetHeight() )
536 {
537  m_image = image;
538 
539  m_filename = wxT( "" );
540 
541  m_type = wxBITMAP_TYPE_PNG;
542 
543  m_drawPatternOnTop = false;
544 }
545 
546 a2dImageMM::a2dImageMM( const wxImage& image, double x, double y, double w, double h )
547  : a2dRectMM( x, y, w, h )
548 {
549  m_image = image;
550 
551  m_filename = wxT( "" );
552 
553  m_type = wxBITMAP_TYPE_PNG;
554 
555  m_drawPatternOnTop = false;
556 }
557 
558 a2dImageMM::a2dImageMM( const wxString& imagefile, wxBitmapType type, double x, double y, double w, double h )
559  : a2dRectMM( x, y, w, h )
560 {
561  wxBitmap bitmap;
562 
563  m_type = type;
564  if ( !wxFileExists( imagefile ) )
565  {
566  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "could not open file %s image file" ), imagefile.c_str() );
567  m_image = wxImage( 100, 100 );
568  }
569  else
570  {
571  bitmap.LoadFile( imagefile, type );
572  m_image = bitmap.ConvertToImage();
573  }
574 
575  m_filename = imagefile;
576 
577  m_drawPatternOnTop = false;
578 }
579 
580 a2dImageMM::a2dImageMM( a2dCanvasObject* torender, double x, double y, double w, double h, int imagew, int imageh )
581  : a2dRectMM( x, y, w, h )
582 {
583  RenderObject( torender, imagew, imageh );
584 
585  m_filename = wxT( "" );
586 
587  m_type = wxBITMAP_TYPE_PNG;
588 
589  m_drawPatternOnTop = false;
590 }
591 
592 a2dImageMM::a2dImageMM( const a2dImageMM& other, CloneOptions options, a2dRefMap* refs )
593  : a2dRectMM( other, options, refs )
594 {
595  m_image = other.m_image;
596  m_filename = other.m_filename;
597  m_type = other.m_type;
598  m_drawPatternOnTop = other.m_drawPatternOnTop;
599 }
600 
601 a2dImageMM::~a2dImageMM()
602 {
603 }
604 
605 a2dCanvasObjectList* a2dImageMM::GetAsRectangles( const wxColour& col1, const wxColour& col2, bool transform )
606 {
607  a2dAffineMatrix pworld;
608  if ( transform )
609  pworld = m_lworld;
610 
611  a2dCanvasObjectList* canpathlist = new a2dCanvasObjectList();
612 
613  unsigned char* source_data = m_image.GetData();
614  long w = m_image.GetWidth();
615  long h = m_image.GetHeight();
616 
617  double rectw = GetWidth() / m_image.GetWidth();
618  double recth = GetHeight() / m_image.GetHeight();
619 
620  for ( long y = 0; y < h; y++ )
621  {
622  long rowlenght = 0;
623  long startrow = 0;
624  for ( long x = 0; x < w; x++ )
625  {
626  unsigned char* pixel = source_data + ( y * w ) * 3 + x * 3;
627  unsigned char red = pixel[0] ;
628  unsigned char green = pixel[1] ;
629  unsigned char blue = pixel[2] ;
630  unsigned char alpha = 255 ;
631 
632  if ( red >= col1.Red() && red <= col2.Red() &&
633  green >= col1.Green() && green <= col2.Green() &&
634  blue >= col1.Blue() && blue <= col2.Blue()
635  )
636  {
637  if ( !rowlenght )
638  startrow = x;
639  rowlenght++;
640  }
641  else if ( rowlenght )
642  {
643  a2dRect* pixRect = new a2dRect( -GetWidth() / 2.0 + startrow * rectw, GetHeight() / 2.0 - y * recth, rectw * rowlenght, -recth );
644  pixRect->Transform( pworld );
645  pixRect->SetLayer( m_layer );
646  pixRect->SetContourWidth( GetContourWidth() );
647  pixRect->SetRoot( m_root, false );
648  canpathlist->push_back( pixRect );
649  rowlenght = 0;
650  }
651 
652  }
653  if ( rowlenght )
654  {
655  a2dRect* pixRect = new a2dRect( -GetWidth() / 2.0 + startrow * rectw, GetHeight() / 2.0 - y * recth, rectw * rowlenght, -recth );
656  pixRect->Transform( pworld );
657  pixRect->SetLayer( m_layer );
658  pixRect->SetContourWidth( GetContourWidth() );
659  pixRect->SetRoot( m_root, false );
660  canpathlist->push_back( pixRect );
661  rowlenght = 0;
662  }
663  }
664 
665  return canpathlist;
666 }
667 
668 void a2dImageMM::RenderObject( a2dCanvasObject* torender, int imagew, int imageh )
669 {
670 /* TODO
671  a2dSmrtPtr<a2dCanvasDocument> doc = torender->GetCanvasDocument();
672  bool hasDoc = true;
673  if ( !doc )
674  {
675  doc = new a2dCanvasDocument(); //will use default layers
676  doc->Append( torender );
677  hasDoc = false;
678  }
679 
680  a2dSmrtPtr<a2dCanvasView> drawer = new a2dCanvasView( imagew, imageh );
681  drawer->SetDocument( doc );
682  drawer->SetShowObject( torender );
683  doc->SetCanvasDocumentRecursive();
684  //set some mapping in order to be able to calculate boundingboxes
685  drawer->GetDrawer2D()->SetMappingWidthHeight( 0, 0, 1000, 1000 );
686  drawer->GetDrawer2D()->SetMappingWidthHeight( torender->GetUnTransformedBbox( a2dCANOBJ_BBOX_CHILDREN ) );
687  drawer->UpdateArea( 0, 0, imagew, imageh );
688 
689  m_image = drawer->GetDrawer2D()->GetBuffer().ConvertToImage();
690 
691  drawer->SetClosed();
692 
693  if ( !hasDoc )
694  {
695  torender->SetCanvasDocument( NULL );
696  }
697 */
698 }
699 
700 void a2dImageMM::SetFilename( const wxString filename, wxBitmapType type, bool doread )
701 {
702  m_filename = filename;
703  m_type = type;
704 
705  SetPending( true );
706 
707  if ( doread )
708  {
709  if ( !wxFileExists( m_filename ) )
710  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "image file %s not found" ), m_filename.c_str() );
711  wxCHECK_RET( m_image.LoadFile( m_filename, type ), wxT( "invalid image file" ) );
712  }
713 }
714 
715 
716 void a2dImageMM::SetDrawPatternOnTop( bool drawPatternOnTop )
717 {
718 
719  // change only if really neccessary
720  if ( m_drawPatternOnTop != drawPatternOnTop )
721  {
722  m_drawPatternOnTop = drawPatternOnTop;
723  SetPending( true );
724  }
725 }
726 
727 bool a2dImageMM::DoStartEdit( wxUint16 editmode, wxEditStyle editstyle )
728 {
729  if ( m_flags.m_editable )
730  {
731  PROPID_IncludeChildren->SetPropertyToObject( this, false );
732  PROPID_Allowrotation->SetPropertyToObject( this, true );
733  PROPID_Allowskew->SetPropertyToObject( this, false );
734 
735  return a2dRectMM::DoStartEdit( editmode, editstyle );
736  }
737 
738  return false;
739 }
740 
741 
743 {
744  return new a2dImageMM( *this, options, refs );
745 };
746 
747 void a2dImageMM::DoRender( a2dIterC& ic, OVERLAP WXUNUSED( clipparent ) )
748 {
749 
750  // Draws rectangle / pattern on back if necessary
751  if ( !m_drawPatternOnTop )
752  {
753  if ( m_flags.m_selected )
754  {
755  //layer pens for select layer are set already on higher levels
757  }
758  else if ( !GetStroke().IsSameAs( *a2dTRANSPARENT_STROKE ) || !GetFill().IsSameAs( *a2dTRANSPARENT_FILL ) )
759  {
761  }
762  }
763 
765  {
766  ic.GetDrawer2D()->DrawImage( m_image, m_minx + GetWidth() / 2.0, m_miny + GetHeight() / 2.0, m_maxx - m_minx, m_maxy - m_miny );
767  }
768  else
770 
771  // Draws rectangle / pattern on top if necessary
772  if ( m_drawPatternOnTop )
773  {
774  if ( m_flags.m_selected )
775  {
776  //layer pens for select layer are set already on higher levels
778  }
779  else if ( GetStroke() != *a2dTRANSPARENT_STROKE || GetFill() != *a2dTRANSPARENT_FILL )
780  {
781  //feature of shape, so why not use it.
783  }
784  }
785 }
786 
787 bool a2dImageMM::DoIsHitWorld( a2dIterC& WXUNUSED( ic ), a2dHitEvent& hitEvent )
788 {
789  hitEvent.m_how = a2dHit::stock_fill;
790  return true;
791 }
792 
793 #if wxART2D_USE_CVGIO
794 void a2dImageMM::DoLoad( wxObject* parent, a2dIOHandlerXmlSerIn& parser, a2dXmlSer_flag xmlparts )
795 {
796  a2dRectMM::DoLoad( parent, parser, xmlparts );
797  if ( xmlparts == a2dXmlSer_attrib )
798  {
799  m_filename = parser.GetAttributeValue( wxT( "filename" ) );
800  m_type = ( wxBitmapType ) parser.GetAttributeValueLong( wxT( "type" ) );
801  m_drawPatternOnTop = parser.GetAttributeValueBool( wxT( "patternontop" ) );
802 
803  if ( !m_filename.IsEmpty() )
804  {
805  wxString fname = a2dGlobals->GetImagePathList().FindValidPath( m_filename );
806  if ( !::wxFileExists( fname ) )
807  {
808  a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _( "could not open file %s for loading image" ), m_filename.c_str() );
809  return;
810  }
811  m_image.LoadFile( fname, m_type );
812  }
813  else
814  {
815  m_image = wxImage( ( int ) GetWidth(), ( int ) GetHeight() );
816  }
817  }
818  else
819  {
820  }
821 }
822 
823 void a2dImageMM::DoSave( wxObject* parent, a2dIOHandlerXmlSerOut& out, a2dXmlSer_flag xmlparts , a2dObjectList* towrite )
824 {
825  a2dRectMM::DoSave( parent, out, xmlparts, towrite );
826  if ( xmlparts == a2dXmlSer_attrib )
827  {
828  if ( m_filename.IsEmpty() )
829  {
830  m_image.SaveFile( GetName(), m_type );
831  out.WriteAttribute( wxT( "filename" ), GetName() );
832  }
833  else
834  {
835  out.WriteAttribute( wxT( "filename" ), m_filename );
836  }
837  out.WriteAttribute( wxT( "type" ), ( wxInt32 ) m_type );
838  out.WriteAttribute( wxT( "patternontop" ), m_drawPatternOnTop );
839  }
840  else
841  {
842  }
843 }
844 #endif //wxART2D_USE_CVGIO
845 
a2dHit m_how
return in which way the object was hit (stroke, fill, ...)
Definition: canobj.h:301
bool GetAttributeValueBool(const wxString &attrib, bool defaultv=false)
Returns the boolean value of an attribute.
Definition: genxmlpars.cpp:537
void DoLoad(wxObject *parent, a2dIOHandlerXmlSerIn &parser, a2dXmlSer_flag xmlparts)
load object specific CVG data
Definition: rectangle.cpp:572
void RenderObject(a2dCanvasObject *torender, int imagew=100, int imageh=100)
render the given object into the image of this object
Definition: canimage.cpp:668
void SetRoot(a2dDrawing *root, bool recurse=true)
Sets this object to a a2dCanvasDocument.
Definition: canobj.cpp:5933
virtual void DoSave(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dXmlSer_flag xmlparts, a2dObjectList *towrite)
write object specific CVGL data
Definition: canimage.cpp:823
void DoLoad(wxObject *parent, a2dIOHandlerXmlSerIn &parser, a2dXmlSer_flag xmlparts)
load object specific CVG data
Definition: canimage.cpp:503
virtual wxString GetName() const
Returns the name of this object, if no name is given the internal id will be returned.
Definition: gen.cpp:1310
wxString FindValidPath(const wxString &filename, bool reportError=true)
Find the first full path for which the file exists.
Definition: gen.cpp:4486
class to map references to objects stored in XML, in order to make the connection later on...
Definition: gen.h:3462
a2dDrawing * m_root
root group for rendering and accessing the canvas&#39;s also contains layer settings
Definition: canobj.h:2525
a2dCanvasOFlags m_flags
holds flags for objects
Definition: canobj.h:2528
double m_maxx
maximum x of rectangle
Definition: rectangle.h:164
Ref Counted base object.
Definition: gen.h:1045
const a2dError a2dError_FileCouldNotOpen
Input and output handler for the XmlSer format.
Definition: genxmlpars.h:819
virtual void SetPending(bool pending)
set this object pending for update
Definition: canobj.cpp:2585
OVERLAP
Result of a a2dBoundingBox intersection or hittest.
Definition: bbox.h:24
virtual a2dObject * DoClone(CloneOptions options, a2dRefMap *refs) const
Clone this object and return a pointer to the new object.
Definition: canimage.cpp:291
a2dGlobal * a2dGlobals
global a2dCanvasGlobal to have easy access to global settings
Definition: artglob.cpp:34
bool DoIsHitWorld(a2dIterC &ic, a2dHitEvent &hitEvent)
Does hit test on the object (exclusif child objects)
Definition: canimage.cpp:341
The base class for all drawable objects in a a2dCanvasDocument.
void SetFilename(const wxString filename, wxBitmapType type, bool doread=true)
set filename and type of image for saving.
Definition: canimage.cpp:249
virtual a2dObject * DoClone(CloneOptions options, a2dRefMap *refs) const
Clone this object and return a pointer to the new object.
Definition: canimage.cpp:450
wxUint16 m_layer
layer of object, default wxLAYER_DEFAULT
Definition: canobj.h:2556
virtual void DoLoad(wxObject *parent, a2dIOHandlerXmlSerIn &parser, a2dXmlSer_flag xmlparts)
load object specific CVG data
Definition: canobj.cpp:5728
void DoLoad(wxObject *parent, a2dIOHandlerXmlSerIn &parser, a2dXmlSer_flag xmlparts)
load object specific CVG data
Definition: canimage.cpp:348
virtual bool DoStartEdit(wxUint16 editmode, wxEditStyle editstyle)
only disables skew and rotation editing
Definition: canimage.cpp:727
a2dCanvasObject is the base class for Canvas Objects.
Definition: canobj.h:371
virtual bool DoStartEdit(wxUint16 editmode, wxEditStyle editstyle)
only used for editable objects and under control of a editing tool.
Definition: rectangle.cpp:291
virtual void DoSave(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dXmlSer_flag xmlparts, a2dObjectList *towrite)
write object specific CVGL data
Definition: canimage.cpp:492
a2dCanvas uses a2dCanvasView for displaying a view on a a2dCanvasDocument.
virtual void DrawCenterRoundedRectangle(double xc, double yc, double width, double height, double radius, bool pixelsize=false)
Draw CenterRoundedRectangle in world coordinates.
Definition: drawer2d.cpp:2040
void DoRender(a2dIterC &ic, OVERLAP clipparent)
render derived object
Definition: canimage.cpp:747
void DoRender(a2dIterC &ic, OVERLAP clipparent)
render derived object
Definition: canimage.cpp:455
double m_minx
minimum x of rectangle
Definition: rectangle.h:158
bool m_selected
object is selected
Definition: candefs.h:235
a2dDrawStyle GetDrawStyle() const
get drawstyle used for drawing.
Definition: drawer2d.h:693
vector path a2dVectorPath derived from a2dCanvasObject
a2dAffineMatrix m_lworld
used for positioning the object (x,y,ang,scale etc.)
Definition: canobj.h:2559
a2dImageMM (will scale/rotate image when needed)
Definition: canimage.h:297
long GetAttributeValueLong(const wxString &attrib, long defaultv=0)
Returns the long value of an attribute.
Definition: genxmlpars.cpp:516
virtual void DoSave(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dXmlSer_flag xmlparts, a2dObjectList *towrite)
write object specific CVGL data
Definition: canimage.cpp:384
void Expand(const a2dPoint2D &, const a2dPoint2D &)
expand boundingbox width two points
Definition: bbox.cpp:155
void SetFilename(const wxString filename, wxBitmapType type, bool doread=true)
set filename and type of image for saving.
Definition: canimage.cpp:700
double GetHeight() const
return height
Definition: rectangle.h:102
virtual void DrawRoundedRectangle(double x, double y, double width, double height, double radius, bool pixelsize=false)
Draw RoundedRectangle in world coordinates.
Definition: drawer2d.cpp:2048
void Transform(const a2dAffineMatrix &tworld)
transform the object using the given matrix
Definition: canobj.h:577
void SetPropertyToObject(a2dObject *obj, const basetype &value, SetFlags setflags=set_none) const
Set the property in obj to value.
Definition: id.inl:238
void SetDrawPatternOnTop(bool drawPatternOnTop)
Sets if the pattern (a rectangle) will be drawn on top of this image.
Definition: canimage.cpp:265
bool DoIsHitWorld(a2dIterC &ic, a2dHitEvent &hitEvent)
Does hit test on the object (exclusif child objects)
Definition: canimage.cpp:787
rectangular shapes derived from a2dCanvasObject
a2dRgbaImage
Definition: canimage.h:220
a2dDrawer2D * GetDrawer2D() const
get current a2dDrawer2D
Definition: canobj.cpp:636
bool m_editable
object can be edited
Definition: candefs.h:295
a2dImage (will scale/rotate image when needed)
Definition: canimage.h:33
a2dCanvasObjectList * GetAsRectangles(const wxColour &col1, const wxColour &col2, bool transform)
return a list of a2dRect, for each pixel which has a colour in the box formed by col1 and col2 ...
Definition: canimage.cpp:605
virtual void DrawImage(const wxImage &image, double x, double y, double width, double height, wxUint8 Opacity=255)=0
Draw wxImage in world coordinates.
A2DGENERALDLLEXP a2dSmrtPtr< a2dGeneralGlobal > a2dGeneralGlobals
a global pointer to get to global instance of important classes.
Definition: comevt.cpp:1148
virtual void DoSave(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dXmlSer_flag xmlparts, a2dObjectList *towrite)
write object specific CVGL data
Definition: canobj.cpp:5569
Input and output handler for the XmlSer format.
Definition: genxmlpars.h:862
a2dImageMM()
constructor
Definition: canimage.cpp:522
virtual a2dObject * DoClone(CloneOptions options, a2dRefMap *refs) const
Clone this object and return a pointer to the new object.
Definition: canimage.cpp:742
a2dPathList & GetImagePathList()
Path for Images.
Definition: artglob.h:152
void RenderObject(a2dCanvasObject *torender, int imagew=100, int imageh=100)
render the given object into the image of this object
Definition: canimage.cpp:217
virtual bool DoStartEdit(wxUint16 editmode, wxEditStyle editstyle)
only used for editable objects and under control of a editing tool.
Definition: canobj.cpp:1739
A 2x3 affine matrix class for 2D transformations.
Definition: afmatrix.h:53
double GetAttributeValueDouble(const wxString &attrib, double defaultv=0)
Returns the double value of an attribute.
Definition: genxmlpars.cpp:474
double m_maxy
maximum y of rectangle
Definition: rectangle.h:167
virtual double GetContourWidth() const
get the Contour width of the shape
Definition: canobj.h:1411
while iterating a a2dCanvasDocument, this holds the context.
Definition: canobj.h:3212
void DoRender(a2dIterC &ic, OVERLAP clipparent)
render derived object
Definition: canimage.cpp:305
void SetDrawPatternOnTop(bool drawPatternOnTop)
Sets if the pattern (a rectangle) will be drawn on top of this image.
Definition: canimage.cpp:716
wxString GetAttributeValue(const wxString &attrib, const wxString &defaultv=wxT(""))
Returns the value of an attribute.
Definition: genxmlpars.cpp:450
virtual void DoSave(wxObject *parent, a2dIOHandlerXmlSerOut &out, a2dXmlSer_flag xmlparts, a2dObjectList *towrite)
write object specific CVGL data
Definition: rectangle.cpp:552
a2dCanvasObjectList * GetAsRectangles(const wxColour &col1, const wxColour &col2, bool transform)
return a list of a2dRect, for each pixel which has a colour in the box formed by col1 and col2 ...
Definition: canimage.cpp:154
bool Translate(double x, double y)
Translate by dx, dy:
Definition: afmatrix.cpp:420
a2dRectMM
Definition: rectangle.h:39
const a2dStroke * a2dTRANSPARENT_STROKE
global a2dStroke stock object for TRANSPARENT stroking
double m_miny
minimum of rectangle
Definition: rectangle.h:161
virtual bool DoStartEdit(wxUint16 editmode, wxEditStyle editstyle)
only disables skew and rotation editing
Definition: canimage.cpp:276
a2dRect
Definition: canprim.h:440
The a2dBoundingBox class stores one a2dBoundingBox of a a2dCanvasObject.
Definition: bbox.h:39
double GetWidth() const
return width
Definition: rectangle.h:99
static a2dHit stock_fill
Stock object for a fill hit.
Definition: polyver.h:137
the a2dDrawingPart is a a2dView specially designed for displaying parts of a a2dDrawing. It uses a a2dDrawer2D to actually redraw things from the document, by giving that a2dDrawer2D as drawing context to the document, and telling the document to redraw a certain rectangular area. At that last is what this class is for. It optimizes the areas to be redrawn after object in the document were changed. To do that it combines redraw areas to a minimal set of redrawing areas. All the administration for this and the way things will be redrawn is from this view.
void DoLoad(wxObject *parent, a2dIOHandlerXmlSerIn &parser, a2dXmlSer_flag xmlparts)
load object specific CVG data
Definition: canimage.cpp:794
a2dBoundingBox DoGetUnTransformedBbox(a2dBboxFlags flags=a2dCANOBJ_BBOX_NON) const
In derived object this should be overriden to calculate the boundingbox of the object without its chi...
Definition: canimage.cpp:296
This template class is for property ids with a known data type.
Definition: id.h:477
a2dImage()
constructor
Definition: canimage.cpp:46
virtual void SetLayer(wxUint16 layer)
set layer index where this object is drawn upon.
Definition: canobj.cpp:5920
void SetContourWidth(double width)
set the Contour width of the shape
Definition: canprim.h:482
list of a2dObject&#39;s
Definition: gen.h:3157
double GetContourWidth() const
get the Contour width of the shape
Definition: rectangle.h:114
CloneOptions
options for cloning
Definition: gen.h:1200
structure to give as parameter to member functions of a2dCanvasObject
Definition: canobj.h:252
#define A2D_PROPID_GSI(type, classname, propname, defaultval)
to define a get set property more easily
Definition: id.h:698
const a2dFill * a2dTRANSPARENT_FILL
global a2dFill stock object for TRANSPARENT filling
general canvas module declarations and classes
wxEditStyle
Definition: canobj.h:109
canimage.cpp Source File -- Sun Oct 12 2014 17:04:13 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation