EIMInterface.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "EIMInterface.h"
  2. #include "EIMBase.h"
  3. #include "scale.h"
  4. #include "../Container/ExtendedImage.h"
  5. #include "../IO/DicomIO.h"
  6. #include "../Mouchard/Mouchard.h"
  7. #include <cmath>
  8. const CScale *g_pCurrentScale;
  9. #define SQR( x ) ( ( x ) * ( x ) )
  10. EIMInterface::EIMInterface()
  11. : Singleton< EIMInterface >(),
  12. m_p0( 0, 0 ),
  13. m_p1( 0, 0 )
  14. {
  15. }
  16. bool EIMInterface::initialize( std::string fileName, double pixelSizeX, double pixelSizeY )
  17. {
  18. bool status = false;
  19. if ( Mouchard::getInstance().IsDLLValid() )
  20. {
  21. m_image.Destroy();
  22. double m_pixelSizeX = pixelSizeX;
  23. double m_pixelSizeY = pixelSizeY;
  24. if ( DicomIO::getInstance().read( fileName, m_image ) )
  25. {
  26. if ( ( pixelSizeX < 0.0 ) && ( pixelSizeY < 0.0 ) )
  27. {
  28. m_pixelSizeX = m_image.GetResolutionX();
  29. m_pixelSizeY = m_image.GetResolutionY();
  30. }
  31. status = true;
  32. }
  33. else
  34. {
  35. status = false;
  36. }
  37. if ( status && ( m_pixelSizeX > 0.0 ) && ( m_pixelSizeY > 0.0 ) )
  38. {
  39. double pixelSize = m_pixelSizeX < m_pixelSizeY ? m_pixelSizeX : m_pixelSizeY;
  40. g_pCurrentScale = new CScale("DLL scale", pixelSize, pixelSize);
  41. }
  42. }
  43. return status;
  44. }
  45. bool EIMInterface::initialize( const char* buffer, int width, int height, int bpp, bool upsideDown, double pixelSizeX, double pixelSizeY )
  46. {
  47. if ( Mouchard::getInstance().IsDLLValid() && buffer &&
  48. ( pixelSizeX > 0.0 ) && ( pixelSizeY > 0.0 ) &&
  49. ( ( bpp == 8 ) || ( bpp == 24 ) || ( bpp == 32 ) ) )
  50. {
  51. bool padding = false;
  52. const unsigned long destWidth = 3 * width;
  53. const long nextRow = upsideDown ? -2 * width * bpp / 8 : 0;
  54. register const char* p = buffer + ( upsideDown ? ( height - 1 ) * width * bpp / 8 : 0 );
  55. const int gap = padding ? ( 4 - destWidth & 0x3 ) & 0x3 : 0;
  56. //const unsigned long count = ( destWidth + gap ) * height;
  57. m_image.Destroy();
  58. if ( m_image.Create( width, height, 24 ) )
  59. {
  60. if ( bpp == 8 )
  61. {
  62. register char* q = m_image.GetBuffer();
  63. register int x;
  64. register int y;
  65. register char value;
  66. for ( y = height; y--; p += nextRow, q += gap )
  67. {
  68. x = width;
  69. while ( x-- )
  70. {
  71. value = *p++;
  72. *q++ = value;
  73. *q++ = value;
  74. *q++ = value;
  75. }
  76. }
  77. }
  78. else if ( bpp == 24 )
  79. {
  80. register char* q = m_image.GetBuffer();
  81. register int x;
  82. register int y;
  83. for ( y = height; y--; p += nextRow, q += gap )
  84. {
  85. x = destWidth;
  86. while ( x-- )
  87. {
  88. *q++ = *p++;
  89. }
  90. }
  91. }
  92. else if ( bpp == 32 )
  93. {
  94. register char* q = m_image.GetBuffer();
  95. register int x;
  96. register int y;
  97. for ( y = height; y--; p += nextRow, q += gap )
  98. {
  99. x = width;
  100. while ( x-- )
  101. {
  102. *q++ = *p;
  103. *q++ = *( p + 1 );
  104. *q++ = *( p + 2 );
  105. p += 4;
  106. }
  107. }
  108. }
  109. double pixelSize = ( pixelSizeX < pixelSizeY ) ? pixelSizeX : pixelSizeY;
  110. g_pCurrentScale = new CScale("DLL scale", pixelSize, pixelSize);
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. bool EIMInterface::setFirstPoint( int x, int y )
  117. {
  118. if ( !m_image.IsNull() )
  119. {
  120. m_p0.x = x;
  121. m_p0.y = y;
  122. return true;
  123. }
  124. return false;
  125. }
  126. float EIMInterface::getDistanceToFirstPoint( int x, int y )
  127. {
  128. if ( g_pCurrentScale && !m_image.IsNull() )
  129. {
  130. Point pt( x, y );
  131. return (float)g_pCurrentScale->Distance( pt, m_p0 );
  132. }
  133. return 0.0f;
  134. }
  135. bool EIMInterface::setSecondPoint( int x, int y, CEIMResult* result )
  136. {
  137. if ( !m_image.IsNull() && result )
  138. {
  139. m_p1.x = x;
  140. m_p1.y = y;
  141. CEIMBase ceim;
  142. ceim.SetEimAssisted( true );
  143. ceim.Measure(&m_image, m_p0, m_p1, false, result);
  144. result->result->p0.x = m_p0.x;
  145. result->result->p0.y = m_p0.y;
  146. result->result->p1.x = m_p1.x;
  147. result->result->p1.y = m_p1.y;
  148. result->result->distance = g_pCurrentScale->Distance( result->result->numberOfPoints );
  149. return true;
  150. }
  151. return false;
  152. }
  153. int EIMInterface::getImageWidth()
  154. {
  155. return m_image.GetWidth();
  156. }
  157. int EIMInterface::getImageHeight()
  158. {
  159. return m_image.GetHeight();
  160. }
  161. int EIMInterface::getImageBitsPerPixel()
  162. {
  163. return m_image.GetBpp();
  164. }
  165. char* EIMInterface::getPixelArray()
  166. {
  167. return m_image.GetBuffer();
  168. }