【问题标题】:How do you rotate a given image?你如何旋转给定的图像?
【发布时间】:2014-08-31 23:44:03
【问题描述】:

假设图像是 i 对象文件“a”,我创建了另一个名为 b 的对象文件来旋转图像。我将 a 的 (j,i) 提供给 b 的 (i,j),但我的代码无法正常工作,因为没有正确使用名为 operator 的函数,我有“//”我尝试过的东西,但我一直报错,我该怎么办? rgbapixels 是一个像素类,就像 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("I have put the image in "a" here");
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);
}
}
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

谢谢

【问题讨论】:

    标签: c++ image function class header-files


    【解决方案1】:

    () 运算符在具有此运算符的类的实例作为函数调用被调用时使用。

    因此,对于您的 PNG 类,以及它的两个实例称为 ab,它们的 () 运算符将被简单地调用为:

    a(i, j)
    

    b(i, j)
    

    由于您的() 运算符返回RGBAPixel *,因此此类伪函数调用的最终结果是一个指针,我认为应该取消引用。

    示例中第一行注释掉的代码:

    // *b(i, j) = *a(j, i);
    

    在我看来,这就像您的 () 运算符的正确用法。我认为编译它没有任何问题。

    【讨论】:

    • 但它失败了,它给了我错误; clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp main.cpp:21:62: 错误:使用未声明的标识符 'erata' *b(i, j ) = *a(j, i);产生了 ^ 1 个错误。 make: *** [main.o] 错误 1
    • 您的错误消息清楚地引用了“erata”,无论它是什么。该错误消息似乎与赋值语句没有任何关系。有用的提示:在放弃并向他人寻求帮助之前,请务必阅读至少三遍错误消息。尤其是 clang 的错误消息,它以清晰和有用而闻名。
    • 它可以工作,但是由于某种原因,当我在“b”中输出图像时,它是空的,并且它显示没有像素被复制。 th given warnings;.......[EasyPNG]: 警告:试图访问不存在的像素 (511, 383);截断请求以适应范围 [0,0] x [0,0]。 [jonathan2@linux-a1 lab_intro]$
    【解决方案2】:

    您不应该以这种方式调用运算符函数,而是这样做:

    *b(i, j) = *a(j, i);
    

    【讨论】:

    • 但它失败了,它给了我错误; clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp main.cpp:21:62: 错误:使用未声明的标识符 'erata' *b(i, j ) = *a(j, i);产生了 ^ 1 个错误。 make: *** [main.o] 错误 1
    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多