-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpgmFile.cpp
More file actions
298 lines (251 loc) · 7 KB
/
pgmFile.cpp
File metadata and controls
298 lines (251 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "stdafx.h"
#include "pgmFile.h"
#include <io.h>
#include <iostream>
using namespace std;
#include "gdal//gdal_priv.h" //using gdal lib to realize the transform to bmp file
#pragma comment(lib,"gdal_i.lib")
/*open pgm and get the head file*/
/*
get file information of the pgm file
the version of the pgm data, the width
and height of the pgm image, the datatype
of the pgm image.
*/
PGMHeadInfo pgmFile::openFile(const char* path)
{
PGMHeadInfo _pgmHeadInfo;
memset(&_pgmHeadInfo,0,sizeof(PGMHeadInfo));
m_readPGM.open(path,ios_base::in|ios_base::binary);
if(!m_readPGM.is_open())
{
cerr<<"error file is not opened";
return _pgmHeadInfo;
}
else
{
char temp[10];
m_readPGM.getline(temp,10);
_pgmHeadInfo.pgm_version[0]=temp[0];
_pgmHeadInfo.pgm_version[1]=temp[1];
if(!(strcmp(_pgmHeadInfo.pgm_version,"P5")||strcmp(_pgmHeadInfo.pgm_version,"P2")))
cerr<<"error file is not the format of PGM";
m_readPGM>>_pgmHeadInfo.pgm_width>>_pgmHeadInfo.pgm_height>>_pgmHeadInfo.pgm_max;
// m_readPGM.getline(temp,10);
// sscanf_s(temp,"%d%d",&_pgmHeadInfo.pgm_width,&_pgmHeadInfo.pgm_height);
// m_readPGM.getline(temp,10);
int type=_pgmHeadInfo.pgm_max;
if(type<=255)
_pgmHeadInfo.pgm_dataType=_256Level;
else
_pgmHeadInfo.pgm_dataType=_65536Level;
return _pgmHeadInfo;
}
}
/*close the opened pgm file*/
void pgmFile::closeFile()
{
if(m_readPGM.is_open())
m_readPGM.close();
if(m_writePGM.is_open())
m_writePGM.close();
}
/*get pgm data width and height*/
/*
to get the private of the HeadInfo one
should use these functions to get the info
*/
int pgmFile::getPGMWidth (PGMHeadInfo _pgmHeadInfo)
{
int width=_pgmHeadInfo.pgm_width;
return width;
}
int pgmFile::getPGMHeight(PGMHeadInfo _pgmHeadInfo)
{
int height=_pgmHeadInfo.pgm_height;
return height;
}
int pgmFile::getPGMMax(PGMHeadInfo _pgmHeadInfo)
{
int maxnumber=_pgmHeadInfo.pgm_max;
return maxnumber;
}
/*get the data type of the pgm file*/
PGMDATATYPE pgmFile::getPGMDataType(PGMHeadInfo _pgmHeadInfo)
{
PGMDATATYPE _pgmDataType;
_pgmDataType=_pgmHeadInfo.pgm_dataType;
return _pgmDataType;
}
/*get image data from the file*/
/*
if get the data successfully return 0
else return other error value
*/
long pgmFile::readPGMData(PGMHeadInfo _pgmHeadInfo,void* pBuffer,PGMDATATYPE dataType)
{
//check the validation import data
if(_pgmHeadInfo.pgm_version==0x00000000||pBuffer==NULL)
{
cerr<<"error imput data please check the imput"<<endl;
return -1;
}
if(_pgmHeadInfo.pgm_dataType!=dataType)
{
cerr<<"data type do not match please check the imput data type"<<endl;
return -2;
}
//get image data
int width =_pgmHeadInfo.pgm_width;
int height=_pgmHeadInfo.pgm_height;
//calculate the byte number according to the data type, width and height
int byteCount=0;
if(_pgmHeadInfo.pgm_dataType==_256Level)
byteCount=width*height;
else
byteCount=width*height*2;
m_readPGM.read(((char*)pBuffer),byteCount);
m_readPGM.seekg(-byteCount,ios::cur);
return 0;
}
/*save the image to file as the format of bmp*/
/*
* first get the info and data of the pgm file
* then use gdal lib to save the data as the format
* of bmp
*/
long pgmFile::tranPGMToBMP(PGMHeadInfo _pgmHeadInfo,void* data,const char* pathOut)
{
//check the imput data
if(pathOut==NULL)
{
cerr<<" error imput path name please check the imput"<<endl;
return -1;
}
//get image data
pgmFile optPgmFile;
int width =optPgmFile.getPGMWidth(_pgmHeadInfo);
int height=optPgmFile.getPGMHeight(_pgmHeadInfo);
PGMDATATYPE datatype=optPgmFile.getPGMDataType(_pgmHeadInfo);
GDALAllRegister();
GDALDatasetH dataset;
if(datatype==_256Level)
{
dataset=GDALCreate(GDALGetDriverByName("BMP"),pathOut,width,height,1,GDT_Byte,0);
GDALRasterIO(GDALGetRasterBand(dataset,1),GF_Write,0,0,width,height,data,width,height,GDT_Byte,0,0);
GDALClose(dataset);
}
if(datatype==_65536Level)
{
dataset=GDALCreate(GDALGetDriverByName("BMP"),pathOut,width,height,1,GDT_Int16,0);
GDALRasterIO(GDALGetRasterBand(dataset,1),GF_Write,0,0,width,height,data,width,height,GDT_Int16,0,0);
GDALClose(dataset);
}
return 0;
}
/*trans pgm in path pathPgm to bmp in path pathBmp*/
long pgmFile::tranPGMToBMP(char* pathPgm,char* pathBmp)
{
PGMHeadInfo pgm_headinfo=openFile(pathPgm);
int xsize=getPGMWidth(pgm_headinfo);
int ysize=getPGMHeight(pgm_headinfo);
int maxnum=getPGMMax(pgm_headinfo);
//获取PGM数据
PGMDATATYPE pgm_datatype;
pgm_datatype=getPGMDataType(pgm_headinfo);
//PGM数据转换为bmp数据
if(pgm_datatype==_256Level)
{
char *data=new char[xsize*ysize];
readPGMData(pgm_headinfo,data,pgm_datatype);
tranPGMToBMP(pgm_headinfo,data,pathBmp);
delete []data;data=NULL;
}
if (pgm_datatype==_65536Level)
{
short *data=new short[xsize*ysize];
readPGMData(pgm_headinfo,data,pgm_datatype);
tranPGMToBMP(pgm_headinfo,data,pathBmp);
delete []data;data=NULL;
}
closeFile();
return 0;
}
/*save all image to file as bmp type*/
/*
* translate all pgm file in dir to bmp
*/
long pgmFile::transDirPGMToBMP(char* pgmImageDir,char* bmpImageDir)
{
char dirPath[280];
char pgmPath[280];
char bmpPath[280];
sprintf(dirPath,"%s\\*.pgm",pgmImageDir);
_finddata_t file;
long lError;
int total=1;
if((lError=_findfirst(dirPath,&file))==-1)
{
printf("cannot find the file..\n");
return lError;
}
else
{
//handle the first file
printf("\rtrans %d image...",total);
sprintf(bmpPath,"%s\\%s%s",bmpImageDir,file.name,".bmp");
sprintf(pgmPath,"%s\\%s",pgmImageDir,file.name);
tranPGMToBMP(pgmPath,bmpPath);
while(_findnext(lError,&file)==0)
{
total++;
sprintf(bmpPath,"%s\\%s%s",bmpImageDir,file.name,".bmp");
sprintf(pgmPath,"%s\\%s",pgmImageDir,file.name);
printf("\rtrans %d image...",total);
tranPGMToBMP(pgmPath,bmpPath);
}
printf("finished..\n");
return lError;
}
}
//test funtion
void pgmFile::test_pgm_trans()
{
char* pathPGM="pgm\\air-force.1.pgm";
char* pathBMP="pgm\\air-force.1.bmp";
printf("打开PGM文件:%s\n",pathPGM);
PGMHeadInfo pgm_headinfo=openFile(pathPGM);
int xsize=getPGMWidth(pgm_headinfo);
int ysize=getPGMHeight(pgm_headinfo);
int maxnum=getPGMMax(pgm_headinfo);
printf("PGM影像数据大小为:%d/%d\n",xsize,ysize);
printf("PGM影像最大值为:%d\n",maxnum);
//获取PGM数据
printf("获取PGM数据\n");
PGMDATATYPE pgm_datatype;
pgm_datatype=getPGMDataType(pgm_headinfo);
//PGM数据转换为bmp数据
printf("PGM数据转换为BMP\n");
if(pgm_datatype==_256Level)
{
char *data=new char[xsize*ysize];
readPGMData(pgm_headinfo,data,pgm_datatype);
tranPGMToBMP(pgm_headinfo,data,pathBMP);
delete []data;data=NULL;
}
if (pgm_datatype==_65536Level)
{
short *data=new short[xsize*ysize];
readPGMData(pgm_headinfo,data,pgm_datatype);
tranPGMToBMP(pgm_headinfo,data,pathBMP);
delete []data;data=NULL;
}
closeFile();
pathPGM="pgm\\altschul.1.pgm";
pathBMP="pgm\\altschul.1.bmp";
tranPGMToBMP(pathPGM,pathBMP);
printf("转换完成!\n");
pathPGM="pgm";
pathBMP="bmp";
transDirPGMToBMP(pathPGM,pathBMP);
}