944,147 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 19585
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 28th, 2004
0

Image read and write operations in turbo C

Expand Post »
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.


thanks..
Reputation Points: 10
Solved Threads: 0
Light Poster
dalaharp is offline Offline
31 posts
since Oct 2004
Oct 28th, 2004
0

Re: Image read and write operations in turbo C

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!
[PHP]/******************************* 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 */
}
[/PHP]
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 28th, 2004
0

Re: Image read and write operations in turbo C

>Well partner, it's a start!
If by start you mean poor quality code that doesn't address the problem at all, yes.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 2nd, 2004
0

Re: Image read and write operations in turbo C

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???
Reputation Points: 10
Solved Threads: 0
Light Poster
dalaharp is offline Offline
31 posts
since Oct 2004
Nov 2nd, 2004
0

Re: Image read and write operations in turbo C

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mr_mooz is offline Offline
11 posts
since Oct 2004
Nov 2nd, 2004
0

Re: Image read and write operations in turbo C

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..:-)
Reputation Points: 10
Solved Threads: 0
Light Poster
dalaharp is offline Offline
31 posts
since Oct 2004
Nov 2nd, 2004
0

Re: Image read and write operations in turbo C

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).
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 3rd, 2004
0

Re: Image read and write operations in turbo C

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

or am i overlooking a simple fact..i dono...
Reputation Points: 10
Solved Threads: 0
Light Poster
dalaharp is offline Offline
31 posts
since Oct 2004
Nov 3rd, 2004
0

Re: Image read and write operations in turbo C

Quote originally posted by dalaharp ...
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...
Reputation Points: 17
Solved Threads: 9
Posting Whiz in Training
frrossk is offline Offline
220 posts
since Sep 2004
Nov 4th, 2004
0

Re: Image read and write operations in turbo C

Quote originally posted by frrossk ...
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..
Reputation Points: 10
Solved Threads: 0
Light Poster
dalaharp is offline Offline
31 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reading from a floppy disk
Next Thread in C Forum Timeline: Programming Problem Help for school





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC