KiCad PCB EDA Suite
CIMAGE Class Reference

Class CIMAGE manages a 8-bit channel image. More...

#include <cimage.h>

Public Member Functions

 CIMAGE (unsigned int aXsize, unsigned int aYsize)
 Constructor CIMAGE constructs a CIMAGE based on image size. More...
 
 CIMAGE (const CIMAGE &aSrcImage)
 CIMAGE constructs a CIMAGE based on an existent image. More...
 
 ~CIMAGE ()
 
void Setpixel (int aX, int aY, unsigned char aValue)
 Function Setpixel set a value in a pixel position, position is clamped in accord with the current clamp settings. More...
 
unsigned char Getpixel (int aX, int aY) const
 Function Getpixel get the pixel value from pixel position, position is clamped in accord with the current clamp settings. More...
 
void Hline (int aXStart, int aXEnd, int aY, unsigned char aValue)
 hline - Draws an horizontal line More...
 
void CircleFilled (int aCx, int aCy, int aRadius, unsigned char aValue)
 CircleFilled. More...
 
void CopyFull (const CIMAGE *aImgA, const CIMAGE *aImgB, E_IMAGE_OP aOperation)
 Function CopyFull perform a copy operation, based on operation type. More...
 
void Invert ()
 Function Invert invert the values of image this <- (255 - this) More...
 
void EfxFilter (CIMAGE *aInImg, E_FILTER aFilterType)
 Function EfxFilter apply a filter to the input image and stores it in the image class this <- FilterType(aInImg) More...
 
void SaveAsPNG (wxString aFileName) const
 Function SaveAsPNG save image buffer to a PNG file into the working folder. More...
 
void SetPixelsFromNormalizedFloat (const float *aNormalizedFloatArray)
 Function SetPixelsFromNormalizedFloat set the current channel from a float normalized (0.0 - 1.0) buffer this <- CLAMP(NormalizedFloat * 255) More...
 
unsigned char * GetBuffer () const
 Function GetBuffer get the image buffer pointer. More...
 
unsigned int GetWidth () const
 
unsigned int GetHeight () const
 

Private Member Functions

bool wrapCoords (int *aXo, int *aYo) const
 Function wrapCoords calculate the coordinates points in accord with the current clamping settings. More...
 
void plot8CircleLines (int aCx, int aCy, int aX, int aY, unsigned char aValue)
 

Private Attributes

unsigned char * m_pixels
 buffer to store the image 8bit-channel More...
 
unsigned int m_width
 width of the image More...
 
unsigned int m_height
 height of the image More...
 
unsigned int m_wxh
 width * height precalc value More...
 
E_WRAP m_wraping
 current wrapping type More...
 

Detailed Description

Class CIMAGE manages a 8-bit channel image.

Definition at line 86 of file cimage.h.

Constructor & Destructor Documentation

CIMAGE::CIMAGE ( unsigned int  aXsize,
unsigned int  aYsize 
)

Constructor CIMAGE constructs a CIMAGE based on image size.

Parameters
aXsizex size
aYsizey size

Definition at line 39 of file cimage.cpp.

References m_height, m_pixels, m_width, m_wraping, m_wxh, and WRAP_CLAMP.

40 {
41  m_wxh = aXsize * aYsize;
42  m_pixels = (unsigned char*)malloc( m_wxh );
43  memset( m_pixels, 0, m_wxh );
44  m_width = aXsize;
45  m_height = aYsize;
47 }
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_width
width of the image
Definition: cimage.h:223
unsigned int m_wxh
width * height precalc value
Definition: cimage.h:225
E_WRAP m_wraping
current wrapping type
Definition: cimage.h:226
E_WRAP
Image wrap type enumeration.
Definition: cimage.h:52
Coords are clamped to image size.
Definition: cimage.h:54
CIMAGE::CIMAGE ( const CIMAGE aSrcImage)

CIMAGE constructs a CIMAGE based on an existent image.

It will copy the image to the new

Parameters
aSrcImage

Definition at line 50 of file cimage.cpp.

References GetBuffer(), GetHeight(), GetWidth(), m_height, m_pixels, m_width, m_wraping, m_wxh, and WRAP_CLAMP.

51 {
52  m_wxh = aSrcImage.GetWidth() * aSrcImage.GetHeight();
53  m_pixels = (unsigned char*)malloc( m_wxh );
54  memcpy( m_pixels, aSrcImage.GetBuffer(), m_wxh );
55  m_width = aSrcImage.GetWidth();
56  m_height = aSrcImage.GetHeight();
58 }
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned char * GetBuffer() const
Function GetBuffer get the image buffer pointer.
Definition: cimage.cpp:67
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int GetHeight() const
Definition: cimage.h:207
unsigned int GetWidth() const
Definition: cimage.h:206
unsigned int m_width
width of the image
Definition: cimage.h:223
unsigned int m_wxh
width * height precalc value
Definition: cimage.h:225
E_WRAP m_wraping
current wrapping type
Definition: cimage.h:226
E_WRAP
Image wrap type enumeration.
Definition: cimage.h:52
Coords are clamped to image size.
Definition: cimage.h:54
CIMAGE::~CIMAGE ( )

Definition at line 61 of file cimage.cpp.

References m_pixels.

62 {
63  free( m_pixels );
64 }
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222

Member Function Documentation

void CIMAGE::CircleFilled ( int  aCx,
int  aCy,
int  aRadius,
unsigned char  aValue 
)

CircleFilled.

Parameters
aCx
aCy
aRadius
aValue

Definition at line 168 of file cimage.cpp.

References plot8CircleLines().

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

169 {
170  int x = aRadius;
171  int y = 0;
172  int xChange = 1 - 2 * aRadius;
173  int yChange = 0;
174  int radiusError = 0;
175 
176  while( x >= y )
177  {
178  plot8CircleLines( aCx, aCy, x, y, aValue );
179  y++;
180  radiusError += yChange;
181  yChange += 2;
182 
183  if( (2 * radiusError + xChange) > 0 )
184  {
185  x--;
186  radiusError += xChange;
187  xChange += 2;
188  }
189  }
190 }
void plot8CircleLines(int aCx, int aCy, int aX, int aY, unsigned char aValue)
Definition: cimage.cpp:108
void CIMAGE::CopyFull ( const CIMAGE aImgA,
const CIMAGE aImgB,
E_IMAGE_OP  aOperation 
)

Function CopyFull perform a copy operation, based on operation type.

The result destination is the self image class

Parameters
aImgAan image input
aImgBan image input
aOperationoperation to perform COPY_RAW this <- aImgA COPY_ADD this <- CLAMP(aImgA + aImgB) COPY_SUB this <- CLAMP(aImgA - aImgB) COPY_DIF this <- abs(aImgA - aImgB) COPY_MUL this <- aImgA * aImgB COPY_AND this <- aImgA & aImgB COPY_OR this <- aImgA | aImgB COPY_XOR this <- aImgA ^ aImgB COPY_BLEND50 this <- (aImgA + aImgB) / 2 COPY_MIN this <- (aImgA < aImgB)?aImgA:aImgB COPY_MAX this <- (aImgA > aImgB)?aImgA:aImgB

Definition at line 200 of file cimage.cpp.

References abs, COPY_ADD, COPY_AND, COPY_BLEND50, COPY_DIF, COPY_MAX, COPY_MIN, COPY_MUL, COPY_OR, COPY_RAW, COPY_SUB, COPY_XOR, m_pixels, and m_wxh.

201 {
202  int aV, bV;
203 
204  if( aOperation == COPY_RAW )
205  {
206  if( aImgA == NULL )
207  return;
208  }
209  else
210  {
211  if( (aImgA == NULL) || (aImgB == NULL) )
212  return;
213  }
214 
215  switch(aOperation)
216  {
217  case COPY_RAW:
218  memcpy( m_pixels, aImgA->m_pixels, m_wxh );
219  break;
220 
221  case COPY_ADD:
222  for( unsigned int it = 0;it < m_wxh; it++ )
223  {
224  aV = aImgA->m_pixels[it];
225  bV = aImgB->m_pixels[it];
226 
227  aV = (aV + bV);
228  aV = (aV > 255)?255:aV;
229 
230  m_pixels[it] = aV;
231  }
232  break;
233 
234  case COPY_SUB:
235  for( unsigned int it = 0;it < m_wxh; it++ )
236  {
237  aV = aImgA->m_pixels[it];
238  bV = aImgB->m_pixels[it];
239 
240  aV = (aV - bV);
241  aV = (aV < 0)?0:aV;
242 
243  m_pixels[it] = aV;
244  }
245  break;
246 
247  case COPY_DIF:
248  for( unsigned int it = 0;it < m_wxh; it++ )
249  {
250  aV = aImgA->m_pixels[it];
251  bV = aImgB->m_pixels[it];
252 
253  m_pixels[it] = abs( aV - bV );
254  }
255  break;
256 
257  case COPY_MUL:
258  for( unsigned int it = 0;it < m_wxh; it++ )
259  {
260  aV = aImgA->m_pixels[it];
261  bV = aImgB->m_pixels[it];
262 
263  m_pixels[it] = (unsigned char)((((float)aV / 255.0f) * ((float)bV / 255.0f)) * 255);
264  }
265  break;
266 
267  case COPY_AND:
268  for( unsigned int it = 0;it < m_wxh; it++ )
269  {
270  m_pixels[it] = aImgA->m_pixels[it] & aImgB->m_pixels[it];
271  }
272  break;
273 
274  case COPY_OR:
275  for( unsigned int it = 0;it < m_wxh; it++ )
276  {
277  m_pixels[it] = aImgA->m_pixels[it] | aImgB->m_pixels[it];
278  }
279  break;
280 
281  case COPY_XOR:
282  for( unsigned int it = 0;it < m_wxh; it++ )
283  {
284  m_pixels[it] = aImgA->m_pixels[it] ^ aImgB->m_pixels[it];
285  }
286  break;
287 
288  case COPY_BLEND50:
289  for( unsigned int it = 0;it < m_wxh; it++ )
290  {
291  aV = aImgA->m_pixels[it];
292  bV = aImgB->m_pixels[it];
293 
294  m_pixels[it] = (aV + bV) / 2;
295  }
296  break;
297 
298  case COPY_MIN:
299  for( unsigned int it = 0;it < m_wxh; it++ )
300  {
301  aV = aImgA->m_pixels[it];
302  bV = aImgB->m_pixels[it];
303 
304  m_pixels[it] = (aV < bV)?aV:bV;
305  }
306  break;
307 
308  case COPY_MAX:
309  for( unsigned int it = 0;it < m_wxh; it++ )
310  {
311  aV = aImgA->m_pixels[it];
312  bV = aImgB->m_pixels[it];
313 
314  m_pixels[it] = (aV > bV)?aV:bV;
315  }
316  break;
317 
318  default:
319  break;
320  }
321 }
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
#define abs(a)
Definition: auxiliary.h:84
unsigned int m_wxh
width * height precalc value
Definition: cimage.h:225
Definition: cimage.h:43
void CIMAGE::EfxFilter ( CIMAGE aInImg,
E_FILTER  aFilterType 
)

Function EfxFilter apply a filter to the input image and stores it in the image class this <- FilterType(aInImg)

Parameters
aInImginput image
aFilterTypefilter type to apply

Definition at line 465 of file cimage.cpp.

References CLAMP, S_FILTER::div, Getpixel(), S_FILTER::kernel, m_height, m_pixels, m_width, m_wraping, S_FILTER::offset, and WRAP_CLAMP.

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

466 {
467  S_FILTER filter = FILTERS[aFilterType];
468 
469  aInImg->m_wraping = WRAP_CLAMP;
471 
472  #pragma omp parallel for
473  for( int iy = 0; iy < (int)m_height; iy++)
474  {
475  for( int ix = 0; ix < (int)m_width; ix++ )
476  {
477  int v = 0;
478 
479  for( int sy = 0; sy < 5; sy++ )
480  {
481  for( int sx = 0; sx < 5; sx++ )
482  {
483  int factor = filter.kernel[sx][sy];
484  unsigned char pixelv = aInImg->Getpixel( ix + sx - 2,
485  iy + sy - 2 );
486 
487  v += pixelv * factor;
488  }
489  }
490 
491  v /= filter.div;
492 
493  v += filter.offset;
494 
495  CLAMP(v, 0, 255);
496 
497  m_pixels[ix + iy * m_width] = v;
498  }
499  }
500 }
unsigned int m_height
height of the image
Definition: cimage.h:224
#define CLAMP(n, min, max)
Definition: cimage.cpp:35
unsigned char Getpixel(int aX, int aY) const
Function Getpixel get the pixel value from pixel position, position is clamped in accord with the cur...
Definition: cimage.cpp:124
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned char offset
Definition: cimage.h:78
unsigned int m_width
width of the image
Definition: cimage.h:223
E_WRAP m_wraping
current wrapping type
Definition: cimage.h:226
signed char kernel[5][5]
Definition: cimage.h:76
unsigned int div
Definition: cimage.h:77
5x5 Filter struct parameters
Definition: cimage.h:75
static const S_FILTER FILTERS[]
Definition: cimage.cpp:326
Coords are clamped to image size.
Definition: cimage.h:54
unsigned char * CIMAGE::GetBuffer ( ) const

Function GetBuffer get the image buffer pointer.

Returns
unsigned char * - the pointer of the buffer 8bit channel

Definition at line 67 of file cimage.cpp.

References m_pixels.

Referenced by CIMAGE(), and OGL_LoadTexture().

68 {
69  return m_pixels;
70 }
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int CIMAGE::GetHeight ( ) const
inline

Definition at line 207 of file cimage.h.

References m_height.

Referenced by CIMAGE(), and OGL_LoadTexture().

207 { return m_height; }
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned char CIMAGE::Getpixel ( int  aX,
int  aY 
) const

Function Getpixel get the pixel value from pixel position, position is clamped in accord with the current clamp settings.

Parameters
aXx position
aYy position
Returns
unsigned char - pixel value

Definition at line 124 of file cimage.cpp.

References m_pixels, m_width, and wrapCoords().

Referenced by EfxFilter().

125 {
126  if( wrapCoords( &aX, &aY ) )
127  return m_pixels[aX + aY * m_width];
128  else
129  return 0;
130 }
bool wrapCoords(int *aXo, int *aYo) const
Function wrapCoords calculate the coordinates points in accord with the current clamping settings...
Definition: cimage.cpp:73
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_width
width of the image
Definition: cimage.h:223
unsigned int CIMAGE::GetWidth ( void  ) const
inline

Definition at line 206 of file cimage.h.

References m_width.

Referenced by CIMAGE(), and OGL_LoadTexture().

206 { return m_width; }
unsigned int m_width
width of the image
Definition: cimage.h:223
void CIMAGE::Hline ( int  aXStart,
int  aXEnd,
int  aY,
unsigned char  aValue 
)

hline - Draws an horizontal line

Parameters
aXStart- x start position
aXEnd- x end position
aY- y positoin
aValue- value to add

Definition at line 133 of file cimage.cpp.

References m_height, m_pixels, and m_width.

Referenced by plot8CircleLines().

134 {
135  if( ( aY < 0 ) ||
136  ( aY >= (int)m_height ) ||
137  ( ( aXStart < 0 ) && ( aXEnd < 0) ) ||
138  ( ( aXStart >= (int)m_width ) && ( aXEnd >= (int)m_width) ) )
139  return;
140 
141  if( aXStart > aXEnd )
142  {
143  int swap = aXStart;
144 
145  aXStart = aXEnd;
146  aXEnd = swap;
147  }
148 
149  // Clamp line
150  if( aXStart < 0 )
151  aXStart = 0;
152 
153  if( aXEnd >= (int)m_width )
154  aXEnd = m_width - 1;
155 
156  unsigned char* pixelPtr = &m_pixels[aXStart + aY * m_width];
157  unsigned char* pixelPtrEnd = pixelPtr + (unsigned int)((aXEnd - aXStart) + 1);
158 
159  while( pixelPtr < pixelPtrEnd )
160  {
161  *pixelPtr = aValue;
162  pixelPtr++;
163  }
164 }
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_width
width of the image
Definition: cimage.h:223
void CIMAGE::Invert ( )

Function Invert invert the values of image this <- (255 - this)

Definition at line 193 of file cimage.cpp.

References m_pixels, and m_wxh.

194 {
195  for( unsigned int it = 0; it < m_wxh; it++ )
196  m_pixels[it] = 255 - m_pixels[it];
197 }
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_wxh
width * height precalc value
Definition: cimage.h:225
void CIMAGE::plot8CircleLines ( int  aCx,
int  aCy,
int  aX,
int  aY,
unsigned char  aValue 
)
private

Definition at line 108 of file cimage.cpp.

References Hline().

Referenced by CircleFilled().

109 {
110  Hline( aCx - aX, aCx + aX, aCy + aY, aValue );
111  Hline( aCx - aX, aCx + aX, aCy - aY, aValue );
112  Hline( aCx - aY, aCx + aY, aCy + aX, aValue );
113  Hline( aCx - aY, aCx + aY, aCy - aX, aValue );
114 }
void Hline(int aXStart, int aXEnd, int aY, unsigned char aValue)
hline - Draws an horizontal line
Definition: cimage.cpp:133
void CIMAGE::SaveAsPNG ( wxString  aFileName) const

Function SaveAsPNG save image buffer to a PNG file into the working folder.

each of RGB channel will have the 8bit-channel from the image.

Parameters
aFileNamefime name (without extension)

Definition at line 515 of file cimage.cpp.

References DBG_SaveBuffer(), m_height, m_pixels, and m_width.

516 {
517  DBG_SaveBuffer( aFileName, m_pixels, m_width, m_height );
518 }
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_width
width of the image
Definition: cimage.h:223
void DBG_SaveBuffer(wxString aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
void CIMAGE::Setpixel ( int  aX,
int  aY,
unsigned char  aValue 
)

Function Setpixel set a value in a pixel position, position is clamped in accord with the current clamp settings.

Parameters
aXx position
aYy position
aValuevalue to set the pixel

Definition at line 117 of file cimage.cpp.

References m_pixels, m_width, and wrapCoords().

118 {
119  if( wrapCoords( &aX, &aY ) )
120  m_pixels[aX + aY * m_width] = aValue;
121 }
bool wrapCoords(int *aXo, int *aYo) const
Function wrapCoords calculate the coordinates points in accord with the current clamping settings...
Definition: cimage.cpp:73
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_width
width of the image
Definition: cimage.h:223
void CIMAGE::SetPixelsFromNormalizedFloat ( const float *  aNormalizedFloatArray)

Function SetPixelsFromNormalizedFloat set the current channel from a float normalized (0.0 - 1.0) buffer this <- CLAMP(NormalizedFloat * 255)

Parameters
aNormalizedFloatArraya float array with the same size of the image

Definition at line 503 of file cimage.cpp.

References CLAMP, m_pixels, and m_wxh.

504 {
505  for( unsigned int i = 0; i < m_wxh; i++ )
506  {
507  int v = aNormalizedFloatArray[i] * 255;
508 
509  CLAMP( v, 0, 255 );
510  m_pixels[i] = v;
511  }
512 }
#define CLAMP(n, min, max)
Definition: cimage.cpp:35
unsigned char * m_pixels
buffer to store the image 8bit-channel
Definition: cimage.h:222
unsigned int m_wxh
width * height precalc value
Definition: cimage.h:225
bool CIMAGE::wrapCoords ( int *  aXo,
int *  aYo 
) const
private

Function wrapCoords calculate the coordinates points in accord with the current clamping settings.

Parameters
aXoX coordinate to be converted (output)
aXoY coordinate to be converted (output)
Returns
bool - true if the coordinates are inside the image, false otherwise

Definition at line 73 of file cimage.cpp.

References m_height, m_width, m_wraping, WRAP_CLAMP, and WRAP_WRAP.

Referenced by Getpixel(), and Setpixel().

74 {
75  int x = *aXo;
76  int y = *aYo;
77 
78  switch(m_wraping)
79  {
80  case WRAP_CLAMP:
81  x = (x < 0 )?0:x;
82  x = (x >= (int)(m_width - 1))?(m_width - 1):x;
83  y = (y < 0)?0:y;
84  y = (y >= (int)(m_height - 1))?(m_height - 1):y;
85  break;
86 
87  case WRAP_WRAP:
88  x = (x < 0)?((m_width - 1)+x):x;
89  x = (x >= (int)(m_width - 1))?(x - m_width):x;
90  y = (y < 0)?((m_height - 1)+y):y;
91  y = (y >= (int)(m_height - 1))?(y - m_height):y;
92  break;
93 
94  default:
95  break;
96  }
97 
98  if( (x < 0) || (x >= (int)m_width) ||
99  (y < 0) || (y >= (int)m_height) )
100  return false;
101 
102  *aXo = x;
103  *aYo = y;
104 
105  return true;
106 }
Coords are wrapped arround.
Definition: cimage.h:55
unsigned int m_height
height of the image
Definition: cimage.h:224
unsigned int m_width
width of the image
Definition: cimage.h:223
E_WRAP m_wraping
current wrapping type
Definition: cimage.h:226
Coords are clamped to image size.
Definition: cimage.h:54

Member Data Documentation

unsigned int CIMAGE::m_height
private

height of the image

Definition at line 224 of file cimage.h.

Referenced by CIMAGE(), EfxFilter(), GetHeight(), Hline(), SaveAsPNG(), and wrapCoords().

unsigned char* CIMAGE::m_pixels
private

buffer to store the image 8bit-channel

Definition at line 222 of file cimage.h.

Referenced by CIMAGE(), CopyFull(), EfxFilter(), GetBuffer(), Getpixel(), Hline(), Invert(), SaveAsPNG(), Setpixel(), SetPixelsFromNormalizedFloat(), and ~CIMAGE().

unsigned int CIMAGE::m_width
private

width of the image

Definition at line 223 of file cimage.h.

Referenced by CIMAGE(), EfxFilter(), Getpixel(), GetWidth(), Hline(), SaveAsPNG(), Setpixel(), and wrapCoords().

E_WRAP CIMAGE::m_wraping
private

current wrapping type

Definition at line 226 of file cimage.h.

Referenced by CIMAGE(), EfxFilter(), and wrapCoords().

unsigned int CIMAGE::m_wxh
private

width * height precalc value

Definition at line 225 of file cimage.h.

Referenced by CIMAGE(), CopyFull(), Invert(), and SetPixelsFromNormalizedFloat().


The documentation for this class was generated from the following files: