Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

c++ - Importing BMP file in Turboc++ issue:BMP file is not being displayed properly in the output screen

I am trying to import Anand.BMP file in the graphics window of TCPP,
for this its source code is as follows
(NOTE: I have not mentioned the header files in the source code):

struct A
{
 char type[2];
 unsigned long size;
 unsigned short int reserved1,reserved2;
 unsigned long offset;
 unsigned long width,height;
 unsigned short int planes;
 unsigned short int bits;
 unsigned long compression;
 unsigned long imagesize;
 unsigned long xresolution,yresolution;
 unsigned long ncolors;
 unsigned long importantcolors;
}HEADER;
huge DetectSvga()
{
 return 2;
}
void show()
{
 fstream File;
 File.open("C:\TURBOC3\BIN\Anand.BMP",ios::in|ios::binary);
 char ch;
 File.read((char*)&HEADER,sizeof(HEADER));
 unsigned int i;
 char ColorBytes[4];
 char *PaletteData;
 PaletteData=new char[256*3];
 if(PaletteData)
 {
  for(i=0;i<256;i++)
  {
   File.read(ColorBytes,4);
   PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
   PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
  }
  outp(0x03c8,0);
  for(i=0;i<256*3;i++)
   outp(0x03c9,PaletteData[i]);
  delete[]PaletteData;
 }
 for(i=0;i<HEADER.height;i++)
 {
  for(int j=0;j<HEADER.width;)
  {
   File.read(&ch,1);
   putpixel(0+(j++),0+HEADER.height-i-1,ch);
  }
 }
 File.close();
}
void main()
{
 clrscr();
 int gd=DETECT,gm,a;
 initgraph(&gd,&gm,"C:\TURBOC3\BGI");
 installuserdriver("svga256",&DetectSvga);
 show();
 getch();
 closegraph();
}

Now, i am not getting the BMP file in the graphics window,
i.e,
Graphics Window is not displaying Anand.bmp properly; Output is displayed like this
so how to fix it?
Here I am attaching my Anand.BMP file for convenience.

I think palette is not properly displayed through PaletteData pointer,
i.e, error is in this block of codes:

for(i=0;i<256;i++)
  {
   File.read(ColorBytes,4);
   PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
   PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
  }

As per suggestions I have modified the above codes as follows:
[EDIT] :

typedef unsigned long   DWORD;
typedef unsigned int  WORD;
typedef unsigned short  BYTE;

//---------------------------------------------------------------------------
class BMP
{
 public:
  BYTE *data;
  DWORD size;
  #pragma pack(push,1)
  struct _hdr
  {
   char ID[2];
   DWORD size;
   WORD reserved1[2];  // ?
   DWORD offset;
   DWORD reserved2;    // ?
   DWORD width,height;
   WORD planes;
   WORD bits;
   DWORD compression;
   DWORD imagesize;
   DWORD xresolution,yresolution;
   DWORD ncolors;
   DWORD importantcolors;
  };
  #pragma pack(pop)
  BMP(){ data=NULL; free(); }

  ~BMP(){ free(); }

  void free(){ if (data) delete[] data; data=NULL; size=0;  }
  void load(char* filename)
  {
   FILE *hnd;
   free();
   if ((hnd=fopen(filename, "rb")) == NULL) return; // open file for read binary (not sure with the "b" check in build help)
   size=fseek(hnd,0,2);
   fseek(hnd,0,0);
   BYTE data[256];
   if (data==NULL)          // not enough memory or empty file
   {
    size=0;
    fclose(hnd);      
    return;
   }
   fread(data,256,1,hnd); // read 256 of 1 BYTES into data array
   fclose(hnd); // close file
  }
  void draw(int x0,int y0)
  {
   _hdr *hdr=(_hdr*)data;
   int x,y,xs,ys,skip;
   DWORD pal[256],c;  // palete to convert 8bpp -> 32bit VCL color
   BYTE *p;
   if (size<2) return;
   if (hdr->ID[0]!='B') return;    // check magic number
   if (hdr->ID[1]!='M') return;
   if (hdr->planes!=1) return;     // check format
   if (hdr->bits!=8) return;
   if (hdr->compression!=0) return;
   // palette
   p=data+hdr->offset-(3*256);
   p=data+sizeof(_hdr);
   for (x=0;x<256;x++)
   {
    c =(*p)    ; p++;   // B
    c|=(*p)<< 8; p++;   // G
    c|=(*p)<<16; p++;   // R
         p++;   // A
    pal[x]=c;
   }
   // image
   xs=hdr->width;
   ys=hdr->height;
   p=data+hdr->offset;
   skip=(((hdr->bits*hdr->width)+31)>>5)<<2;  // compute scanline align
   skip-=hdr->width;
   for (y=0;y<ys;y++)
   {
    for (x=0;x<xs;x++,p++)
    {
     putpixel(x0+x,y0+ys-y-1,*p);
    }
    p+=skip;                       // handle align
   }
   y++;
  }
};
//---------------------------------------------------------------------------

    huge DetectSvga()
{
 return 2;
}

void main()
{
 clrscr();
 int gd=DETECT,gm,a;
 initgraph(&gd,&gm,"C:\TURBOC3\BGI");
 installuserdriver("svga256",&DetectSvga);
 BMP bmp;
 bmp.load("C:\TURBOC3\BIN\Anand.BMP");
 bmp.draw(0,0);
 getch();
 closegraph();
}

Now,the above code is giving no errors but 2 warnings !!

WARNING :
1: for(x=0;x<256;x++) : "Functions containing for are not expanded inline"
2 : } , i.e, at the end of void load() function : "Functions containing some if statements are not expanded inline"

As a result image is not displaying in the output window
Output is displayed like this

I think y++; should be inside for (y=0;y<ys;y++){...} loop
So,please analyse the edited code...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your decoding BMP code has a lot of problems... As I mentioned in my comments BMP is a mess with too many variations of format where you get lost very quickly so you need to have BMP format matching your decoding routine...

Yes you change BMP to 8bpp but still its format is different to yours slightly ...

Ok let use this image of yours (why to heck imgur is not supporting this???).

After a while of (de)coding I come up with this C++/VCL code that decodes your bmp properly:

//---------------------------------------------------------------------------
class BMP
    {
public:
    BYTE *data;
    DWORD size;
    #pragma pack(push,1)
    struct _hdr
        {
        char ID[2];
        DWORD size;
        WORD reserved1[2];  // ?
        DWORD offset;
        DWORD reserved2;    // ?
        DWORD width,height;
        WORD planes;
        WORD bits;
        DWORD compression;
        DWORD imagesize;
        DWORD xresolution,yresolution;
        DWORD ncolors;
        DWORD importantcolors;
        };
    #pragma pack(pop)
    BMP(){ data=NULL; free(); }
    ~BMP(){ free(); }
    void free(){ if (data) delete[] data; data=NULL; size=0;  }

    void load(AnsiString filename)                      // load BMP into memory
        {
        int hnd;
        free();
        hnd=FileOpen(filename,fmOpenRead);              // open file
        if (hnd<0) return;
        size=FileSeek(hnd,0,2);                         // seek to end of file to obtain filesize
             FileSeek(hnd,0,0);                         // seek to start of file
        data=new BYTE[size];                            // allocate memory space for the BMP
        if (data==NULL)                                 // not enough memory or empty file
            {
            size=0;
            FileClose(hnd);
            return;
            }
        FileRead(hnd,data,size);                        // load the data
        FileClose(hnd);
        }
    void draw(Graphics::TBitmap *bmp,int x0,int y0)     // decode/render bitmap onto VCL bitmap
        {
        _hdr *hdr=(_hdr*)data;
        int x,y,xs,ys,skip;
        DWORD pal[256],c;                               // palete to convert 8bpp -> 32bit VCL color
        BYTE *p;
        if (size<2) return;
        if (hdr->ID[0]!='B') return;                    // check magic number
        if (hdr->ID[1]!='M') return;
        if (hdr->planes!=1) return;                     // check format
        if (hdr->bits!=8) return;
        if (hdr->compression!=0) return;
        // palette
        p=data+hdr->offset-(3*256);
        p=data+sizeof(_hdr);
        for (x=0;x<256;x++)
            {
            c =(*p)    ; p++;   // B
            c|=(*p)<< 8; p++;   // G
            c|=(*p)<<16; p++;   // R
                         p++;   // A
            pal[x]=c;
            }
        // image
        xs=hdr->width;
        ys=hdr->height;
        p=data+hdr->offset;
        skip=(((hdr->bits*hdr->width)+31)>>5)<<2;       // compute scanline align
        skip-=hdr->width;
        for (y=0;y<ys;y++)
            {
            DWORD *q=(DWORD*)bmp->ScanLine[y0+ys-y-1];  // target VCL bitmap scanline pointer
            for (x=0;x<xs;x++,p++) q[x0+x]=pal[*p];     // copy pixels to target VCL bitmap
            p+=skip;                                    // handle align
            }
        y++;
        }
    };
//---------------------------------------------------------------------------

And usage:

BMP bmp;
bmp.load("Anand.bmp");
bmp.draw(target_VCL_bitmap,0,0);

Well as I do have different compiler (but also Borland/Embarcadero) and OS you need to ignore the VCL stuff and replace the rendering with your BGI ... Then change the AnsiString to char* and change the file access routines to your environment (do not forget it should be binary access but IIRC even that did not always work in TCPP had problems with it in past while loading textures into my 3D renderer some control codes where processed regardless of binary access...

Now what you have missing:

  1. header

    the header of BMP used is different than yours (there are a lot of variations out there that is why I suggested to use PCX instead). So take a look at mine _hdr struct and mimic yours ... The DWORD is unsigned 32 bit int, WORD is unsigned 16 bit int and BYTE is unsigned 8 bit int. I think that TCPP knows them but was ages i code in it so I might be wrong so if the case use relevant data types instead.

    You also do not check for the correct BMP format which is wrong and might lead to crashes so you should at least check the magic number and bpp, compression etc ... like I do

    Also do not forget to set the code alignment to 1 Byte (that is what the #pragma pack are for but not sure if TCPP support that. If not the align should be somewhere in the TCPP IDE settings probably in linker or compiler ...

  2. palette

    Your palette loading is suspicious I do not like it ... compare it with mine in the draw routine.

    Also your setting VGA palette routine is wrong see how it should be done. So the target color should be set for each color not just once for whole palette so you need move the out inside loop:

    for(i=0;i<256*3;)
       {
       outp(0x03c8,i/3);
       outp(0x03c9,PaletteData[i]); i++; // R
       outp(0x03c9,PaletteData[i]); i++; // G
       outp(0x03c9,PaletteData[i]); i++; // B
       }
    
  3. Image data

    you are not aligning the scan lines at all that is why your decoded image is shifted (skew like). According to Wiki each scan line is aligned to size:

    (((bits*width)+31)>>5)<<2
    

    So just skip unused BYTEs in the file after each row decoded.

    You also do not use offset which tells you where in the file the image data starts. That is important because the image data can be anywhere not just directly after palette as there might be more data present in the file like important colors etc ...

Also as you can see I loaded whole image into memory and decode from there. As you are in 16bit environment you might not want to do this as your OS might prevent you from allocating as much memory and also you are limited in memory size quite a lot... But I coded the whole stuff so I do not go back and forward so you should have no problems to port it to decoding directly from file like you have now ...

[Edit1]

here I dig some ancient example of file access from TCPP:

#include <stdio.h>
FILE *hnd;
BYTE data[256];
if ((hnd=fopen("texture.txr", "rb")) == NULL) return; // open file for read binary (not sure with the "b" check in build help)
fread(data,256,1,hnd); // read 256 of 1 BYTES into data array
fclose(hnd); // close file

just verify the usage with your inbuild help (CTRL+F1 while cursor is on a keyword there you will also see which include it needs if stdio is not the one) as I used this ~20 years ago and do not remember exactly... You will also need seek I think its called fseek and parameters are similar to mine FileSeek.

[Edit2] from your updated code its obvious you just copy paste code without thinking...

I managed to code this in TCPP+DOSBOX (ouch man that was pain in the ass as DOSBOX keyboard conflicts borland shortcuts ...)

You did not check the inbuild TCPP help and did not port the stuff correctly. For example yours fseek does not return file size like mine which you would detect right away if you try to debug (F8/F7) ... So here my new C++ (TCPP compatible) code for this:

//---------------------------------------------------------------------------
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------
typedef unsigned long DWORD;
typedef unsigned int  WORD;
typedef unsigned char BYTE;
//---------------------------------------------------------------------------
char far* scr;              // VGA screen
const _sx= 320;             // physical screen size
const _sy= 200;
void gfxinit()
    {
    asm {   mov ax,19
        int 16
        }
    scr=(char far*)0xA0000000;
    }
void gfxexit()
    {
    asm {   mov ax,3
        int 16
        }
    }
void clrscr()
    {
    asm {   push    es
        mov ax,0xA000
        mov es,ax
        mov di,0x0000
        sub ax,ax
        mov cx,32000
        rep stosw
        pop es
        }
    }
void putpixel(int x,int y,char c)
    {
    unsigned int adr;
    if ((x<_sx)&&(x>=0))
     if ((y<_sy)&&(y>=0))
        {
        adr=x+(y*_sx);
        scr[adr]=c;
        }
    }
//---------------------------------------------------------------------------
class BMP
    {
public:
    BYTE *data;
    DWORD size;
    #pragma pack(push,1)
    struct _hdr
        {
        char ID[2];
        DWORD size;
        DWORD reserved1;  // ?
        DWORD offset;
        DWORD reserved2;  // ?
        DWORD width,height;
        WORD planes;
        WORD bits;
        DWORD compression;
        DWORD imagesize;
        DWORD xresolution,yresolution;
        DWORD ncolors;
        DWORD importantcolors;
        };
    #pragma pack(pop)
    BMP(){ data=NULL; free(); }
    ~BMP(){ free(); }
    void free(){ if (data) delete[] data; data=NULL; size=0;  }
    void load(char* filename);
    void draw(int x0,int y0);
    };
//---------------------------------------------------------------------------
void BMP::load(char* filename)
    {
    FILE *hnd;
    free();
    if ((hnd=fopen(filename, "rb")) == NULL) return; // open file for read binary (not sure with the "b" check in build help)
    _hdr hdr;
    hdr.ID[0]=0;
    hdr.ID[1]=0;
    hdr.size=0;
    fread(&hdr,sizeof(_hdr),1,hnd); // read BMP header into memory
    if (hdr.ID[0]=='B')
     if (hdr.ID[1]=='M')
      size=hdr.size;    // get file size
    fseek(hnd,0,0);     // seek back to start
    data=new BYTE[size];
    if (data==NULL)     // not enough memory or empty file
        {
        size=0;
        fclose(hnd);
        return;
        }
    fread(data,size,1,hnd); // read BMP into memory
    fclose(hnd);        // close file
    }
//---------------------------------------------------------------------------
void BMP::draw(int x0,int y0)
    {
    _hdr *hdr=(_hdr*)data;
    int x,y,xs,ys,skip;
    BYTE *p;
    if (size<2) return;
    if (hdr->ID[0]!='B') return;    // check magic number
    if (hdr->ID[1]!='M') return;
    if (hdr->planes!=1) return;     // check format
    if (hdr->bits!=8) return;
    if (hdr->compression!=0) return;
    // palette
    p=data+sizeof(_hdr);
    for (x=0;x<256;x++)
        {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...