Hey all,
thanks for the comments. Please find attached a standalone version of
my code. Please forgive the mess, I just did a quick copy and paste of
many files.
I am compiling with VS 2005 and the latest Intel compiler.
##### CUT #####
#include <windows.h>
#include <stdio.h>
#include <math.h>
////////////////////
///// INTEGERS
////////////////////
// unsigned
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
typedef unsigned long long u64;
// signed
typedef signed char i8;
typedef signed short i16;
typedef signed long i32;
typedef signed long long i64;
////////////////////
///// FLOATING-POINT VALUES
////////////////////
typedef float f32;
typedef double f64;
////////////////////
///// MEMORY ROUTINES
////////////////////
enum { MemoryAlignment=64};
void* AllocateMemory(size_t size)
{
return _aligned_malloc(size, MemoryAlignment);
}
void ReleaseMemory(void *memblock)
{
return _aligned_free(memblock);
}
int ComputeAlignedWidth(int width)
{
int alignment_needed = MemoryAlignment / sizeof(float);
return (int)ceil((float)width/(float)alignment_needed) *
alignment_needed;
}
////////////////////
///// CLASS DECLARATION
////////////////////
template <typename T>
struct Image
{
public: // members
// std information
int width, height, depth;
// actual width of the buffer
// buffer holding image data is padded to be a multiple
// of MemoryAlignment for optimisation purposes
int width_padded;
// dimensions helper
int firstRow, lastRow, firstCol, lastCol;
// pointer to the image data
T* data;
public: // methods
// ctor
Image():
width(0),height(0),depth(0),
width_padded(0),
firstRow(0), lastRow(0), firstCol(0), lastCol(0),
data(NULL)
{
}
// dtor
~Image()
{
}
// memory management
void Allocate() { data =
static_cast<T*>(AllocateMemory(width_padded*height *depth*sizeof(T)));}
void Release () { ReleaseMemory(data);}
// pixel access
// virtual T& operator() (int row, int col)
// dimensions management
void SetDimensions(int h, int w, int d){
height = h;
width = w;
depth = d;
width_padded = ComputeAlignedWidth(width);
firstRow = 0;
firstCol = 0;
lastRow = height-1;
lastCol = width-1;
}
// size information
int GetTotalSize(bool padded=false){
if (padded) return width_padded*height*depth*sizeof(T);
else return width *height*depth*sizeof(T);
}
int GetImageSize(bool padded=false){
if (padded) return width_padded*height*depth;
else return width *height*depth;
}
int GetPlaneSize(bool padded=false){
if (padded) return width_padded*height;
else return width *height;
}
};
template <typename T>
struct GrayImage : public Image<T>
{
public: // methods
// ctor
GrayImage():
Image()
{
depth=1;
}
// pixel access
T& operator() (int row, int col)
{
return data[row*width_padded + col];
}
};
template <typename T>
struct ColourImage : public Image<T>
{
public: // definitions
enum{red=0, green=1, blue=2};
enum{r =0, g =1, b =2};
public: // methods
// ctor
ColourImage():
Image()
{
depth=3;
}
// pixel access
T& operator() (int row, int col, int channel)
{
return data[(row*width_padded + col)*3 + channel];
}
};
#define MIN(A,B) ((A < B) ? A : B)
#define MAX(A,B) ((A > B) ? A : B)
#define MAX3(R, G, B) ((((R) > (G) ? (R) : (G)) > B) ? ((R) > (G) ?
(R) : (G)) : B)
template <typename T, typename U>
void RGB2Log(ColourImage<U> &input, GrayImage<T> &buffer, bool linear,
T snr)
{
for(int row=input.firstRow ; row<=input.lastRow ; ++row){
for(int col=input.firstCol ; col<input.lastCol; ++col){
buffer(row, col) = MAX3(input(row, col,
ColourImage<U>::r),
input(row, col,
ColourImage<U>::g),
input(row, col,
ColourImage<U>::b));
}
}
return;
}
template <typename T>
void test(GrayImage<T> &input, GrayImage<T> &output)
{
for(int row=input.firstRow ; row<=input.lastRow ; ++row){
for(int col=input.firstCol ; col<input.lastCol; ++col){
output(row, col) = input(row, col)/2;
}
}
}
int main(int argc, char* argv[])
{
GrayImage<f32> gray, tmp;
ColourImage<u8> color;
gray.SetDimensions(2000, 2000, 3); gray.Allocate();
color.Allocate();
gray.SetDimensions(color.height, color.width, 1);
gray.Allocate();
tmp.SetDimensions(color.height, color.width, 1);
tmp.Allocate();
RGB2Log(color, gray, true, 1.0);
//RGB2Log<f32, u8>(color, gray, true, 1.0);
test(gray, tmp);
color.Release(); gray.Release(); tmp.Release();
return 0;
}
##### CUT #####