Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.05 KB | None | 0 0
  1. #include "./lib/lodepng.h"
  2. #include "imgToArr.cpp"
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6. #include <bits/stdc++.h>
  7.  
  8. //g++ ./primis.cpp ./lib/lodepng.cpp -I./ -ansi -pedantic -Wall -Wextra -O3 -std=c++14 -o clbl
  9.  
  10. using namespace std;
  11. typedef char byte;
  12.  
  13.  
  14. int avg(pixel pix)
  15. {
  16.     int
  17.         r = pix.r,
  18.         g = pix.g,
  19.         b = pix.b;
  20.  
  21.     return (r+g+b)/3;
  22. }
  23.  
  24. struct coord{
  25.     int x;
  26.     int y;
  27. };
  28.  
  29. vector<coord> getPreCoords(coord size, coord currentCoord){
  30.     vector<coord> outCoords;
  31.     for(int x = currentCoord.x; x <= currentCoord.x+1; ++x)
  32.         for(int y = currentCoord.y; y <= currentCoord.y+1; ++y)
  33.             if(x < size.x && y < size.y)
  34.                 outCoords.push_back({x, y});
  35.     return outCoords;
  36. }
  37.  
  38. struct color {
  39.     unsigned r, g, b;
  40.     static color avg(vector<color>);
  41.     static color fromPixel(pixel pix);
  42.     color operator+(color other);
  43.     color operator/(int div);
  44.    
  45. };
  46.  
  47. color color::operator+(color other)
  48. {
  49.     return {other.r+r, other.g+g, other.b+b};
  50. }
  51.  
  52. color color::fromPixel(pixel pix)
  53. {
  54.     return {pix.r, pix.g, pix.b};
  55. }
  56.  
  57. color color::operator/(int div)
  58. {
  59.     return {r/div, g/div, b/div};
  60. }
  61.  
  62. color color::avg(vector<color> colors)
  63. {
  64.     color result;
  65.     unsigned count = 0;
  66.  
  67.     for(auto color:colors)
  68.     {
  69.         result = result + color;
  70.         count++;
  71.     }
  72.  
  73.     result = result/count;
  74.     return result;
  75. }
  76.  
  77. ///
  78. color findAvgCol(Frame *img, vector<coord> coords)
  79. {
  80.     vector<color> colors;
  81.     for(auto coord:coords)
  82.     {
  83.         pixel *temp = img->getPixelAt(coord.x, coord.y);
  84.         color temppix = color::fromPixel(*temp);
  85.         colors.push_back(temppix);
  86.     }
  87.     return color::avg(colors);
  88. }
  89.  
  90. struct node {
  91.     //координаты указывающие на точки прошлого слоя
  92.     vector<coord> preCoords;
  93.     //средний цвет юнита
  94.     color avgCol = {0, 0, 0};
  95. };
  96.  
  97. struct Layer {
  98.     vector<vector<node>> layer;
  99.     int width, height;
  100.     int deep = 0;
  101.     Layer *past = nullptr;
  102.     static Layer fromFrame(Frame *);
  103.     static Layer fromLayer(Layer *);
  104.     vector<coord> getAllUnder(coord, vector<Layer>);
  105. };
  106.  
  107. vector<coord> Layer::getAllUnder(coord point, vector<Layer> all)
  108. {
  109.     int curentDeep = deep;
  110.     cout<<"curentDeep: "<<curentDeep <<endl;
  111.     cout<<"deep: "<<deep <<endl;
  112.  
  113.     Layer currentLayer = all[curentDeep];
  114.  
  115.     vector<coord> pointerCoords = layer[point.x][point.y].preCoords;
  116.  
  117.     while(curentDeep > 0){
  118.         cout<<"usize: "<<pointerCoords.size()<<endl;
  119.         currentLayer = all[--curentDeep];
  120.         cout<<curentDeep<<endl;
  121.         vector<coord> newCoords;
  122.         for(coord one:pointerCoords)
  123.         {
  124.             vector<coord> buffer = currentLayer.layer[one.x][one.y].preCoords;
  125.             newCoords.insert(newCoords.end(), buffer.begin(), buffer.end());
  126.         }
  127.         pointerCoords = newCoords;
  128.  
  129.     }
  130.  
  131.     return pointerCoords;
  132. }
  133.  
  134. color findAvgColL(Layer *img, vector<coord> coords)
  135. {
  136.     vector<color> colors;
  137.     for(auto coord:coords)
  138.     {
  139.         color temp = img->layer[coord.x][coord.y].avgCol;
  140.         colors.push_back(temp);
  141.     }
  142.     return color::avg(colors);
  143. }
  144. ////
  145.  
  146. Layer Layer::fromFrame(Frame *main)
  147. {
  148.     int width = main->width/2 + main->width%2;
  149.     int height = main->height/2 + main->height%2;
  150.     int pastWidth = main->width;
  151.     int pastHeight = main->height;
  152.  
  153.     vector<vector<node>> layer;
  154.     layer.resize(width+1, vector<node>(height+1));
  155.  
  156.     for(int x = 0; x < width; x++)
  157.     for(int y = 0; y < height; y++)
  158.     {
  159.         auto pastCoords = getPreCoords({pastWidth, pastHeight}, {x*2, y*2});
  160.         if(pastCoords.size()>4)
  161.             cout<<"AAAAAAAAAAAAAAAAAAAAAA"<<endl;
  162.  
  163.         layer[x][y] = node{pastCoords, findAvgCol(main, pastCoords)};
  164.     }
  165.    
  166.     return Layer {
  167.         layer,
  168.         width,
  169.         height,
  170.         0,
  171.         nullptr
  172.     };
  173. }
  174.  
  175. Layer Layer::fromLayer(Layer *past)
  176. {
  177.     int deep = past->deep + 1;
  178.     int width = past->width/2 + past->width%2;
  179.     int height = past->height/2 + past->height%2;
  180.     int pastWidth = past->width;
  181.     int pastHeight = past->height;
  182.  
  183.     vector<vector<node>> layer;
  184.     layer.resize(width+1, vector<node>(height+1));
  185.    
  186.     for(int x = 0; x < width; x++)
  187.     for(int y = 0; y < height; y++)
  188.     {
  189.         auto pastCoords = getPreCoords({pastWidth, pastHeight}, {x*2, y*2});
  190.         if(pastCoords.size()>4)
  191.             cout<<"AAAAAAAAAAAAAAAAAAAAAA"<<endl;
  192.         layer[x][y] = node{pastCoords, findAvgColL(past, pastCoords)};
  193.     }
  194.  
  195.     return Layer {
  196.         layer,
  197.         width,
  198.         height,
  199.         deep,
  200.         past
  201.     };
  202. }
  203.  
  204. int main(int argc, char *argv[])
  205. {
  206.     Frame *image;
  207.     char const *out = "out.png";
  208.     char const *inFilename;//= "/home/fulcanelly/cod/cpp/img/pyyramid/pack.png";
  209.  
  210.     if(argc<2)
  211.         exit(1);
  212.     inFilename = argv[1];
  213.  
  214.     image = decodeOneStep(inFilename);
  215.     image->toPixelArray();
  216.  
  217.     auto base = Layer::fromFrame(image);
  218.     //collecting pyramid
  219.     vector<Layer> pyramid;
  220.     pyramid.push_back(Layer::fromLayer(&base));
  221.    
  222.     int lastIndex;
  223.     do {
  224.         lastIndex = pyramid.size()-1;
  225.         pyramid.push_back(Layer::fromLayer(&pyramid[lastIndex]));
  226.     } while (
  227.         pyramid[lastIndex].width > 2 ||
  228.         pyramid[lastIndex].height > 2
  229.     ); //add layers until it's expression is not equal to false
  230.    
  231.     ///
  232.     cout << "last w: " << pyramid[lastIndex].width << endl;
  233.     cout << "last h: " << pyramid[lastIndex].height << endl;
  234.  
  235.     vector<coord> points = pyramid[lastIndex].getAllUnder({0, 0}, pyramid);
  236.     cout<< "size : "<<points.size()<<endl;
  237.     cout<< "size : "<<pyramid.size()<<endl;
  238.  
  239.     Frame *outImage = decodeOneStep(inFilename);
  240.     outImage->toPixelArray();
  241.    
  242.    
  243.     auto col =  pyramid[lastIndex].layer[0][0].avgCol;
  244.     for(coord pix:points)
  245.     {
  246.         delete outImage->arr[pix.x][pix.y];
  247.         outImage->arr[pix.x][pix.y] = new pixel{col.r,col.g,col.b,255};
  248.     }
  249.  
  250.     outImage->save(out);
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement