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 (const 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 43 of file cimage.cpp.

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

44 {
45  m_wxh = aXsize * aYsize;
46  m_pixels = (unsigned char*)malloc( m_wxh );
47  memset( m_pixels, 0, m_wxh );
48  m_width = aXsize;
49  m_height = aYsize;
51 }
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 54 of file cimage.cpp.

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

55 {
56  m_wxh = aSrcImage.GetWidth() * aSrcImage.GetHeight();
57  m_pixels = (unsigned char*)malloc( m_wxh );
58  memcpy( m_pixels, aSrcImage.GetBuffer(), m_wxh );
59  m_width = aSrcImage.GetWidth();
60  m_height = aSrcImage.GetHeight();
62 }
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:71
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 65 of file cimage.cpp.

References m_pixels.

66 {
67  free( m_pixels );
68 }
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 172 of file cimage.cpp.

References plot8CircleLines().

Referenced by C3D_RENDER_OGL_LEGACY::initializeOpenGL().

173 {
174  int x = aRadius;
175  int y = 0;
176  int xChange = 1 - 2 * aRadius;
177  int yChange = 0;
178  int radiusError = 0;
179 
180  while( x >= y )
181  {
182  plot8CircleLines( aCx, aCy, x, y, aValue );
183  y++;
184  radiusError += yChange;
185  yChange += 2;
186 
187  if( (2 * radiusError + xChange) > 0 )
188  {
189  x--;
190  radiusError += xChange;
191  xChange += 2;
192  }
193  }
194 }
void plot8CircleLines(int aCx, int aCy, int aX, int aY, unsigned char aValue)
Definition: cimage.cpp:112
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 204 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.

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

470 {
471  S_FILTER filter = FILTERS[aFilterType];
472 
473  aInImg->m_wraping = WRAP_CLAMP;
475 
476  std::atomic<size_t> nextRow( 0 );
477  std::atomic<size_t> threadsFinished( 0 );
478 
479  size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
480 
481  for( size_t ii = 0; ii < parallelThreadCount; ++ii )
482  {
483  std::thread t = std::thread( [&]()
484  {
485  for( size_t iy = nextRow.fetch_add( 1 );
486  iy < m_height;
487  iy = nextRow.fetch_add( 1 ) )
488  {
489  for( size_t ix = 0; ix < m_width; ix++ )
490  {
491  int v = 0;
492 
493  for( size_t sy = 0; sy < 5; sy++ )
494  {
495  for( size_t sx = 0; sx < 5; sx++ )
496  {
497  int factor = filter.kernel[sx][sy];
498  unsigned char pixelv = aInImg->Getpixel( ix + sx - 2,
499  iy + sy - 2 );
500 
501  v += pixelv * factor;
502  }
503  }
504 
505  v /= filter.div;
506  v += filter.offset;
507  CLAMP(v, 0, 255);
508  //TODO: This needs to write to a separate buffer
509  m_pixels[ix + iy * m_width] = v;
510  }
511  }
512 
513  threadsFinished++;
514  } );
515 
516  t.detach();
517  }
518 
519  while( threadsFinished < parallelThreadCount )
520  std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
521 }
unsigned int m_height
height of the image
Definition: cimage.h:224
#define CLAMP(n, min, max)
Definition: cimage.cpp:39
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:128
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:330
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 71 of file cimage.cpp.

References m_pixels.

Referenced by CIMAGE(), and OGL_LoadTexture().

72 {
73  return m_pixels;
74 }
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, plot8CircleLines(), and wrapCoords().

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 128 of file cimage.cpp.

References m_pixels, m_width, and wrapCoords().

Referenced by EfxFilter().

129 {
130  if( wrapCoords( &aX, &aY ) )
131  return m_pixels[aX + aY * m_width];
132  else
133  return 0;
134 }
bool wrapCoords(int *aXo, int *aYo) const
Function wrapCoords calculate the coordinates points in accord with the current clamping settings...
Definition: cimage.cpp:77
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 ( ) 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 137 of file cimage.cpp.

References m_height, m_pixels, and m_width.

Referenced by plot8CircleLines().

138 {
139  if( ( aY < 0 ) ||
140  ( aY >= (int)m_height ) ||
141  ( ( aXStart < 0 ) && ( aXEnd < 0) ) ||
142  ( ( aXStart >= (int)m_width ) && ( aXEnd >= (int)m_width) ) )
143  return;
144 
145  if( aXStart > aXEnd )
146  {
147  int swap = aXStart;
148 
149  aXStart = aXEnd;
150  aXEnd = swap;
151  }
152 
153  // Clamp line
154  if( aXStart < 0 )
155  aXStart = 0;
156 
157  if( aXEnd >= (int)m_width )
158  aXEnd = m_width - 1;
159 
160  unsigned char* pixelPtr = &m_pixels[aXStart + aY * m_width];
161  unsigned char* pixelPtrEnd = pixelPtr + (unsigned int)((aXEnd - aXStart) + 1);
162 
163  while( pixelPtr < pixelPtrEnd )
164  {
165  *pixelPtr = aValue;
166  pixelPtr++;
167  }
168 }
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 197 of file cimage.cpp.

References m_pixels, and m_wxh.

198 {
199  for( unsigned int it = 0; it < m_wxh; it++ )
200  m_pixels[it] = 255 - m_pixels[it];
201 }
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 112 of file cimage.cpp.

References Hline().

Referenced by CircleFilled(), and GetHeight().

113 {
114  Hline( aCx - aX, aCx + aX, aCy + aY, aValue );
115  Hline( aCx - aX, aCx + aX, aCy - aY, aValue );
116  Hline( aCx - aY, aCx + aY, aCy + aX, aValue );
117  Hline( aCx - aY, aCx + aY, aCy - aX, aValue );
118 }
void Hline(int aXStart, int aXEnd, int aY, unsigned char aValue)
hline - Draws an horizontal line
Definition: cimage.cpp:137
void CIMAGE::SaveAsPNG ( const 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 536 of file cimage.cpp.

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

537 {
538  DBG_SaveBuffer( aFileName, m_pixels, m_width, m_height );
539 }
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
void DBG_SaveBuffer(const wxString &aFileName, const unsigned char *aInBuffer, unsigned int aXSize, unsigned int aYSize)
unsigned int m_width
width of the image
Definition: cimage.h:223
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 121 of file cimage.cpp.

References m_pixels, m_width, and wrapCoords().

122 {
123  if( wrapCoords( &aX, &aY ) )
124  m_pixels[aX + aY * m_width] = aValue;
125 }
bool wrapCoords(int *aXo, int *aYo) const
Function wrapCoords calculate the coordinates points in accord with the current clamping settings...
Definition: cimage.cpp:77
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 524 of file cimage.cpp.

References CLAMP, i, m_pixels, and m_wxh.

525 {
526  for( unsigned int i = 0; i < m_wxh; i++ )
527  {
528  int v = aNormalizedFloatArray[i] * 255;
529 
530  CLAMP( v, 0, 255 );
531  m_pixels[i] = v;
532  }
533 }
#define CLAMP(n, min, max)
Definition: cimage.cpp:39
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
size_t i
Definition: json11.cpp:597
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 77 of file cimage.cpp.

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

Referenced by GetHeight(), Getpixel(), and Setpixel().

78 {
79  int x = *aXo;
80  int y = *aYo;
81 
82  switch(m_wraping)
83  {
84  case WRAP_CLAMP:
85  x = (x < 0 )?0:x;
86  x = (x >= (int)(m_width - 1))?(m_width - 1):x;
87  y = (y < 0)?0:y;
88  y = (y >= (int)(m_height - 1))?(m_height - 1):y;
89  break;
90 
91  case WRAP_WRAP:
92  x = (x < 0)?((m_width - 1)+x):x;
93  x = (x >= (int)(m_width - 1))?(x - m_width):x;
94  y = (y < 0)?((m_height - 1)+y):y;
95  y = (y >= (int)(m_height - 1))?(y - m_height):y;
96  break;
97 
98  default:
99  break;
100  }
101 
102  if( (x < 0) || (x >= (int)m_width) ||
103  (y < 0) || (y >= (int)m_height) )
104  return false;
105 
106  *aXo = x;
107  *aYo = y;
108 
109  return true;
110 }
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: