Can't display a PNG using Glut or OpenGL

我只是一个虾纸丫 提交于 2019-12-24 12:24:12

问题


Code is here:

void readOIIOImage( const char* fname, float* img)
{
   int xres, yres;
   ImageInput *in = ImageInput::create (fname);
   if (! in) {return;}
   ImageSpec spec;
   in->open (fname, spec);
   xres = spec.width;
   yres = spec.height;
   iwidth = spec.width;
   iheight = spec.height;

   channels = spec.nchannels;
   cout << "\n";

   pixels = new float[xres*yres*channels];
   in->read_image (TypeDesc::FLOAT, pixels);
   long index = 0;
   for( int j=0;j<yres;j++)
   {
      for( int i=0;i<xres;i++ )
      {
         for( int c=0;c<channels;c++ )
         {
            img[ (i + xres*(yres - j - 1))*channels + c ] = pixels[index++];

         }
      }
   }
   in->close ();
   delete in;
}

Currently, my code produces JPG files fine. It has the ability to read the file's information, and display it fine. However, when I try reading in a PNG file, it doesn't display correctly at all. Usually, it kind of displays the same distorted version of the image in three separate columns on the display. It's very strange. Any idea why this is happening with the given code?

Additionally, the JPG files all have 3 channels. The PNG has 2.

fname is simply a filename, and img is `new float[3*size];

Any help would be great. Thanks.`


回答1:


Usually, it kind of displays the same distorted version of the image in three separate columns on the display. It's very strange. Any idea why this is happening with the given code?

This reads a lot like the output you get from the decoder is in row-planar format. Planar means, that you get individual rows one for every channel one-after another. The distortion and the discrepancy between number of channels in PNG and apparent count of channels are likely due to alignment mismatch. Now you didn't specify which image decoder library you're using exactly, so I can't look up information in how it communicates the layout of the pixel buffer. I suppose you can read the necessary information from ImageSpec.

Anyway, you'll have to rearrange your pixel buffer rearrangement loop indexing a bit so that consecutive row-planes are interleaved into channel-tuples.

Of course you could as well use a ready to use imagefile-to-OpenGL reader library. DevIL is thrown around a lot, but it's not very well maintained. SOIL seems to be a popular choice these days.



来源:https://stackoverflow.com/questions/34936418/cant-display-a-png-using-glut-or-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!