Article View: rec.games.programmer
Article #531Re: Image saving in MSC7.0
From: Dixie Cup
Date: Sun, 09 Aug 1992 15:09
Date: Sun, 09 Aug 1992 15:09
59 lines
2594 bytes
2594 bytes
In article <485@audfax.audiofax.com> brian@audiofax.com (Brian Troxell) writes:
>I am attempting to write my first graphics game, so forgive me if
>this is a simple question.
>
>I am using Microsoft C 7.0 on a 386SX. The basic thing I want to do is
>to use getimage and putimage to move images around. The images are small
>(20 pels by 20 pels), so speed is not a problem. However, I don't want
>to create the images and then use getimage to get it. I would like to save
>the image to a hard drive, then just read it in and putimage it on the
>screen.
>
>Any ideas on how to do this?
>
>I tried the simple approach: usr fopen and fwrite and write out the data
>stored by a pointer defined in a getimage call, then use fread and putimage
>on the file. Putimage returns 0, but nothing gets displayed.
>
>Any help, pointers, references, etc. would be greatly appreciated.
Well, wrting to a file is easy, reading is a little trickier. I use
Borland C so I'm not sure if the same format will apply for reading.
But here goes:
void SaveImage(void *image, unsigned size){
FILE *imgfile = fopen("image","w");
fwrite(image, size, 1, imgfile);
fclose(imgfile);
}
You have to pass the size of the image that is returned by the imagesize
function. Now to read an image from a file if you already know the size
of the image is easy.
void ReadImage(void *image, unsigned size){
FILE *imgfile = fopen("image","r");
image = malloc(size);
fread(image, size, 1, imgfile);
fclose(imgfile);
}
If you don't already know the size ahead of time you can take a couple
of strategies. You can write the size at the beginning of the file and
then just read that first and then continue to read the image or you
can try to determine the size from the info in the image format. This
is pretty much what you would have to do to read the size from the image
format:
x = fgetc(imgfile) | (fgetc(imgfile) << 8);
y = fgetc(imgfile) | (fgetc(imgfile) << 8);
size = imagesize(0, 0, x, y);
image = malloc(size);
rewind(imgfile);
Then insert the ReadImage code.
Daniel
Technical Details
Message-ID:
<1992Aug9.150916.21282@wam.umd.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!haven.umd.edu!wam.umd.edu!loffitReferences:
<485@audfax.audiofax.com>Show Full Headers
Complete Article Headers
Newsgroups: rec.games.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!ames!haven.umd.edu!wam.umd.edu!loffit From: loffit@wam.umd.edu (Dixie Cup) Subject: Re: Image saving in MSC7.0 Message-ID: <1992Aug9.150916.21282@wam.umd.edu> Sender: usenet@wam.umd.edu (USENET News system) Nntp-Posting-Host: rac2.wam.umd.edu Organization: University of Maryland, College Park References: <485@audfax.audiofax.com> Distribution: na Date: Sun, 9 Aug 1992 15:09:16 GMT Lines: 59