Comparing image pixels and using mod
Apr 14, 2011 at 9:03am UTC
Hello. I'm working on an assignment where I have to read a "test.gif" image and output a photonegative of that image. After that, I need to add row mod 7 to the red components and subtract col mod 11 from the green components of every pixel while checking for underflow and overflow. Next, I read the file back in and compare the difference in pixels from each image. I'm having problems with the syntax when using mod and also ostream.
I have put in comments what i'm having problems with in main.cpp
Image.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#ifndef Image_H
#define Image_H
#include <ostream>
#include <string>
#include "ImageLib.h"
using namespace std;
class Image {
public :
Image(string filename);
Image(int r, int c);
Image(const Image &object);
~Image();
Image operator =(const Image &);
pixel getPixel(int , int );
void setPixel( int row, int col, int r, int g, int b );
void writeFile(string filename);
bool operator ==(const Image &) const ;
bool operator !=(const Image &) const ;
friend ostream & operator <<(ostream &, const Image &);
int getWidth() const ;
int getHeight() const ;
Image invert() const ;
bool overFlow(int , int );
bool underFlow(int , int );
private :
image img;
int height;
int width;
};
#endif
Image.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#include "Image.h"
#include "ImageLib.h"
Image::Image(string filename) {
img = ReadGIF(filename);
height = img.rows;
width = img.cols;
}
Image::Image(int r, int c) {
img = CreateImage(r,c);
height = r;
width = c;
}
Image::Image(const Image &object) {
if ( this != &object) {
img = CopyImage(object.img);
height = object.height;
width = object.width;
}
}
Image::~Image() {
DeallocateImage(img);
}
Image Image::operator =(const Image &object){
if ( this != &object) {
DeallocateImage(img);
img = CopyImage(object.img);
height = object.height;
width = object.width;
}
return *this ;
}
pixel Image::getPixel(int r, int c) {
return img.pixels[r][c];
}
void Image::setPixel(int row, int col, int r, int g, int b) {
img.pixels[row][col].red = r;
img.pixels[row][col].green = g;
img.pixels[row][col].blue = b;
}
void Image::writeFile(std::string filename) {
WriteGIF("output.gif" , img);
}
bool Image::operator ==(const Image &object) const {
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (img.pixels[row][col].red != object.img.pixels[row][col].red ||
img.pixels[row][col].green != object.img.pixels[row][col].green ||
img.pixels[row][col].blue != object.img.pixels[row][col].blue)
return false ;
}
}
return true ;
}
bool Image::operator !=(const Image &object) const {
if ( *this == object )
return false ;
else
return true ;
}
ostream& operator <<(ostream &cout, const Image &object){
cout << "Rows:" << object.height << "\n"
<< "Columns:" << object.width;
return cout;
}
Image Image::invert() const {
const int MAX = 255;
Image newObject = *this ;
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
newObject.img.pixels[row][col].red = MAX - img.pixels[row][col].red;
newObject.img.pixels[row][col].green = MAX - img.pixels[row][col].green;
newObject.img.pixels[row][col].blue = MAX - img.pixels[row][col].blue;
}
}
return newObject;
}
bool Image::overFlow(int r, int red)
{
int x = (r % 7) + red;
if ( x > 255)
return true ;
else
return false ;
}
bool Image::underFlow(int c, int green)
{
int x = green - (c % 11);
if ( x < 0)
return true ;
else
return false ;
}
int Image::getHeight() const {
return height;
}
int Image::getWidth() const {
return width;
}
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "ImageLib.h"
#include "Image.h"
#include <iostream>
void main()
{
Image input("test.gif" );
Image vert = input.invert();
vert.writeFile("output.gif" );
// ostream not printing the rows and columns
ostream& operator <<(ostream cout, const Image vert);
// I think I need to read the image back in?
// Row Mod 7, Col mod 11
// SYNTAX ISSUES
for (int row = 0; row < vert.height; row++) {
for (int col = 0; col < vert.width; col++) {
getPixel(row,col).blue;
}
Apr 14, 2011 at 1:23pm UTC
You are aware that line 12 must be
cout << vert;
?
I think I need to read the image back in?
No, you have 2 independend objects 'input' and 'vert' which you can use for any operation
EDIT: Line 19/20: since 'height' and 'width' are private you can't use them directly use 'getHeight()' and 'getWidth()' instead
Last edited on Apr 14, 2011 at 1:25pm UTC
Apr 14, 2011 at 5:23pm UTC
Thank you for your help! I'll give that a try.
Apr 14, 2011 at 10:40pm UTC
Alright, I was able to fix a couple issues but now I'm having trouble with syntax in the for loop to mod red and green for every pixel.
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "ImageLib.h"
#include "Image.h"
#include <iostream>
void main()
{
Image input("test.gif" );
Image vert = input.invert();
vert.writeFile("output.gif" );
cout << vert;
// Row Mod 7, Col mod 11
// SYNTAX ISSUES
for (int row = 0; row < vert.getHeight(); row++) {
for (int col = 0; col < vert.getWidth(); col++) {
if (Image::overFlow(row, vert.getPixel[row][col]))
vert.getPixel[row][col] = 255;
else
vert.setRed(row, col, row % 7);
}
I am getting the error:
'Image::getPixel': function call missing argument list; use '&Image::getPixel' to create a pointer to member
Apr 15, 2011 at 5:48am UTC
Line 17: vert.getPixel[row][col]
-> vert.getPixel(row, col)
Line 18: vert.getPixel[row][col] = 255;
-> vert.setPixel(row, col, 255, 255, 255);
Topic archived. No new replies allowed.