Member Avatar for Member #572493

how to display 256 color bmp files in trubo c++ 3.0??? i ve already displayed a 16 color bitmap in turbo c++...
i came across this code but it doesnt work...pls tell me whts the error...

#include<iostream.h>
#include<graphics.h>
#include<fstream.h>
#include<conio.h>
struct A{
    char type[2];                 /* Magic identifier            */
    unsigned long size;                       /* File size in bytes          */
    unsigned short int reserved1, reserved2;
    unsigned long offset;                     /* Offset to image data, bytes */
}HEADER,HEADER1;
struct B{
    unsigned long size;               /* Header size in bytes      */
    unsigned long width,height;                /* Width and height of image */
    unsigned short int planes;       /* Number of colour planes   */
    unsigned short int bits;         /* Bits per pixel            */
    unsigned long compression;        /* Compression type          */
    unsigned long imagesize;          /* Image size in bytes       */
    unsigned long xresolution,yresolution;     /* Pixels per meter          */
    unsigned long ncolours;           /* Number of colours         */
    unsigned long importantcolours;   /* Important colours         */
}INFOHEADER,INFOHEADER1;

huge DetectSvga()
{
    return 2;
}

void Show()
{
    fstream File;
    File.open("c:\\tc\\bin\\tet.bmp",ios::in);
    char Ch;
    File.read((char*)&HEADER,14); //This is the header part of the Bitmap. 
    File.read((char*)&INFOHEADER,40); //This is another part of the bitmap
    unsigned int i;
    char ColorBytes[4];
    char*PaletteData;

    PaletteData=new char[256*3];

    if(PaletteData)//if memory allocated successfully
    {
	//read color data
	for(i=0;i<256;i++)
	{
	    //pls xplain wht this part does..i don understand it fully
	    File.read(ColorBytes,4);
	    PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
	    PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
	    PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
	}

	outp(0x03c8,0);      //tell DAC that data is coming

	for(i=0;i<256*3;i++) //send data to SVGA DAC
	{
	    outp(0x03c9,PaletteData[i]);
	}
	delete[]PaletteData;
    }

    for(i=0;i<INFOHEADER.height;i++)       //This for loop is used to display the bitmap.
    {
	for(int j=0;j<INFOHEADER.width;)
	{
	    File.read(&Ch,1); // Here Ch reads the color of your bitmap.
	    putpixel(0+(j++),0+INFOHEADER.height-i-1,Ch); 

	}
    }

    File.close();

}
void Show1()
{

    char Ch;
    fstream File;
    File.open("c:\\tc\\bin\\ab.bmp",ios::in);
    File.seekg(54,ios::beg);   
    File.seekg(256*4,ios::cur);
    for(int i=0;i<214;i++) 
    {
	for(int j=0;j<356;j++)
	{
	    File.read(&Ch,1); //Here Ch is the character which reads the color of your bitmap.
	    putpixel(10+j,10+39+i,Ch);
	}
    }
}
void main()
{
clrscr();
    int gd = DETECT, md, a;
    initgraph(&gd,&md,"c:\\tc\\bgi"); //Path may be different in your computer.
    installuserdriver("svga256",&DetectSvga);
Show();
  getch();
setcolor(BLACK);
setfillstyle(1,0);
bar(0,0,640,480);//clears the screen..cant use clrscr() as den myscreen turns grey
   Show1();
   getch();

}

PS-
1. i have svga256.bgi in c:\\tc\\bgi...do i need sum other files??
2. this is to be used in my skool project...n i asked my teacher n she said u cant use allegero,fastgraph or ne other compiler...i HAVE to use turbo c++ onli<sigh>
3."ab.bmp"(file i m trying to open) is attached with this msg
4.when i try to run the code...if i use show(),i get a completely destroyed form of the pic...its worse thn wht i get by displaying 16 colors... if i use show1(),i get the same output but this tym its inverted also...pls help!!!!!!!!!

Dani AI

Generated

If you must stick with TC++ 3.0 + SVGA256, the two big gotchas behind the inverted/garbled output @skandh saw are file I/O mode and BMP row padding. Open the BMP in binary mode and seek to the pixel data using the file header offset (bfOffBits). BMP scanlines are stored bottom-up and each row is padded to a 4-byte boundary; if you read only width bytes per row you will drift and the image tiles or turns hazy. Compute the stride as ((width + 3) & ~3), read that many bytes per row, and plot only the first width bytes. Also ensure 8bpp and no compression (BI_RGB); BI_RLE8 needs decompression. See Microsoft’s BITMAPFILEHEADER (bfOffBits) and compression notes, and the stride/bottom-up rules in the BMP spec. BITMAPFILEHEADER, Bitmap Compression, BMP file format.

Palette details: as hinted, 8bpp BMP palette entries are stored as B, G, R, 0x00. VGA’s DAC uses 6 bits per component (0..63), so the common shift by 2 (>>2) maps BMP’s 0..255 values down for the DAC. You load the palette by writing the start index to port 0x3C8, then R, G, B bytes to 0x3C9 (auto-incrementing). That explains both the BGRA order you read and why >>2 produces correct colors. BMP file format, VGA palette ports 0x3C8/0x3C9.

Minimal fix pattern (sketch, not a drop-in):

ifstream f(path, ios::in | ios::binary);  // text mode will corrupt bytes
// ...read headers/palette...
f.seekg(bfOffBits, ios::beg);
const int stride = (width + 3) & ~3;
char* row = new char[stride];
for (int y = 0; y < height; ++y) {
    f.read(row, stride);
    for (int x = 0; x < width; ++x)
        putpixel(x0 + x, y0 + (height - 1 - y), (unsigned char)row[x]);
}
delete[] row;

Tip: there is no requirement that width be divisible by 4; only the stored row size is padded to a multiple of 4. And for clarity, : DAC is the VGA palette hardware; the >>2 narrows 8-bit BMP channels to the DAC’s 6-bit range. std::ios_base::openmode, VGA palette ports.

Recommended Answers

All 16 Replies

Try installing the lib folder again... from some other copy...Its a common problem in TC...you wont have that problem again once you replace that folder with the proper ones.

Or get a real compiler!!!!

> this is to be used in my skool project...n i asked my teacher n she said u cant use allegero,fastgraph or ne other compiler
Scratch that, get a real teacher instead.
They don't know C any better than you do. They "learnt" some tricks for TurboC 20 years ago, and are now too scared to update to another compiler because all that they know is in fact useless.

Which in turn means that all that YOU will know will in fact be useless.

commented: Agreed! +16
Member Avatar for Member #572493

hey...thnk u sooo much...all ur replies were really helpful specially the last one..but one more thing...is using dev c++ REALLY THT simple???
fr eg- for loading a bmp file,is dere ne command lyk

loadbitmap(x_cord,y_cord,filename);

???

Member Avatar for Member #572493

***this is fr other CBSE guys who have the same problem***

i just solved the prob...i successfully displayed 256 color bitmap in turbo C++ 3.0....i m so tempted to post the code but its against the rules...so i cant :(
but ill c if i cn upload the code in snippets section...
btw, i used mode13h n bitmap headers..google fr it...

Member Avatar for Member #572493

but one small prob....the size i get is 320x200...i noe deres no way i cn get 640x480 in mode13h...so the prob remains...still cant get 640x320x256 in turbo c++ 3.0...

Member Avatar for Member #572493

o wow!! u r so funi!!!!!!

commented: Go and dribble on some other forum -7

The problem is that the palet is BGRA

B = Blue
G = Green
R = Red
A = Reserved (not used)

so each color in the palet has 4 bytes.

Member Avatar for Member #572493

i managed to get it "almost" right...now i cn display the bmp clearly in 1024x768 or 640x420...but the bmp is tiled...cant spot the prob..

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<stdio.h>
struct A{
    char type[2];                 /* Magic identifier            */
    unsigned long size;                       /* File size in bytes          */
    unsigned short int reserved1, reserved2;
    unsigned long offset;                     /* Offset to image data, bytes */
};

A HEADER;

struct B{
    unsigned long size;               /* Header size in bytes      */
    unsigned long width,height;                /* Width and height of image */
    unsigned short int planes;       /* Number of colour planes   */
    unsigned short int bits;         /* Bits per pixel            */
    unsigned long compression;        /* Compression type          */
    unsigned long imagesize;          /* Image size in bytes       */
    unsigned long xresolution,yresolution;     /* Pixels per meter          */
    unsigned long ncolours;           /* Number of colours         */
    unsigned long importantcolours;   /* Important colours         */
};
B INFOHEADER;
void Show();
huge DetectSvga()
{
    return 4;// 4 for 1024x768,2 for 640x480
}
void main()
{
    int gdriver = DETECT,gmode;
    installuserdriver("SVGA256",&DetectSvga);
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); //Path may be different in your computer.
    getch();
    Show();
    getch();
}
void Show()
{
    fstream File;

    File.open("c:\\tc\\bin\\bri1.bmp",ios::in);

    unsigned char Ch;

    File.read((char*)&HEADER,14); 
    File.read((char*)&INFOHEADER,40); 

    unsigned int i;
    char ColorBytes[4];
    char*PaletteData;

    PaletteData=new char[256*3];

    if(PaletteData)//if memory allocated successfully
    {
	//read color data
	for(i=0;i<256;i++)
	{
	    File.read(ColorBytes,4);
	    PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
	    PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
	    PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
	}

	outp(0x03c8,0);      //tell DAC that data is coming

	for(i=0;i<256*3;i++) //send data to SVGA DAC
	{
	    outp(0x03c9,PaletteData[i]);
	}
	delete[]PaletteData;
    }
    for(i=0;i<INFOHEADER.height;i++)       //This for loop is used to display the bitmap.
    {
	for(int j=0;j<INFOHEADER.width;j++)
	{
	    File.read(&Ch,1); // Here Ch reads the color of bitmap.
	    putpixel(100+j,100+INFOHEADER.height-i-1,Ch); //puts the bmp at 100,100

	}
    }

    File.close();
}

bri1.bmp is attached

.....

commented: Worst post of the day award! -6
Member Avatar for Member #572493

k..finally figured out the mistake..this is the correct code...
this code can display 256 color bitmap in 640x480 or 1024x768

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<fstream.h>
#include<stdio.h>
struct A
{
char type[2];                 /* Magic identifier            */
unsigned long size;                       /* File size in bytes          */
unsigned short int reserved1, reserved2;
unsigned long offset;                     /* Offset to image data, bytes */
}HEADER;
struct B
{
unsigned long size;               /* Header size in bytes      */
unsigned long width,height;                /* Width and height of image */
unsigned short int planes;       /* Number of colour planes   */
unsigned short int bits;         /* Bits per pixel            */
unsigned long compression;        /* Compression type          */
unsigned long imagesize;          /* Image size in bytes       */
unsigned long xresolution,yresolution;     /* Pixels per meter          */
unsigned long ncolours;           /* Number of colours         */
unsigned long importantcolours;   /* Important colours         */
}INFOHEADER;
void loadbitmap(int x,int y,char filename[8])
{
fstream File;
File.open(filename,ios::in);
unsigned char Ch;
File.read((char*)&HEADER,14);
File.read((char*)&INFOHEADER,40);
unsigned int i;
char ColorBytes[4];
char*PaletteData;
PaletteData=new char[256*3];
if(PaletteData)//if memory allocated successfully
{
for(i=0;i<256;i++)
{
File.read(ColorBytes,4);
PaletteData[(int)(i*3+2)]=ColorBytes[0]>>2;
PaletteData[(int)(i*3+1)]=ColorBytes[1]>>2;
PaletteData[(int)(i*3+0)]=ColorBytes[2]>>2;
}
outp(0x03c8,0);      //tell DAC that data is coming
for(i=0;i<256*3;i++) //send data to SVGA DAC
{
outp(0x03c9,PaletteData[i]);
}
delete[]PaletteData;
}
for(i=0;i<INFOHEADER.height;i++)
{
for(int j=0;j<INFOHEADER.width;j++)
{
File.read(&Ch,1); // Here Ch reads the color ofbitmap
putpixel(x+j,y+INFOHEADER.height-i-1,Ch); //puts the bmp at x,y
}
}
File.close();
}
int huge DetectSvga()
{
return 4;// 4 for 1024x768,2 for 640x480
}
void main()
{
int gdriver = DETECT,gmode;
installuserdriver("SVGA256",&DetectSvga);
initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); //path maybe diff. in your comp.
char a[8]="mm3.bmp";//this file should be placed in TC\BIN
loadbitmap(100,200,a);
getch();
}

some imp. things-
1.ALL the images should be first converted into 256 color bitmap using micosoft paint.
2.make sure that the image dimensions are divisible by 4...for eg-340x288 picture will work but a 342x289 picture wont work.
3.you need svga256.bgi to run this prog...i cant attach this file as the extension is nt supported...heres the link-
http://www.filefactory.com/file/ah8f7h6/n/svga256_bgi
...copy that file into TC\BGI
4.incase your computer is not able to run this prog. ,your comp. does not have correct drivers installed..
5.if u have any doubts,mail to - <snip>

skandh.

commented: N.A -3

Nothing is happening ... the code is worthless. and you should use cleardevice() instead of drawing a bar

Skand this correct verison is doing the job but half downward image is turns out to be hazy...where is the prob

commented: A one year bump of easily the worst thread I have ever seen on this website. -1

hey... what is DAC and '>>2' in d palette data reading.. plz tell me how the palette data is read and what is d port in d outp function(0x03c9)???????????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.