我的代码工作正常,直到我添加了块的注释然后我收到了两个随机的错误。一个说明我在pxm_utils.cpp的早期行之前的'}'之前没有函数调用,另一个是我在输入结尾处缺少一个括号但我的所有括号都检查出来。任何帮助解释这个和转换函数非常感谢。
要完全了解我在做什么,这是我试图解决的问题。 - 实现读写PGM和PPM图像文件的功能。在此阶段,将图像存储为1D阵列。不要使用向量 - 自己处理所有内存分配,并将此代码放在支持函数pxm :: newimg()和pxm :: deleteimg()中。 读取图像文件时,提取文件后缀。确保后缀有效(pgm或ppm)并且magicid(P5或P6)与之匹配。然后提取剩余的标题信息,为新图像分配内存,并从输入文件中读取图像数据。
编写图像文件时,请更改名称以指示已完成的操作,并确保后缀与文件包含的内容相匹配。例如,如果输入为“test.pgm”且唯一操作为“-invert”,则输出应命名为“test_i.pgm”。如果“-invert”后跟“-convert”,则输出应命名为“test_ic.ppm”。注意后缀的变化。请参阅下面的说明,了解“-invert”和“-convert”操作对图像的作用。
- 实现函数pxm :: negative(),它计算pgm和ppm照片底片。
- 实现函数pxm :: convert(),它将PPM彩色图像更改为PGM graylelve强度图像,反之亦然,具体取决于当前活动的格式。
- 修改上述代码以使用2D图像索引方案。除了索引本身之外,您的代码必须能够分配和释放此类数据结构。此时,所有代码都必须基于嵌套图像行和列的嵌套循环
我现在正在使用两个文件。一个pxm_utils.h和pxm_utils.cpp。 pxm_magic.cpp已经准备好有一个工作代码pxm_magic.cpp
Pxm_utils.h
#ifndef PXMUTILS_H
#define PXMUTILS_H
#include <string>
using namespace std;
typedef unsigned char uchar;
class pxm {
public:
pxm();
~pxm();
void read(const string &);
void write(const string &, const string &);
void negative();
void convert();
void set_cmap(const char *cmap_fname="jet.cmap");
private:
string magicid; // file identifier: P5 for PGM, P6 for PPM
int maxvalue; // always 255
int nrows, ncols; // data dependent
int bpp; // bytes-per-pixel: 1 for PGM, 3 for PPM
//uchar *cmap;
uchar **cmap;
//store image as 1D array
//uchar *img;
//uchar *newimg(int, int, int);
//void deleteimg(uchar *);
// store image as 2D array
uchar **img;
uchar **newimg(int, int, int);
void deleteimg(uchar **);
};
#endif
pxm_utils.cpp
#include "pxm_utils.h"
#include string
#include iostream
#include fstream
#include cstdlib
using namespace std;
//class constructor
pxm::pxm()
{
typedef unsigned char uchar;
uchar ** newimg(int nrows, int ncols,string magicid)
{
uchar **img= new uchar *[nrows];
img[0]=new uchar [nrows*ncols*magicid];
for(int i=1; i<nrows; i++)
{
img[i]=img[i-1]+ncols*magicid;
}
return img;
}
string filetype = "pgm";
magicid = "P5";
nrows = 0;
ncols = 0;
img = NULL;
}
//class deconstructor
pxm::~pxm() {
deleteimg(img);
{if(img)
{ if(img[0])
{delete[] img[0];}
}
}
}
//Reads the header string along with the binary data of img
void pxm::read(const string & fname)
{
ifstream fin(fname.c_str());
fin.open ("test.pgm");
{
if (fin.fail())
{
cout << "Input file opening failed. " << endl;
exit(1);
}
fin >> magicid >> ncols >> nrows >> maxvalue;
{ if( magicid == "P5")
filetype = "PGM";
else if( magicid == "P6")
filetype = "PPM";
}
{ if(maxvalue != 225)
cout << "Image maxvalue was not 225!"<< endl;
exit(1);
}
{ if( ncols >= 0)
cout << "Number of columns can not be negative! \n";
exit(1);
if( nrows >= 0)
cout << "Number of rows can not be negative! \n";
exit(1);
}
while (fin.get() != '\n') {}
img = newimg(nrows, ncols);
fin.read((char *)img[0], nrows*ncols);
cout << "The magicid was: " << magicid << endl;
cout << "Which means the file type is " << filetype << endl;
}
}
//allocate data for cmap
void pxm::set_cmap(const char *cmpa_fname="jet.cmap")
{
cmap = newimg(nrows,ncols,3);
unchar gray, red, green, blue;
for (int i=0; i<nrows; i++){
for (int j=0; j<ncols; j++){
gray= img[i][j];
red= cmap_fname[gray][0];
green= cmap_fname[gray][1];
blue= cmap_fname[gray][2];
cmap[i][(j*3)-2]= red;
cmap[i][(j*3)-1]= green;
cmap[i][(j*3)]= blue;
}
}
}
for (int i=0; i < 256; i++){
cmap[i][0]=cmap_fname[i*3 -21];
cmap[i][1]=cmap_fname[i*3-1];
cmap[1][2]=cmap_fname[i*3];
}
}
//inverses the colors of the image
void pxm::negative()
{
for(int i=0; i<nrows; i++) {
for(int j=0; j<ncols; j++){
int y=img[i][j];
img[i][j]=(255-y);
}
}
}
/*
void pxm:convert()
{
if(magicid=="P6"){
for(int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{
int p=img[i][j];
img[i][j]=(0.229*)
}
}
}
if(magicid=="P5")
for(int i=0; i<nrows; i++)
{
for int (j=0; j<ncols; j++)
{
int y= img[i]
}
}
}
*/
//writes out the img file after operation is complete
void pxm::write(const string & fname, const string & fname);
{
ofstream fout (fname.c_str(), ios::out);
size_p dp;
if ((dp= fnamerfind(".pgm")) != string::npos)
{
fout<<"P6"<<endl;
}
if((dp= fname.rfind(".ppm")) != string::npos)
{
fout<<"P6"<<endl;
}
fout<< ncols<< " " << nrows << endl;
fout<< maxvalue << endl;
for(int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{ fout<< img[i][j]<< " "; }
fout << endl;
}
fout.close();
}
答案 0 :(得分:3)
您正在编写嵌套函数,而C ++中不允许这样做。特别是newimg
被定义到pmx
构造函数中。
某些编译器可能允许它作为扩展,但IMO不值得。
解决方案就是将函数移动到类范围。如果您不需要访问this
对象,请执行静态操作。