0

How to convert an IpLImage to SDL_Surface.

I used this postto convert an image with 8 nChannels and it worked perfectly, but when I try this with an image of a single nChannel it displays the following message on the console:

 Depth 8, nChannels 1, pitch 640
 Unable to convert the IplImage image ! SDL Error: Unknown pixel format

And this is the code I used :

int pitch = colored_capture->nChannels * colored_capture->width;
printf("Depth %d, nChannels %d, pitch %d\n", colored_capture->depth,
                colored_capture->nChannels, pitch);

surface_1 = SDL_CreateRGBSurfaceFrom((void*)colored_capture->imageData,
                       colored_capture->width,
                       colored_capture->height,
                       colored_capture->depth * colored_capture->nChannels,
                       colored_capture->widthStep,
                       0x0000ff, 0x00ff00, 0xff0000, 0);

if (surface_1 == NULL)
{
   fprintf ( stderr ,"Unable to convert the IplImage image ! SDL 
Error: %s \n", SDL_GetError());
}
asendjasni
  • 963
  • 1
  • 16
  • 36
  • If it is only 8 bits per pixel, why masks describe it as 24 bit? What is your source data? Is it grayscale? Indexed? In what format do you want to get your resulting surface? – keltar Nov 19 '18 at 19:00
  • I applied the `CannyEdgeFilter` on a image captured by a camera and when I tried to convert it to `SDL_Surface` with the code appove, it didn't work. – asendjasni Nov 20 '18 at 16:31
  • ..which creates grayscale image, as I had to google just now. Still no indication of what you need SDL surface for, and what format should it have. If you really don't care (can't imagine why), take a look at e.g. https://stackoverflow.com/questions/27204397/greyscale-image-in-sdl2 – keltar Nov 20 '18 at 17:44
  • well, I need to visualize two images, the original one and another that I will apply some CV processing on it, and all that using only low-level library, that why I chose SDL. For the format, I think it doesn't matter cuz all I do is visualization. – asendjasni Nov 20 '18 at 20:43
  • @keltar what do you think about using Gtk instead of SDL ? – asendjasni Nov 24 '18 at 17:50
  • SDL is simplier if you need to display custom image data. Have you tried setting palette as specified in a link to another question? Your initial problem is that your image is grayscale and SDL thinks 8 (or 4) bits per pixel is paletted image. Other method would be duplicating grayscale data to each channel (the same in R, G, and B). – keltar Nov 25 '18 at 07:59
  • Actually, I'm not quite familiar with these concepts, although I tried as you suggested in a previous comment, but It didn't work. – asendjasni Nov 25 '18 at 16:39

0 Answers0