Witam. Próbuje w kodzie dostać sie do kolorów poszczegulnych pixeli za pomocą biblioteki lodepng. Napisałem sobie prostą klasę i funkcje odczytującą wartości kolejnych pixeli na ekranie.
klasa:
class Texture
{
private:
unsigned int width;
unsigned int height;
unsigned char * bitmapData;
public:
bool LoadInit( char * name );
inline void Delete();
//Coordinates from left down corner
inline void GetPixel( unsigned int x, unsigned int y, unsigned char & r, unsigned char & g, unsigned char & b, unsigned char & a );
inline void SetPixel( unsigned int x, unsigned int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a );
Texture();
~Texture();
};
bool Texture::LoadInit( char * src )
{
if( bitmapData != NULL )
{
delete[] bitmapData;
bitmapData = NULL;
}
{ // if texture file exist
fstream file;
file.open( src, ios::in );
if(!file.good()){
return false;
}
file.close();
}
{
{
vector < unsigned char > png;
vector < unsigned char > image;
lodepng::load_file( png, src );
lodepng::decode( image, width, height, png );
int i, x, y;
bitmapData = new unsigned char[ image.size() ];
for( y = 0; y < height; y++ )
{
for( x = 0; x < width*4; x++ )
{
bitmapData[(y*width*4)+x] = image[((height-(y+1))*width*4)+x];
}
}
png.resize( 0 );
image.resize( 0 );
}
}
return true;
}
inline void Texture::Delete()
{
width = 0;
height = 0;
if( bitmapData != NULL )
delete[] bitmapData;
}
inline void Texture::GetPixel( unsigned int x, unsigned int y, unsigned char & r, unsigned char & g, unsigned char & b, unsigned char & a )
{
x %= width;
y %= height;
r = bitmapData[(y*width*4)+(x*width*4)];
g = bitmapData[(y*width*4)+(x*width*4)+1];
b = bitmapData[(y*width*4)+(x*width*4)+2];
a = bitmapData[(y*width*4)+(x*width*4)+3];
}
inline void Texture::SetPixel( unsigned int x, unsigned int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a )
{
x %= width;
y %= height;
bitmapData[(y*width*4)+(x*width*4)] = r;
bitmapData[(y*width*4)+(x*width*4)+1] = g;
bitmapData[(y*width*4)+(x*width*4)+2] = b;
bitmapData[(y*width*4)+(x*width*4)+3] = a;
}
Texture::Texture()
{
width = 0;
height = 0;
bitmapData = NULL;
}
Texture::~Texture()
{
if( bitmapData != NULL )
{
delete[] bitmapData;
bitmapData = NULL;
}
width = 0;
height = 0;
}
Funkcja:
string src = "";
src += "Letters/";
src += (char)('A' + taskID);
src += ".png";
Texture letter; unsigned char R, G, B, A;
assert(letter.LoadInit(&(src[0])));
for(unsigned i = 0; i < 5; i++)
{
for(unsigned j = 0; j < 7; j++)
{
letter.GetPixel(i, j, R, G, B, A);
cout<<(int)R<<" "<<(int)G<<" "<<(int)B<<" "<<(int)A<<endl;
}
}
Podaje mu prosty obrazek
i w rezultacie dostaje jakieś idiotyczne wyniki:
255 255 255 255
0 0 0 255
0 0 0 255
0 0 0 255
0 0 0 255
0 0 0 255
255 255 255 255
0 0 0 255
0 0 0 255
0 0 0 255
0 0 0 255
0 0 0 255
255 255 255 255
0 0 0 0
0 0 0 255
0 0 0 255
0 0 0 255
0 0 0 255
255 255 255 255
0 0 0 0
88 77 173 49
0 0 0 255
0 0 0 255
0 0 0 255
255 255 255 255
0 0 0 0
88 77 173 49
0 0 0 255
0 0 0 255
0 0 0 255
255 255 255 255
0 0 0 0
88 77 173 49
0 0 0 255
0 0 0 255