Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "./lib/lodepng.h"
- #include "imgToArr.cpp"
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <bits/stdc++.h>
- //g++ ./primis.cpp ./lib/lodepng.cpp -I./ -ansi -pedantic -Wall -Wextra -O3 -std=c++14 -o clbl
- using namespace std;
- typedef char byte;
- int avg(pixel pix)
- {
- int
- r = pix.r,
- g = pix.g,
- b = pix.b;
- return (r+g+b)/3;
- }
- struct coord{
- int x;
- int y;
- };
- vector<coord> getPreCoords(coord size, coord currentCoord){
- vector<coord> outCoords;
- for(int x = currentCoord.x; x <= currentCoord.x+1; ++x)
- for(int y = currentCoord.y; y <= currentCoord.y+1; ++y)
- if(x < size.x && y < size.y)
- outCoords.push_back({x, y});
- return outCoords;
- }
- struct color {
- unsigned r, g, b;
- static color avg(vector<color>);
- static color fromPixel(pixel pix);
- color operator+(color other);
- color operator/(int div);
- };
- color color::operator+(color other)
- {
- return {other.r+r, other.g+g, other.b+b};
- }
- color color::fromPixel(pixel pix)
- {
- return {pix.r, pix.g, pix.b};
- }
- color color::operator/(int div)
- {
- return {r/div, g/div, b/div};
- }
- color color::avg(vector<color> colors)
- {
- color result;
- unsigned count = 0;
- for(auto color:colors)
- {
- result = result + color;
- count++;
- }
- result = result/count;
- return result;
- }
- ///
- color findAvgCol(Frame *img, vector<coord> coords)
- {
- vector<color> colors;
- for(auto coord:coords)
- {
- pixel *temp = img->getPixelAt(coord.x, coord.y);
- color temppix = color::fromPixel(*temp);
- colors.push_back(temppix);
- }
- return color::avg(colors);
- }
- struct node {
- //координаты указывающие на точки прошлого слоя
- vector<coord> preCoords;
- //средний цвет юнита
- color avgCol = {0, 0, 0};
- };
- struct Layer {
- vector<vector<node>> layer;
- int width, height;
- int deep = 0;
- Layer *past = nullptr;
- static Layer fromFrame(Frame *);
- static Layer fromLayer(Layer *);
- vector<coord> getAllUnder(coord, vector<Layer>);
- };
- vector<coord> Layer::getAllUnder(coord point, vector<Layer> all)
- {
- int curentDeep = deep;
- cout<<"curentDeep: "<<curentDeep <<endl;
- cout<<"deep: "<<deep <<endl;
- Layer currentLayer = all[curentDeep];
- vector<coord> pointerCoords = layer[point.x][point.y].preCoords;
- while(curentDeep > 0){
- cout<<"usize: "<<pointerCoords.size()<<endl;
- currentLayer = all[--curentDeep];
- cout<<curentDeep<<endl;
- vector<coord> newCoords;
- for(coord one:pointerCoords)
- {
- vector<coord> buffer = currentLayer.layer[one.x][one.y].preCoords;
- newCoords.insert(newCoords.end(), buffer.begin(), buffer.end());
- }
- pointerCoords = newCoords;
- }
- return pointerCoords;
- }
- color findAvgColL(Layer *img, vector<coord> coords)
- {
- vector<color> colors;
- for(auto coord:coords)
- {
- color temp = img->layer[coord.x][coord.y].avgCol;
- colors.push_back(temp);
- }
- return color::avg(colors);
- }
- ////
- Layer Layer::fromFrame(Frame *main)
- {
- int width = main->width/2 + main->width%2;
- int height = main->height/2 + main->height%2;
- int pastWidth = main->width;
- int pastHeight = main->height;
- vector<vector<node>> layer;
- layer.resize(width+1, vector<node>(height+1));
- for(int x = 0; x < width; x++)
- for(int y = 0; y < height; y++)
- {
- auto pastCoords = getPreCoords({pastWidth, pastHeight}, {x*2, y*2});
- if(pastCoords.size()>4)
- cout<<"AAAAAAAAAAAAAAAAAAAAAA"<<endl;
- layer[x][y] = node{pastCoords, findAvgCol(main, pastCoords)};
- }
- return Layer {
- layer,
- width,
- height,
- 0,
- nullptr
- };
- }
- Layer Layer::fromLayer(Layer *past)
- {
- int deep = past->deep + 1;
- int width = past->width/2 + past->width%2;
- int height = past->height/2 + past->height%2;
- int pastWidth = past->width;
- int pastHeight = past->height;
- vector<vector<node>> layer;
- layer.resize(width+1, vector<node>(height+1));
- for(int x = 0; x < width; x++)
- for(int y = 0; y < height; y++)
- {
- auto pastCoords = getPreCoords({pastWidth, pastHeight}, {x*2, y*2});
- if(pastCoords.size()>4)
- cout<<"AAAAAAAAAAAAAAAAAAAAAA"<<endl;
- layer[x][y] = node{pastCoords, findAvgColL(past, pastCoords)};
- }
- return Layer {
- layer,
- width,
- height,
- deep,
- past
- };
- }
- int main(int argc, char *argv[])
- {
- Frame *image;
- char const *out = "out.png";
- char const *inFilename;//= "/home/fulcanelly/cod/cpp/img/pyyramid/pack.png";
- if(argc<2)
- exit(1);
- inFilename = argv[1];
- image = decodeOneStep(inFilename);
- image->toPixelArray();
- auto base = Layer::fromFrame(image);
- //collecting pyramid
- vector<Layer> pyramid;
- pyramid.push_back(Layer::fromLayer(&base));
- int lastIndex;
- do {
- lastIndex = pyramid.size()-1;
- pyramid.push_back(Layer::fromLayer(&pyramid[lastIndex]));
- } while (
- pyramid[lastIndex].width > 2 ||
- pyramid[lastIndex].height > 2
- ); //add layers until it's expression is not equal to false
- ///
- cout << "last w: " << pyramid[lastIndex].width << endl;
- cout << "last h: " << pyramid[lastIndex].height << endl;
- vector<coord> points = pyramid[lastIndex].getAllUnder({0, 0}, pyramid);
- cout<< "size : "<<points.size()<<endl;
- cout<< "size : "<<pyramid.size()<<endl;
- Frame *outImage = decodeOneStep(inFilename);
- outImage->toPixelArray();
- auto col = pyramid[lastIndex].layer[0][0].avgCol;
- for(coord pix:points)
- {
- delete outImage->arr[pix.x][pix.y];
- outImage->arr[pix.x][pix.y] = new pixel{col.r,col.g,col.b,255};
- }
- outImage->save(out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement