假設rgbapixel是rgbapixel.h文件中的一類像素,所以它具有綠色,藍色,紅色等公共成員的顏色。 PNG是png.h文件中的一類圖像,因此它具有圖像寬度和高度作爲私有成員,然後它有兩個公共函數返回寬度和高度。我應該如何糾正這個指針問題,它給我一個錯誤
在我的main.cpp中,這裏是代碼;
// sets up the output image
PNG * setupOutput(int w, int h)
{
PNG * image = new PNG(w, h);
return image;
}
void sketchify()
{
// Load in.png
PNG * original = new PNG;
original->readFromFile("in.png");
int width = original->width();
int height = original->height();
// Create out.png
// PNG * output; // i change this
PNG * output = new PNG;
setupOutput(width, height);
// Loud our favorite color to color the outline
RGBAPixel * myPixel = myFavoriteColor(192);
// Go over the whole image, and if a pixel differs from that to its upper
// left, color it my favorite color in the output
for (int y = 1; y < height; y++)
{
for (int x = 1; x < width; x++)
{
// Calculate the pixel difference
RGBAPixel * prev = (*original)(x-1, y-1); // previous top lfet
RGBAPixel * curr = (*original)(x , y ); // current
// subtracting to see diffrence between pixels
int diff = abs(curr->red - prev->red ) +
abs(curr->green - prev->green) +
abs(curr->blue - prev->blue);
// If the pixel is an edge pixel,
// color the output pixel with my favorite color
RGBAPixel * currOutPixel = (*output)(x,y);
if (diff > 100)
currOutPixel = myPixel; // something wrong
}
}
// Save the output file
output->writeToFile("out.png");
// Clean up memory
delete myPixel;
delete output;
delete original;
當我執行代碼時,出現類似錯誤;
[EasyPNG]: Warning: attempted to access non-existent pixel (407, 306);
Truncating request to fit in the range [0,0] x [0,0].
[EasyPNG]: Warning: attempted to access non-existent pixel (408, 306);
Truncating request to fit in the range [0,0] x [0,0].
我在哪裏寫的「什麼是錯的」,我已被告知,這裏面就有一個錯誤。我沒看到它。 'mypixel'和currout,兩者都成功地作爲指針聲明,所以我不明白這個聲明怎麼會是錯誤的。如果我試圖改變它,我會遇到編譯錯誤。幫助
我沒有看到任何設置輸出大小的地方。 – user3344003 2014-09-05 01:10:34
我沒有看到任何輸出「EasyPNG」的地方,這意味着這是_not_你的[testcase](http://sscce.org)。 – 2014-09-05 02:19:55