bmp_v1/test/main.cpp

76 lines
1.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "image.h"
#include <cstdio>
#include <cstdlib>
int main(int argc, char* argv[])
{
Image img; //创建对象
//读写BMP文件
img.ReadBMP("hello.bmp");
img.WriteBMP("FruitsCopy.bmp");
img.WriteText("FruitsCopy.text");
//读写text文件
Image readtxtImage;
readtxtImage.ReadText("FruitsCopy.text");
readtxtImage.WriteText("writeFruits.text");
//图像的上下翻转,并保存结果图像文件
img.Flip(true);
//图像的左右翻转,如img.Flip(true);并保存结果图像文件
img.Flip(false);
//左右翻转需要注意每个像数占3个字节翻转时不能简单直接翻转
//而需要每个字节对应地翻转
//图像的缩放,并保存结果图像文件
img.Resize(false);//图像缩小
img.Resize(true);//图像放大
//缩小时也需要注意每个像数占3个字节不能简单通过删除字节而缩小
//而需要每个字节对应地删除
//放大也是一样的道理
//获取图像的某点的像素值,并修改, 并保存结果图像文件
int row = 100;
int col = 100;
img.At(row, col);
img.Set(row, col, 10);
img.Set(100);
//使用拷贝构造函数创建新的对象
Image new_img(img);
new_img.WriteBMP("new_img.bmp");
//截取指定区域内的图像,并保存结果图像文件(x1,y1) (x2,y2)
new_img.Cut(120,120,360,360);
//需要保证 x2 > x1 && y2 > y1
//顺时针旋转图像并保存结果图像文件简单起见旋转角度为90度的整数倍
img.Rotate(90);
img.Rotate(180);
img.Rotate(270);
//求图像的均值和方差,并在命令行输出
float ave = 0;
float vAve = 0;
img.Mean_Variance(ave, vAve);
printf("平均值为:%f\n方差为:%f\n", ave, vAve);
//交换两个图像的数据
Image img1("Baboon.bmp");
Image img2("Lena.bmp");
Swap(img1, img2);
//保存交换完的结果图像文件
img1.WriteBMP("S_img1_baboon.bmp");
img2.WriteBMP("S_img2_lena.bmp");
return 0;
}