Import image using openGL in C

In web programming, it’s always easy to add an image to our project page but, it’s not easy to add images using openGL in C. This need little work to be done. OpenGL is a rendering language, and has no native support for loading JPEG, GIF, TIF, BMP or any other popular image type.
Here I am going to explain a quick way about how we can import a jpg and etc..image using openGL in C.
Note: This is for 2D images.

First fo all, we need to convert this image into a format readable by OpenGL. There are libraries out there to do the conversion and yes, we can use them instead of this method but doing the conversion this way will reduce the load of our programs at run time and also eliminate the need for external image files. For this example,  I am using an image editing tool GIMP in Ubuntu 14.04.

Open the image with GIMP and go to the Save A Copy screen. After clicking this at the bottom you will see Select File Type (By Extension) button. Click it, select C source code and hit save. You should see another prompt with some options. Select the Use Glib types checkbox.

You can do this in one more way, After you open the image in GMIP, export it into c file(you will get an option after clicking export). Now, You should see another prompt with some options. Select the Use GLib types checkbox. It done now 🙂

Now go to the directory, where you saved the image. there should be a C file, open it. You should now have a file which looks like this:

/* GIMP RGB C-Source image dump (minion.c) */

static const struct {
guint width;
guint height;
guint bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */
guint8 pixel_data[265 * 190 * 3 + 1];
} gimpMinnion = {
265, 190, 3,
“\321\321\321\352\352\352\347\347\347\347\347\347\354\354\354\347\347\347”
“\351\351\351\346\346\346\352\352\350\352\352\350\352\352\352\352\352\354”
“\352\352\354\352\352\352\352\352\350\352\352\350\352\353\345\352\353\346”



};

Woohoo We are done.

This is the raw data of our image. Now, Just #include this file in your OpenGL program and you are done 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s