Skip to content

Instantly share code, notes, and snippets.

@ShiedaKayn1975
Created June 30, 2020 10:17
Show Gist options
  • Save ShiedaKayn1975/e6bc7614261e48573b98fb21d7e3db90 to your computer and use it in GitHub Desktop.
Save ShiedaKayn1975/e6bc7614261e48573b98fb21d7e3db90 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#define BYTE unsigned char
class Pixel{
public:
BYTE R,G,B;
public:
void setR(BYTE r){
this-> R = r;
}
void setG(BYTE g){
this-> G = g;
}
void setB(BYTE b){
this-> B = b;
}
BYTE getR() const{
return this->R;
}
BYTE getG() const{
return this->G;
}
BYTE getB() const{
return this->B;
}
public:
Pixel();
Pixel(BYTE r,BYTE g,BYTE b);
Pixel(int value);
int GrayScale() const;
friend Pixel operator+(const Pixel &left,const Pixel &right);
friend ostream &operator<<(ostream &left,const Pixel &right);
void operator=(const Pixel &left);
Pixel operator()(int x, int y);
};
Pixel Pixel::operator()(int x,int y){
Pixel val;
val.setR(10);
val.setG(10);
val.setB(10);
return val;
}
Pixel::Pixel(){
this->R = 0;
this->G = 0;
this->B = 0;
}
Pixel::Pixel(BYTE r,BYTE g,BYTE b){
this->R = r;
this->G = g;
this->B = b;
}
int Pixel::GrayScale() const{
return int((2*this->R + 3*this->G + this->B)/8);
}
Pixel::Pixel(int value){
this->B = value%256;
this->G = ((value-B)/256)%256;
this->R = ((value - B - 256*G)/(256*256))%256;
}
Pixel operator+(const Pixel &left,const Pixel &right){
Pixel val;
val.setR(left.getR() + right.getR());
val.setG(left.getG() + right.getG());
val.setB(left.getB() + right.getB());
return val;
}
void Pixel::operator=(const Pixel &left){
this->setR(left.getR());
this->setG(left.getG());
this->setB(left.getB());
}
ostream &operator<<(ostream &left,const Pixel &right){
left << "R:" << int(right.getR()) << " G:" << int(right.getG()) << " B:" << int(right.getB()) << endl;
return left;
}
///////////////////////class bitmap
class Bitmap{
int width,height;
Pixel **data;
void freeData();
void createData(int w,int h);
public:
~Bitmap(){freeData();}
Bitmap();
Bitmap(int width,int height);
Bitmap(const Bitmap &bmp);
int getWidth() const;
int getHeight() const;
Pixel** getData() const;
Bitmap Crop(int x,int y,int w,int h);
int *Histogram();
Pixel &operator()(int x,int y);
Bitmap operator=(const Bitmap &bmp);
};
Pixel &Bitmap::operator()(int x,int y){
return this->getData()[x][y];
}
Pixel** Bitmap::getData() const{
return this->data;
}
void Bitmap::freeData(){
if (data == 0) return;
for(int i =0;i< height;i++){
delete[] data[i];
}
delete[] data;
}
void Bitmap::createData(int w,int h){
this->width = w;
this->height = h;
data = new Pixel *[height];
for(int i = 0;i< height;i++){
data[i] = new Pixel[width];
}
}
Bitmap::Bitmap(){
createData(1,1);
}
Bitmap::Bitmap(int w,int h){
createData(w,h);
}
int Bitmap::getWidth() const{
return this->width;
}
int Bitmap::getHeight() const{
return this->height;
}
Bitmap::Bitmap(const Bitmap &bmp){
int w = bmp.getWidth();
int h = bmp.getHeight();
createData(bmp.getWidth(),bmp.getHeight());
for(int i =0;i< w ;i++){
for(int j = 0;j< h;j++){
this->getData()[i][j] = bmp.getData()[i][j];
}
}
}
Bitmap Bitmap::operator=(const Bitmap &bmp){
int w = bmp.getWidth();
int h = bmp.getHeight();
createData(bmp.getWidth(),bmp.getHeight());
for(int i =0;i< w ;i++){
for(int j = 0;j< h;j++){
this->getData()[i][j] = bmp.getData()[i][j];
}
}
return *this;
}
Bitmap Bitmap::Crop(int x,int y,int w,int h){
Bitmap res;
res.createData(w,h);
for(int i = x;i< this->getWidth();i++){
for(int j = y;j < this->getHeight();j++){
res.getData()[i-x][j-y] = this->getData()[i][j];
}
}
return res;
}
int* Bitmap::Histogram(){
int *histogram = new int[256];
for(int i = 0;i< 256;i++){
histogram[i] = 0;
}
for(int i =0;i< this->getWidth();i++){
for(int j = 0;j< this->getHeight();j++){
Pixel x = this->getData()[i][j];
histogram[x.getR()] +=1;
histogram[x.getG()] +=1;
histogram[x.getB()] +=1;
}
}
return histogram;
}
int main()
{
Pixel p(0,60,120);
cout << p + Pixel(10,10,10) << endl;
Bitmap A;
Bitmap B(100,100);
for(int y = 0;y< B.getHeight();y++){
for(int x = 0;x < B.getWidth();x++){
B(x,y) = Pixel(rand());
}
}
A = B;
int * hist = A.Histogram();
for(int i = 0;i< 256;i++){
cout << hist[i] << endl;
}
delete[] hist;
Bitmap C(B.Crop(20,20,40,60));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment