From: io_x on

"Gert-Jan de Vos" <gert-jan.de.vos(a)onsneteindhoven.nl> ha scritto nel messaggio
news:4a41375a-4445-4ffb-bf67-ebd403d600fe(a)j5g2000yqm.googlegroups.com...
On Jan 7, 10:40 am, "olivier.scalb...(a)algosyn.com"
<olivier.scalb...(a)algosyn.com> wrote:
> Hello,
>
> I would like to create an Image class. A use of this class could be
> something as:

What about this?

---------------
; nasmw -fobj this.asm
; bcc32 -v that.cpp this.obj
section _DATA use32 public class=DATA

global @Image(a)fill2$qr7Color3d

section _TEXT use32 public class=CODE

;
; 0j, 4i, 8ra, 12&This_i, 16&Color3d_j
align 4
@Image(a)fill2$qr7Color3d:
push esi
push edi
mov esi, dword[esp+ 12]
cmp esi, 0
jne .1
..e: mov eax, 0
stc
jmp short .z
..1: mov eax, [esi]
mov ecx, [esi+4] ;eax=h, ecx=w
mul ecx
cmp edx, 0
jne .e
cmp eax, 0
jl .e
mov ecx, eax ;ecx=h*w
mov edi, [esi+8]
cmp edi, 0
je .e ;edi=pxs
mov esi, dword[esp+ 16]
mov eax, [esi]
mov edx, [esi+4]
mov esi, [esi+8]
..2: mov [edi], eax
mov [edi+4], edx
mov [edi+8], esi
add edi, 12
loop .2
mov eax, 1
clc
..z:
pop edi
pop esi
ret
----------------------
#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 f64 double

#define S sizeof
#define R return
#define P printf
#define F for

using namespace std;

struct Color3d{f32 r, g, b;};

class Image{
public:
u32 height;
u32 width;
Color3d* pxs;

Image(i32 h, i32 w)
{i32 i, g;

height=0; width=0; pxs=0;
if(w<=0||h<=0) return;
g =h*S(Color3d);
if(g<=0) return;
g*=w;
if(g<=0) return;
cout << "Size=" << g << "\n";
pxs=(Color3d*) malloc(g);
if(pxs==0) return;
height=h; width=w;
}

~Image(){free(pxs);}

Color3d& v(int h, int w)
{return *(pxs+w+h*width);}

int fill (Color3d& color)
{u32 x, y;
if(pxs==0) R 0;
for(x=0; x<height; ++x)
{for(y=0; y<width; ++y)
(*this).v(x,y)=color;
}
R 1;
}

int fill1(Color3d& color)
{u32 x, end;
Color3d *p;
if(pxs==0) R 0;
p=pxs;
end=height*width;
for(x=0; x<end; ++x, ++p)
*p=color;
R 1;
}

int fill2(Color3d& color);

class Indexer{
public:
Color3d* data;
Indexer(Color3d* dat) : data(dat){}
Color3d& operator[](int x){return data[x];}
};

// img[y] is Indexer
// img[y][x] is *&Color3d
Indexer operator[](int y)
{return Indexer(pxs+y*width);}

};


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"; return 0;}

g.fill2(h);
cout << "(r,g,b)==(" << g.v(0,0).r << ", "
<< g.v(0,0).g << ", "
<< g.v(767,1024).b << ")\n";
cout << "g[767][1024].b==" << g[767][1024].b << "\n";
cout << "g[767][1023].b==" << g[767][1023].b << "\n";
cout << "end\n";
R 0;
}