c++ - 从同一目录读取图像?

标签 c++ image file makefile header-files

假设我在与我的 main.cpp 相同的目录中获得了一个名为 in.png 的图像。我必须读取该图像并将其放入我在 main 正文中创建的 b 中。我得到了一个 class PNG,以及读取图像的函数。我还在下面发布了我的算法,我上一次学习 C 编程语言是 3 年前,现在我在 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;
}

谢谢

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

您好,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;
}

我在使用该函数时遇到错误,我不知道哪里出了问题。所以,我不知道我的算法是否会使图像旋转

一些错误;

[jonathan2@linux-a1 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

谢谢

最佳答案

只需使用:

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

关于c++ - 从同一目录读取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25588756/

相关文章:

C++模板题

javascript - 在固定高度的 div 中垂直对齐图像

c++ - Linux C++ ._MyFirst ._MyLast vector 等效

javascript - 查找两个图像中的哪一个被单击并将其报告给数据库

java - 我无法显示当我在 netbeans 中执行 "cleaning and building"项目时不存在的那些图像

.net - 如何检查文件是否正在被另一个进程使用-Powershell

c - 从 C 中的文件扫描

java - 有没有办法让需要文件参数的命令行程序使用标准输入?

c++ - 在C++登录程序中屏蔽密码

c++ - 根据数据成员排序或过滤对象数组