| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:bmp文件读取
只看楼主 加入收藏
hulipingo
Rank: 1
等 级:新手上路
帖 子:27
专家分:1
注 册:2010-12-13
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:6 
bmp文件读取
bmp的调色板怎么用,putpixel输出,思路
搜索更多相关主题的帖子: 调色板 
2011-12-15 21:01
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:13 
啥意思??顺便接分
2011-12-16 12:38
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:13 
守候

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-12-16 14:22
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:0 
叫我干嘛呢 想我了?
2011-12-16 15:14
silent_world
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:1
帖 子:258
专家分:1138
注 册:2011-9-24
收藏
得分:13 
bmp调色板是在图像大小较小时,对颜色位的扩充。最常用的是bmp8以下的图片。
按照图像每个数据,0——255,分别对应调色板中的一个颜色位,主要是灰度,即R = G = B。
能不能具体描述你碰到的问题,以便一起讨论?
2011-12-16 16:35
南都布衣
Rank: 2
等 级:论坛游民
帖 子:2
专家分:14
注 册:2011-12-16
收藏
得分:13 

#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;

/*下面的就是调色板的每一项,包括红、绿、蓝的值,和一个保留位。为了保险起见,你可以建立一个256个颜色的调色板,每个调色板的颜色的r、g、b都是一致的,比如:
for(i=0;i<256;i++)
{
pal[i].rgbBlue = (BYTE)i;
pal[i].rgbGreen = (BYTE)i;
pal[i].rgbRed = (BYTE)i;
pal[i].rgbReserved = (BYTE)0;
}
*/
/*
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
--------------------------------------------------------------------------------------------

#include "Bitmap.h"

#include <stdio.h>
#include <stdlib.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 = (unsigned short)(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;
}
2011-12-16 16:42
hulipingo
Rank: 1
等 级:新手上路
帖 子:27
专家分:1
注 册:2010-12-13
收藏
得分:0 
有指向调色板的结构体指针吗?怎么定义的,其实我就是把bmp图片读成了二进制数据,再怎么用putpixel打出每个点~?
2011-12-16 23:36
快速回复:bmp文件读取
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018594 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved