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.

268 lines
7.0 KiB

// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
  1. /**
  2. * @file sch_bitmap.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2011 jean-pierre.charras
  8. * Copyright (C) 2011 KiCad Developers, see change_log.txt for contributors.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include "fctsys.h"
  28. #include "gr_basic.h"
  29. #include "macros.h"
  30. #include "class_drawpanel.h"
  31. #include "trigo.h"
  32. #include "common.h"
  33. #include "richio.h"
  34. #include "plot_common.h"
  35. #include "class_bitmap_base.h"
  36. #include <wx/mstream.h>
  37. /**********************/
  38. /* class BITMAP_BASE */
  39. /**********************/
  40. BITMAP_BASE::BITMAP_BASE( const wxPoint& pos )
  41. {
  42. m_Scale = 1.0; // 1.0 = original bitmap size
  43. m_bitmap = NULL;
  44. m_image = NULL;
  45. m_pixelScaleFactor = 3.33; // a value OK for bitmaps using 300 PPI
  46. // (Eeschema uses currently 1000PPI
  47. }
  48. BITMAP_BASE::BITMAP_BASE( const BITMAP_BASE& aSchBitmap )
  49. {
  50. m_Scale = aSchBitmap.m_Scale;
  51. m_pixelScaleFactor = aSchBitmap.m_pixelScaleFactor;
  52. m_image = new wxImage( *aSchBitmap.m_image );
  53. m_bitmap = new wxBitmap( *m_image );
  54. }
  55. /**
  56. * Function ImportData
  57. * Copy aItem image to me and update m_bitmap
  58. */
  59. void BITMAP_BASE::ImportData( BITMAP_BASE* aItem )
  60. {
  61. *m_image = *aItem->m_image;
  62. *m_bitmap = *aItem->m_bitmap;
  63. m_Scale = aItem->m_Scale;
  64. m_pixelScaleFactor = aItem->m_pixelScaleFactor;
  65. }
  66. bool BITMAP_BASE::ReadImageFile( const wxString& aFullFilename )
  67. {
  68. wxImage* new_image = new wxImage();
  69. if( !new_image->LoadFile( aFullFilename ) )
  70. {
  71. delete new_image;
  72. return false;
  73. }
  74. delete m_image;
  75. m_image = new_image;
  76. m_bitmap = new wxBitmap( *m_image );
  77. return true;
  78. }
  79. bool BITMAP_BASE::SaveData( FILE* aFile ) const
  80. {
  81. if( m_image )
  82. {
  83. wxMemoryOutputStream stream;
  84. m_image->SaveFile( stream, wxBITMAP_TYPE_PNG );
  85. // Write binary data in hexadecimal form (ASCII)
  86. wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
  87. char* begin = (char*) buffer->GetBufferStart();
  88. int ii;
  89. for( ii = 0; begin <= buffer->GetBufferEnd(); begin++, ii++ )
  90. {
  91. if( ii >= 32 )
  92. {
  93. ii = 0;
  94. if( fprintf( aFile, "\n" ) == EOF )
  95. return false;
  96. }
  97. if( fprintf( aFile, "%2.2X ", *begin & 0xFF ) == EOF )
  98. return false;
  99. }
  100. if( fprintf( aFile, "$EndBitmap" ) == EOF )
  101. return false;
  102. }
  103. return true;
  104. }
  105. bool BITMAP_BASE::LoadData( LINE_READER& aLine, wxString& aErrorMsg )
  106. {
  107. wxMemoryOutputStream stream;
  108. char* line;
  109. while( true )
  110. {
  111. if( !aLine.ReadLine() )
  112. return false;
  113. line = aLine.Line();
  114. if( strnicmp( line, "EndData", 4 ) == 0 )
  115. {
  116. // all the PNG date is read.
  117. // We expect here m_image and m_bitmap are void
  118. m_image = new wxImage();
  119. wxMemoryInputStream istream( stream );
  120. m_image->LoadFile( istream, wxBITMAP_TYPE_PNG );
  121. m_bitmap = new wxBitmap( *m_image );
  122. break;
  123. }
  124. // Read PNG data, stored in hexadecimal,
  125. // each byte = 2 hexadecimal digits and a space between 2 bytes
  126. // and put it in memory stream buffer
  127. int len = strlen( line );
  128. for( ; len > 0; len -= 3, line += 3 )
  129. {
  130. int value = 0;
  131. if( sscanf( line, "%X", &value ) == 1 )
  132. stream.PutC( (char) value );
  133. else
  134. break;
  135. }
  136. }
  137. return true;
  138. }
  139. EDA_RECT BITMAP_BASE::GetBoundingBox() const
  140. {
  141. EDA_RECT rect;
  142. wxSize size = GetSize();
  143. rect.Inflate( size.x / 2, size.y / 2 );
  144. return rect;
  145. }
  146. void BITMAP_BASE::DrawBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPos )
  147. {
  148. if( m_bitmap == NULL )
  149. return;
  150. wxPoint pos = aPos;
  151. wxSize size = GetSize();
  152. // To draw the bitmap, pos is the upper left corner position
  153. pos.x -= size.x / 2;
  154. pos.y -= size.y / 2;
  155. double scale;
  156. int logicalOriginX, logicalOriginY;
  157. aDC->GetUserScale( &scale, &scale );
  158. aDC->GetLogicalOrigin( &logicalOriginX, &logicalOriginY );
  159. aDC->SetUserScale( scale * GetScalingFactor(), scale * GetScalingFactor() );
  160. aDC->SetLogicalOrigin( logicalOriginX / GetScalingFactor(),
  161. logicalOriginY / GetScalingFactor() );
  162. aDC->DrawBitmap( *m_bitmap,
  163. KiROUND( pos.x / GetScalingFactor() ),
  164. KiROUND( pos.y / GetScalingFactor() ),
  165. true );
  166. aDC->SetUserScale( scale, scale );
  167. aDC->SetLogicalOrigin( logicalOriginX, logicalOriginY );
  168. }
  169. /* Function GetSize
  170. * returns the actual size (in user units, not in pixels) of the image
  171. */
  172. wxSize BITMAP_BASE::GetSize() const
  173. {
  174. wxSize size;
  175. if( m_bitmap )
  176. {
  177. size.x = m_bitmap->GetWidth();
  178. size.y = m_bitmap->GetHeight();
  179. size.x = KiROUND( size.x * GetScalingFactor() );
  180. size.y = KiROUND( size.y * GetScalingFactor() );
  181. }
  182. return size;
  183. }
  184. /*
  185. * Mirror image vertically (i.e. relative to its horizontal X axis )
  186. * or horizontally (i.e relative to its vertical Y axis)
  187. * param aVertically = false to mirror horizontally
  188. * or true to mirror vertically
  189. */
  190. void BITMAP_BASE::Mirror( bool aVertically )
  191. {
  192. if( m_image )
  193. {
  194. *m_image = m_image->Mirror( not aVertically );
  195. RebuildBitmap();
  196. }
  197. }
  198. void BITMAP_BASE::Rotate( bool aRotateCCW )
  199. {
  200. if( m_image )
  201. {
  202. *m_image = m_image->Rotate90( aRotateCCW );
  203. RebuildBitmap();
  204. }
  205. }
  206. void BITMAP_BASE::PlotImage( PLOTTER* aPlotter,
  207. const wxPoint& aPos,
  208. EDA_COLOR_T aDefaultColor,
  209. int aDefaultPensize )
  210. {
  211. if( m_image == NULL )
  212. return;
  213. // These 2 lines are useful only fot plotters that cannot plot a bitmap
  214. // and plot a rectangle instead of.
  215. aPlotter->SetColor( aDefaultColor );
  216. aPlotter->SetCurrentLineWidth( aDefaultPensize );
  217. aPlotter->PlotImage( *m_image, aPos, GetScalingFactor() );
  218. }