65,211
社区成员
发帖
与我相关
我的任务
分享
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;
}
/**
* @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;
}
#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];
}
}