BMP save EGA in Turbo C help needed (C source here-) 
Author Message
 BMP save EGA in Turbo C help needed (C source here-)

I have been having real problems trying to save a 640x350 16 color EGA
image to a .BMP file on disk.  I am using Borland Turbo C 2.0 and have
come up with the following code.  It does save the screen but the
resulting
.BMP image is overlapped and has pixels missing.  The resulting file
created
is twice the size it should be.  The file should be around 112,300 or
so
bytes.  The following code produces a file around 224,000 bytes.

I have tried to reduce this by incrementing the for loop that scans the
x
coordinates by 2.  This does reduce the file size and save the image.
But,
there are pixels missing, like this 1=on 0=off:
                      101010101010101010101010101
The 0 is a black pixel, the 1 is the color of the original image.

I would like to stick with using BGI functions to perform this.  

Can someone PLEASE help me?!  It seems so simple yet my head is balding
due
to all the hair I'm pulling.

REMEMBER:  I want it to be EGA, not VGA

Here is the source code   (GRAPHICS.H and the GRAPHICS.LIB are to be
included
in the main program)  This is just an excerpt:

/************
extern void WriteBMP (char *);                  /* func  prototype to
write BMP file */
extern BITMAPFILEHEADER BMPHeader;
extern BITMAPINFOHEADER BMPInfo;
**************/

#define NULLIMAGESIZE 0x0                     /* If image is 64K+,
imagesize()*/
                                                                                                          /* will return
this value*/
typedef struct BITMAPFILEHEADER
{
        unsigned int  Type;                   /* "BM" or 0x4D42*/
        unsigned long Size;                   /* Size of file in bytes*/
        unsigned int  Reserved1;              /* Set to 0*/
        unsigned int  Reserved2;              /* Set to 0*/
        unsigned long OffBits;                 /* Offset in file where*/
                                                                                         /* the bits begin*/

Quote:
}BITMAPFILEHEADER;

typedef struct BITMAPINFOHEADER
{
        unsigned long biSize;                   /* Size of the structure*/
        unsigned long biWidth;                  /* Width in pixels*/
        unsigned long biHeight;                 /* Height in pixels*/
        unsigned int  biPlanes;                 /* # ocolor Planes: Set to 1*/
        unsigned int  biBitCount;               /* Color bits per pixel*/
        unsigned long biCompression;            /* Compression Scheme*/
        unsigned long biSizeImage;              /* Number of bitmap bytes*/
        unsigned long biXPel{*filter*}eter;          /* Horizontal Resolution*/
        unsigned long biYPel{*filter*}eter;          /* Vertical Resolution*/
        unsigned long biClrUsed;                /* Number of colors used*/
        unsigned long biClrImportant;           /* Important colors*/

Quote:
}BITMAPINFOHEADER;

       char colors[64]={00,00,00,00,                
                        170,00,00,00,
                        00,170,00,00,
                        170,170,00,00,
                        00,00,170,00,
                        170,00,170,00,
                        00,55,170,00,
                        170,170,170,00,
                        55,55,55,00,
                        255,55,55,00,
                        55,255,55,00,
                        255,255,55,00,
                        55,55,255,00,
                        255,55,255,00,
                        55,255,255,00,
                        255,255,255,00};

/*********************start of save function**************/
void WriteBMP(char *BMPFile)
{
BITMAPFILEHEADER BMPHeader;
BITMAPINFOHEADER BMPInfo;
unsigned char Color;
int x,y;

FILE *Fn;

BMPHeader.Type=0x4D42;
BMPHeader.Size=(sizeof(BMPHeader)+imagesize(0,0,640,350)+
        sizeof(BMPInfo)+64);

BMPHeader.OffBits=118;
BMPInfo.biSize=sizeof(BMPInfo);
BMPInfo.biWidth=640;
BMPInfo.biHeight=350;
BMPInfo.biPlanes=1;
BMPInfo.biBitCount=4;
BMPInfo.biCompression=0;
BMPInfo.biSizeImage=112000;
BMPInfo.biXPel{*filter*}eter=0;
BMPInfo.biYPel{*filter*}eter=0;
BMPInfo.biClrUsed=0;
BMPInfo.biClrImportant=0;

Fn = fopen(BMPFile,"wb");               /* Open up BMP image file  */

                fwrite(&BMPHeader,sizeof(BMPHeader),1,Fn);
                fwrite(&BMPInfo,sizeof(BMPInfo),1,Fn);

            for(x=0;x<=63;x++)
           {
                                     fwrite(&colors[x],sizeof(char),1,Fn);
                              }
            for(y=350;y>=1;y--)
                  {
                                 for (x=1; x <= 640; x++)
                                          {

                                                     Color=(char)getpixel(x,y);
                                   fwrite(&Color,sizeof(Color),1,Fn);
                             }
                 }

     fclose(Fn);

Quote:
}

========================================================================
========  

Perry Horner                                 Creator of the:
Library Internet Specialist                  Fantasy Dungeon Builder
Arizona State University West                Fantasy War Simulator
(602) 523-8564                    
                          Amiga 2000/5/120 '020



Sat, 07 Dec 1996 19:09:41 GMT  
 BMP save EGA in Turbo C help needed (C source here-)

Quote:

>I have been having real problems trying to save a 640x350 16 color EGA
>image to a .BMP file on disk.  I am using Borland Turbo C 2.0 and have
>come up with the following code.  It does save the screen but the
>resulting
>.BMP image is overlapped and has pixels missing.  The resulting file
>created
>is twice the size it should be.  The file should be around 112,300 or
>so
>bytes.  The following code produces a file around 224,000 bytes.

  [stuff deleted]

Quote:
>BMPInfo.biSizeImage=112000;

  [stuff deleted]

On a 16-bit machine, 112000 needs to be coerced into a long, so maybe
using 112000L will make a difference.

Dave Eberly



Sun, 08 Dec 1996 05:54:10 GMT  
 BMP save EGA in Turbo C help needed (C source here-)

Quote:

>I have been having real problems trying to save a 640x350 16 color EGA
>image to a .BMP file on disk.  I am using Borland Turbo C 2.0 and have
>come up with the following code.  It does save the screen but the
>resulting .BMP image is overlapped and has pixels missing.  The
>resulting file created is twice the size it should be.  The file
>should be around 112,300 or so bytes.  The following code produces a
>file around 224,000 bytes.
>I have tried to reduce this by incrementing the for loop that scans
>the x coordinates by 2.  This does reduce the file size and save the
>image.  But, there are pixels missing, like this 1=on 0=off:
>                      101010101010101010101010101
>The 0 is a black pixel, the 1 is the color of the original image.  I
>would like to stick with using BGI functions to perform this.  Can
>someone PLEASE help me?!  It seems so simple yet my head is balding
>due to all the hair I'm pulling.
>REMEMBER:  I want it to be EGA, not VGA
>Here is the source code   (GRAPHICS.H and the GRAPHICS.LIB are to be
>included
>in the main program)  This is just an excerpt:
>/************
>extern void WriteBMP (char *);                  /* func  prototype to
>write BMP file */
>extern BITMAPFILEHEADER BMPHeader;
>extern BITMAPINFOHEADER BMPInfo;
>**************/
>#define NULLIMAGESIZE 0x0                     /* If image is 64K+,
>imagesize()*/
>                                                                                                      /* will return
>this value*/
>typedef struct BITMAPFILEHEADER
>{
>    unsigned int  Type;                   /* "BM" or 0x4D42*/
>    unsigned long Size;                   /* Size of file in bytes*/
>    unsigned int  Reserved1;              /* Set to 0*/
>    unsigned int  Reserved2;              /* Set to 0*/
>    unsigned long OffBits;                 /* Offset in file where*/
>                                                                                     /* the bits begin*/
>}BITMAPFILEHEADER;
>typedef struct BITMAPINFOHEADER
>{
>    unsigned long biSize;                   /* Size of the structure*/
>    unsigned long biWidth;                  /* Width in pixels*/
>    unsigned long biHeight;                 /* Height in pixels*/
>    unsigned int  biPlanes;                 /* # ocolor Planes: Set to 1*/
>    unsigned int  biBitCount;               /* Color bits per pixel*/
>    unsigned long biCompression;            /* Compression Scheme*/
>    unsigned long biSizeImage;              /* Number of bitmap bytes*/
>    unsigned long biXPel{*filter*}eter;          /* Horizontal Resolution*/
>    unsigned long biYPel{*filter*}eter;          /* Vertical Resolution*/
>    unsigned long biClrUsed;                /* Number of colors used*/
>    unsigned long biClrImportant;           /* Important colors*/
>}BITMAPINFOHEADER;
>       char colors[64]={00,00,00,00,                
>                    170,00,00,00,
>                    00,170,00,00,
>                    170,170,00,00,
>                    00,00,170,00,
>                    170,00,170,00,
>                    00,55,170,00,
>                    170,170,170,00,
>                    55,55,55,00,
>                    255,55,55,00,
>                    55,255,55,00,
>                    255,255,55,00,
>                    55,55,255,00,
>                    255,55,255,00,
>                    55,255,255,00,
>                    255,255,255,00};
>/*********************start of save function**************/
>void WriteBMP(char *BMPFile)
>{
>BITMAPFILEHEADER BMPHeader;
>BITMAPINFOHEADER BMPInfo;
>unsigned char Color;
>int x,y;
>FILE *Fn;
>BMPHeader.Type=0x4D42;
>BMPHeader.Size=(sizeof(BMPHeader)+imagesize(0,0,640,350)+
>    sizeof(BMPInfo)+64);

imagesize() doesn't return just the number of bytes used in the image.
It also includes a 4(?) byte header used by GetImage() and PutImage().
I may have the names wrong here, but I'm certain imagesize() includes
the header size.

Quote:
>BMPHeader.OffBits=118;
>BMPInfo.biSize=sizeof(BMPInfo);
>BMPInfo.biWidth=640;
>BMPInfo.biHeight=350;
>BMPInfo.biPlanes=1;
>BMPInfo.biBitCount=4;
>BMPInfo.biCompression=0;
>BMPInfo.biSizeImage=112000;

Should this number be hard coded?

Quote:
>BMPInfo.biXPel{*filter*}eter=0;
>BMPInfo.biYPel{*filter*}eter=0;
>BMPInfo.biClrUsed=0;
>BMPInfo.biClrImportant=0;
>Fn = fopen(BMPFile,"wb");               /* Open up BMP image file  */
>            fwrite(&BMPHeader,sizeof(BMPHeader),1,Fn);
>            fwrite(&BMPInfo,sizeof(BMPInfo),1,Fn);
>        for(x=0;x<=63;x++)
>           {
>                                 fwrite(&colors[x],sizeof(char),1,Fn);
>                          }
>        for(y=350;y>=1;y--)

I think rows are numbered from 0 to 349, not 1 to 350.  Also, are you
sure you want to go from 349 down to 0 and not the other way around?
Pixel 0,0 is the top left corner of the image.

Quote:
>              {
>                             for (x=1; x <= 640; x++)

Same thing here.  columns are numbered 0 to 639.

Quote:
>                                      {
>                                                 Color=(char)getpixel(x,y);
>                                   fwrite(&Color,sizeof(Color),1,Fn);
>                             }
>             }
>     fclose(Fn);
>}
>========================================================================
>========  
>Perry Horner                                 Creator of the:
>Library Internet Specialist                  Fantasy Dungeon Builder
>Arizona State University West                Fantasy War Simulator
>(602) 523-8564                    
>                          Amiga 2000/5/120 '020

Dave Hayden



Sun, 08 Dec 1996 05:47:17 GMT  
 BMP save EGA in Turbo C help needed (C source here-)

Quote:

> On a 16-bit machine, 112000 needs to be coerced into a long, so maybe
> using 112000L will make a difference.

Not in C.  From the standard (ISO 9899-1990, section 6.1.3.2 Integer
constants):

        The type of an integer constant is the first of the corresponding
        list in which its value can be represented.  Unsuffixed decimal:
        int, long int, unsigned long int;  - - -

If your compiler can't handle that, then your compiler is broken.  Go
yell at whoever made it.

[ Followups redirected to me; this is a comp.lang.c issue, but it's
  somewhat recently been discussed and doesn't need rehashing.  The
  situation is quite clear. ]

--

My name is Ozymandias, king of kings/Look on my works, ye Mighty, and despair!



Mon, 09 Dec 1996 04:28:02 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. newbe/cs student, need help w/ code

3. Compile CS source code using ICodeCompiler

4. cs source code

5. resx files needed for cs - files ??

6. Need C++ text for non cs major course

7. Compiler error CS 1595 - Help

8. Help!!! Novice CS Advice

9. How to show/call Form2.cs from Form1.cs ?

10. Include code in other Cs files

11. Reuse of cs files, namespace, arch advice pls

12. word - automatic numbering/bold/underline/italics

 

 
Powered by phpBB® Forum Software