hi,
i have to do image read and write in Turbo C.
i am very new to C.
so please help me out as to how to do these operations and provide some sample code if you have any..
i have a vague idea that it can be read as .dat file and stored in a 2d array.
:confused:

thanks..

Turbo C?

I didn't know anybody still used this old horse!
Anyway, I found something in my archives and removed the dust! This reads a character file, you want to read a binary file. The end of file marker will be different. Well partner, it's a start!

/*******************************   fgetc.c  ********************************
**
**  Experiments with disk file I/O:
**     int fgetc(FILE *stream)
**  Get a character from the device indicated by stream  ( set by fopen() ).
**  Written in Turbo C by vegaseat 6/23/88
**
****************************************************************************/

#include <stdio.h>       /* defines FILE and EOF */

#define AMOUNT  5        /* amount of data items */
#define FALSE   0
#define TRUE    1

int display_file(void);

void main(void)
{
  if (display_file() == FALSE)
    puts("\n  Couldn't open data file!");
  printf("\n\n  Press any key to go on .... "); 
  getchar();  /* wait */
}

int display_file(void)  /* read an ASCII file and display on screen */
{
  int  ch;  /* use int since EOF = -1 */
  char filename[25];
  FILE *fptr;

  clrscr();
  printf("\n  Enter name of ASCII file : ");
  gets(filename);
  fptr = fopen(filename,"r");  /* open file for disk reading */

  if (fptr == 0)
    return (FALSE);            /* file opening unsuccessful */

  while ((ch = fgetc(fptr)) != EOF)  /* read entire file and display */
    putch(ch);                       /* it on the screen */

  fclose(fptr);                      /* don't forget to close the file! */
  return (TRUE);                     /* file succesfully scanned */
}

>Well partner, it's a start!
If by start you mean poor quality code that doesn't address the problem at all, yes.

thanks a lot vegaseat..
ur code is working great..

i am actually reading a gray scale image, which has 0-255 levels of gray intensities.
so each pixel in the image or each character represents a level of gray intensity(eight bits in binary).
but as far as characters are concerned, there are only 128 ascii chars, so how can 128 ascii chars represent 256 levels of image intensities??

guess i made my question clear enough..
can anyone clear it for me???

what u need to do is to find out the header and pixel structures for the type of file ur reading and then declare them as structs at the start of ur code. Then u can read in chunks of data from your binary file using fwrite and fread and cast it into ur structs and edit it.

sounds kinda simple, but i am a C novice, so looks very complicated for my understanding.

if some example codes are there, it will help me understand it.
please..anybody......

if i am asking too much, i cant help it..:-)

You got me stomped, which is easy to do.
Looking through the dusty old code I noticed that I assigned the incoming character to type integer to trap the End Of File marker (-1). It makes me suspicious that in Turbo C type char was unsigned 8bit (0 to 255).

yes vegaseat, char stores 1byte.
but with 128 characters, how will it represent 256 levels????

or am i overlooking a simple fact..i dono...

yes vegaseat, char stores 1byte.
but with 128 characters, how will it represent 256 levels????

or am i overlooking a simple fact..i dono...

In fact, 1 byte = 8 bits; so a char variable can have 2^8 = 256 values; this have nothing to do with ASCII chars...

In fact, 1 byte = 8 bits; so a char variable can have 2^8 = 256 values; this have nothing to do with ASCII chars...

i have an 256x256 grayscale image in .dat format.
i am reading the file in turbo C in an 2d char array and comparing with another similar picture to check if they are the same..
and i am doing some enhancement of the original image,
and to view the enhanced image, am changing .dat to .raw format and viewing in adobe photoshop.

well is this process right?
in the sense that getting an gray scale image in 2d char array.
will it be exactly reproduced.
will the 2d char array handle a gray scale image?
thats my basic doubt..

i have an 256x256 grayscale image in .dat format.
i am reading the file in turbo C in an 2d char array and comparing with another similar picture to check if they are the same..
and i am doing some enhancement of the original image,
and to view the enhanced image, am changing .dat to .raw format and viewing in adobe photoshop.

well is this process right?
in the sense that getting an gray scale image in 2d char array.
will it be exactly reproduced.
will the 2d char array handle a gray scale image?
thats my basic doubt..

Sure...In fact, your image (with the size 256x256 and 256 grey levels) is exactly a 2D array with the dimensions 256x256, and it has to store 256 different values, that means 1 byte => char array_img[256][256]...

well i guess turbo C can accept a max array size of [250][250]..
above that it gives an error..

but for starters i took unsighned char image[250][250]
now i have to change this gray scale image to binary image..
i set a threshold of 125 and all pixel values above 125 become 255(white)
or 0(black) if below 125..
for(i=0;i<250;i++)
{
for(j=o;j<250;j++)
{
if(image[j]>125)
image[j]=255;
else
image[j]=0;
}
}

this is not working, it says lvalue required??
thats why i thought that maybe it is not possible to represent 256 gray levels with 128 ascii chars..

help me out....... :?:

Comments
Use code tags.

will the above code work in dev C compiler?Please also tell me how to capture ,store and read images using dev C.It is urgent if somebody can tell it fast I will be so thankful to him.

will the above code work in dev C compiler?

who knows? this thread FOUR YEARS OLD and is about a Turbo C program that was probably 10 years old when the thread was written.

Please also tell me how to capture ,store and read images using dev C. It is urgent

No it's not urgent.

your lack of foresight in starting your homework assignments constitutes no urgency on our part to go dig through some crappy old TURBO C program.

look here, son, going through life stealing other peoples programs off the internet without any understanding of what you are doing is going to come back and bite you in the ass, and hard.

And I'm not going to be the one to give you rope to hang yourself with.

you need to define your own problem, start coding your own ** solution, and then --- when you get stuck --- start your **own thread using coherent questions and posting your own code using CODE tags.

and if you're using Turbo C, which i fear that you are, do yourself and the rest of the world a favor, and throw that crap into your recycle bin and get a REAL compiler.

Edited 3 Years Ago by happygeek: fixed formatting

Thank you for your advice.My problem is to capture the image and compress and transmit it.It is a part of a wireless sensor ntwork project we do.I already done this in MatLab.I just need to know how to capture an image using C (only the functions and library I dont know.)I can code it my self if I know the library and functions.Please let me know if you know this.I searched many books for this.(Capturing).And I am using dev C.

(1) i dont answer unsolicited PMs for programming help

(2) you didnt read my reply, did you? Because if you did, you would Start a new thread

This article has been dead for over six months. Start a new discussion instead.