5 Replies - 20668 Views - Last Post: 04 March 2008 - 08:17 AM Rate Topic: -----

#1 sreena  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-March 08

Read an image using c

Posted 03 March 2008 - 11:06 AM

Hai,
I used the below code to read an image from a database.....and i got the output...as i'm new to c, i don't know whether'm correct or not. My doubt is whether 'm getting the pixel values of image as output?
Please help me...
Thanks in advance






#include<stdio.h>
int main(void)
{
FILE *fp;
unsigned char image[128][128];
int r,c;

if((fp = fopen("image001.jpg","r+")) == NULL)
{
printf("Unable to open the specified file.\n");
getch();
exit(1);
}
else
{
for(r=0;r<128;r++)
{
for(c=0;c<128;c++)
{
image[r][c]=(unsigned char) fgetc (fp);
printf("%d",image);
}
}
fclose(fp);
}
getch();
return(0);
}

This post has been edited by sreena: 03 March 2008 - 11:09 AM


Is This A Good Question/Topic? 1

Replies To: Read an image using c

#2 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: Read an image using c

Posted 03 March 2008 - 11:10 AM

:code:
Makes it easier for us to read, and to help you!

:)
Was This Post Helpful? 0
  • +
  • -

#3 mohdsyukri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-February 08

Re: Read an image using c

Posted 03 March 2008 - 11:15 AM

i think you miss something call prototype

#include<stdlib.h>
Was This Post Helpful? 0
  • +
  • -

#4 taure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-September 07

Re: Read an image using c

Posted 03 March 2008 - 11:37 AM

I'm afraid loading a picture isn't that simple. The answer is: no, you're not getting pixel colors. That's because
1. jpeg files (just like I suppose all of graphic files) begin with a header specifying width, height, compression ratio etc.
2. you may think of skipping the header information to get to the pixels. This also wouldn't work as JPEGs contain compressed data. You can't get direct access to data in a ZIP or RAR file and so you can't handle JPEGs this way.

This sucks, doesn't it. Now what should you do? You may find a third-party library that would load JPEG into memory on its own. You may learn to handle (relatively) easier format of BMP.
Was This Post Helpful? 0
  • +
  • -

#5 taure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-September 07

Re: Read an image using c

Posted 03 March 2008 - 11:56 AM

In the end you may do what I did when I began to learn OpenGL. OpenGL requires textures to be arrays of RGB triples (unsigned char a_picture[width][height][3]) - just what you need in this case.

To get them from the file in an easy way, I made an additional app in Delphi (you may use C++ Builder or any RAD environment you have access to). The app loaded a file from disk into a TBITMAP object which provides direct access to pixels. Then it copied information from TBITMAP bits to a raw file, which I then used in the first program.

If you want to I may rewrite this "converter" for you ;-)
Was This Post Helpful? 0
  • +
  • -

#6 mmakrzem  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 56
  • Joined: 11-January 08

Re: Read an image using c

Posted 04 March 2008 - 08:17 AM

loading in an image is not quite that easy. Each image format is saved in a different way. This means you need to know the algorithm used so that you can extract the pixel data inside the file.

On my website, you can see how to implement a TGA file format loader if you are interested. You can also find a bunch of image libraries on the internet that allow you to load in different file formats. For example check out http://freeimage.sourceforge.net/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1