Skip to content

Changed color(float->int) #4

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 9 commits 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
61 changes: 61 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"files.associations": {
"iosfwd": "cpp",
"scoped_allocator": "cpp",
"iostream": "cpp",
"ostream": "cpp",
"chrono": "cpp",
"limits": "cpp",
"valarray": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ratio": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
}
76 changes: 76 additions & 0 deletions Base/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include "color.h"

Color::Color(){_r = _g = _b = 0;}
Color::Color(int r, int g, int 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(int r, int g, int b) {
_r = r;
_g = g;
_b = b;
}
int Color::get_r() const{return _r;}
int Color::get_g() const{return _g;}
int Color::get_b() const{return _b;}

Color Color::scalar_product(float a) {
Color res;
res._r = a*_r;
res._g = a*_g;
res._b = a*_b;

return res;
}

bool Color::operator==(const Color& c) {
return (_r == c._r) && (_g == c._g) && (_b == c._b);
}

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

return *this;
}

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

return *this;
}

// Color& Color::operator*(const float& a) {
// _r *= a;
// _g *= a;
// _b *= a;

// return *this;
// }

// Color operator*(const float& a, const Color& c) {
// Color res(c);
// res._r *= a;
// res._g *= a;
// res._b *= a;

// return res;
// }

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

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

class Color {
private:
int _r, _g, _b;
public:
Color();
Color(int, int, int);
virtual ~Color();
Color(const Color&);
void set_color(int, int, int);
int get_r() const;
int get_g() const;
int get_b() const;
Color& operator=(const Color &other);
Color& operator+(const Color& other);
Color scalar_product(float a);
// Color& operator*(const float& a);

// friend Color operator*(const float& a, const Color& c);
bool operator==(const Color& c);
friend std::ostream& operator<<(std::ostream& os, const Color& c);
friend std::istream& operator>>(std::istream& is, Color& c);
};

#endif
36 changes: 32 additions & 4 deletions VJ-Raghvan/image.cpp → Base/image.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#include <iostream>
#include <algorithm>
#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();
set_arr();
max_pixel = 255;
_encoding = "P3";
}
Image::Image(){
_encoding = "P3";
}
Image::Image(const Image& im) {
_w = im._w; _h = im._h;
set_arr();
for(int i = 0; i < _h; i++) {
for(int j = 0; j < _w; j++) {
_arr[i][j] = Pixel(im._arr[i][j]);
}
}
}
Image::Image(){}
Image::~Image() {if(!_arr)delete _arr;}

void Image::set_arr() {
Expand All @@ -20,13 +33,28 @@ void Image::set_arr() {
}
}

void Image::set_arr2(Pixel** a) {
for(int i = 0; i < _h; i++) {
for(int j = 0; j < _w; j++) {
_arr[i][j] = a[i][j];
}
}
}
void Image::set_pixel(int i, int j, int r, int g, int b) {
_arr[i][j].set_color(r, g, b);
}
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);
}
}
}

int Image::get_w() const {return _w;}
int Image::get_h() const {return _h;}
Pixel** Image::get_arr() const {return _arr;}

Color Image::color_at(int x, int y) const{
return _arr[x][y].get_color();
}
Expand Down
10 changes: 9 additions & 1 deletion VJ-Raghvan/image.h → Base/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
#define IMAGE_H

class Image {
protected:
int _w, _h, max_pixel;
std::string _encoding;
Pixel** _arr;
public:
Image(int w, int h);
Image();
Image(const Image& i);
virtual ~Image();
Color color_at(int x, int y) const;
int get_w() const;
int get_h() const;
Pixel** get_arr() const;

void set_pixel(int i, int j, int r, int g, int b);
Color color_at(int i, int j) const;
void filter(const Color& c, float a);
void set_arr();
void set_arr2(Pixel**);
friend std::ostream& operator<<(std::ostream& os, const Image& i);
friend std::istream& operator>>(std::istream& in, Image& i);
};
Expand Down
49 changes: 49 additions & 0 deletions Base/pixel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include "color.h"
#include "pixel.h"

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;}
int Pixel::get_brightness() const {return (_c.get_r() + _c.get_g() + _c.get_b())/3;}

bool Pixel::get_min_blue() const {
return min_blue;
}

void Pixel::set_min_blue(bool m) {
min_blue = m;
}

void Pixel::set_color(int r, int g, int b) {
_c.set_color(r, g, b);
}
void Pixel::set_color(const Color& c) {
_c = c;
}
void Pixel::set_loc(int x, int y) {
_x = x;
_y = y;
}
void Pixel::apply_filter(const Color& c, float a) {
_c.set_color(static_cast<int>( (1-a)*_c.get_r() + a*c.get_r() ),
static_cast<int>( (1-a)*_c.get_g() + a*c.get_g() ),
static_cast<int>( (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;
}
26 changes: 26 additions & 0 deletions Base/pixel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef PIXEL_H
#define PIXEL_H

class Pixel {
int _x, _y;
Color _c;
bool min_blue;
public:
Pixel();
virtual ~Pixel();
Pixel(const Pixel& p);
Color get_color () const;
int get_brightness() const;
bool get_min_blue() const;

void set_min_blue(bool m);
void set_color(int r, int g, int b);
void set_color(const Color& c);
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
60 changes: 0 additions & 60 deletions Color.cpp

This file was deleted.

Loading