9 Replies - 7798 Views - Last Post: 21 January 2010 - 02:39 AM Rate Topic: -----

#1 skyrulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-January 10

Displaying images in C/C++

Posted 06 January 2010 - 03:00 AM

hi all,

I wrote a small piece of code which can read images into a buffer.


#include <sys/types.h>
 11 #include <sys/stat.h>
 12 #include <unistd.h>
 13 #include <stdio.h>
 14 #include <fcntl.h>
 15 #include <malloc.h>
 16 
 17 int main(int argc,char **argv)
 18 {
 19 
 20	 struct stat ImageStat;
 21	 unsigned int ImageSize=0;
 22	 char *ImageBuffer = NULL;
 23	 int ImageFileDescp = 0;
 24 
 25	 if (argc!=2){
 26		 printf("Usage: ./a.out <filename.jpg> \n");
 27		 return 0;
 28	 }
 29 
 30	 // Getting the stat of image file
 31	 stat(argv[1],&ImageStat);
 32	 ImageSize = ImageStat.st_size;
 33	 printf("Iamge Size is %d\t\n",ImageSize);
 34 
 35	 // Allocate memory for the image
 36	 ImageBuffer = (char *)malloc(ImageSize);
 37 
 38	 // Opening the image
 39	 ImageFileDescp = open(argv[1],0600);
 40 
 41	 // Reading the image
 42	 read(ImageFileDescp,ImageBuffer,ImageSize);
 43 
 44	 // Do the processing
 45	 //
 46	 // close the file.
 47	 close(ImageFileDescp);
 48	free(ImageBuffer);
 49	 return 0;
 50 
 51 }




Now, the pixel values are present in variable called ImageBuffer. Can some one please tell how do i use this Buffer values and display the images.

Thank you in advance

This post has been edited by skyrulz: 06 January 2010 - 03:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying images in C/C++

#2 tommynator128  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 127
  • Joined: 05-January 10

Re: Displaying images in C/C++

Posted 06 January 2010 - 03:05 AM

you need a graphics library like sdl/qt or something because the "normal" c++ does not contain anything about graphics. i would use sdl because it's perfect for little things like this
Was This Post Helpful? 0
  • +
  • -

#3 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 305
  • View blog
  • Posts: 1,507
  • Joined: 16-May 09

Re: Displaying images in C/C++

Posted 06 January 2010 - 06:59 AM

Yes as tommynator128 said, you have to use a GUI library of your choice to display visuals, for example, I use wxWidgets, you may prefer some other library. It all depends on your choice.
Was This Post Helpful? 0
  • +
  • -

#4 skyrulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-January 10

Re: Displaying images in C/C++

Posted 15 January 2010 - 11:00 PM

hi tommynator128 and Anarion,

Thank you very much for the reply in Displaying the images. In the code which i had given, the pixel values are present in single dimensional array.But, i don't exactly how the values are present (i.e, row wise or column wise).

I work with gray scale images. Since i need to do some processing on the image, is there a way to read images in two dimensional arrays. I would like to have the code in C/C++.

thanks in advance....
Was This Post Helpful? 0
  • +
  • -

#5 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: Displaying images in C/C++

Posted 15 January 2010 - 11:29 PM

If you want to do processing on the images (editing them in some way I assume) then an image library can help you with that. Many of the popular widget toolkits have image editing capabilities. Qt, wxWidgets, GTK+, but here are some libraries specifically for image editing.
CImg
CxImage
Magick++
Cario
Was This Post Helpful? 0
  • +
  • -

#6 skyrulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-January 10

Re: Displaying images in C/C++

Posted 15 January 2010 - 11:43 PM

View PostDarkhack, on 15 Jan, 2010 - 10:29 PM, said:

If you want to do processing on the images (editing them in some way I assume) then an image library can help you with that. Many of the popular widget toolkits have image editing capabilities. Qt, wxWidgets, GTK+, but here are some libraries specifically for image editing.
CImg
CxImage
Magick++
Cario


hi darkhack,

thank you very much for the reply. I had already tried CImg for my work. I tried to read images using this libraries. But, i could not understand much of it. Can you please tell me how to use it if you had done it.

It will be very great of you if you can tell me how to read an image into 2-dimensional array using CImg. I need to access the image pixel by pixel. One last point, i work only with gray scale images. So, 2D array should do the job.

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#7 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: Displaying images in C/C++

Posted 16 January 2010 - 12:02 AM

I haven't used CImg myself, but it seems like it can do what you want. These two pages in the documentation are what you should focus on.
Overview
Image Loops
Was This Post Helpful? 0
  • +
  • -

#8 skyrulz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-January 10

Re: Displaying images in C/C++

Posted 16 January 2010 - 01:05 AM

View PostDarkhack, on 15 Jan, 2010 - 11:02 PM, said:

I haven't used CImg myself, but it seems like it can do what you want. These two pages in the documentation are what you should focus on.
Overview
Image Loops


hi darkhack,

Thank you very much for the links.
I will go through them and see how to use them.Let me know if you had figured it out.
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Displaying images in C/C++

Posted 16 January 2010 - 02:15 PM

openGL has a nice function calles "glDrawPixels()" for example, a typical function call might look something like this for an array of bytes that contain RBG pixel data

glDrawPixels(imageWidth, imageHeight, GL_RGB, GL_UNSIGNED_BYTE, imageData);

Was This Post Helpful? 0
  • +
  • -

#10 gnutz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-January 10

Re: Displaying images in C/C++

Posted 21 January 2010 - 02:39 AM

magick++ and cImage appear to be the most simple and straightforward, as far as images are concerned. You can load and display an image in just a few lines of code. Here are some "quick start" links for both libraries:

http://cimg.sourcefo...__tutorial.html
http://www.imagemagi...ck++/Image.html

This post has been edited by gnutz: 21 January 2010 - 02:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1