Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Verminaard

24/32 bit pcx/bitmaps

This topic is 6789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
all i know about is bmp, so dont ask me about pcx
first of all, each bmp contains header information about the actually image, and the image''s palette. Now, I could sit here and explain the whole thing to you, or i could give you some code to handle the job. Choice 2, for me
BTW, the following code will convert the 24 bit image (if it is one) into 16-bit, since you probably don''t want to make anything in 24-bit:

int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
// this function opens a bitmap file and loads the data into bitmap

int file_handle, // the file handle
index; // looping index

UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
OFSTRUCT file_data; // the file data information

// open the file if it exists
if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
return(0);

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));

// test if this is a bitmap file
if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
{
// close the file
_lclose(file_handle);

// return error
return(0);
} // end if

// now we know this is a bitmap, so read in all the sections

// first the bitmap infoheader

// now load the bitmap file header
_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));

// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8)
{
_lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));

// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
for (index=0; index < MAX_COLORS_PALETTE; index++)
{
// reverse the red and green fields
int temp_color = bitmap->palette[index].peRed;
bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
bitmap->palette[index].peBlue = temp_color;

// always set the flags word to this
bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
} // end for index

} // end if

// finally the image data itself
_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);

// now read in the image
if (bitmap->bitmapinfoheader.biBitCount==8 // bitmap->bitmapinfoheader.biBitCount==16)
{
// delete the last image if there was one
if (bitmap->buffer)
free(bitmap->buffer);

// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
{
// close the file
_lclose(file_handle);

// return error
return(0);
} // end if

// now read it in
_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);

} // end if
else
if (bitmap->bitmapinfoheader.biBitCount==24)
{
// allocate temporary buffer to load 24 bit image
if (!(temp_buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
{
// close the file
_lclose(file_handle);

// return error
return(0);
} // end if

// allocate final 16 bit storage buffer
if (!(bitmap->buffer=(UCHAR *)malloc(2*bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight)))
{
// close the file
_lclose(file_handle);

// release working buffer
free(temp_buffer);

// return error
return(0);
} // end if

// now read the file in
_lread(file_handle,temp_buffer,bitmap->bitmapinfoheader.biSizeImage);

// now convert each 24 bit RGB value into a 16 bit value
for (index=0; index < bitmap->bitmapinfoheader.biWidth*bitmap->bitmapinfoheader.biHeight; index++)
{
// build up 16 bit color word
USHORT color;

// build pixel based on format of directdraw surface
if (dd_pixel_format==DD_PIXEL_FORMAT555)
{
// extract RGB components (in BGR order), note the scaling
UCHAR blue = (temp_buffer[index*3 + 0] >> 3),
green = (temp_buffer[index*3 + 1] >> 3),
red = (temp_buffer[index*3 + 2] >> 3);
// use the 555 macro
color = _RGB16BIT555(red,green,blue);
} // end if 555
else
if (dd_pixel_format==DD_PIXEL_FORMAT565)
{
// extract RGB components (in BGR order), note the scaling
UCHAR blue = (temp_buffer[index*3 + 0] >> 3),
green = (temp_buffer[index*3 + 1] >> 3),
red = (temp_buffer[index*3 + 2] >> 3);

// use the 565 macro
color = _RGB16BIT565(red,green,blue);

} // end if 565

// write color to buffer
((USHORT *)bitmap->buffer)[index] = color;

} // end for index

// finally write out the correct number of bits
bitmap->bitmapinfoheader.biBitCount=16;

// release working buffer
free(temp_buffer);

} // end if 24 bit
else
{
// serious problem
return(0);

} // end else

#if 0
// write the file info out
printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",
filename,
bitmap->bitmapinfoheader.biSizeImage,
bitmap->bitmapinfoheader.biWidth,
bitmap->bitmapinfoheader.biHeight,
bitmap->bitmapinfoheader.biBitCount,
bitmap->bitmapinfoheader.biClrUsed,
bitmap->bitmapinfoheader.biClrImportant);
#endif

// close the file
_lclose(file_handle);

// flip the bitmap
Flip_Bitmap(bitmap->buffer,
bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8),
bitmap->bitmapinfoheader.biHeight);

// return success
return(1);

} // end Load_Bitmap_File




int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
{
// this function is used to flip bottom-up .BMP images

UCHAR *buffer; // used to perform the image processing
int index; // looping index

// allocate the temporary buffer
if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
return(0);

// copy image to work area
memcpy(buffer,image,bytes_per_line*height);

// flip vertically
for (index=0; index < height; index++)
memcpy(&image[((height-1) - index)*bytes_per_line],
&buffer[index*bytes_per_line], bytes_per_line);

// release the memory
free(buffer);

// return success
return(1);

} // end Flip_Bitmap



Im sorry if the margins are screwed up, since i don''t know the "code block" thingy. Just copy and paste.

Share this post


Link to post
Share on other sites
yes I''ve seen it done before... I don''t have code(never bothered with bmp... rather use bmp or my own format) for it but look around it is posable....

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!