|
-
Nov 1st, 2001, 08:18 AM
#1
Thread Starter
New Member
Opening 256-color bitmaps in C/C++
I wish to open a 256 color bitmap designed at painprush through a C program.
I have got a problem in opening the image, when the image is displayed the background of the image is not white.
I am attaching the source code with this thread and the header file used by my program given below.
#include <dos.h>
#include <dir.h>
#include <stdlib.h>
#include <alloc.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#define VGALOW 0x101
#define VGAMED 0x110
#define VGAHI 0x111
#define VGA16M 0x112
#define VGASUP 0x103
#define SVGALO 0x113
#define SVGAME 0x114
#define SVGAHI 0x105
#define SVGASU 0x107
typedef unsigned int UINT;
typedef unsigned char UCHAR;
struct VgaInfoBlock
{
char signature[4];
short version;
char far* oemname;
long capabilities;
unsigned far *modes;
char buffer[238];
};
typedef struct
{
char red;
char green;
char blue;
}RGB;
struct VgaModeInfoBlock
{
UINT ModeAttributes;
UCHAR WinAAttributes;
UCHAR WinBAttributes;
UINT WindowGranularity;
UINT WinSize;
UINT WinASegment;
UINT WinBSegment;
void (far*WinFuncPtr)(void);
UINT BytesPerScanLine;
UINT XResolution;
UINT YResolution;
UCHAR XCharSize;
UCHAR YCharSize;
UCHAR NumberOfPlanes;
UCHAR BitsPerPixel;
UCHAR NumberOfBanks;
UCHAR MemoryModel;
UCHAR BankSize;
UCHAR NumberOfImagePages;
UCHAR Reserved1;
UCHAR RedMaskSize;
UCHAR RedMaskPosition;
UCHAR GreenMaskSize;
UCHAR GreenMaskPosition;
UCHAR BlueMaskSize;
UCHAR BlueMaskPosition;
UCHAR ReservedMaskSize;
UCHAR ReservedMaskPosition;
UCHAR DirectScreenModeInfo;
UCHAR Reserved2[216];
};
typedef enum
{
memPL = 3,
memPK = 4,
memRGB=6,
memYUV=7
};
typedef struct tagBMPHEADER
{
UCHAR bftype[2];
unsigned long bfsize;
UINT bfres1,bfres2;
unsigned long bfoffbits;
unsigned long bisize,biwidth,biheight;
UINT biplanes,bibitcount;
unsigned long bicompression,bisizeimage,bixpelspermeter,biypelspermeter;
unsigned long biclrused,biclrimportant;
}BMPHEADER;
typedef struct tagRGBQUAD
{
UCHAR blue,green,red,rgbreserved;
}RGBQUAD;
typedef struct tagBMPINFO
{
BMPHEADER bmiheader;
RGBQUAD bmicolors[256];
}BMPINFO;
#ifdef MAIN
#define DECLARE
#else
#define DECLARE extern
#endif
int maxx,maxy;
int xres,yres;
int bytesperline;
int curbank;
unsigned int bankshift;
int oldmode;
char far* screenptr;
void(far* bankswitch)(void);
struct VgaModeInfoBlock modeinfo;
int pcolor,xp,yp;
int ccolor;
DECLARE char IsValidBitmap(char*);
DECLARE int showbmp(char *infname,int xs,int ys);
DECLARE void vinitgraph(int);
DECLARE void vclosegraph(void);
DECLARE void setwidth(int);
DECLARE void setvesamode(int);
DECLARE int getvesamode(void);
DECLARE int GetSvgaInfo(struct VgaInfoBlock far*);
DECLARE int GetSvgaModeInfo(int,struct VgaModeInfoBlock far*);
DECLARE void SetPalette(RGB Pal[256]);
#include "img_head.h"
char IsValidBitmap(char *fname)
{
BMPINFO bmpinfo;
FILE *fp;
fp=fopen(fname,"rb");
if(fp==NULL)
{
printf("Cannot open the mentioned filename\n");
return 0;
}
fread(&bmpinfo,sizeof(bmpinfo),1,fp);
fclose(fp);
if(!(bmpinfo.bmiheader.bftype[0]=='B'&&bmpinfo.bmiheader.bftype[1]=='M'))
{
printf("Not a valid bitmap\n");
return 0;
}
if(!(bmpinfo.bmiheader.bicompression==0))
{
printf("File is not RLE encoded\n");
return 0;
}
if(!(bmpinfo.bmiheader.bibitcount==8))
{
printf("Cannot read more than 8 bits per color\n");
return 0;
}
return 1;
}
int getvesamode(void)
{
union REGS in,out;
in.x.ax=0x4f03;
int86(0x10,&in,&out);
return out.x.bx;
}
int GetSvgaModeInfo(int mode,struct VgaModeInfoBlock far* buffer)
{
struct REGPACK reg;
reg.r_ax = 0x4f01;
reg.r_es = FP_SEG(buffer);
reg.r_di = FP_OFF(buffer);
reg.r_cx = mode;
intr(0x10,®);
if(reg.r_ax != 0x004f)
return 1;
else
return 0;
}
void setvesamode(int mode)
{
struct REGPACK reg;
oldmode=getvesamode();
reg.r_ax = 0x4f02;
reg.r_bx = mode;
intr(0x10,®);
GetSvgaModeInfo(getvesamode(),&modeinfo);
xres = modeinfo.XResolution;
yres = modeinfo.YResolution;
maxx = xres;
bytesperline = modeinfo.BytesPerScanLine;
bankshift = 0;
while((unsigned)(64>>bankshift)!=modeinfo.WindowGranularity)
bankshift++;
bankswitch = modeinfo.WinFuncPtr;
curbank = 1;
screenptr = (char far*)(((long)0xa000)<<16|0);
return;
}
void setwidth(int width)
{
union REGS in,out;
in.x.ax = 0x4f06;
in.x.bx = 0x0000;
in.x.cx = width;
int86(0x10,&in,&out);
bytesperline=(int)out.x.bx;
maxx=(int)out.x.cx;
maxy=(int)out.x.dx;
return;
}
void SetPalette(RGB Pal[256])
{
union REGS reg;
struct SREGS inreg;
reg.x.ax = 0x1012;
segread(&inreg);
inreg.es = inreg.ds;
reg.x.bx = 0;
reg.x.cx = 256;
reg.x.dx = (int)&Pal[0];
int86x(0x10,®,®,&inreg);
return;
}
void vinitgraph(int mode)
{
setvesamode(mode);
setwidth(xres);
}
void vclosegraph(void)
{
vinitgraph(oldmode);
maxx=xres;
return;
}
int showbmp(char *infname,int xs,int ys)
{
BMPINFO bmpinfo;
RGB pal[256];
FILE *fpt;
int i,j,w,h,c,bank;
unsigned char byte[1056];
unsigned int k;
long addr;
long cl;
fpt = fopen(infname,"rb");
fread(&bmpinfo,sizeof(bmpinfo),1,fpt);
fseek(fpt,bmpinfo.bmiheader.bfoffbits,SEEK_SET);
w=bmpinfo.bmiheader.biwidth;
h=bmpinfo.bmiheader.biheight;
for(i=0;i<255;i++)
{
pal[i].red = bmpinfo.bmicolors[i].red/4;
pal[i].green = bmpinfo.bmicolors[i].green/4;
pal[i].blue = bmpinfo.bmicolors[i].blue/4;
}
vinitgraph(VGALOW);
setwidth(1000);
SetPalette(pal);
for(i=0;i<h;i++)
{
fread(&byte[0],sizeof(unsigned char),w,fpt);
for(j=0;j<w;j++)
{
c = (int)byte[j];
addr = (long)(ys+h-i)*bytesperline+xs+j;
bank = (int)(addr >> 16);
if(curbank!=bank)
{
curbank=bank;
bank <<= bankshift;
asm mov bx,0;
asm mov dx,bank;
bankswitch();
asm mov bx,1;
bankswitch();
}
*(screenptr+(addr & 0xffff)) = (char)c;
}
}
fclose(fpt);
getch();
vclosegraph();
return (0);
}
int ColorToGrey(char *infname,int xs,int ys)
{
BMPINFO bmpinfo;
FILE *fp1,*fp2;
char fname[13];
unsigned char r,g,b,byte[1056],pal[256];
double grey;
int i,j,h,w,pcnt=0;
long size,curpos;
strcpy(fname,infname);
fp1=fopen(fname,"rb");
fp2=fopen("grey.bmp","wb");
printf("Preparing target\n");
fseek(fp1,0,SEEK_END);
size=ftell(fp1)+256;
fseek(fp1,0,SEEK_SET);
fread(&bmpinfo,sizeof(bmpinfo),1,fp1);
curpos=ftell(fp1);
pcnt=(int)ceil((float)curpos*100.0/(float)size);
gotoxy(25,1);printf("%d% completed\n",pcnt);
for(i=0;i<=255;i++)
{
r=bmpinfo.bmicolors[i].red;
g=bmpinfo.bmicolors[i].green;
b=bmpinfo.bmicolors[i].blue;
grey=(double)0.3*(double)r+(double)0.59*(double)g+(double)0.11*(double)b;
if(grey-(int)grey>=0.5)
grey++;
if(grey>255) grey=255;
pal[i]=(unsigned char)grey;
bmpinfo.bmicolors[i].red=(unsigned char)i;
bmpinfo.bmicolors[i].green=(unsigned char)i;
bmpinfo.bmicolors[i].blue=(unsigned char)i;
curpos++;
}
bmpinfo.bmiheader.biclrused=0;
i=bmpinfo.bmiheader.bfoffbits;
bmpinfo.bmiheader.bfoffbits=1078;
fwrite(&bmpinfo,sizeof(bmpinfo),1,fp2);
fseek(fp1,i,SEEK_SET);
fseek(fp2,bmpinfo.bmiheader.bfoffbits,SEEK_SET);
w=bmpinfo.bmiheader.biwidth;
h=bmpinfo.bmiheader.biheight;
curpos=ftell(fp1)+256;
for(i=0;i<h;i++)
{
fread(&byte[0],sizeof(unsigned char),w,fp1);
for(j=0;j<w;j++)
byte[j]=pal[byte[j]];
fwrite(&byte[0],sizeof(unsigned char),w,fp2);
curpos+=w;
pcnt=(int)ceil((float)curpos*100.0/(float)size);
gotoxy(25,1);printf("%d% completed\n",pcnt);
}
fclose(fp1);
fclose(fp2);
showbmp("grey.bmp",xs,ys);
return 0;
}
void main()
{
int x;
clrscr();
x=IsValidBitmap("f:\\kas\\sample5.bmp");
if(x==0)
{
printf("Sorry");
}
else
{
showbmp("f:\\kas\\sample5.bmp",0,0);
ColorToGrey("f:\\kas\\sample5.bmp",0,0);
}
}
Last edited by kansy; Nov 1st, 2001 at 08:23 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|