关于图片倒置的代码求注释,请写的详细点,谢谢了

#include <stdio.h>
#include <windows.h> //bmp
#include <iostream>
using namespace std;
#define BYTE unsigned char
int m_Width;
int m_Height;
int i; int j;
int a1; int a2;
int b1; int b2;
BITMAPFILEHEADER hf;
BITMAPINFOHEADER hInfo;
void SetPixel(BYTE *lpImg, int i, int j, BYTE r, BYTE g, BYTE b);
void Raw2BMP(BYTE *lpImg);
void MakeBMPFile(char *FileName, BYTE *OutImage);
void main()
m_Width = 60;
m_Height = 60;
BYTE *lpImg = new BYTE[m_Width * m_Height*3];
memset(lpImg, 0, m_Width*m_Height*3);
// pixel drawing in(i,j)=>(r,g,b)
BYTE r=255;
BYTE g=255;
BYTE b=255;
SetPixel(lpImg, i, j, r, g, b); //lpImp,10,100,123,200,40
Raw2BMP (lpImg);
// save to bmp
// BMP header
hInfo.biSize=40;
hInfo.biWidth=m_Width;
hInfo.biHeight=m_Height*-1;
hInfo.biPlanes=1;
hInfo.biBitCount=24;
hInfo.biCompression=0;
hInfo.biSizeImage=m_Width*m_Height*3;
hInfo.biXPelsPerMeter=0;
hInfo.biYPelsPerMeter=0;
hInfo.biClrUsed=0;
hInfo.biClrImportant=0;
hf.bfType=19778;
hf.bfOffBits=54;
hf.bfReserved1=0;
hf.bfReserved2=0;
hf.bfSize=hInfo.biSizeImage+hf.bfOffBits;
MakeBMPFile ("BMP.bmp",lpImg);
void SetPixel (BYTE *lpImg, int i, int j, BYTE r, BYTE g, BYTE b)//lpImp,10,100,123,200,40
cout << "X(0-60)" << endl;
cin >> b1;
cout << "X(0-60)" << endl;
cin >> b2;
cout << "Y(0-60)" << endl;
cin >> a1;
cout << "Y(0-60)" << endl;
cin >> a2;
for ( i = a1; i < a2; i++){
for( j = b1; j < b2; j++){
*(lpImg+m_Width*3*i+j*3+0)=r; //red
*(lpImg+m_Width*3*i+j*3+1)=g; //green
*(lpImg+m_Width*3*i+j*3+2)=b; //blue
void Raw2BMP (BYTE *lpImg)
BYTE *TmpBuf = new BYTE[m_Width*m_Height*3];
for(i = 0 ; i < m_Height; i++) {
for( j=0 ; j < m_Width; j++) {
*(TmpBuf+m_Width*3*i+j*3+2) = *(lpImg+m_Width*3*i+j*3+0);
*(TmpBuf+m_Width*3*i+j*3+1) = *(lpImg+m_Width*3*i+j*3+1);
*(TmpBuf+m_Width*3*i+j*3+0) = *(lpImg+m_Width*3*i+j*3+2);
delete []TmpBuf;
void MakeBMPFile(char *FileName, BYTE *OutImage)
FILE *OutFile;
OutFile = fopen(FileName, "wb");
fwrite(&hf,sizeof(char), sizeof(BITMAPFILEHEADER), OutFile);
fwrite(&hInfo, sizeof(char), sizeof(BITMAPINFOHEADER), OutFile);
fwrite(OutImage, m_Width * m_Height * 3, 1, OutFile);
fclose(OutFile);

这段程序看不出倒置。
m_Width = 60; m_Height = 60; // 图像宽度高度为60*60
BYTE *lpImg = new BYTE[m_Width * m_Height*3]; //声明和动态分配存图像素值的数组
memset(lpImg, 0, m_Width*m_Height*3); //初始化数组全 0
循环,设图像像素点的红绿蓝数值,范围 y=a1到a2, x=b1到b2:
for ( i = a1; i < a2; i++){for( j = b1; j < b2; j++){
*(lpImg+m_Width*3*i+j*3+0)=r; //red
*(lpImg+m_Width*3*i+j*3+1)=g; //green
*(lpImg+m_Width*3*i+j*3+2)=b; //blue

下面这段照理应当做倒置,即,第i行应赋值给 60-i-1 行, 但做的却是交换各像素点的红蓝值。
for(i = 0 ; i < m_Height; i++) {
for( j=0 ; j < m_Width; j++) {
*(TmpBuf+m_Width*3*i+j*3+2) = *(lpImg+m_Width*3*i+j*3+0); // 红值给蓝值
*(TmpBuf+m_Width*3*i+j*3+1) = *(lpImg+m_Width*3*i+j*3+1); // 绿不变
*(TmpBuf+m_Width*3*i+j*3+0) = *(lpImg+m_Width*3*i+j*3+2); // 蓝值给红值

delete []TmpBuf; // 释放内存 太早,还没写出去呢。

hInfo.biHeight=m_Height*-1; BMP info 头里把高度写成负的 是不是 倒置的窍门,我不清楚。
hInfo.biBitCount=24; // RGB 24 bits
hInfo.biCompression=0; // 是否压缩,info 头里参数 设 0 为默认

这段是输出 BMP 格式图像:
OutFile = fopen(FileName, "wb");
fwrite(&hf,sizeof(char), sizeof(BITMAPFILEHEADER), OutFile); //写头
fwrite(&hInfo, sizeof(char), sizeof(BITMAPINFOHEADER), OutFile); //写信息头
fwrite(OutImage, m_Width * m_Height * 3, 1, OutFile); //写 image 本身。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-15
上下倒 还是左右倒? 原理很简单,可以将像素数据想象成一个二维数组, 以像素为单位(bmp格式是3个字节), 将像素进行交换。追问

上下倒

追答

原理很简单。 伪代码:

void swap(rgb* data, int w, int h)
{
k = h/2;
for(i=0; ir = (data+(h-i)*w)->r;
/* 个人建议用rgb结构体表示较好, 用[0], [1], [2]表示不利于rgb转换。 windows下,位图按照bgr 存储的。
*/
(data+i*w)->g = (data+(h-i)*w)->g;
(data+i*w)->b= (data+(h-i)*w)->b;
}
}

相似回答