我正在为 freeimage 编写一个小包装器,用于图像加载和像素抓取等。我有一个PImage
处理所有加载和显示的类,其中包含一个PixelColorBuffer
类。我使用PixelColorBuffer
for 一种方便的方法unsigned char
从 s 中获取 stexturebuffer
并将它们转换为另一个名为的类color
(我将其排除在外,因为它工作正常)。我还希望能够使用此类设置像素PixelColorBuffer
,这就是它具有colortobuffer
和buffertocolor
. 我PixelColorBuffer
用指向位置的指针进行实例化unsigned char array
(注意:它保存图片的 rgba 值)。但是,这似乎可行,但是当我调用get(10, 10)
已加载并显示的图像时,我得到以下信息:
(GNU Debugger)
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bc66d9 in cprocessing::PixelColorBuffer::buffertocolor (this=<optimized out>, n=<error reading variable: Unhandled dwarf expression opcode 0x0>) at pixelcolorbuffer.cpp:17
17 c.rgba[0]=(*b)[(n*4)+0];
和类被编译成 aPImage
并正确链接。我假设我在设置指针时做错了,这是我第一次处理指向指针的指针......但我一生都无法弄清楚我做错了什么。这是所有相关代码。PixelColorBuffer
.so
///MAIN_PROGRAM.CPP
PImage t;
t.loadImage("image.png"); //loads image (works)
image(t, mouseX, mouseY); //draws image (works)
color c = t.get(10, 10); //SEGFAULT
///PIMAGE.HPP
class PImage {
public:
GLubyte * texturebuffer; //holds rgba bytes here
PixelColorBuffer * pixels;
PImage();
color get(int x, int y);
};
///PIMAGE.CPP
PImage::PImage() {
this->pixels = new PixelColorBuffer((unsigned char *) texturebuffer);
}
void PImage::loadImage(const char * src) {
//...snip...freeimage loading / opengl code ...
char * tempbuffer = (char*)FreeImage_GetBits(imagen);
texturebuffer = new GLubyte[4*w*h];
//FreeImage loads in BGR format, so we swap some bytes
for(int j= 0; j<w*h; j++){
texturebuffer[j*4+0]= tempbuffer[j*4+2];
texturebuffer[j*4+1]= tempbuffer[j*4+1];
texturebuffer[j*4+2]= tempbuffer[j*4+0];
texturebuffer[j*4+3]= tempbuffer[j*4+3];
}
//...snip...freeimage loading / opengl code ...
}
color PImage::get(int x, int y) {
return pixels->buffertocolor((y*w)+x);
}
///PIXELCOLORBUFFER.HPP
class PixelColorBuffer {
public:
unsigned char ** b;
PixelColorBuffer(unsigned char * b);
/**Converts a pixel from the buffer into the color
* @param n pixel ((y*width)+x)
* @return color*/
color buffertocolor(int n);
/**Converts a pixel from the buffer into the color
* @param n pixel ((y*width)+x)
* @param c color to put into buffer*/
void colortobuffer(int n, const color& c);
};
///PIXELCOLORBUFFER.CPP
PixelColorBuffer::PixelColorBuffer(unsigned char * b) {
this->b = &b;
}
color PixelColorBuffer::buffertocolor(int n) {
color c(0, styles[styles.size()-1].maxA);
c.rgba[0]=(*b)[(n*4)+0];
c.rgba[1]=(*b)[(n*4)+1];
c.rgba[2]=(*b)[(n*4)+2];
c.rgba[3]=(*b)[(n*4)+3];
return c;
}
void PixelColorBuffer::colortobuffer(int n, const color& c) {
(*b)[(n*4)+0] = c.rgba[0];
(*b)[(n*4)+1] = c.rgba[1];
(*b)[(n*4)+2] = c.rgba[2];
(*b)[(n*4)+3] = c.rgba[3];
}