Find 2 bugs in Code? (c++)
include
include "png.h"
include
using namespace std;
// sets up the output image
PNG * setupOutput(int w, int h);
// Returns my favorite color
RGBAPixel * myFavoriteColor(int intensity);
int main()
{
// Load in.png
PNG * original = new PNG();
cout << "Reached line 14" << endl;
original->readFromFile("in.png");
cout << "Reached line 14" << endl;
int width = original->width();
cout << "Reached line 14" << endl;
int height = original->height();
cout << "Reached line 14" << endl;
// Create out.png
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
cout << "Reached line 35" << endl;
for (int y = 1; y < height; y++)
{
cout << "Reached line 35" << endl;
for (int x = 1; x < width; x++)
{
cout << "Reached line 39" << endl;
// Calculate the pixel difference
RGBAPixel * prev = (*original)(x-1, y-1);
RGBAPixel * curr = (*original)(x , y );
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;
cout << "Reached line 54" << endl;
}
}
// Save the output file
output->writeToFile("out.png");
// Clean up memory
delete myPixel;
delete output;
delete original;
return 0;
}
// sets up the output image
PNG * setupOutput(int w, int h)
{
PNG * image = new PNG(w, h);
return image;
}
// Returns my favorite color
RGBAPixel * myFavoriteColor(int intensity)
{
RGBAPixel color;
color.red = 0;
color.green = intensity/2;
color.blue = intensity;
&color;
}
//interface info
*/
bool readFromFile(string const & file_name);
/**
* Writes a PNG image to a file.
* @param file_name Name of the file to write to.
* @return Whether the file was written successfully or not.
*/
bool writeToFile(string const & file_name);
/**
* Gets the width of this image.
* @return Width of the image.
*/
size_t width() const;
/**
* Gets the height of this image.
* @return Height of the image.
*/
size_t height() const;
/**
* Resizes the image to the given coordinates. Attempts to preserve
* existing pixel data in the image when doing so, but will crop if
* necessary. No pixel interpolation is done.
* @param width New width of the image.
* @param height New height of the image.
*/
void resize(size_t width, size_t height);
private:
// storage
size_t _width;
size_t _height;
RGBAPixel * _pixels;
// private helper functions
bool _read_file(string const & file_name);
void _clear();
void _copy(PNG const & other);
void _blank();
void _init();
void _min_clamp_x(size_t & width) const;
void _min_clamp_y(size_t & height) const;
void _min_clamp_xy(size_t & width, size_t & height) const;
void _clamp_xy(size_t & width, size_t & height) const;
bool _pixels_same(const RGBAPixel & first, const RGBAPixel & second)
const;
RGBAPixel & _pixel(size_t x, size_t y) const;
};
endif // EPNG_H
No comments:
Post a Comment