Crash on Rasterize - how to fix?

Post Reply
LamarLatrell
Posts: 26
Joined: Tue Dec 09, 2014 4:47 pm

Crash on Rasterize - how to fix?

Post by LamarLatrell »

Following information here:

https://forums.naturalpoint.com/viewtop ... =65&t=9107

I've included the following code in a working openCV project:

Code: Select all

CameraManager::X().WaitForInitialization();
Camera *camera = CameraManager::X().GetCamera();
if(!camera)
{
	fprintf(stderr, "...Error opening V120\n");
}
camera->Start();
camera->SetVideoType(Core::GrayscaleMode); 

int camWidth  = camera->Width();
int camHeight = camera->Height();
cv::Mat matFrame(cv::Size(camWidth, camHeight), CV_8UC1);
const int BACKBUFFER_BITSPERPIXEL = 8;

Frame *frame = camera->GetFrame();
frame->Rasterize(camWidth, camHeight, matFrame.step, BACKBUFFER_BITSPERPIXEL, matFrame.data);
cv::imshow("My Window Name", matFrame);
frame->Release();

(VisualStudio2012 - openCV2.5.9)

It'll compile, but it will crash at the rasterize function.

openCV has been working same project file for a while now, I've even had rasterize not crashing previously (but not working as expected but that's another discussion).

Crash is obviously not what I'm after so I tried to look at Rasterize, but cant get past a simple function declaration ~what is 'span' ?

~How does rasterize work?

~Most importantly, any idea how to get this working?

Also - as an aside - you'll note I had to type 'Core::' in from of the GrayscaleMode enum as it wasn't being recognized, is this something to do with the other thread I posted earlier (I would link it but I cant access it before it's been moderator approved) - it wouldn't even accept '1'.
beckdo
Posts: 520
Joined: Tue Jan 02, 2007 2:02 pm

Re: Crash on Rasterize - how to fix?

Post by beckdo »

Hey Lamar,

The Frame::Rasterize function populates the buffer that you provide. It does this for speed so that you're not in a situation where Frame::Rasterize populates it's own buffer and then you're in situation where you need to do the expensive step of copying it where you need it.

Now, the one critical point here is that Frame::Rasterize has no way of confirming that buffer you provide is a valid buffer that you've allocated and is the proper size. If the buffer you provide is not large enough or the pointer to the buffer is not valid, Frame::Rasterize will crash. This is why your code is crashing.

In your code above, instead of storing the pixel data to matFrame.data, as a simple test, store it into a buffer you allocate the crash will go away. If you replace the frame->Rasterize() line of your code with the following it will not crash:

unsigned char * buffer = (unsigned char*) malloc( camWidth * camHeight);
frame->Rasterize(camWidth, camHeight, 0, 8, buffer);

The span value is there for situations where you need to have more control over the number of total bytes for each row of pixel data. A good example is when you're creating a texture and the texture must have a width that is a power of two. So span, simply, is the number of bytes from the start of one row to the next. If you leave it zero it will use Width*BytesPerPixel. This default value means that in memory the first pixel of the next row is immediately following the last pixel of the previous row in memory.
Post Reply