FotoSHOCK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
BWconversion/BWconversion.cpp

This is an example of how to implement an ImageOperation that converts image to grayscale using the transformImage() function.

/*
* Copyright 2011, 2012 Lukas Jirkovsky
*
* This file is part of FotoSHOCK.
*
* FotoSHOCK is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* FotoSHOCK 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FotoSHOCK. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BWconversion.hpp"
#include <TransformImage.hpp>
template <typename PixelFormat>
class BWFunctor {
public:
BWFunctor(unsigned int bands) : m_bands(bands) {};
void operator()(PixelFormat* in, PixelFormat* out) {
for (unsigned int i = 0; i < m_bands; i++) {
out[i] = in[0];
}
}
private:
unsigned int m_bands;
};
template <typename PixelFormat>
public:
void operator()(PixelFormat* in, PixelFormat* out) {
PixelFormat color = 0.3 * in[0] + 0.59 * in[1] + 0.11 * in[2];
for (unsigned int i = 0; i < 3; i++) {
out[i] = color;
}
}
};
template <>
class BWFunctor_RGB<ValueTypeInfo<ValueTypeEnum::uint8>::Type> {
public:
typedef ValueTypeInfo<ValueTypeEnum::uint32>::Type uint32;
typedef ValueTypeInfo<ValueTypeEnum::uint8>::Type uint8;
void operator()(uint8* in, uint8* out) {
uint32 R = 77 * (in[0] << 8);
uint32 G = 151 * (in[1] << 8);
uint32 B = 25 * (in[2] << 8);
uint8 color = (R + G + B) >> 16;
for (unsigned int i = 0; i < 3; i++) {
out[i] = color;
}
}
};
void BWconversion::runOperation (const Inputs& sources, const Outputs& dest, vector<UpdateInfo>& ROI, const long stamp) {
if (sources[0]->getPixelData().colorSpace() == ColorSpaceEnum::RGB) {
switch(sources[0]->getPixelData().format()) {
#define FORMAT_DO_STH(T,U) transformImage<T>(sources[0], dest[0], BWFunctor_RGB<U>(), ROI, stamp);
case_ANY;
#undef FORMAT_DO_STH
}
} else {
switch(sources[0]->getPixelData().format()) {
#define FORMAT_DO_STH(T,U) transformImage<T>(sources[0], dest[0], \
BWFunctor<U>(sources[0]->getPixelData().numOfBands()), ROI, stamp);
case_ANY;
#undef FORMAT_DO_STH
}
}
}