User avatar
Grumpy Mike
Posts: 934
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Jpeg pictures

Sun Aug 12, 2012 7:21 pm

I am very new to OpenGLES so this might not be possible but does anyone know how to display a jpeg picture in a display buffer?

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: Jpeg pictures

Mon Aug 13, 2012 8:37 am

The easiest way is to load the image (I use ImageMagick for this) and then set it as an OpenGL texture and render to a quad (if using GLES 2.0 this will have to be a quad made of triangles).

The basic process is as follows

1) load image and access pixel data, pack this into a buffer ( unsigned char)

data = new unsigned char [width*heigh*3]; // for RGB

2) create an OpenGL texture object

Code: Select all

 glGenTextures(1,&m_textureName);
 glBindTexture(GL_TEXTURE_2D,m_textureName);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,data);
 glGenerateMipmap(GL_TEXTURE_2D); //  Allocate the mipmaps
Once this is done, we have a texture object ID which we must enable every time we need to draw it.

3) Draw a quad with textures enabled, this will be different depending upon the GL version you are using.

This may seem quite complex but that is how OpenGL works, If you are only trying to show images and not do 3D stuff it may be better using other methods (SDL can blit bmp images quite easily and this is simple to do, with other libs SDL can be extended to do more).

blu
Posts: 55
Joined: Tue Jul 17, 2012 9:57 pm

Re: Jpeg pictures

Mon Aug 13, 2012 10:20 am

To add to what jmacey said, the quad (read: the two triangles) you'd use to span the output might need to have a specific mapping if you want to get exact 1:1 pixel:texel mapping. Generally, GL's texel grid (and viewport pixel grid, in case you use the gl_FragCoord built-in shader variable) is not integer-centered, but .5-centered. That is, the centre of the texel/pixel is at (x + .5, y + .5) where x, y are integers. So, in normalized texel coordinates, i.e. [0, 1], your first and last texel along a given axis is addressed as .5 / axis_length, and (axis_length - .5) / axis_length, respectively. In your case, given a bitmap of dimensions (width, height), your quad mapping at the "(0, 0)" corner would actually be (.5 / width, .5 / height), and at the "(1, 1)" corner it would be ((width - .5) / width, (height - .5) / height).

User avatar
Grumpy Mike
Posts: 934
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Jpeg pictures

Mon Aug 13, 2012 11:52 am

Thanks I will give this a try.
Yes I am only wanting to display an image, but I want to use the two swap buffers to alternate the image I display every other frame scan. This is of course for displaying a stereo on a TV that is not designed to do such a thing. I do understand that the frame rate will be halved and that I will get flicker, but this is not a concern with my project.
Is there a simpler way of doing that on the Pi other than OpenGL ES?

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: Jpeg pictures

Mon Aug 13, 2012 12:04 pm

I think a combination of SDL blit (http://www.libsdl.org/docs/html/sdlblitsurface.html) and SDL image library http://www.libsdl.org/projects/SDL_image/ would give you a simpler way of quickly doing a proof of concept. It would not be hardware accelerated but should be a good way to do simple tests.

Are you wanting to do single images in stereo or frames? If so frames would become difficult due to the CPU / I/O loading of reading the images etc.

The process would be to load the left / right image into an SDL_Surface (one for each) then do the blit. something like this (this is a simple sprite draw routine I have but the concept is the same and I know it works on the pi)

Code: Select all

void drawSprite(SDL_Surface* _bitmap,SDL_Surface* _screen,int _x, int _y)
{
  // Part of the bitmap that we want to draw
  static SDL_Rect source;
  source.x = 0;
  source.y = 0;
  source.w = 64;
  source.h = 64;

  // Part of the screen we want to draw the sprite to
  SDL_Rect destination;
  destination.x = _x;
  destination.y = _y;
  destination.w = 64;
  destination.h = 64;
  SDL_BlitSurface(_bitmap, &source, _screen, &destination);

  SDL_Flip(_screen);

}

jmacey
Posts: 135
Joined: Thu May 31, 2012 1:05 pm

Re: Jpeg pictures

Mon Aug 13, 2012 12:05 pm

btw if you pm me your email I can send the code (don't want to put it publicly as it forms part of an assignment I give to my students)

Jon

iso9660
Posts: 25
Joined: Sun Sep 16, 2012 1:48 pm

Re: Jpeg pictures

Wed Sep 19, 2012 9:15 pm

You can find the code example as part of libjpeg-dev package:
sudo apt-get install libjpeg-dev

You can inspect the files in that package or any other (this way you can access to jpeg examples and documentation):
dpkg -L libjpeg-dev

At last you can see a nice example here (which is also included in the package):
http://download.blender.org/source/ches ... /example.c

Return to “OpenGLES”