用计算机的随机数做成的图像(灰度和彩色

/*随机图像显示*/
//生成的图片保存在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);  
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值