Skip to content

VJ's headers added #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions VJ-Raghvan/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "color.h"
#include <iostream>

Color::Color(){_r = _g = _b = 0;}
Color::Color(float r, float g, float b): _r(r), _g(g), _b(b) {}
Color::~Color(){}
Color::Color(const Color& p) {
_r = p._r;
_g = p._g;
_b = p._b;
}

void Color::set_color(float r, float g, float b) {
_r = r;
_g = g;
_b = b;
}
float Color::get_r() const{return _r;}
float Color::get_g() const{return _g;}
float Color::get_b() const{return _b;}

Color& Color::operator=(const Color &other) {
_r = other._r;
_g = other._g;
_b = other._b;

return *this;
}

std::ostream& operator<<(std::ostream& os, const Color& c) {
os << static_cast<int>(c._r) << " " << static_cast<int>(c._g) << " " << static_cast<int>(c._b);
return os;
}

std::istream& operator>>(std::istream& is, Color& c) {
is >> c._r >> c._g >> c._b;
return is;
}
21 changes: 21 additions & 0 deletions VJ-Raghvan/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef COLOR_H
#define COLOR_H

class Color {
private:
float _r, _g, _b;
public:
Color();
Color(float, float, float);
virtual ~Color();
Color(const Color&);
void set_color(float, float, float);
float get_r() const;
float get_g() const;
float get_b() const;
friend std::ostream& operator<<(std::ostream& os, const Color& c);
friend std::istream& operator>>(std::istream& is, Color& c);
Color& operator=(const Color &other);
};

#endif
67 changes: 67 additions & 0 deletions VJ-Raghvan/image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "color.h"
#include "pixel.h"
#include "image.h"
#include <iostream>
#include <algorithm>

Image::Image(int w, int h):_w(w), _h(h) {
set_arr();
}
Image::Image(){}
Image::~Image() {if(!_arr)delete _arr;}

void Image::set_arr() {
_arr = new Pixel*[_h];
for(int i = 0; i < _h; i++) {
_arr[i] = new Pixel[_w];
for(int j = 0; j < _w; j++) {
_arr[i][j].set_loc(i, j);
}
}
}

void Image::filter(const Color& c, float a) {
for(int i = 0; i < _h; i++) {
for(int j = 0; j < _w; j++) {
_arr[i][j].apply_filter(c, a);
}
}
}
Color Image::color_at(int x, int y) const{
return _arr[x][y].get_color();
}

std::istream& operator>>(std::istream& in, Image& im) {
in >> im._encoding;
if(im._encoding != "P3") {
std::cout << "unsupported file encoding!" << std::endl;
exit(1);
}
in >> im._w >> im._h >> im.max_pixel;

im.set_arr();
Pixel p;
for(int i = 0; i < im._h; i++) {
for(int j = 0; j < im._w; j++) {
in >> p;
p.set_loc(j, i);
im._arr[i][j] = p;
}
}
return in;
}

std::ostream& operator<<(std::ostream& os, const Image& im) {
os << im._encoding << std::endl;
os << im._w << " " << im._h << std::endl;

os << im.max_pixel << std::endl;

for(unsigned i = 0; i < im._h; i++) {
for(unsigned j = 0; j < im._w; j++) {
os << im._arr[i][j] << " ";
}
os << std::endl;
}
return os;
}
19 changes: 19 additions & 0 deletions VJ-Raghvan/image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef IMAGE_H
#define IMAGE_H

class Image {
int _w, _h, max_pixel;
std::string _encoding;
Pixel** _arr;
public:
Image(int w, int h);
Image();
virtual ~Image();
Color color_at(int x, int y) const;
void filter(const Color& c, float a);
void set_arr();
friend std::ostream& operator<<(std::ostream& os, const Image& i);
friend std::istream& operator>>(std::istream& in, Image& i);
};

#endif
35 changes: 35 additions & 0 deletions VJ-Raghvan/pixel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "color.h"
#include "pixel.h"
#include <iostream>

Pixel::Pixel() {}
Pixel::~Pixel(){}
Pixel::Pixel(const Pixel& p) {
_x = p._x;
_y = p._y;
_c = p._c;
}

Color Pixel::get_color() const {return _c;}
void Pixel::set_color(float r, float g, float b) {
_c.set_color(r, g, b);
}
void Pixel::set_loc(int x, int y) {
_x = x;
_y = y;
}
void Pixel::apply_filter(const Color& c, float a) {
_c.set_color((1-a)*_c.get_r() + a*c.get_r(),
(1-a)*_c.get_g() + a*c.get_g(),
(1-a)*_c.get_b() + a*c.get_b());
}

std::ostream& operator<<(std::ostream& os, const Pixel& p) {
os << p._c; // Outputs the color
return os;
}

std::istream& operator>>(std::istream& is, Pixel& p) {
is >> p._c;
return is;
}
19 changes: 19 additions & 0 deletions VJ-Raghvan/pixel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef PIXEL_H
#define PIXEL_H

class Pixel {
int _x, _y;
Color _c;
public:
Pixel();
virtual ~Pixel();
Pixel(const Pixel& p);
Color get_color () const;
void set_color(float r, float g, float b);
void set_loc(int x, int y);
void apply_filter(const Color& c, float a);
friend std::ostream& operator<<(std::ostream& os, const Pixel& p);
friend std::istream& operator>>(std::istream& is, Pixel& p);
};

#endif