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:
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 ...
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
}
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++)
{