////题目:图像读取及输出//
////程序
//所需头文件#include <stdio.h>#include <stdlib.h>
////功能:读入bmp图像//输入:*image -图像像素存放矩阵// xsize -图像宽度// ysize -图像高度// *filename -图像文件名//输出:0 -正确执行// -1 -未能正确执行//int bmp_read(unsigned char *image, int xsize, int ysize, char *filename) { char fname_bmp[128]; sprintf(fname_bmp, "%s.bmp", filename); FILE *fp; if (!(fp = fopen(fname_bmp, "rb"))) return -1; unsigned char header[54]; fread(header, sizeof(unsigned char), 54, fp); fread(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp); fclose(fp); return 0;}
////功能:生成bmp文件//输入:*image -图像像素存放矩阵// xsize -图像宽度// ysize -图像高度// *filename -图像文件名//输出:0 -正确执行// -1 -未能正确执行//注:header为bmp的头部信息//int bmp_write(unsigned char *image, int xsize, int ysize, char *filename) { unsigned char header[54] = { 0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; long file_size = (long)xsize * (long)ysize * 3 + 54; header[2] = (unsigned char)(file_size &0x000000ff); header[3] = (file_size >> 8) & 0x000000ff; header[4] = (file_size >> 16) & 0x000000ff; header[5] = (file_size >> 24) & 0x000000ff; long width = xsize; header[18] = width & 0x000000ff; header[19] = (width >> 8) &0x000000ff; header[20] = (width >> 16) &0x000000ff; header[21] = (width >> 24) &0x000000ff; long height = ysize; header[22] = height &0x000000ff; header[23] = (height >> 8) &0x000000ff; header[24] = (height >> 16) &0x000000ff; header[25] = (height >> 24) &0x000000ff;
char fname_bmp[128]; sprintf(fname_bmp, "%s.bmp", filename); FILE *fp; if (!(fp = fopen(fname_bmp, "wb"))) return -1; fwrite(header, sizeof(unsigned char), 54, fp); fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp); fclose(fp); return 0;}
////主函数//输出:0 -正确执行// -1 -未能正确执行//注:实现5项功能并生成对应图像文件//int main(){ unsigned char *image; unsigned char *B,*G,*R,*Gray; int xsize = 512; int ysize = 512; int i,j; int r; int temp; char z;
image = (unsigned char *)malloc((size_t)xsize*ysize* 3); B=(unsigned char *)malloc((size_t)xsize*ysize); R=(unsigned char *)malloc((size_t)xsize*ysize); G=(unsigned char *)malloc((size_t)xsize*ysize); Gray=(unsigned char *)malloc((size_t)xsize*ysize);
if (image == NULL) return -1;
L1: bmp_read(image, xsize, ysize, "lena512"); printf("/n读入图像成功!您可以对图像进行如下操作:/n"); printf("/n1.拷贝 2.反色 3.变暗2倍 4.旋转180° 5.黑白/n"); printf("/n请输入你想要对图像进行的操作:/n"); scanf("%d",&r); switch(r) { case 1: bmp_write(image, xsize, ysize, "拷贝结果"); break; case 2: for(i=0;i<xsize*ysize*3;i++) image[i]=-image[i]; bmp_write(image, xsize, ysize, "反色结果"); break; case 3: for(i=0;i<xsize*ysize*3;i++) image[i]=image[i]/2; bmp_write(image, xsize, ysize, "变暗结果"); break; case 4: for(i=0;i<xsize*ysize/2;i++) {
temp=image[3*i]; image[3*i]=image[(3*xsize*ysize-3)-3*i]; image[(3*xsize*ysize-3)-3*i]=temp; temp=image[3*i+1]; image[3*i+1]=image[(3*xsize*ysize-3)-3*i+1]; image[(3*xsize*ysize-3)-3*i+1]=temp; temp=image[3*i+2]; image[3*i+2]=image[(3*xsize*ysize-3)-3*i+2]; image[(3*xsize*ysize-3)-3*i+2]=temp; }
bmp_write(image, xsize, ysize, "旋转180°结果"); break; case 5: for(i=0;i<xsize*ysize;i++) { B[i]=image[3*i]; G[i]=image[3*i+1]; R[i]=image[3*i+2]; Gray[i]=0.39*R[i]+0.5*G[i]+0.11*B[i]; if(Gray[i]>127) Gray[i]=255; else Gray[i]=0; image[3*i]=Gray[i]; image[3*i+1]=Gray[i]; image[3*i+2]=Gray[i]; } bmp_write(image, xsize, ysize, "黑白结果"); break; default: printf("/n请重新出入!/n"); goto L1; break;
} printf("/n是否继续?(y/n)/n"); fflush(stdin); scanf("%c",&z); if(z=='y') goto L1;
free(image); return 0;}