image – Pixel manipulation libraries / C ++ arrays

I was tired of always manipulating the pixels one by one. So I started creating my own template-based header to perform a pixel-based calculation (RGB or RGBA), but I also did it for ND.

There are few comments in the code for some features that I am not sure yet.

I'm also developing it as a std :: array for operator overhead, although the classic C array could have been better for my pipeline (mainly data transmission).

Usually, these methods are used on int or float and sometimes on uchar. As you can see, I did not manage the limits or the negatives. I still wonder to create a dedicated model for uchar.

And one last question that I have, I've never really timed it, nor checked the behavior of the compiler, but should it be better to use a dedicated function for 3D and 4D?

#ifndef PX_ND_STD_ARRAY_UTILITY_H
#define PX_ND_STD_ARRAY_UTILITY_H

// ============================================= === ==========================
// We prefer to use std :: array because we are able to define an operator function on it.
//
// using pxNDArr = std :: array;
//               against
// using pxNDArr = T[N];
//
// ============================================= === ==========================

#understand 
#understand 

// ============================================= === ==========================
// Set the type alias for std :: array based on a template. (ND, 3D and 4D).
// ============================================= === ==========================
model 
using pxNDArr = std :: array;

// Special class for RGB color pixels.
model 
using px3DArr = pxNDArr;

// Special class for RGBA color pixels.
model 
using px4DArr = pxNDArr;

// ============================================= === ==========================
// Define an alias to make sure you use the correct type of input, output or input, identical for returns.
// ============================================= === ==========================

model
using InArg = const pxNDArr&;

model
using OutArg = pxNDArr&;

model
using InOutArg = OutArg;

model
using OutRet = pxNDArr;


// Constant is longer than const T, but is it safer to use to avoid
// is one of our const parameters defined as non-const?
// NOTE: not used yet, wondering if I should do it.
model
using Constant = const T;

// ============================================= === ==========================
// Special methods applying pixel manipulation.
// ============================================= === ==========================

// Apply a mathematical mathematical product between the vectors arr1 and arr2. Display the result in dst.
model
empty crossProduct (const px3DArr& arr1, const px3DArr& arr2, px3DArr& dst)
{
dst[0] = arr1[1] * arr2[2] - arr1[2] * arr2[1];
dst[1] = arr1[2] * arr2[0] - arr1[0] * arr2[2];
dst[2] = arr1[0] * arr2[1] - arr1[1] * arr2[0];
}


model
px3DArr crossProduct (const px3DArr& arr1, const px3DArr& arr2)
{
px3DArr dst;
crossProduct (arr1, arr2, dst);
back dst;
}


// Swap the color channel: RGB to BGR and vice versa.
model
void swapChannel (const px3DArr& arr, px3DArr& dst)
{
dst[0] = arr[2];
dst[1] = arr[1];
dst[2] = arr[0];
}


model
px3DArr swapChannel (const px3DArr& arr)
{
px3DArr dst;
swapChannel (arr, dst);
back dst;
}


// ============================================= === ==========================
// Method for all types of tables (ND, 3D or 4D)
// ============================================= === ==========================


// This method sets the InArg value between a minimum and maximum range limit.
model
empty clamp (InArg arr, OutArg dst, const T minVal, const T maxVal)
{
for (int i = 0; i <N; i ++)
dst[i] = ((arr[i] < minVal ) ? minVal : ( ( arr[i] >    maxVal)? maxVal: (arr[i] )));
}


model
OutRet clip (InArg arr, const T minVal, const T maxVal)
{
pxNDArr dst;
clip (arr, dst, minVal, maxVal);
back dst;
}


// Add operator between a std :: array and a constant of the same type.
model 
OutRet operator + (InArg arr constant T2 constant)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr[i] + constant;
back dst;
}


// Add operator between a std :: array and another array (same type)
model 
OutRet operator + (InArg arr1, InArg arr2)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr1[i] + arr2[i];
back dst;
}


// subtraction operator between a std :: array and a constant (same type)
model 
OutRet operator- (InArg arr constant T2 constant)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr[i] - constant;
back dst;
}


// subtraction operator between a std :: array and another array (same type)
model 
OutRet operator- (InArg arr1, InArg arr2)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr1[i] - arr2[i];
back dst;
}


// Multiplication operator between a std :: array and a constant (same type)
model 
OutRet operator * (InArg arr constant T2 constant)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr[i] * constant;
back dst;
}


// Multiplication operator between a std :: array and another array (same type)
model 
OutRet operator * (InArg arr1, InArg arr2)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr1[i] * arr2[i];
back dst;
}


// Division operator between a std :: array and a constant (same type)
model 
OutRet operator / (InArg arr constant T2 constant)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr[i] / constant;
back dst;
}


// Division operator between a std :: array and another array (same type)
model 
OutRet operator / (InArg arr1, InArg arr2)
{
pxNDArr dst;
for (int i = 0; i <N; i ++)
dst[i] = arr1[i] / arr2[i];
back dst;
}


// This addition two vector elements by elements.
model
void add (InArg arr1, InArg arr2, outArg dst)
{
dst = arr1 + arr2;
}


// This subtracts two vector elements by elements.
model
cancel subtraction (InArg arr1, InArg arr2, outArg dst)
{
dst = arr1 - arr2;
}


// This multiplies two vector elements with elements.
model
void multiply (InArg arr1, InArg arr2, outArg dst)
{
dst = arr1 * arr2;
}


// This divides two vector elements by elements.
model
vacuum division (InArg arr1, InArg arr2, outArg dst)
{
dst = arr1 / arr2;
}


// This calculates the sum of a vector.
model
T sum (InArg arr)
{
T sum = arr[0];
for (int i = 1; i <N; i ++)
sum + = arr[i];
sum of return;
}


// This applies a scalar product calculation between any type and any size vector.
model
T dotProduct (InArg arr1, InArg arr2)
{
sum of return (arr1 * arr2);
}


// This applies an exponent power on vector A and stores the result in B.
model
empty pow (InArg arr, OutArg dst, exponent const T)
{
for (int i = 0; i <N; i ++)
dst[i] = std :: pow (arr[i], exponent);
}


// This applies an InArg exponent power on vector A.
model
void powInplace (InOutArg arr, exponent const T)
{
for (auto & it: arr)
it = std :: pow (it, exponent);
}


// This applies an exponent power on the vector A and returns the result in a new array.
model
OutRet pow (InArg arr, exponent const T)
{
pxNDArr dst;
pow (arr, dst, exponent);
back dst;
}


// This applies one sqrt element per element.
model
void sqrt (InArg arr, OutArg dst)
{
for (int i = 0; i <N; i ++)
dst[i] = std :: sqrt (arr[i])
}


// This applies a place InArg place element by element.
model
void sqrtInplace (InOutArg& arr)
{
for (auto & it: arr)
it = std :: sqrt (it);
}


model
OutRet sqrt (InArg arr)
{
pxNDArr dst;
sqrt (arr, dst);
back dst;
}


// This returns the length of any vector.
// NOTE: The length is always float.
model
float vectorLength (InArg arr)
{
returns std :: sqrt (static_cast(dotProduct (arr, arr)));
}


// Calculate the distance between vectors A and B.
// NOTE: The distance is always float.
model
float distanceBetweenVector (InArg arr1, InArg arr2)
{
vector back length (arr1 - arr2);
}


// TODO: Should we limit T to a floating point? Since vectorLength only returns float.
// InArg-place vector standardization.
model
void normalizeVectorInplace (InOutArg& arr)
{
arr / = vectorLength (arr);
}


// TODO: Should we limit T to a floating point? Since vectorLength only returns float.
// Normalized vector A and output of the result in a vector dst (not mean InArg-place).
model
void normalizeVector (InArg arr, OutArg dst)
{
dst = arr / vectorLength (arr);
}


// Vector A normalized and output of the result in a new array
model
OutRet normalizeVector (InArg arr)
{
OutArg dst;
normalizeVector (arr, dst);
back dst;
}


// This function is used to convert from type T1 to type T2 a vector.
// can be useful when you want to use the sum of any other process on a
// unsigned character vector.
model
void convert (InArg arr, InArg dst)
{
for (int i = 0; i <N; i ++)
dst[i] = static_cast(arr[i] )
}


model
OutRet convert (InArg arr)
{
pxNDArr dst;
convert (arr, dst);
back dst;
}


// This function prints the vector values.
model
void printVector (InArg arr)
{
for (auto & it: arr)
std :: cout << << <<
std :: cout << std :: endl << std :: endl;
}

// TODO: Implement rounding methods, etc.

#endif // PX_ND_STD_ARRAY_UTILITY_H