Re: Multidimensional array member initialization
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4b46f995$0$1116$4fafbaef@reader2.news.tin.it...
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4b463e69$0$1105$4fafbaef@reader4.news.tin.it...
here is my solution (don't know if it is right)
#include <iostream>
#include <cstdlib>
#include <stdint.h>
#define u8 uint8_t
#define i8 int8_t
#define u32 uint32_t
#define i32 int32_t
// float 32 bits
#define f32 float
#define S sizeof
#define R return
#define P printf
#define F for
using namespace std;
// one point is 3 32 bits float
struct Color3d{
f32 r, g, b;
};
class Image{
public:
u32 height;
u32 width;
u8* pxs;
u32 wmul;
Image(i32 height, i32 width);
~Image();
Color3d& v(int h, int w);
int fill (Color3d& color);
int fill1(Color3d& color);
};
Color3d& Image::v(i32 h, i32 w)
{return (Color3d&) *(pxs+w*S(Color3d)+h*wmul);}
// pixel are square coord: from 0..h-1, 0..w-1
Image::Image(i32 h, i32 w)
{i32 i, g;
height=0; width=0; pxs=0; wmul=0;
if(w<=0||h<=0) return;
g =h*sizeof(Color3d);
if(g<=0) return;
g*=w;
if(g<=0) return;
pxs=(char*) malloc(g);
if(pxs==0) return;
height=h; width=w;
wmul=width*S(Color3d);
}
Image::~Image(){free(pxs);}
i32 Image::fill(Color3d& color)
{u32 x, y;
if(pxs==0) R 0;
F(x=0; x<height; ++x)
{F(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
R 1;
}
i32 Image::fill1(Color3d& color)
{u32 x, end;
Color3d *p;
if(pxs==0) R 0;
p=(Color3d*) pxs;
end=height*width;
F(x=0; x<end; ++x, ++p)
*p=color;
R 1;
}
int main(void)
{Color3d h={0.78373, 0.1383, 1-0.78373-0.1383};
Image g(768, 1024);
cout << "Inizio\n";
if(g.pxs==0)
{cout << "Error\n"; R 0;}
g.fill1(h);
cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "
<< g.v(767,1023).b << ")\n";
cout << "end\n";
R 0;
}