• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

256 Color Bitmap Reading

How do I read a 256 Color Bitmap quite fast onto the screen???
0
Professor
Asked:
Professor
  • 3
1 Solution
 
sumant032199Commented:
If you don't mind I can give you C code.
#include <alloc.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//h file
typedef unsigned char BYTE;
typedef unsigned int  WORD;
typedef long       DWORD;
typedef struct tagBITMAPFILEHEADER{
    WORD   bfType;
    DWORD  bfSize;
    WORD   bfReserv1;
    WORD   bfReserv2;
    DWORD  bfOffbits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
    DWORD   biSize;
    DWORD   biWidth;
    DWORD   biHeight;
    WORD    biPlanes;
    WORD    biBitCount;
    DWORD   biCompression;
    DWORD   biSizeImage;
    DWORD   biXPelPerMeter;
    DWORD   biYPelPerMeter;
    DWORD   biClrUsed;
    DWORD   biClrImpotant;
} BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
    BYTE    rgbBlue;
    BYTE    rgbGreen;
    BYTE    rgbRed;
    BYTE    rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPINFO{
    BITMAPINFOHEADER  bmiHeader;
    RGBQUAD           bmiColors[1];
} BITMAPINFO;
#define GALLOC(P,S)   if ((P = (unsigned char *)farmalloc(S))==NULL) error(0)
#define VALLOC(P,N,E) if ((P = (E *)calloc(N,sizeof(E)))==NULL) error(0)

/* constants for the biCompression field */
#define BI_RGB      0L
#define BI_RLE8     1L
#define BI_RLE4     2L
//c file
static RGBQUAD *rgbTable = NULL;
static BYTE *szFile = NULL;
static int nColor;
static BITMAPFILEHEADER bmHeader;
static BITMAPINFO bmInfo;
void main (int argc, char **argv);
int  ReadBitMap(char *name, int *nX, int *nY, int nType, int nYMax);
static int  GetColor256(RGBQUAD *rgbTable);
static BYTE Normal(BYTE color);
int  ReadBit(char *name);
void FreeMap(void);
static void Error_Graph(void);
void   error (int n);
/*=========================================================================*/
/*  Name       : main    */
/*  Descript   : test programme    */
/*  Parameters : name of bmp file    */
/*=========================================================================*/
void cdecl main (int argc, char **argv)
{
  int gdriver = VGA, gmode = VGAMED; //graphic initialization
  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
  /* read result of initialization */
  Error_Graph();
  cleardevice();
  if (argc >= 2)
    {
      int nX,nY;
      ReadBitMap(argv[1],&nX,&nX,0,480);
      getch();
    }
  closegraph();     /* clean up */
}  // end main
/*=========================================================================*/
/*  Name       : Error_Graph       */
/*  Descript   : If there are errors ==> exit    */
/*  Parameters : None    */
/*  Return     : None    */
/*  Call       : main    */
/*=========================================================================*/
void Error_Graph(void)
{
  int errorcode = graphresult();

  if (errorcode != grOk)   // an error occurred
   {                                    // message of error
     printf("graph error: %s\n", grapherrormsg(errorcode));
     printf("press any symbol\n");
     getch();
     exit(1); // terminate with an error code
   }
} // end Error_Graph
/*=========================================================================*/
/*  Name       : ReadBitMap    */
/*  Descript   : Read BMP file (without Compression) and Display in 1-st   */
/*        :  Page of Graphik. Memory    */
/*  Parameters : name of file, # of column, # of lines    */
/*             : nType = 0: disply pixels with y < MAXY    */
/*        :       = 1 display pixels only > MAXY (in begin of screen) */
/*  Return     : -1: no such file, 0: OK    */
/*=========================================================================*/
int ReadBitMap(char *name, int *nX, int *nY, int nType, int nYMax)
{
   int x, y,color, n, ind, ind1, i, ret = -1;
   long lLen;

   if (ReadBit(name) == 0) //OK?
     {
       ind = 0;
       for (y = 0; y < bmInfo.bmiHeader.biHeight; y++)
{
ind1 = 0;
for (x = 0; x < bmInfo.bmiHeader.biWidth; x++)
{
n = szFile[ind]; //type of BMP;
switch(nColor)
{
case 2:          //monochrom
color = szFile[ind] & (1<< (7 - ind1) );
if (color) color = 1;
ind1++;
if (ind1 >= 8)
{
ind1 = 0; ind++;
}
break;
case 16:          //16 colors
if (!ind1)
{
color = n/16; ind1 = 1;
}
else
{
color = n%16; ind++; ind1 = 0;
}
break;
case 256: //256 colors
color = n; ind++;
break;
default:  //error (can be 24 : without RGBcolors: not in use for Dos
ind++;
break;
}
if (!nType && bmInfo.bmiHeader.biHeight - y < nYMax)
putpixel(x, bmInfo.bmiHeader.biHeight - y,GetColor256(&rgbTable[color]) );
else if (nType && bmInfo.bmiHeader.biHeight - y >= nYMax)
putpixel(x, bmInfo.bmiHeader.biHeight - y - nYMax,GetColor256(&rgbTable[color]) );
}
if (ind1 || ind%4) //next?
{
ind /= 4; ind = (ind+1)*4;
}
}
       FreeMap(); //delete RGB table
       ret = 0;
       *nX = bmInfo.bmiHeader.biWidth;  //return values
       *nY = bmInfo.bmiHeader.biHeight;
     }
     return ret;
} // end ReadBitMap
/*=========================================================================*/
/*  Name       : GetColor256    */
/*  Descript   : Get colr from Predefined table by real color    */
/*  Parameters : Color of Pixel    */
/*  Return     : index of Borlandc color/0(Black): no such    */
/*=========================================================================*/
int GetColor256(RGBQUAD *rgbT)
{
  int
  color, i;
  static RGBQUAD rgbTest[16] =
    {
      {  0,   0,   0, 0},
      {127,   0,   0, 0},
      {  0, 127,   0, 0},
      {127, 127,   0, 0},
      {  0,   0, 127, 0},
      {127,   0, 127, 0},
      {  0, 127, 127, 0},
      {127, 127, 127, 0},
      { 63,  63,  63, 0},
      {255,   0,   0, 0},
      {  0, 255,   0, 0},
      {255, 255,   0, 0},
      {  0,   0, 255, 0},
      {255,   0, 255, 0},
      {  0, 255, 255, 0},
      {255, 255, 255, 0}
    };

  for (i = 0; i < 16; i++)
    if (rgbT->rgbBlue == rgbTest[i].rgbBlue  &&
rgbT->rgbGreen == rgbTest[i].rgbGreen && 
rgbT->rgbRed == rgbTest[i].rgbRed)
      return i;

  return 0;
}
/*=========================================================================*/
/*  Name       : Normal    */
/*  Descript   : From Windows color goto Dos color    */
/*  Parameters : Color(from 0 to 255)    */
/*  Return     : 0/127/255:  nearest    */
/*=========================================================================*/
BYTE Normal(BYTE color)
{
  if (color != 0 && color <= 63)
    color = 0;
  else if (color != 127 && color > 63 && color <= 192)
    color = 127;
  else if (color != 255 && color > 192)
    color = 255;

  return color;
}
/*=========================================================================*/
/*  Name       : ReadBit    */
/*  Descript   : Read BMP file (without Compression)    */
/*  Parameters : name of file    */
/*  Return     : -1: no such file, 0: OK    */
/*=========================================================================*/
int ReadBit(char *name)
{
   int fIn = open(name, O_RDONLY | O_BINARY);
   if(fIn==-1) error(1);
   int n,i, ret = -1;
   long lLen;

   rgbTable = NULL;
   szFile = NULL;

   if (fIn >= 0)
     {
       read(fIn, &bmHeader, sizeof(bmHeader));    //read Header
       if (bmHeader.bfType == 19778)        //"BM"
{
read(fIn, &bmInfo, sizeof(BITMAPINFOHEADER ));    //read Header
switch (bmInfo.bmiHeader.biBitCount)       //find N of colors
{
case  1:  nColor = 2;  break; //MONO
case  4:  nColor = 16; break; //16 colors
case  8:  nColor = 256;break; //256 colors
case 24:  nColor = 0;break; //2**24 colors
}
if (nColor)
{
VALLOC(rgbTable,nColor,RGBQUAD);
read(fIn, rgbTable, nColor*sizeof(RGBQUAD)); //read Color Table

for (n = 0; n < nColor; n++)
{
rgbTable[n].rgbBlue = Normal(rgbTable[n].rgbBlue);
rgbTable[n].rgbGreen = Normal(rgbTable[n].rgbGreen);
rgbTable[n].rgbRed = Normal(rgbTable[n].rgbRed);
}
}
lseek(fIn, bmHeader.bfOffbits ,  SEEK_SET); //read file
lLen = bmHeader.bfSize - bmHeader.bfOffbits;
GALLOC(szFile,lLen); //alloc place for Bits
n = lLen/30000l;
if (lLen % 30000l) n++;
for (i = 0; i < n; i++) //read bits
read(fIn, szFile+i*30000l,30000);
ret = 0;
}
       close(fIn);
     }
  return ret;
} // end ReadBitMap
/*=========================================================================*/
/*  Name       : reeMap    */
/*  Descript   : Free color Table    */
/*=========================================================================*/
void FreeMap(void)
{
  if (rgbTable)
    {
      free(rgbTable);
      rgbTable = NULL;
    }

  if (szFile)
    {
      farfree(szFile);
      szFile = NULL;
    }
}
/*=========================================================================*/
/* Description: error - Exits program in case of fatal error.              */
/* Parameter  : nEInd   - index of code of error message to display.    */
/* szText  - added test of error message    */
/* Return val : None.    */
/*=========================================================================*/
void error (int n)
{
   fcloseall ();
   closegraph(); //clear screen
   perror("ERROR");
   exit (n); //exit from utile
} // end error
0
 
amgedeldirdiriCommented:
you can use this code also for 256 color bitmap but you want to have (SVGA256.BGI) To Get It To Be OutPut On The Screen If You A Yahoo Messenger Login I Can Send The File (SVGA256.BGI) To You
Uses Crt,Dos,Graph;
Type
        ColorValue = Record
                  Rv,Gv,Bv: Byte;
                 End;
    PaletteType = Array [0..255] Of ColorValue;
        Palette = Record
                 Rv : Byte;
               Gv : Byte;
               Bv : Byte;
               Rr : Byte;
                   End;
        BitMapType = Record
               BfType      : Word;
                 BfSize      : LongInt;
                 BfReserved1 : Word;
               BfReserved2 : Word;
               BfOffSetBits: LongInt;
                 Size        : LongInt;
               Width       : LongInt;
               Height      : LongInt;
               Planes      : Word;
               BitCount    : Word;
               Comp        : LongInt;
               iSize        : LongInt;
               xpelspermeter: LongInt;
               ypelspermeter: LongInt;
               ColorsUsed   : LongInt;
               ColorsImportant:LongInt;
                 PalArray       :Array[0..255] of Palette;
                 End;

var
   Bit      : BitMapType;
   i,j,Gd,Gm: Integer;
   P        : PaletteType;
   Width,Hight : Word;

Procedure SetVGAPalette( Tp : PaletteType);
    Var
       Regs: Registers;
  Begin
    With Regs Do
      Begin
        AH:=$10;
        Al:=$12;
        BX:=0;
        CX:=256;
        ES:=Seg(tp);
        DX:=Ofs(tp);
      End;
    Intr($10,Regs);
  End;

Procedure Read_BmfHeader;
var
   fh : File Of BitMapType;
Begin
        Assign(fh,ParamStr(1)+'.bmp');
        Reset(fh);
        Read(fh,Bit);
        Close(fh);
        If(Bit.BfType<>19778) Then
        Begin
          Writeln('FORMAT IS NOT RECOGNIZED!');
          halt;
        End;
        Width:=Bit.Width;
        Hight:=Bit.Height;
        For i:= 0 To 255 Do
        Begin
          P[i].Rv:=Bit.PalArray[i].Bv Shr 2;
          P[i].Gv:=Bit.PalArray[i].Gv Shr 2;
          P[i].Bv:=Bit.PalArray[i].Rv Shr 2;
        End;
End;

Procedure DisplayBitMap;
var
     i,j : Integer;
     f : File Of Byte;
     b : Byte;
Begin
     Assign(f,ParamStr(1)+'.bmp');
     Reset(f);
     Seek(f,sizeof(BitMapType));
     For i :=1 To Hight Do
     For j :=1 To Width Do
       Begin
            Read(f,b);
            PutPixel(j,Hight-i,B);
       End;
End;

Procedure DefPal;
Begin
      SetVgaPalette(P);
End;

Begin
      Read_BmfHeader;
      Gd := InstallUserDriver ('Svga256',Nil);
      Gm := 2;
      InitGraph(Gd,Gm,'');
      DefPal;
      DisplayBitMap;
      Readln;
      CloseGraph;
End.

0
 
ProfessorAuthor Commented:
Actually I wanted a program in pascal code; but I think I can learn from your C- Code as well.

Thanx very much.
0
 
ProfessorAuthor Commented:
Actually I wanted a program in pascal code; but I think I can learn from your C- Code as well.

Thanx very much.
0
 
ProfessorAuthor Commented:
Can you send the File to Hf_Monk@yahoo.com please. I would be very pleased.

Thanx very much indeed.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now