Mirroring a Vector of Vectors

Hi all. I've been tasked with reading and modifying a PPM file. The problem I've run into is being able to flip the image across the y-axis. An example vector would look like this:

< <0,0,0>, <255,255,255>, <111,111,111>, <33, 33, 33>, <43, 43, 43>, <90, 90, 90> >

And, with a width of 2 and a height of 3, it would have this structure when displayed in an image viewer:

< 0, 0, 0> <255,255,255>
<111,111,111> < 33, 33, 33>
< 43, 43, 43> < 90, 90, 90>

How would I flip the one dimensional version around the y-axis?

P.S: Sorry about the bad question. I've been dealing with this problem for awhile now.

EDIT: The vectors are in RGB format.
Last edited on
How would I flip the one dimensional version around the y-axis?


For example :
< 0, 0, 0> <255,255,255>
<111,111,111> < 33, 33, 33>
< 43, 43, 43> < 90, 90, 90>

How would the result be after you flip it?
@dragonStarN
The new result would be:
<255,255,255> <0,0,0>
< 33, 33, 33> <111,111,111>
< 43, 43, 43> <90,90,90>
<255,255,255> <0,0,0>
< 33, 33, 33> <111,111,111>
< 43, 43, 43> <90,90,90>


Should it be :
<255,255,255> <0,0,0>
< 33, 33, 33> <111,111,111>
<90,90,90> < 43, 43, 43>

?
Oh yes. Sorry about that.
Flipping the image across the y-axis is just reversing the positions of pixels in each row, isn't it?

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" ;
    }
}

http://coliru.stacked-crooked.com/a/79469b4bc2ee216b
Topic archived. No new replies allowed.