2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-07-19 17:35:25 +00:00
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-13 23:02:57 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-03 22:23:00 +00:00
|
|
|
* @file image.cpp
|
|
|
|
* @brief one 8bit-channel image implementation.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "image.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "buffers_debug.h"
|
2019-12-05 14:03:15 +00:00
|
|
|
#include <cstring> // For memcpy
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-05-08 14:34:28 +00:00
|
|
|
#include <algorithm>
|
2018-09-21 04:23:15 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
#ifndef CLAMP
|
2020-12-13 23:02:57 +00:00
|
|
|
#define CLAMP( n, min, max ) {if( n < min ) n=min; else if( n > max ) n = max;}
|
2015-12-08 07:31:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
IMAGE::IMAGE( unsigned int aXsize, unsigned int aYsize )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
m_wxh = aXsize * aYsize;
|
|
|
|
m_pixels = new unsigned char[m_wxh];
|
2015-12-08 07:31:57 +00:00
|
|
|
memset( m_pixels, 0, m_wxh );
|
|
|
|
m_width = aXsize;
|
|
|
|
m_height = aYsize;
|
2019-12-30 13:01:06 +00:00
|
|
|
m_wraping = IMAGE_WRAP::CLAMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
IMAGE::IMAGE( const IMAGE& aSrcImage )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
m_wxh = aSrcImage.GetWidth() * aSrcImage.GetHeight();
|
|
|
|
m_pixels = new unsigned char[m_wxh];
|
2015-12-08 07:31:57 +00:00
|
|
|
memcpy( m_pixels, aSrcImage.GetBuffer(), m_wxh );
|
|
|
|
m_width = aSrcImage.GetWidth();
|
|
|
|
m_height = aSrcImage.GetHeight();
|
2019-12-30 13:01:06 +00:00
|
|
|
m_wraping = IMAGE_WRAP::CLAMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
IMAGE::~IMAGE()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
delete[] m_pixels;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
unsigned char* IMAGE::GetBuffer() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return m_pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool IMAGE::wrapCoords( int* aXo, int* aYo ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
int x = *aXo;
|
|
|
|
int y = *aYo;
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
switch( m_wraping )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_WRAP::CLAMP:
|
2020-12-13 23:02:57 +00:00
|
|
|
x = ( x < 0 ) ? 0 : x;
|
|
|
|
x = ( x >= (int) ( m_width - 1 ) ) ? ( m_width - 1 ) : x;
|
|
|
|
y = ( y < 0 ) ? 0 : y;
|
|
|
|
y = ( y >= (int) ( m_height - 1 ) ) ? ( m_height - 1 ) : y;
|
2015-12-08 07:31:57 +00:00
|
|
|
break;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_WRAP::WRAP:
|
2020-12-13 23:02:57 +00:00
|
|
|
x = ( x < 0 ) ? ( ( m_width - 1 ) + x ) : x;
|
|
|
|
x = ( x >= (int) ( m_width - 1 ) ) ? ( x - m_width ) : x;
|
|
|
|
y = ( y < 0 ) ? ( ( m_height - 1 ) + y ) : y;
|
|
|
|
y = ( y >= (int) ( m_height - 1 ) ) ? ( y - m_height ) : y;
|
2015-12-08 07:31:57 +00:00
|
|
|
break;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-12-10 00:31:44 +00:00
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
if( ( x < 0 ) || ( x >= (int) m_width ) || ( y < 0 ) || ( y >= (int) m_height ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
2015-12-10 00:31:44 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
*aXo = x;
|
|
|
|
*aYo = y;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::plot8CircleLines( int aCx, int aCy, int aX, int aY, unsigned char aValue )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
Hline( aCx - aX, aCx + aX, aCy + aY, aValue );
|
|
|
|
Hline( aCx - aX, aCx + aX, aCy - aY, aValue );
|
|
|
|
Hline( aCx - aY, aCx + aY, aCy + aX, aValue );
|
|
|
|
Hline( aCx - aY, aCx + aY, aCy - aX, aValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::Setpixel( int aX, int aY, unsigned char aValue )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( wrapCoords( &aX, &aY ) )
|
|
|
|
m_pixels[aX + aY * m_width] = aValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
unsigned char IMAGE::Getpixel( int aX, int aY ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
if( wrapCoords( &aX, &aY ) )
|
|
|
|
return m_pixels[aX + aY * m_width];
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::Hline( int aXStart, int aXEnd, int aY, unsigned char aValue )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-13 23:02:57 +00:00
|
|
|
if( ( aY < 0 ) || ( aY >= (int) m_height ) || ( ( aXStart < 0 ) && ( aXEnd < 0 ) )
|
|
|
|
|| ( ( aXStart >= (int) m_width ) && ( aXEnd >= (int) m_width ) ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( aXStart > aXEnd )
|
|
|
|
{
|
|
|
|
int swap = aXStart;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
aXStart = aXEnd;
|
|
|
|
aXEnd = swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clamp line
|
|
|
|
if( aXStart < 0 )
|
|
|
|
aXStart = 0;
|
|
|
|
|
|
|
|
if( aXEnd >= (int)m_width )
|
|
|
|
aXEnd = m_width - 1;
|
|
|
|
|
|
|
|
unsigned char* pixelPtr = &m_pixels[aXStart + aY * m_width];
|
2020-12-13 23:02:57 +00:00
|
|
|
unsigned char* pixelPtrEnd = pixelPtr + (unsigned int) ( ( aXEnd - aXStart ) + 1 );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
while( pixelPtr < pixelPtrEnd )
|
|
|
|
{
|
|
|
|
*pixelPtr = aValue;
|
|
|
|
pixelPtr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
// Based on paper
|
|
|
|
// http://web.engr.oregonstate.edu/~sllu/bcircle.pdf
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::CircleFilled( int aCx, int aCy, int aRadius, unsigned char aValue )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
int x = aRadius;
|
|
|
|
int y = 0;
|
|
|
|
int xChange = 1 - 2 * aRadius;
|
|
|
|
int yChange = 0;
|
|
|
|
int radiusError = 0;
|
|
|
|
|
|
|
|
while( x >= y )
|
|
|
|
{
|
|
|
|
plot8CircleLines( aCx, aCy, x, y, aValue );
|
|
|
|
y++;
|
|
|
|
radiusError += yChange;
|
|
|
|
yChange += 2;
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
if( ( 2 * radiusError + xChange ) > 0 )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
x--;
|
|
|
|
radiusError += xChange;
|
|
|
|
xChange += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::Invert()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
for( unsigned int it = 0; it < m_wxh; it++ )
|
|
|
|
m_pixels[it] = 255 - m_pixels[it];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::CopyFull( const IMAGE* aImgA, const IMAGE* aImgB, IMAGE_OP aOperation )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
int aV, bV;
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
if( aOperation == IMAGE_OP::RAW )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
if( aImgA == nullptr )
|
2015-12-08 07:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( aImgA == nullptr ) || ( aImgB == nullptr ) )
|
2015-12-10 00:31:44 +00:00
|
|
|
return;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
switch( aOperation )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::RAW:
|
2015-12-08 07:31:57 +00:00
|
|
|
memcpy( m_pixels, aImgA->m_pixels, m_wxh );
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::ADD:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
|
|
|
aV = (aV + bV);
|
|
|
|
aV = (aV > 255)?255:aV;
|
|
|
|
|
|
|
|
m_pixels[it] = aV;
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::SUB:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
|
|
|
aV = (aV - bV);
|
|
|
|
aV = (aV < 0)?0:aV;
|
|
|
|
|
|
|
|
m_pixels[it] = aV;
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::DIF:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
m_pixels[it] = abs( aV - bV );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::MUL:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
m_pixels[it] =
|
|
|
|
(unsigned char) ( ( ( (float) aV / 255.0f ) * ( (float) bV / 255.0f ) ) * 255 );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::AND:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
m_pixels[it] = aImgA->m_pixels[it] & aImgB->m_pixels[it];
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::OR:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
m_pixels[it] = aImgA->m_pixels[it] | aImgB->m_pixels[it];
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::XOR:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
m_pixels[it] = aImgA->m_pixels[it] ^ aImgB->m_pixels[it];
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-10 00:31:44 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::BLEND50:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
|
|
|
m_pixels[it] = (aV + bV) / 2;
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::MIN:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
|
|
|
m_pixels[it] = (aV < bV)?aV:bV;
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
case IMAGE_OP::MAX:
|
2015-12-08 07:31:57 +00:00
|
|
|
for( unsigned int it = 0;it < m_wxh; it++ )
|
|
|
|
{
|
|
|
|
aV = aImgA->m_pixels[it];
|
|
|
|
bV = aImgB->m_pixels[it];
|
|
|
|
|
|
|
|
m_pixels[it] = (aV > bV)?aV:bV;
|
|
|
|
}
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
default:
|
2020-12-13 23:02:57 +00:00
|
|
|
break;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
// TIP: If you want create or test filters you can use GIMP
|
|
|
|
// with a generic convolution matrix and get the values from there.
|
|
|
|
// http://docs.gimp.org/nl/plug-in-convmatrix.html
|
2019-12-30 13:01:06 +00:00
|
|
|
// clang-format off
|
2015-12-08 07:31:57 +00:00
|
|
|
static const S_FILTER FILTERS[] = {
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::HIPASS
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 0, -1, -1, -1, 0},
|
|
|
|
{-1, 2, -4, 2, -1},
|
|
|
|
{-1, -4, 13, -4, -1},
|
|
|
|
{-1, 2, -4, 2, -1},
|
|
|
|
{ 0, -1, -1, -1, 0}
|
|
|
|
},
|
|
|
|
7,
|
|
|
|
255
|
|
|
|
},
|
2015-12-10 00:31:44 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::GAUSSIAN_BLUR
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 3, 5, 7, 5, 3},
|
|
|
|
{ 5, 9, 12, 9, 5},
|
|
|
|
{ 7, 12, 20, 12, 7},
|
|
|
|
{ 5, 9, 12, 9, 5},
|
|
|
|
{ 3, 5, 7, 5, 3}
|
|
|
|
},
|
|
|
|
182,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::GAUSSIAN_BLUR2
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 1, 4, 7, 4, 1},
|
|
|
|
{ 4, 16, 26, 16, 4},
|
|
|
|
{ 7, 26, 41, 26, 7},
|
|
|
|
{ 4, 16, 26, 16, 4},
|
|
|
|
{ 1, 4, 7, 4, 1}
|
|
|
|
},
|
|
|
|
273,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::INVERT_BLUR
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
{ { 0, 0, 0, 0, 0},
|
|
|
|
{ 0, 0, -1, 0, 0},
|
|
|
|
{ 0, -1, 0, -1, 0},
|
|
|
|
{ 0, 0, -1, 0, 0},
|
|
|
|
{ 0, 0, 0, 0, 0}
|
|
|
|
},
|
|
|
|
4,
|
|
|
|
255
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::CARTOON
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ {-1, -1, -1, -1, 0},
|
|
|
|
{-1, 0, 0, 0, 0},
|
|
|
|
{-1, 0, 4, 0, 0},
|
|
|
|
{ 0, 0, 0, 1, 0},
|
|
|
|
{ 0, 0, 0, 0, 4}
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::EMBOSS
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ {-1, -1, -1, -1, 0},
|
|
|
|
{-1, -1, -1, 0, 1},
|
|
|
|
{-1, -1, 0, 1, 1},
|
|
|
|
{-1, 0, 1, 1, 1},
|
|
|
|
{ 0, 1, 1, 1, 1}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
128
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::SHARPEN
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ {-1, -1, -1, -1, -1},
|
|
|
|
{-1, 2, 2, 2, -1},
|
|
|
|
{-1, 2, 8, 2, -1},
|
|
|
|
{-1, 2, 2, 2, -1},
|
|
|
|
{-1, -1, -1, -1, -1}
|
|
|
|
},
|
|
|
|
8,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::MELT
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 4, 2, 6, 8, 1},
|
|
|
|
{ 1, 2, 5, 4, 2},
|
|
|
|
{ 0, -1, 1, -1, 0},
|
|
|
|
{ 0, 0, -2, 0, 0},
|
|
|
|
{ 0, 0, 0, 0, 0}
|
|
|
|
},
|
|
|
|
32,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::SOBEL_GX
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 0, 0, 0, 0, 0},
|
|
|
|
{ 0, -1, 0, 1, 0},
|
|
|
|
{ 0, -2, 0, 2, 0},
|
|
|
|
{ 0, -1, 0, 1, 0},
|
|
|
|
{ 0, 0, 0, 0, 0}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::SOBEL_GY
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
{ { 1, 2, 4, 2, 1},
|
|
|
|
{-1, -1, 0, 1, 1},
|
|
|
|
{-2, -2, 0, 2, 2},
|
|
|
|
{-1, -1, 0, 1, 1},
|
|
|
|
{-1, -2, -4, -2, -1},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
0
|
2016-07-19 17:35:25 +00:00
|
|
|
},
|
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
// IMAGE_FILTER::BLUR_3X3
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
{ { 0, 0, 0, 0, 0},
|
|
|
|
{ 0, 1, 2, 1, 0},
|
|
|
|
{ 0, 2, 4, 2, 0},
|
|
|
|
{ 0, 1, 2, 1, 0},
|
|
|
|
{ 0, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
16,
|
|
|
|
0
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
};// Filters
|
2019-12-30 13:01:06 +00:00
|
|
|
// clang-format on
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
2020-12-13 23:02:57 +00:00
|
|
|
/// @todo: This function can be optimized slipping it between the edges and
|
|
|
|
/// do it without use the getpixel function.
|
|
|
|
/// Optimization can be done to m_pixels[ix + iy * m_width]
|
|
|
|
/// but keep in mind the parallel process of the algorithm
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::EfxFilter( IMAGE* aInImg, IMAGE_FILTER aFilterType )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2019-12-30 13:01:06 +00:00
|
|
|
S_FILTER filter = FILTERS[static_cast<int>( aFilterType )];
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2019-12-30 13:01:06 +00:00
|
|
|
aInImg->m_wraping = IMAGE_WRAP::CLAMP;
|
|
|
|
m_wraping = IMAGE_WRAP::CLAMP;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
std::atomic<size_t> nextRow( 0 );
|
|
|
|
std::atomic<size_t> threadsFinished( 0 );
|
|
|
|
|
|
|
|
size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
|
|
|
|
|
|
|
|
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
std::thread t = std::thread( [&]()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
for( size_t iy = nextRow.fetch_add( 1 ); iy < m_height; iy = nextRow.fetch_add( 1 ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
for( size_t ix = 0; ix < m_width; ix++ )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
int v = 0;
|
|
|
|
|
|
|
|
for( size_t sy = 0; sy < 5; sy++ )
|
|
|
|
{
|
|
|
|
for( size_t sx = 0; sx < 5; sx++ )
|
|
|
|
{
|
|
|
|
int factor = filter.kernel[sx][sy];
|
2021-01-02 21:05:29 +00:00
|
|
|
unsigned char pixelv = aInImg->Getpixel( ix + sx - 2, iy + sy - 2 );
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
v += pixelv * factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v /= filter.div;
|
|
|
|
v += filter.offset;
|
|
|
|
CLAMP(v, 0, 255);
|
2020-12-13 23:02:57 +00:00
|
|
|
|
|
|
|
/// @todo This needs to write to a separate buffer.
|
2018-09-21 04:23:15 +00:00
|
|
|
m_pixels[ix + iy * m_width] = v;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-10 00:31:44 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
threadsFinished++;
|
|
|
|
} );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2018-09-21 04:23:15 +00:00
|
|
|
t.detach();
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
2018-09-21 04:23:15 +00:00
|
|
|
|
|
|
|
while( threadsFinished < parallelThreadCount )
|
|
|
|
std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-20 21:19:11 +00:00
|
|
|
void IMAGE::EfxFilter_SkipCenter( IMAGE* aInImg, IMAGE_FILTER aFilterType, unsigned int aRadius )
|
|
|
|
{
|
|
|
|
if( ( !aInImg ) || ( m_width != aInImg->m_width ) || ( m_height != aInImg->m_height ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
S_FILTER filter = FILTERS[static_cast<int>( aFilterType )];
|
|
|
|
|
|
|
|
aInImg->m_wraping = IMAGE_WRAP::ZERO;
|
|
|
|
|
|
|
|
const unsigned int radiusSquared = aRadius * aRadius;
|
|
|
|
|
|
|
|
const unsigned int xCenter = m_width / 2;
|
|
|
|
const unsigned int yCenter = m_height / 2;
|
|
|
|
|
|
|
|
for( size_t iy = 0; iy < m_height; iy++ )
|
|
|
|
{
|
|
|
|
int yc = iy - yCenter;
|
|
|
|
|
2021-06-06 19:30:31 +00:00
|
|
|
unsigned int ycsq = yc * yc;
|
2021-04-20 21:19:11 +00:00
|
|
|
|
|
|
|
for( size_t ix = 0; ix < m_width; ix++ )
|
|
|
|
{
|
|
|
|
int xc = ix - xCenter;
|
|
|
|
|
2021-06-06 19:30:31 +00:00
|
|
|
unsigned int xcsq = xc * xc;
|
2021-04-20 21:19:11 +00:00
|
|
|
|
2021-06-06 19:30:31 +00:00
|
|
|
if( ( xcsq + ycsq ) < radiusSquared )
|
2021-04-20 21:19:11 +00:00
|
|
|
{
|
|
|
|
const unsigned int offset = ix + iy * m_width;
|
|
|
|
|
|
|
|
m_pixels[offset] = aInImg->m_pixels[offset];
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int v = 0;
|
|
|
|
|
|
|
|
for( size_t sy = 0; sy < 5; sy++ )
|
|
|
|
{
|
|
|
|
for( size_t sx = 0; sx < 5; sx++ )
|
|
|
|
{
|
|
|
|
int factor = filter.kernel[sx][sy];
|
|
|
|
unsigned char pixelv = aInImg->Getpixel( ix + sx - 2, iy + sy - 2 );
|
|
|
|
|
|
|
|
v += pixelv * factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v /= filter.div;
|
|
|
|
v += filter.offset;
|
|
|
|
CLAMP(v, 0, 255);
|
|
|
|
|
|
|
|
m_pixels[ix + iy * m_width] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::SetPixelsFromNormalizedFloat( const float* aNormalizedFloatArray )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < m_wxh; i++ )
|
|
|
|
{
|
|
|
|
int v = aNormalizedFloatArray[i] * 255;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
CLAMP( v, 0, 255 );
|
2015-12-08 07:31:57 +00:00
|
|
|
m_pixels[i] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void IMAGE::SaveAsPNG( const wxString& aFileName ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
DBG_SaveBuffer( aFileName, m_pixels, m_width, m_height );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|