图像以1D阵列数据传入。需要将其迁移到2D数据。 2D数组的大小必须为640行和1176列,并且最终必须使用.pgm标头写入文件。
void saveFile(unsigned short* mImStack) // Image to be accessed
{
std::ofstream f("D:\\tester.txt", ios::out);
int rows = 0;
int cols = 0;
int t_rows = (mImageRaw->m_ImageHeight); // height of image (640)
int t_cols = (mImageRaw->m_ImageWidth); // width of image (1176)
int ncells = t_rows * t_cols; // total cells of the 1D array which has the image data
unsigned short** mImage2D = new unsigned short*[cols]; // defining a 2D array
for (int i = 0; i < t_cols; ++i)
{
mImage2D[i] = new unsigned short[rows];
}
for (int n = 0; n < ncells; n++)
{
mImage2D[rows][cols] = mImStack[n]; // Error ocuring here as Access violation reading location
cols++;
if (cols == t_cols && rows != t_rows + 1)
{
rows++;
cols = 0;
}
}
f << "P2\n" << t_rows << " " << t_cols << "\n4096\n" << mImage2D[rows][cols];
f.close();
}