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.

232 lines
6.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
  5. * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file cimage.h
  26. * @brief one 8bit-channel image definition
  27. */
  28. #ifndef CIMAGE_H
  29. #define CIMAGE_H
  30. #include <wx/string.h>
  31. /// Image operation type
  32. enum class IMAGE_OP
  33. {
  34. RAW,
  35. ADD,
  36. SUB,
  37. DIF,
  38. MUL,
  39. AND,
  40. OR,
  41. XOR,
  42. BLEND50,
  43. MIN,
  44. MAX
  45. };
  46. /// Image wrap type enumeration
  47. enum class IMAGE_WRAP
  48. {
  49. ZERO, ///< Coords that wraps are not evaluated
  50. CLAMP, ///< Coords are clamped to image size
  51. WRAP ///< Coords are wrapped arround
  52. };
  53. /// Filter type enumeration
  54. enum class IMAGE_FILTER
  55. {
  56. HIPASS,
  57. GAUSSIAN_BLUR,
  58. GAUSSIAN_BLUR2,
  59. INVERT_BLUR,
  60. CARTOON,
  61. EMBOSS,
  62. SHARPEN,
  63. MELT,
  64. SOBEL_GX,
  65. SOBEL_GY,
  66. BLUR_3X3,
  67. };
  68. /// 5x5 Filter struct parameters
  69. typedef struct {
  70. signed char kernel[5][5];
  71. unsigned int div;
  72. unsigned char offset;
  73. }S_FILTER;
  74. /**
  75. * CIMAGE
  76. * manages a 8-bit channel image
  77. */
  78. class CIMAGE
  79. {
  80. public:
  81. /**
  82. * Constructor CIMAGE
  83. * constructs a CIMAGE based on image size
  84. * @param aXsize x size
  85. * @param aYsize y size
  86. */
  87. CIMAGE( unsigned int aXsize, unsigned int aYsize );
  88. /**
  89. * @brief CIMAGE
  90. * constructs a CIMAGE based on an existent image. It will copy the image to
  91. * the new
  92. * @param aSrcImage
  93. */
  94. CIMAGE( const CIMAGE &aSrcImage );
  95. ~CIMAGE();
  96. /**
  97. * Function Setpixel
  98. * set a value in a pixel position, position is clamped in accord with the
  99. * current clamp settings
  100. * @param aX x position
  101. * @param aY y position
  102. * @param aValue value to set the pixel
  103. */
  104. void Setpixel( int aX, int aY, unsigned char aValue );
  105. /**
  106. * Function Getpixel
  107. * get the pixel value from pixel position, position is clamped in accord with the
  108. * current clamp settings
  109. * @param aX x position
  110. * @param aY y position
  111. * @return unsigned char - pixel value
  112. */
  113. unsigned char Getpixel( int aX, int aY ) const;
  114. /**
  115. * @brief hline - Draws an horizontal line
  116. * @param aXStart - x start position
  117. * @param aXEnd - x end position
  118. * @param aY - y positoin
  119. * @param aValue - value to add
  120. */
  121. void Hline( int aXStart, int aXEnd, int aY, unsigned char aValue );
  122. /**
  123. * @brief CircleFilled
  124. * @param aCx
  125. * @param aCy
  126. * @param aRadius
  127. * @param aValue
  128. */
  129. void CircleFilled( int aCx, int aCy, int aRadius, unsigned char aValue );
  130. /**
  131. * Function CopyFull
  132. * perform a copy operation, based on operation type.
  133. * The result destination is the self image class
  134. * @param aImgA an image input
  135. * @param aImgB an image input
  136. * @param aOperation operation to perform
  137. * IMAGE_OP::RAW this <- aImgA
  138. * IMAGE_OP::ADD this <- CLAMP(aImgA + aImgB)
  139. * IMAGE_OP::SUB this <- CLAMP(aImgA - aImgB)
  140. * IMAGE_OP::DIF this <- abs(aImgA - aImgB)
  141. * IMAGE_OP::MUL this <- aImgA * aImgB
  142. * IMAGE_OP::AND this <- aImgA & aImgB
  143. * IMAGE_OP::OR this <- aImgA | aImgB
  144. * IMAGE_OP::XOR this <- aImgA ^ aImgB
  145. * IMAGE_OP::BLEND50 this <- (aImgA + aImgB) / 2
  146. * IMAGE_OP::MIN this <- (aImgA < aImgB)?aImgA:aImgB
  147. * IMAGE_OP::MAX this <- (aImgA > aImgB)?aImgA:aImgB
  148. */
  149. void CopyFull( const CIMAGE* aImgA, const CIMAGE* aImgB, IMAGE_OP aOperation );
  150. /**
  151. * Function Invert
  152. * invert the values of image this <- (255 - this)
  153. */
  154. void Invert();
  155. /**
  156. * Function EfxFilter
  157. * apply a filter to the input image and stores it in the image class
  158. * this <- FilterType(aInImg)
  159. * @param aInImg input image
  160. * @param aFilterType filter type to apply
  161. */
  162. void EfxFilter( CIMAGE* aInImg, IMAGE_FILTER aFilterType );
  163. /**
  164. * Function SaveAsPNG
  165. * save image buffer to a PNG file into the working folder.
  166. * each of RGB channel will have the 8bit-channel from the image.
  167. * @param aFileName fime name (without extension)
  168. */
  169. void SaveAsPNG( const wxString& aFileName ) const;
  170. /**
  171. * Function SetPixelsFromNormalizedFloat
  172. * set the current channel from a float normalized (0.0 - 1.0) buffer
  173. * this <- CLAMP(NormalizedFloat * 255)
  174. * @param aNormalizedFloatArray a float array with the same size of the image
  175. */
  176. void SetPixelsFromNormalizedFloat( const float * aNormalizedFloatArray );
  177. /**
  178. * Function GetBuffer
  179. * get the image buffer pointer
  180. * @return unsigned char * - the pointer of the buffer 8bit channel
  181. */
  182. unsigned char* GetBuffer() const;
  183. unsigned int GetWidth() const { return m_width; }
  184. unsigned int GetHeight() const { return m_height; }
  185. private:
  186. /**
  187. * Function wrapCoords
  188. * calculate the coordinates points in accord with the current clamping settings
  189. * @param aXo X coordinate to be converted (output)
  190. * @param aXo Y coordinate to be converted (output)
  191. * @return bool - true if the coordinates are inside the image, false otherwise
  192. */
  193. bool wrapCoords( int *aXo, int *aYo ) const;
  194. void plot8CircleLines( int aCx, int aCy, int aX, int aY, unsigned char aValue );
  195. private:
  196. unsigned char* m_pixels; ///< buffer to store the image 8bit-channel
  197. unsigned int m_width; ///< width of the image
  198. unsigned int m_height; ///< height of the image
  199. unsigned int m_wxh; ///< width * height precalc value
  200. IMAGE_WRAP m_wraping; ///< current wrapping type
  201. };
  202. #endif // CIMAGE_H