You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
10 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011 jean-pierre.charras
  5. * Copyright (C) 2022 Mike Williams
  6. * Copyright (C) 2011-2023 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <pcb_draw_panel_gal.h>
  26. #include <plotters/plotter.h>
  27. #include <settings/color_settings.h>
  28. #include <pcb_painter.h>
  29. #include <bitmaps.h>
  30. #include <base_units.h>
  31. #include <common.h>
  32. #include <eda_draw_frame.h>
  33. #include <core/mirror.h>
  34. #include <board.h>
  35. #include <pcb_reference_image.h>
  36. #include <trigo.h>
  37. #include <geometry/shape_rect.h>
  38. #include <wx/mstream.h>
  39. using KIGFX::PCB_PAINTER;
  40. using KIGFX::PCB_RENDER_SETTINGS;
  41. PCB_REFERENCE_IMAGE::PCB_REFERENCE_IMAGE( BOARD_ITEM* aParent, const VECTOR2I& pos,
  42. PCB_LAYER_ID aLayer ) :
  43. BOARD_ITEM( aParent, PCB_REFERENCE_IMAGE_T, aLayer )
  44. {
  45. m_pos = pos;
  46. m_bitmapBase = new BITMAP_BASE();
  47. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  48. }
  49. PCB_REFERENCE_IMAGE::PCB_REFERENCE_IMAGE( const PCB_REFERENCE_IMAGE& aPCBBitmap ) :
  50. BOARD_ITEM( aPCBBitmap )
  51. {
  52. m_pos = aPCBBitmap.m_pos;
  53. m_bitmapBase = new BITMAP_BASE( *aPCBBitmap.m_bitmapBase );
  54. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  55. }
  56. PCB_REFERENCE_IMAGE& PCB_REFERENCE_IMAGE::operator=( const BOARD_ITEM& aItem )
  57. {
  58. wxCHECK_MSG( Type() == aItem.Type(), *this,
  59. wxT( "Cannot assign object type " ) + aItem.GetClass() + wxT( " to type " )
  60. + GetClass() );
  61. if( &aItem != this )
  62. {
  63. BOARD_ITEM::operator=( aItem );
  64. PCB_REFERENCE_IMAGE* bitmap = (PCB_REFERENCE_IMAGE*) &aItem;
  65. delete m_bitmapBase;
  66. m_bitmapBase = new BITMAP_BASE( *bitmap->m_bitmapBase );
  67. m_pos = bitmap->m_pos;
  68. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  69. }
  70. return *this;
  71. }
  72. bool PCB_REFERENCE_IMAGE::ReadImageFile( const wxString& aFullFilename )
  73. {
  74. if( m_bitmapBase->ReadImageFile( aFullFilename ) )
  75. {
  76. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  77. return true;
  78. }
  79. return false;
  80. }
  81. bool PCB_REFERENCE_IMAGE::ReadImageFile( wxMemoryBuffer& aBuffer )
  82. {
  83. if( m_bitmapBase->ReadImageFile( aBuffer ) )
  84. {
  85. m_bitmapBase->SetPixelSizeIu( (float) pcbIUScale.MilsToIU( 1000 ) / m_bitmapBase->GetPPI() );
  86. return true;
  87. }
  88. return false;
  89. }
  90. EDA_ITEM* PCB_REFERENCE_IMAGE::Clone() const
  91. {
  92. return new PCB_REFERENCE_IMAGE( *this );
  93. }
  94. void PCB_REFERENCE_IMAGE::swapData( BOARD_ITEM* aItem )
  95. {
  96. wxCHECK_RET( aItem->Type() == PCB_REFERENCE_IMAGE_T,
  97. wxString::Format( wxT( "% object cannot swap data with %s object." ),
  98. GetClass(), aItem->GetClass() ) );
  99. PCB_REFERENCE_IMAGE* item = (PCB_REFERENCE_IMAGE*) aItem;
  100. std::swap( m_layer, item->m_layer );
  101. std::swap( m_isKnockout, item->m_isKnockout );
  102. std::swap( m_isLocked, item->m_isLocked );
  103. std::swap( m_flags, item->m_flags );
  104. std::swap( m_parent, item->m_parent );
  105. std::swap( m_forceVisible, item->m_forceVisible );
  106. std::swap( m_pos, item->m_pos );
  107. std::swap( m_bitmapBase, item->m_bitmapBase );
  108. }
  109. double PCB_REFERENCE_IMAGE::ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const
  110. {
  111. constexpr double HIDE = std::numeric_limits<double>::max();
  112. PCB_PAINTER* painter = static_cast<PCB_PAINTER*>( aView->GetPainter() );
  113. PCB_RENDER_SETTINGS* renderSettings = painter->GetSettings();
  114. // All bitmaps are drawn on LAYER_DRAW_BITMAPS, but their
  115. // associated board layer controls their visibility.
  116. if( !GetBoard()->IsLayerVisible( m_layer ) )
  117. return HIDE;
  118. if( renderSettings->GetHighContrast()
  119. && renderSettings->m_ContrastModeDisplay == HIGH_CONTRAST_MODE::HIDDEN
  120. && !renderSettings->GetLayerIsHighContrast( m_layer ) )
  121. {
  122. return HIDE;
  123. }
  124. return aView->IsLayerVisible( LAYER_DRAW_BITMAPS ) ? 0.0 : HIDE;
  125. }
  126. const BOX2I PCB_REFERENCE_IMAGE::GetBoundingBox() const
  127. {
  128. // Bitmaps are center origin, BOX2Is need top-left origin
  129. VECTOR2I size = m_bitmapBase->GetSize();
  130. VECTOR2I topLeft = { m_pos.x - size.x / 2, m_pos.y - size.y / 2 };
  131. return BOX2I( topLeft, size );
  132. }
  133. std::shared_ptr<SHAPE> PCB_REFERENCE_IMAGE::GetEffectiveShape( PCB_LAYER_ID aLayer,
  134. FLASHING aFlash ) const
  135. {
  136. BOX2I box = GetBoundingBox();
  137. return std::make_shared<SHAPE_RECT>( box.GetPosition(), box.GetWidth(), box.GetHeight() );
  138. }
  139. const VECTOR2I PCB_REFERENCE_IMAGE::GetSize() const
  140. {
  141. return m_bitmapBase->GetSize();
  142. }
  143. void PCB_REFERENCE_IMAGE::Flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
  144. {
  145. if( aFlipLeftRight )
  146. {
  147. MIRROR( m_pos.x, aCentre.x );
  148. m_bitmapBase->Mirror( false );
  149. }
  150. else
  151. {
  152. MIRROR( m_pos.y, aCentre.y );
  153. m_bitmapBase->Mirror( true );
  154. }
  155. }
  156. void PCB_REFERENCE_IMAGE::Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle )
  157. {
  158. EDA_ANGLE norm( aAngle.AsDegrees(), DEGREES_T );
  159. RotatePoint( m_pos, aCenter, aAngle );
  160. norm.Normalize();
  161. // each call to m_bitmapBase->Rotate() rotates 90 degrees CCW
  162. for( double ang = 45.0; ang < norm.AsDegrees(); ang += 90.0 )
  163. m_bitmapBase->Rotate( false );
  164. }
  165. #if defined( DEBUG )
  166. void PCB_REFERENCE_IMAGE::Show( int nestLevel, std::ostream& os ) const
  167. {
  168. // XML output:
  169. wxString s = GetClass();
  170. NestedSpace( nestLevel, os ) << '<' << s.Lower().mb_str() << m_pos << "/>\n";
  171. }
  172. #endif
  173. bool PCB_REFERENCE_IMAGE::HitTest( const VECTOR2I& aPosition, int aAccuracy ) const
  174. {
  175. BOX2I rect = GetBoundingBox();
  176. rect.Inflate( aAccuracy );
  177. return rect.Contains( aPosition );
  178. }
  179. bool PCB_REFERENCE_IMAGE::HitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
  180. {
  181. BOX2I rect = aRect;
  182. rect.Inflate( aAccuracy );
  183. if( aContained )
  184. return rect.Contains( GetBoundingBox() );
  185. return rect.Intersects( GetBoundingBox() );
  186. }
  187. BITMAPS PCB_REFERENCE_IMAGE::GetMenuImage() const
  188. {
  189. return BITMAPS::image;
  190. }
  191. void PCB_REFERENCE_IMAGE::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame,
  192. std::vector<MSG_PANEL_ITEM>& aList )
  193. {
  194. aList.emplace_back( _( "Reference Image" ), wxEmptyString );
  195. aList.emplace_back( _( "PPI" ), wxString::Format( wxT( "%d "), GetImage()->GetPPI() ) );
  196. aList.emplace_back( _( "Scale" ), wxString::Format( wxT( "%f "), GetImageScale() ) );
  197. aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( GetSize().x ) );
  198. aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( GetSize().y ) );
  199. aList.emplace_back( _( "Layer" ), LayerName( m_layer ) );
  200. }
  201. void PCB_REFERENCE_IMAGE::ViewGetLayers( int aLayers[], int& aCount ) const
  202. {
  203. aCount = 1;
  204. aLayers[0] = BITMAP_LAYER_FOR( m_layer );
  205. }
  206. bool PCB_REFERENCE_IMAGE::operator==( const BOARD_ITEM& aOther ) const
  207. {
  208. if( aOther.Type() != Type() )
  209. return false;
  210. const PCB_REFERENCE_IMAGE& other = static_cast<const PCB_REFERENCE_IMAGE&>( aOther );
  211. if( m_layer != other.m_layer )
  212. return false;
  213. if( m_pos != other.m_pos )
  214. return false;
  215. if( m_bitmapBase->GetSize() != other.m_bitmapBase->GetSize() )
  216. return false;
  217. if( m_bitmapBase->GetPPI() != other.m_bitmapBase->GetPPI() )
  218. return false;
  219. if( m_bitmapBase->GetScale() != other.m_bitmapBase->GetScale() )
  220. return false;
  221. if( m_bitmapBase->GetImageID() != other.m_bitmapBase->GetImageID() )
  222. return false;
  223. if( m_bitmapBase->GetImageData() != other.m_bitmapBase->GetImageData() )
  224. return false;
  225. return true;
  226. }
  227. double PCB_REFERENCE_IMAGE::Similarity( const BOARD_ITEM& aOther ) const
  228. {
  229. if( aOther.Type() != Type() )
  230. return 0.0;
  231. const PCB_REFERENCE_IMAGE& other = static_cast<const PCB_REFERENCE_IMAGE&>( aOther );
  232. double similarity = 1.0;
  233. if( m_layer != other.m_layer )
  234. similarity *= 0.9;
  235. if( m_pos != other.m_pos )
  236. similarity *= 0.9;
  237. if( m_bitmapBase->GetSize() != other.m_bitmapBase->GetSize() )
  238. similarity *= 0.9;
  239. if( m_bitmapBase->GetPPI() != other.m_bitmapBase->GetPPI() )
  240. similarity *= 0.9;
  241. if( m_bitmapBase->GetScale() != other.m_bitmapBase->GetScale() )
  242. similarity *= 0.9;
  243. if( m_bitmapBase->GetImageID() != other.m_bitmapBase->GetImageID() )
  244. similarity *= 0.9;
  245. if( m_bitmapBase->GetImageData() != other.m_bitmapBase->GetImageData() )
  246. similarity *= 0.9;
  247. return similarity;
  248. }
  249. static struct PCB_REFERENCE_IMAGE_DESC
  250. {
  251. PCB_REFERENCE_IMAGE_DESC()
  252. {
  253. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  254. REGISTER_TYPE( PCB_REFERENCE_IMAGE );
  255. propMgr.InheritsAfter( TYPE_HASH( PCB_REFERENCE_IMAGE ), TYPE_HASH( BOARD_ITEM ) );
  256. propMgr.ReplaceProperty( TYPE_HASH( BOARD_ITEM ), _HKI( "Layer" ),
  257. new PROPERTY_ENUM<PCB_REFERENCE_IMAGE, PCB_LAYER_ID, BOARD_ITEM>( _HKI( "Associated Layer" ),
  258. &PCB_REFERENCE_IMAGE::SetLayer, &PCB_REFERENCE_IMAGE::GetLayer ) );
  259. const wxString groupImage = _HKI( "Image Properties" );
  260. propMgr.AddProperty( new PROPERTY<PCB_REFERENCE_IMAGE, double>( _HKI( "Scale" ),
  261. &PCB_REFERENCE_IMAGE::SetImageScale,
  262. &PCB_REFERENCE_IMAGE::GetImageScale ),
  263. groupImage );
  264. // For future use
  265. const wxString greyscale = _HKI( "Greyscale" );
  266. }
  267. } _PCB_REFERENCE_IMAGE_DESC;