954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

256 color bmp in turbo c++ 3.0

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
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!!!!!!!!!

Attachments ab.bmp (75.45KB)
skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

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.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

skandh,
Your question should be - How to display 256 color bitmap using C++ program?
Use C99 - standard c++ compiler. CodeBlocks, DevC++, and Microsoft Visual C++ are free compilers.

Read more about C-99
http://www.open-std.org/jtc1/sc22/wg14/
http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html
https://developer.mozilla.org/en/C___Portability_Guide

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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);

???

skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

***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...

skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

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...

skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

I thought you wanted help with graphics, not cryptography.

Oh wait, it's not encrypted, it's drivel-speak
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

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.

rcr866
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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

Attachments bri1.bmp (122.16KB)
skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 
george15790
Newbie Poster
1 post since Sep 2009
Reputation Points: 4
Solved Threads: 0
 

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 -

skandh.

skandh
Newbie Poster
7 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

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

C_flat
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You