织梦CMS - 轻松建站从此开始!

罗索

图像格式转换:RGB8转RGB24

落鹤生 发布于 2009-11-23 16:51 点击:次 
将8位位图转换为24位位图
TAG:

图像处理:将8位位图转换为24位位图

这些天也不知道干些什么好,就随便写点小程序。
第一个文件Bitmap.h的内容如下:

File Name:    Bitmap.h
Description:  definations/declarations of the structure Bitmap

#ifndef _BITMAP_H_
#define _BITMAP_H_

#include <stdio.h>

#define GET_B(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j ) )
#define GET_G(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j + 1 ) )
#define GET_R(bmp,i,j) ( *( bmp->ptr + bmp->line_width * i + j + 2 ) )
#define SET_B(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j ) = v )
#define SET_G(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j + 1 ) = v )
#define SET_R(bmp,i,j,v) ( *( bmp->ptr + bmp->line_width * i + j + 2 ) = v )
#define IS8BITS(bmp) ( ( bmp->ptr != NULL && bmp->bit_count == 8 )? 1 : 0 )
#define IS24BITS(bmp) ( ( bmp->ptr != NULL && bmp->bit_count == 24 ) ? 1 : 0 )
#define ISEMPTY(bmp) ( ( bmp->ptr == NULL ) ? 1 : 0 )
#define LIM255(v) ( ( v > 255 ) ? 255 : v )
#define LIMTO0(v) ( ( v < 0 ) ? 0 : v )

typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;

typedef struct tagBitmap
{
BYTE* ptr;
BYTE* palette;
DWORD width;
DWORD height;
WORD  bit_count;
DWORD line_width;
WORD pal_length;
}Bitmap;

typedef struct tagRGBQUAD
{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;    
}RGBQUAD;

//load a bitmap to the structure Bitmap
void load_bitmap( const char* filename, Bitmap *bmp );
//save the structure Bitmap to a file
void save_bitmap( const char* filename, Bitmap *bmp );
//release the memory which is taked by the structure Bitmap
void free_bitmap( Bitmap *bmp );

#endif

第二个文件Bitmap.c的内容如下:
/*
File Name:    Bitmap.c
Description:  implements of the structure Bitmap
Author:       Peng Jun(joypng#gmail.com)
Web Site:     http://pengjun.org.cn
*/
#include "Bitmap.h"

void load_bitmap( const char *filename, Bitmap *bmp )
{
FILE *fp = 0;
DWORD off_bits = 0, width = 0, height = 0, line_width = 0;
WORD bit_count = 0, pal_length = 0;

//open the file to read
if( ( fp = fopen( filename, "rb" ) ) == NULL )
{
printf("can not open the bitmap : %s\n", filename );
return;
}

//read the bfOffBits in the file header of the bitmap
fseek( fp, 10, 0 );
fread( &off_bits, 4, 1, fp );
//read the biWidth and biHeight in the info header of the bitmap
fseek( fp, 18, 0 );
fread( &width, 4, 1, fp );
fread( &height, 4, 1, fp );
//read the biBitCount in the info header of the bitmap
fseek( fp, 28, 0 );
fread( &bit_count, 2, 1, fp );

//read the color palette if the bitmap have a color palette
bmp->pal_length = off_bits - 54;
if( pal_length == 0 )
{
bmp->palette = 0;
}
else
{
bmp->palette = (BYTE*)malloc( pal_length * sizeof(BYTE) );
if( !bmp->palette ) return;
fseek( fp, 54, 0 );
fread( bmp->palette, pal_length * sizeof(BYTE), 1, fp );
}

//set the memeber of the structure Bitmap
line_width = ( width * bit_count + 31 ) / 32 * 4;
bmp->line_width = line_width;
bmp->width = width;
bmp->height = height;
bmp->bit_count = bit_count;
//allocate memory for the pixel of the bitmap
bmp->ptr = (BYTE*)malloc( line_width * height * sizeof(BYTE) );
if( !bmp->ptr )
{
printf("can not allocate memory for the bitmap.\n");
bmp->width = 0;
bmp->height = 0;
bmp->bit_count = 0;
return;
}
//read the pixel matrix from the bitmap
fseek( fp, off_bits, 0 );
fread( bmp->ptr, line_width * height, 1, fp );
//close the file
fclose( fp );
}

void save_bitmap( const char *filename, Bitmap *bmp )
{
FILE *fp = 0;
DWORD dw = 0;
WORD  w = 0;
//open the file for writing
if( ( fp = fopen( filename, "wb" ) ) == NULL )
{
printf("can not create the bitmap : %s\n", filename );
return;
}

//write the bfType
w = 19778;
fwrite( &w, 2, 1, fp );
//write the bfSize
dw = 14;
fwrite( &dw, 4, 1, fp );
//write the bfReserved1 and bfReserved2
w = 0;
fwrite( &w, 2, 1, fp );
fwrite( &w, 2, 1, fp );
//write the bfOffBits
if( bmp->bit_count == 8 )
dw = 1078;
else
dw = 54;

fwrite( &dw, 4, 1, fp );
//write the biSize
dw = 40;
fwrite( &dw, 4, 1, fp );
//write the biWidth
dw = bmp->width;
fwrite( &dw, 4, 1, fp );
//write the biHeight
dw = bmp->height;
fwrite( &dw, 4, 1, fp );
//write the biPlanes
w = 0;
fwrite( &w, 2, 1, fp );
//write the biBitCount
w = bmp->bit_count;
fwrite( &w, 2, 1, fp );
//write the biCompression
dw = 0;
fwrite( &dw, 4, 1, fp );
//write the biSizeImage
dw = bmp->height * bmp->line_width;
fwrite( &dw, 4, 1, fp );
//write the biXPelsPerMeter and the biYPelsPerMeter
dw = 0;
fwrite( &dw, 4, 1, fp );
fwrite( &dw, 4, 1, fp );
//write the biClrUsed
fwrite( &dw, 4, 1, fp );
//write the biClrImportant
fwrite( &dw, 4, 1, fp );

//write the color palette if the bitmap have
if( bmp->bit_count == 8 )
{
fwrite( bmp->palette, bmp->pal_length, 1, fp );
}

//write the pixel of the bitmap
dw = bmp->height * bmp->line_width;
fwrite( bmp->ptr, dw, 1, fp );
//close the file
fclose(fp);
}

void free_bitmap( Bitmap *bmp )
{
free( bmp->ptr );
bmp->ptr = 0;

free( bmp->palette );
bmp->palette = 0;
}

第三个文件8to24.c的内容如下:
/*
File Name:    8to24.c
Description:  convert a 8-bits bitmap to a 24-bits bitmap
Author:       Peng Jun(joypng#gmail.com)
Web Site:     http://pengjun.org.cn
*/
#include <stdio.h>
#include <stdlib.h>
#include "Bitmap.h"

int main(int argc, char *argv[])
{
Bitmap *bmp = (Bitmap*)malloc(sizeof(Bitmap));
DWORD i = 0, j = 0, n = 0;
BYTE r = 0, g = 0, b = 0;
double gg = 0.0;
BYTE *pixel = 0;
DWORD line_width = 0;

if( argc != 3 )
{
printf("Usage: 8to24 <img_src> <img_dst>\n");
free(bmp);
return -1;
}

load_bitmap( argv[1], bmp );

if( ISEMPTY( bmp ) || !IS8BITS( bmp ) )
{
free_bitmap( bmp );
free( bmp );
return -1;
}

line_width = ( bmp->width * 24 + 31 ) / 32 * 4;
pixel = (BYTE*)malloc( bmp->height * line_width * sizeof(BYTE) );
if( !pixel )
{
free_bitmap( bmp );
free( bmp );
return -1;
}

for( i = 0; i < bmp->height; i++ )
{
for( j = 0, n = 0; j < bmp->width; j++, n++ )
{
b = GET_B( bmp, i, j );

*( pixel + line_width * i + n ) = b;
n++;
*( pixel + line_width * i + n ) = b;
n++;
*( pixel + line_width * i + n ) = b;
}
}

bmp->line_width = line_width;
bmp->bit_count = 24;
free( bmp->ptr );
bmp->ptr = pixel;
bmp->pal_length = 0;
bmp->palette = 0;

save_bitmap( argv[2], bmp );

free_bitmap( bmp );
free( bmp );
free( pixel );

return 0;
}
 

(落鹤生)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200911/7984.html]
本文出处:未知 作者:落鹤生
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容