Posted by Ali Bhai on 1999-03-13
Bean is 100% right. You have to make the width a multiple of four while
reading the bmp image. On the other hand, while displaying the read image,
display it according to the width read from the header. Below is the
function I used in FAST Viewer, one of the projects I worked on. The
functions not defined but called here are general mode 13h functions with
only one difference. The plot_pixel (int x, int y, char color) functions
check for overflow conditions (0<=y<=199 and 0<=x<=319).
I had been facing the same problems (slanting images) when I was working on
FAST Viewer. I uploaded the source code to http://ali.someone.net. The site
has a tutorial and source code for bmp images. Click on C section to access
it. The project contains working source code for GIF87a and 256 color BMP
and PCX images. However, I have only checked it with Turbo C++ ver 3.0 for
DOS. Please ask if you don't understand anything. This is the only thing I
know =).
Ali Bhai
BCS III - FAST Institute of Computer Science, Karachi
email: mashah@xxxxx.xxx.xx
web page: www.pak.org/mufta
<----------------------------------------->
[To err is human but to really mess up the things you need a computer.]
/*--------------------------------------------------------------------------
-
FUNCTION: Shows a bmp file on screen
INPUT: Name of file to be shown
PROCESS: Opens up the file, interpret its header and shows it on the
screen. Uses routines defined in mode13h.cpp.
OUTPUT: Error code
---------------------------------------------------------------------------*
/
int show_bmp (char *name)
{
bmpHeaderType bmpHeader; // image information
FILE *dataFile; // file pointer
byte r, g, b; // red, green and blue for palette
int x, y; // coordinates of pixel to plot
unsigned int width; // width of the image
dataFile = fopen (name, "rb"); // open the file
if (dataFile == NULL) return 5; // could not open it? that's an error
read_bmp_header (dataFile, bmpHeader); // and check for errors
if (bmpHeader.nPlanes != 1) // unsupported bmp
{ fclose (dataFile); return 1; }
if (bmpHeader.bitsPerPixel != 8) // unsupported bmp
{ fclose (dataFile); return 2; }
if (bmpHeader.compression != 0) // unsupported bmp
{ fclose (dataFile); return 3; }
if (bmpHeader.magic1 != 'B' || bmpHeader.magic2 != 'M')
{fclose (dataFile); return 4;} // invalid bmp
fseek (dataFile, 54, SEEK_SET); // start reading the image
set_mcga(); // defined in mode13h.cpp
for (x=0; x<256; x++) // handle palette first
{
b = fgetc (dataFile); b = b>>2; // colors are stored as bgr
g = fgetc (dataFile); g = g>>2;
r = fgetc (dataFile); r = r>>2;
set_palette (x, r, g, b); // defined in mode13h.cpp
fgetc (dataFile); // spare next byte
}
width = bmpHeader.cols; // read width from the header
while (width % 4 != 0) width++; // make it a multiple of four
for (y=bmpHeader.rows; y>0; y--) // image is stored upside down
for (x=0; x<width; x++) // for whole width of image
put_pixel (x, y, fgetc(dataFile)); // read and plot
fclose (dataFile); // close the file
getch(); // wait for a key press
set_text(); // return to text mode: in mode13h.cpp
return 0; // reached here? there's no error
}
/*-------------------------------------------------------------------------*
/
Previous post | Next post | Timeline | Home