如何依次读出位深为24的BMP图像的RGB值? 用VC。32位呢?

huguangdieying 2009-05-28 07:46:29
如题.最好给一小段程序。
...全文
442 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2009-06-04
  • 打赏
  • 举报
回复
huguangdieying 2009-05-29
  • 打赏
  • 举报
回复
FILE *fp;
LONG i,j,MapHeight,MapWidth,DataSizePerLine,DataSizePerLine1;

BITMAPINFOHEADER infoheader;
memset(&infoheader,0,sizeof(BITMAPINFOHEADER));


fp=fopen(fileName,"rb");


fseek(fp,14,SEEK_SET); //指针移动
fread(&infoheader,sizeof(BITMAPINFOHEADER),1,fp); //infoheader指向信息头
MapHeight=infoheader.biHeight;
MapWidth=infoheader.biWidth;



if(infoheader.biBitCount==24)
{
DataSizePerLine= (MapWidth* infoheader.biBitCount+31)/8; // 一个扫描行所占的字节数
DataSizePerLine= DataSizePerLine/4*4; // 字节数必须是4的倍数 每个扫描行所占的字节数

DataSizePerLine1=MapWidth* infoheader.biBitCount/8;


for(i=0;i<MapHeight;i++)
{
for(j=0;j<MapWidth;j++)
{
int b1,g1,r1;
b1=fgetc(fp);
g1=fgetc(fp);
r1=fgetc(fp);
}
if(DataSizePerLine1!=DataSizePerLine)
{for(long i=DataSizePerLine;i>DataSizePerLine1;i--) fgetc(fp);
}
}
} //if


fclose(fp);

并没有实现什么功能。各位帮忙看一下,这样读24位的BMP文件每个像素的RGB值对不对?
「已注销」 2009-05-28
  • 打赏
  • 举报
回复

bool zzTexture::LoadImage( CString & path )
{
FILE *file = NULL;

int cx,cy, panels, bitcount;

file = _tfopen( path, _T("rb") );

if ( file == NULL )
{
return false;
}

fseek(file, 18, SEEK_CUR);

// width
{
int c, c1, c2, c3;
c = getc( file );
c1 = getc( file );
c2 = getc( file );
c3 = getc( file );

cx = ((unsigned int) c) + (((unsigned int) c1) << 8) +
(((unsigned int) c2) << 16) + (((unsigned int) c3) << 24);

}

// height
{
int c, c1, c2, c3;
c = getc( file );
c1 = getc( file );
c2 = getc( file );
c3 = getc( file );

cy = ((unsigned int) c) + (((unsigned int) c1) << 8) +
(((unsigned int) c2) << 16) + (((unsigned int) c3) << 24);

}

// panels
{
int c, c1;

c = getc(file);
c1 = getc(file);

panels = ((unsigned int) c) + (((unsigned int) c1) << 8) ;

}

// bit counts
{
int c, c1;

c = getc(file);
c1 = getc(file);

bitcount = ((unsigned int) c) + (((unsigned int) c1) << 8) ;

}

fseek(file, 24, SEEK_CUR);

int size = cx * cy * bitcount / 8;

byte *lpBits = new byte[size];

fread( lpBits, size, 1, file );

// lpBits 指针 指向RGB值 信息 袄 不对应该是BGR格式读进来的你可以自己做个循环调换
glGenTextures(1, &m_ID);

GLboolean o = glIsTexture(
m_ID
);

glBindTexture( GL_TEXTURE_2D, m_ID);
glTexImage2D( GL_TEXTURE_2D,
0,
3,
cx,
cy,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
lpBits);


delete [] lpBits;
return true;
}




过去自己写的一段代码给你参考哈
  • 打赏
  • 举报
回复
楼主凑乎着看吧!~hoho一个截图的程序

ps:斑竹们,我错了,不应该直接给代码的……不过我贴的代码里只有一部分是楼主需要的!~
  • 打赏
  • 举报
回复
/**
* @file 21.c
* @author hoho
* @brief
* This file is used for getting the transversal picture on principle in the form of BMP.
*/

#include <stdio.h>
#include <stdlib.h>

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

#pragma pack (2)
struct SBmpHeader
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biComperssion;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}SSourceHeader,SDestHeader;
#pragma pack ()

/**
* @brief
*The function named judge_getborder is used for judging the area of number from command.
*If the area is beyond the mark,it will show an information and exit the program.
* @param argv0 the point of one of the start coordinate.
* @param argv1 the point of the width or height which needed.
* @param border the border of the picture.
*/
void judge_getborder(char *argv0,char *argv1,WORD border)
{
if((atoi(argv0) + atoi(argv1)) > border)
{
printf("The data you wanted is beyond the mark.\n");
exit(0);
}
}

/**
* @brief
*The function named judge_number is used for judging the validity of number from command.
*If the data is not numbers,it will show an information and exit the program.
* @param argv the point of the parameter.
*/
void judge_number(char *argv,WORD measure)
{
int i;

for(i = 0;i < strlen(argv);)
{
if((argv[i] >= 48) && (argv[i] <= 57))
{
i++;
}
else
{
printf("The x-axis y-axis width height must be the number between 0 and 9.\n");
exit(0);
}
}
if((atoi(argv) < 0) || (atoi(argv) > measure))
{
printf("The data must between 0 and %d.\n",measure);
exit(0);
}
}

/**
* @brief
*The function named judge_validity is used for judging the validity of number from command.
*Call the judge_number and judge_getborder functions to judge.
*If the number is of no effect,it will show an information and exit the program.
* @param argv4 the point of the forth parameter(the x-axis of the start point).
* @param argv5 the point of the fifth parameter(the y-axis of the start point).
* @param argv6 the point of the sixth parameter(the width of the destination).
* @param argv7 the point of the seventh parameter(the height of the destination).
* @param width the width of the source picture.
* @param height the height of the source picture.
*/
void judge_validity(char *argv4,char *argv5,char *argv6,char *argv7,unsigned long int width,unsigned long int height)
{
judge_number(argv4,width);
judge_number(argv5,height);
judge_number(argv6,width);
judge_number(argv7,height);
judge_getborder(argv4,argv6,width);
judge_getborder(argv5,argv7,height);
}

/**
* @brief
*The function named trans_bmp is used for getting the transversal picture on principle in the form of BMP.
*First,read the data of BMP.
*Second,call the function named judge_validity to judging the validity of number from command.
*Then,getting the transerval picture.
* @param argv2 the point of the second parameter(the source picture).
* @param argv3 the point of the third parameter(the destination picture).
* @param argv4 the point of the forth parameter(the x-axis of the start point).
* @param argv5 the point of the fifth parameter(the y-axis of the start point).
* @param argv6 the point of the sixth parameter(the width of the destination).
* @param argv7 the point of the seventh parameter(the height of the destination).
*/
void trans_bmp(char *argv2,char *argv3,char *argv4,char *argv5,char *argv6,char *argv7)
{
FILE *fps,*fpd;
BYTE *sbmpdata,*dbmpdata;
DWORD i,j,count,SourceDataPerLine,DestDataPerLine;
int x,y,width,height;

//if(((fps = fopen(argv2,"rb")) == NULL) || ((fpd = fopen(argv3,"wb")) == NULL))
if((fps = fopen(argv2,"rb")) == NULL)
{
printf("Can't open the file.\n");
exit(0);
}

fread(&SSourceHeader,sizeof(struct SBmpHeader),1,fps);

if((SSourceHeader.bfType != 0x4d42) || (SSourceHeader.biBitCount != 0x18))
{
printf("Error!The type of picture must be 24-Bitmap-File!\n");
exit(0);
}

judge_validity(argv4,argv5,argv6,argv7,SSourceHeader.biWidth,SSourceHeader.biHeight);

if((fpd = fopen(argv3,"wb")) == NULL)
{
printf("Can't open the file.\n");
exit(0);
}

/*
fseek(fps,0,SEEK_SET);
fread(&SDestHeader,sizeof(struct SBmpHeader),1,fps);
fseek(fps,SSourceHeader.bfOffBits,SEEK_SET);
*/
SDestHeader = SSourceHeader;

x = atoi(argv4);
y = atoi(argv5);
width = atoi(argv6);
height = atoi(argv7);
SourceDataPerLine = (SSourceHeader.biWidth * SSourceHeader.biBitCount + 31) / 32 * 4;
DestDataPerLine = (width * SDestHeader.biBitCount + 31) / 32 * 4;

sbmpdata = (BYTE *)malloc(SourceDataPerLine * SSourceHeader.biHeight);
dbmpdata = (BYTE *)malloc(DestDataPerLine * height);

fread(sbmpdata,(SourceDataPerLine * SSourceHeader.biHeight),1,fps);

count = 0;
for(i = 0;i < height;i++)
{
for(j = 0;j < DestDataPerLine;j++)
{
//dbmpdata[count] = sbmpdata[(y + i) * SourceDataPerLine + x * 3 + j]; //get the picture from the default starting point(bottom left corner).
dbmpdata[count] = sbmpdata[(SSourceHeader.biHeight - y - height + i) * SourceDataPerLine + x * 3 + j]; //get the picture from the conceived starting point(top left corner).
count++;
}
}

SDestHeader.biWidth = width;
SDestHeader.biHeight = height;
SDestHeader.biSizeImage = (height * DestDataPerLine);
SDestHeader.bfSize = sizeof(struct SBmpHeader) + SDestHeader.biSizeImage;

fwrite(&SDestHeader,sizeof(struct SBmpHeader),1,fpd);
fwrite(dbmpdata,sizeof(BYTE),SDestHeader.biSizeImage,fpd);
fclose(fpd);
fclose(fps);
}

/**
* @brief
*The function named judge_paramnum is used for judging the number of parameter.
*If the number is less than 7,it will give an information and exit the programme.
* @param argc the number of parameters.
*/
void judge_paramnum(int argc)
{
if(argc < 7)
{
printf("Input Error!Usage:source.bmp des.bmp x-axis y-axis width height\n");
exit(0);
}
}

int main(int argc, char *argv[])
{
judge_paramnum(argc);
trans_bmp(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]);
return 0;
}
  • 打赏
  • 举报
回复
图片有具体的格式的,hoho

以前截取过24位图的信息,找找代码啊!~
blingpro 2009-05-28
  • 打赏
  • 举报
回复
定义BMP位图结构

#pragma pack(1)
typedef struct tagBITMAPFILEHEADER
{
char bfType[2]; // 位图文件的类型,必须为BM
unsigned long int bfSize; // 位图文件的大小,以字节为单位
unsigned short bfReserved1; // 位图文件保留字,必须为0
unsigned short bfReserved2;
unsigned long int bfOffBits; // 位图数据的起始位置,以相对于位图// 文件头的偏移量表示,以字节为单位
} BITMAPFILEHEADER;


typedef struct tagBITMAPINFOHEADER
{
unsigned long int biSize;
unsigned long int biWidth;
unsigned long int biHeight;
unsigned short int biPlanes;
unsigned short int biBitCount;
unsigned long int biCompression;
unsigned long int biSizelmage;
unsigned long int biXPelsPerMeter;
unsigned long int biYPelsPerMeter;
unsigned long int biClrUsed;
unsigned long int biClrlmportant;

}BITMAPINFOHEADER;

typedef struct tagRGBQUAD
{
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char rgbReserved;

}RGBQUAD;


typedef struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColor[1];

}BITMAPINFO;


typedef struct tagBMP
{
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFO bmpInfo;
}BMP;

#pragma pack()


读取文件头结构

void CBmpData::storeHeadFromFile(string strFileName)
{

ifstream in;
locale::global(locale(""));
in.open(strFileName.c_str(),ios_base::binary);
if(!in)
{
cout<<"打开文件失败"<<endl;
return;
}
locale::global(locale("C"));

BMP *bmp = new BMP;

in.read(bmp->bmpFileHeader.bfType,2);
in.read((char*)&(bmp->bmpFileHeader.bfSize),4);
in.read((char*)&(bmp->bmpFileHeader.bfReserved1),2);
in.read((char*)&(bmp->bmpFileHeader.bfReserved2),2);
in.read((char*)&(bmp->bmpFileHeader.bfOffBits),4);

in.read((char*)&(bmp->bmpInfo.bmiHeader.biSize),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biWidth),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biHeight),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biPlanes),2);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biBitCount),2);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biCompression),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biSizelmage),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biXPelsPerMeter),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biYPelsPerMeter),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biClrUsed),4);
in.read((char*)&(bmp->bmpInfo.bmiHeader.biClrlmportant),4);

if(bmp->bmpInfo.bmiHeader.biBitCount != 24)
{
in.read((char*)&(bmp->bmpInfo.bmiColor[0].rgbBlue),1);
in.read((char*)&(bmp->bmpInfo.bmiColor[0].rgbGreen),1);
in.read((char*)&(bmp->bmpInfo.bmiColor[0].rgbRed),1);
in.read((char*)&(bmp->bmpInfo.bmiColor[0].rgbReserved),1);
}

mHead = (void*)bmp;
mHeadLength = bmp->bmpFileHeader.bfOffBits;
width = bmp->bmpInfo.bmiHeader.biWidth;
height = bmp->bmpInfo.bmiHeader.biHeight;

in.close();

}



//偏移一下文件头的位置,然后读入数据



in.seekg(mHeadLength); //偏移

mRed = new int*[height];
mGreen = new int*[height];
mBlue = new int*[height];

for(int i = 0;i<height;i++)
{
mRed[i] = new int[width];
mGreen[i] = new int[width];
mBlue[i] = new int[width];
}

for(int i = height-1;i>=0;i--)
{
for(int j = 0;j<width;j++)
{
unsigned char ci[3];
in.read((char*)&ci,3);

mRed[i][j] = (int)ci[2];
mGreen[i][j] = (int)ci[1];
mBlue[i][j] = (int)ci[0];
}
}



供参考,gl
lingyin55 2009-05-28
  • 打赏
  • 举报
回复
ltc_mouse 2009-05-28
  • 打赏
  • 举报
回复
BMP文件的格式不复杂。网络上有很多现成的,google下吧~~
pathuang68 2009-05-28
  • 打赏
  • 举报
回复
网上搜搜应该有很多的。
我本来要写一片类似的博文的,里面画了一些图,解释得应该比较清楚,可惜这几天根本没有办法发待图片的博文...
CSDN咋就这么...
内容概要:本文聚焦于“通过ADMM进行TV-L1去噪”的研究,系统阐述了基于交替方向乘子法(ADMM)实现总变差(Total Variation, TV)正则化与L1范数稀疏约束相结合的图像去噪模型。文中详细解析了TV-L1模型的数学构建及其在抑制椒盐噪声、保持图像边缘结构方面的优越性,重点介绍了ADMM算法如何将复杂的凸优化问题分解为多个可高效求解的子问题,提升收敛效率与数稳定性。配套提供的Matlab代码实现了完整的去噪流程,便于读者复现算法并开展实验验证。此外,文档还整合了电力系统、信号处理、路径规划、机器学习等多个领域的科研资源,凸显其作为综合性学术资料包的价。; 适合人群:具备良好数学基础与Matlab编程能力,从事图像处理、信号去噪、优化算法或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 深入理解并复现基于ADMM的TV-L1图像去噪算法;② 掌握总变差正则化与L1范数在稀疏噪声去除中的理论与应用;③ 利用所提供的Matlab代码进行算法调试、性能评估与二次开发;④ 借助附带的多领域科研案例拓展研究思路,推动跨学科技术创新。; 阅读建议:建议读者结合理论推导与Matlab代码实践,逐步跟踪ADMM的迭代过程,观察其收敛行为与去噪效果,同时可参考文档末尾提供的丰富科研资源链接,拓展技术视野与研究深度。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧