HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

PPM File Modifier

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileppmmodifier

Problem

I've been working in C++ for the better part of this year and am looking to improve my style and efficiency as much as possible. One thing I've been told is to work on commenting code to make it clearer, so if you have any suggestions about where I could use a comment to clarify what is going on I would be very glad to hear them.

A guide to how PPM is structured can be found here for those unfamiliar with the file type.

Pixel.h

#ifndef __PIXEL_H_INCLUDED__
#define __PIXEL_H_INCLUDED__

#include 

struct Pixel {                                                                  
    public:                                                                       
        int red;                                                                    
        int green;                                                                  
        int blue;                                                                   
        Pixel(): Pixel(0, 0, 0) {};                                                 
        Pixel(int _r, int _g, int _b): red(_r), green(_g), blue(_b) {};
};

std::ifstream& operator >> (std::ifstream& fin, Pixel& temp) {
    fin >> temp.red;
    fin >> temp.green;
    fin >> temp.blue;
    return fin;
}

std::ofstream& operator << (std::ofstream& fout, Pixel& temp) {                            
    fout << temp.red << " ";                                                      
    fout << temp.green << " ";                                                    
    fout << temp.blue;                                                            
    return fout;                                                                  
}

#endif


PortablePixelMap.h

```
#ifndef __PICTURE_H_INCLUDED__
#define __PICTURE_H_INCLUDED__

#include
#include
#include

class PortablePixelMap {
private:
std::vector > picture_dat

Solution

Include guards

  • The include guards must not start with an underscore.



  • Their names should be prefixed with your library name, e.g. PPM_PIXEL_H.



  • Their names must correspond to the name of the file that defines them.



Efficiency

  • Use std::swap in the FlipX method.



Names

  • Consider renaming the FlipX and FlipY methods. I would have named them the exact opposite, hence you should look at other libraries or image editing programs how they name these operations.



  • Don't name function parameters temp.



Comments

  • The code is so clearly structured that it doesn't need any comments.



Code organization

  • The operator



  • The I/O functions from main.cpp should be extracted to ppm_io.cpp`, since they are declared in the header file.

Context

StackExchange Code Review Q#146831, answer score: 2

Revisions (0)

No revisions yet.