Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are two ways to do it: first, use a cv::Mat whose lifetime is longer than SDL_Surface, or manually allocate a pointer.

Use an external buffer Mat. cv::Mat objects share ownership to the data. Pass a matrix to your func. Make sure it does not get deallocated until you're finished with SDL_Surface

SDL_Surface* OpenCvHandler::buildPuzzleSprite(std::string imagePath, cv::Mat& buffer)
{
     // ...
     buffer = cv::imread(imagePath.c_str());
     // ...
     SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)tiledImage.data,
            tiledImage.size().width, tiledImage.size().height, 
        imageBytes(tiledImage.depth()) * tiledImage.channels(), 
            tiledImage.step, 0xff0000, 0x00ff00, 0x0000ff, 0);
     // ...
}

Use a simple raw pointer for your data

SDL_Surface* OpenCvHandler::buildPuzzleSprite(std::string imagePath)
{
     // ...
     cv::Mat image = cv::imread(imagePath.c_str());
     char* rawPtr = new char[image.step() * image.rows()];
     memcpy(rawPtr, (char*)image.data, image.step() * image.rows())
     // ...
     SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)tiledImage.data,
            tiledImage.size().width, tiledImage.size().height, 
        imageBytes(tiledImage.depth()) * tiledImage.channels(), 
            tiledImage.step, 0xff0000, 0x00ff00, 0x0000ff, 0);
     // ...
     // Now you've got a real problem with rawPtr - 
     // you need to store it somewhere to be able to release it when the time comes
}