ブロックのコメントを追加するまで、私のコードは正常に機能し、2 つのランダムなエラーを受け取りました。1 つは、pxm_utils.cpp の前の行の 1 つで '}' の前に関数呼び出しを行うことができなかったというもので、もう 1 つは、入力の最後にブラケットがありませんが、すべてのブラケットがチェックアウトされているというものです。これと変換機能を説明する助けがあれば大歓迎です。
ここで私が何をしているのかを完全に理解するために、私が解決しようとしていた問題があります。-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」操作が画像に対して行うことについては、以下の説明を参照してください。
- pgm と ppm の写真のネガを計算する関数 pxm::negative() を実装します。
-現在アクティブな形式に応じて、PPM カラー画像を PGM グレーレベル画像に、またはその逆に変更する関数 pxm::convert() を実装します。
-上記のコードを変更して、2D 画像のインデックス スキームを使用します。コードは、インデックス作成自体に加えて、そのようなデータ構造を割り当てて解放できる必要があります。この時点で、すべてのコードは、画像の行と列をスイープするネストされた一連のループに基づいている必要があります。
現在作業中のファイルは 2 つあります。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();
}