1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <vector>
#include <array>
#include <iomanip>
#include <cassert>
#include <algorithm>
using pixel = std::array<int,3> ;
std::ostream& operator<< ( std::ostream& stm, const pixel& p )
{
return stm << '<' << std::setw(3) << p[0] << ','
<< std::setw(3) << p[1] << ','
<< std::setw(3) << p[2] << '>' ;
}
struct image
{
std::size_t width ;
std::vector<pixel> pixels ;
std::size_t num_rows() const { assert(width) ; return pixels.size() / width ; }
using iterator = std::vector<pixel>::iterator ;
using const_iterator = std::vector<pixel>::const_iterator ;
std::pair<iterator,iterator> operator[] ( std::size_t row_number )
{
assert( row_number < num_rows() ) ;
return { pixels.begin() + row_number*width, pixels.begin() + row_number*width + width } ;
}
std::pair<const_iterator,const_iterator> operator[] ( std::size_t row_number ) const
{
assert( row_number < num_rows() ) ;
return { pixels.begin() + row_number*width, pixels.begin() + row_number*width + width } ;
}
image& reflect() // reverse each row
{
for( std::size_t i = 0 ; i < num_rows() ; ++i )
{
auto row = (*this)[i] ;
std::reverse( row.first, row.second ) ;
}
return *this ;
}
};
std::ostream& operator<< ( std::ostream& stm, const image& img )
{
for( std::size_t i = 0 ; i < img.num_rows() ; ++i )
{
const auto row = img[i] ;
for( auto iter = row.first ; iter != row.second ; ++iter ) stm << *iter << ' ' ;
std::cout << '\n' ;
}
return stm ;
}
image reflect( image img ) { return img.reflect() ; }
int main()
{
image img = {
4, // width
{ {0,0,0}, {1,1,1}, {2,2,2}, {3,3,3}, {4,4,4}, {5,5,5},
{6,6,6}, {7,7,7}, {8,8,8}, {9,9,9}, {2,4,8}, {3,6,9} } // pixels
};
for( std::size_t w : { 2, 3, 4, 6 } )
{
img.width = w ;
std::cout << "width: " << img.width << "\noriginal:\n----------\n" << img << "\n\n" ;
image reflected = reflect(img) ;
std::cout << "reflected:\n----------\n" << reflected << "\n\n.......................\n\n" ;
}
}
|