#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char * argv[])
{
if (argc != 3)
{
cout << "please use this command like this: PPM2BMP.exe source.ppm result.bmp" << endl;
return 0;
}
string PpmFileName("");
PpmFileName = argv[1];
ifstream InFile;
ofstream OutFile;
InFile.open(argv[1],ios::binary);
OutFile.open(argv[2],ios::binary);
string FileType;
int Width, Height;
int FileSize;
int temp;
InFile >> FileType >> Width >> Height >> temp;
int FileDataSize = Width * Height * 3;
FileSize = 0x36 + FileDataSize;
temp = Width * Height;
cout << FileType << 0x0A << Width << ' ' << Height;
unsigned char bmphead[54] = {0x42,0x4d,0,0,0,0,0,0,0,0,0x36,0,0,0,0x28,0,
0,0,0,0,0,0,0,0,0,0,0x1,0,0x18,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};
int FileSizeCount = 2;
while (FileSize != 0)
{
bmphead[FileSizeCount++] = FileSize % 256;
FileSize = FileSize / 256;
}
int FileDataCount = 0x22;
while (FileDataSize != 0)
{
bmphead[FileDataCount++] = FileDataSize % 256;
FileDataSize = FileDataSize / 256;
}
int FileScaleCount = 0x12;
while (Width != 0)
{
bmphead[FileScaleCount++] = Width % 256;
Width = Width / 256;
}
FileScaleCount = 0x16;
while (Height != 0)
{
bmphead[FileScaleCount++] = Height % 256;
Height = Height / 256;
}
for (int i=0; i<54; i++)
OutFile << static_cast<unsigned char>(bmphead[i]);
char color0,color1,color2;
InFile.ignore();
for (int j=1; j<=temp; j++)
{
InFile.get(color0);
InFile.get(color1);
InFile.get(color2);
OutFile << hex;
OutFile.put(color2);
OutFile.put(color1);
OutFile.put(color0);
}
InFile.close();
OutFile.close();
return 0;
}
ppm图片格式转换为bmp
3星 · 超过75%的资源 需积分: 50 43 浏览量
2014-03-04
19:43:32
上传
评论
收藏 8KB ZIP 举报
自溟
- 粉丝: 1
- 资源: 28