/*随机图像显示*/
//生成的图片保存在C盘根目录下 分别为randgray.bmp和randcor.bmp;
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
//#include "liujia2.cpp"
typedef unsigned long DWORD;
typedef int BOOL;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef float FLOAT;
typedef unsigned char byte;
struct BMP_img
{
WORD bfType;
DWORD size;
DWORD reser;
DWORD header_length;
DWORD infoheader_length;
DWORD width;
DWORD height;
WORD biplanes;
WORD bmp_type; /* 8bit 24bit; */
DWORD compres;
DWORD datasize;
DWORD bixpm;
DWORD biypm;
DWORD clrused;
DWORD relclrused;
BYTE *image;
byte *header_info;
};
struct RGB
{
byte bitb;
byte bitg;
byte bitr;
byte re;
};
void writebmp(const char *path,byte *temp,struct BMP_img img)
{
FILE *infile;
DWORD lineBytes=(img.width*24+31)/32*4;
if((infile=fopen(path,"wb"))==NULL)
{
printf("\n不能创建%s图\n",path);
exit(-1);
}
fwrite(&img.bfType,sizeof(WORD),1,infile);
img.size=54+img.height*lineBytes;
fwrite(&img.size,sizeof(DWORD),1,infile);
img.reser=0;
fwrite(&img.reser,sizeof(DWORD),1,infile);
img.header_length=54;
fwrite(&img.header_length,sizeof(DWORD),1,infile);
img.infoheader_length=40;
fwrite(&img.infoheader_length,sizeof(DWORD),1,infile);
fwrite(&img.width, sizeof(DWORD), 1, infile);
fwrite(&img.height, sizeof(DWORD), 1, infile);
img.biplanes=0;
fwrite(&img.biplanes, sizeof(WORD), 1, infile);
img.bmp_type=24;
fwrite(&img.bmp_type, sizeof(WORD), 1, infile);
img.compres=0;
fwrite(&img.compres, sizeof(DWORD), 1, infile);
img.datasize=img.height*lineBytes;
fwrite(&img.datasize, sizeof(DWORD), 1, infile);
img.bixpm=0;
fwrite(&img.bixpm, sizeof(DWORD), 1, infile);
img.biypm=0;
fwrite(&img.biypm, sizeof(DWORD), 1, infile);
img.clrused=0;
fwrite(&img.clrused, sizeof(DWORD), 1, infile);
img.relclrused=0;
fwrite(&img.relclrused, sizeof(DWORD), 1, infile);
fseek(infile,54*sizeof(byte),SEEK_SET);
if(fwrite(temp,img.height*lineBytes*sizeof(BYTE),1,infile)!=1)
{
printf("不能写入%s图\n",path);
fclose(infile);
}
fclose(infile);
}
void randomgray(struct BMP_img img,DWORD width,DWORD height) //灰度随机
{
DWORD lineBytes=(width*24+31)/32*4;
int i,j;
byte *temp1=(byte *)malloc(sizeof(byte)*height*lineBytes);
byte temp;
img.height=height;
img.width=width;
srand( (unsigned)time( NULL ) );
for( i = 0; i<height;i++ )
{
for(j=0;j<width*3;j++)
{
temp=rand()%256;
*(temp1+i*lineBytes+j)=(byte)temp;
j++;
*(temp1+i*lineBytes+j)=(byte)temp;
j++;
*(temp1+i*lineBytes+j)=(byte)temp;
printf("%5d",temp);
if(j/10)
printf("\n");
}
}
writebmp("c:\\randgray.bmp",temp1,img);
}
void randomcolor(struct BMP_img img,DWORD width,DWORD height) //彩色随机
{
DWORD lineBytes=(width*24+31)/32*4;
int i,j;
byte *temp1=(byte *)malloc(sizeof(byte)*height*lineBytes);
byte b,g,r;
img.height=height;
img.width=width;
srand( (unsigned)time( NULL ) );
for( i = 0; i<height;i++ )
{
for(j=0;j<width*3;j++)
{
b=rand()%256;
*(temp1+i*lineBytes+j)=(byte)b;
j++;
g=rand()%256;
*(temp1+i*lineBytes+j)=(byte)g;
j++;
r=rand()%256;
*(temp1+i*lineBytes+j)=(byte)r;
printf("%d\t",b);
printf("%d\t",g);
printf("%d\t",r);
printf("\n");
}
}
writebmp("c:\\randcor.bmp",temp1,img);
}
void main()
{
BMP_img img1;
printf("\n随机灰度\n");
randomgray(img1,200,200);
printf("\n随机彩色\n");
randomcolor(img1,200,200);
}