2014-08-31 59 views
0

假设我已经在与我的main.cpp相同的目录中给出了名为in.png的图像。我必须读取该图像并将其放入我在main正文中创建的b。我得到了一张class PNG,以及读取图像的功能。我也在下面发布了我的算法,我最后在3年前拿到了C编程语言,现在在C++中,我忘记了很多。为什么我的算法没有工作,出了什么问题从同一目录读取图像?

在png.h;

class PNG 
{ 
public: 
    PNG(string const & file_name); 
    /** 
    * Copy constructor: creates a new PNG image that is a copy of 
    * another. 
    * @param other PNG to be copied. 
    */ 
}; 

在png.cpp中,我们有;

PNG::PNG(string const & file_name) 
{ 
    _pixels = NULL; 
    _read_file(file_name); 
} 

在main.cpp中,这是我的代码;

int main 
{ 
    PNG a;       
    PNG*b = NULL; 
    b = a.PNG(string const & in.png); 
    if(*b = NULL) 
     cout << "The image wasn't read" << endl; 
    return 0; 
} 

感谢

编辑------------------------------------- -----------------

你好Mr. R Sahu, 所以,我希望能够旋转此图像。所以,我创建了另一个名为“b”的对象,我希望能够从“a”得到旋转的数据并将其放入“b”中。我决定使用在PNG中声明的函数,但它被声明为一个像素的“类”,就像PNG是一个图像的类。但是我有错误使用它,我的代码有什么问题。带“//”的代码是我之前尝试过的并且无法使用的代码。

代码有多个文件;

在PNG.h中,我们现在将函数定义为;

#include <iostream> 
#include "rgbapixel.h" 
class PNG 
{ 
public: 
RGBAPixel * operator()(size_t x, size_t y); 
/** 
* Const pixel access operator. Const version of the previous 
* operator(). Does not allow the image to be changed via the 
* pointer. 
* @param x X-coordinate for the pixel pointer to be grabbed from. 
* @param y Y-cooridnate for the pixel pointer to be grabbed from. 
* @return A pointer to the pixel at the given coordinates (can't 
*  change the pixel through this pointer). 
*/ 
size_t width() const; // returns width 
size_t width() const; 
private: 
// storage 
size_t _width; 
size_t _height; 
RGBAPixel * _pixels; 
}; 

函数在png.cpp中为我们实现。所以,在main.cpp中,我有我的代码来使用它们;

#include <algorithm> 
#include <iostream> 

#include "rgbapixel.h" 
#include "png.h" 
using namespace std; 
int main() 
{ 
cout << "me..........." << endl; 
PNG a("in.png"); 
PNG b; 
for(size_t i = 0; i < a.width(); i++) 
{ 
for(size_t j = 0; j <a.height(); j++) 
{ 
// *b(i, j) = *a(j, i);        erata 
// b(i,j) = RGBAPixel * operator()(size_t x, size_t y); 
// b(i, j) = operator()(i, j);    
//b(i,j) = *operator(i,j); 
//b(j,i) = a*operator(i,j); 
//b(j,i) = a.operator(i,j); 
//b(j, i) = a.*operator(i,j); 
} 
} 
b.writeToFile("output.png"); 
return 0; 
} 

我收到使用该功能的错误,我不能说出了什么问题。所以,我不知道我的alogarithm是否会得到旋转的图像

有些错误;

[[email protected] lab_intro]$ make 
clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp 
main.cpp:24:29: error: expected ')' 
b(i,j) = *operator(i,j); 
         ^
main.cpp:24:28: note: to match this '(' 
b(i,j) = *operator(i,j); 
        ^
main.cpp:24:20: error: use of undeclared 'operator()' 
b(i,j) = *operator(i,j); 
      ^
2 errors generated. 
make: *** [main.o] Error 1 

感谢

回答

1

只需使用:

int main() 
{ 
    // Construct a PNG object by passing the name 
    // of the png file to the constructor. 
    PNG a("in.png"); 
    return 0; 
} 
+0

所以如果我想的形象写到这的确给定函数的相对的文件,我做贵方觉得a.write_out(“输出.png“)....会工作吗? – user124627 2014-08-31 05:15:31

+0

@ user124627,如果'PNG'类具有这样的功能,那么是的,它应该。 – 2014-08-31 05:30:49

+0

非常感谢,它确实有这个功能 – user124627 2014-08-31 05:35:14