我有一个练习,我需要读取PPM文件并将其内容保存为C中的图像 . 然后我想计算平均亮度 . 我有3个头文件 . 我设法读取文件并检查P6值,宽度,高度和深度 . 如何从文件中提取图像数据以制作图像对象以计算平均亮度?

Color.h

namespace imaging 
{
    /*! An alias for the floating point representation of color components (32bit per color channel).
     */
    typedef float component_t;


    class Color 
    {
    public: 
        // members
        component_t r, 
                    g, 
                    b; 

        // member functions


        component_t & operator [] (size_t index)
        {
            return *(&r + index);
        }


        Color operator + (Color & right)
        {
            Color left;
            left.r = r + right.r;
            left.g = g + right.g;
            left.b = b + right.b;
            return left;
        }

        // constructors


        Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {}

        Color() : r(0), g(0), b(0) {}
    };

image.h的

namespace imaging {
class Image
    {
    public:
        enum channel_t {RED=0,GREEN, BLUE};                                                          
    protected:
        component_t * buffer;                        

        unsigned int width,                          
                     height;                         

    public:
        // metric accessors


        const unsigned int getWidth() const {return width;}      


        const unsigned int getHeight() const {return height;}   

        // data accessors


        component_t * getRawDataPtr();                           


        Color getPixel(unsigned int x, unsigned int y) const;

        // data mutators


        void setPixel(unsigned int x, unsigned int y, Color & value); 


        void setData(const component_t * & data_ptr);            


        void resize(unsigned int new_width, unsigned int new_height);

        // constructors and destructor

        /*! Default constructor.

         */
        Image();                                                    

        /*! Constructor with width and height specification.
         * 


         */
        Image(unsigned int width, unsigned int height);

        // Constructor with data initialization

        Image(unsigned int width, unsigned int height, const component_t * data_ptr);

        /*! Copy constructor.

         */
        Image(const Image &src);

        /*! The Image destructor.

         */
        ~Image();

        /*! Copy assignment operator.*/

        Image & operator = (const Image & right);

    };
}

ppm_format.h

namespace imaging
{
    /*! Reads a PPM image file and returns a pointer to a newly allocated Image object containing the image. 
     */
    Image * ReadPPM(const char * filename);


} //namespace imaging

我为读取PPM文件方法编写了这段代码:

Image * ReadPPM(const char* filename) {
ifstream ifs;
            string v1; //for the P6 value
            int v2,v3,v4; //for width, height and color depth
            cin.getline(filename, 20);

            ifs.open(filename,ios::binary);  
            if (ifs.is_open()) {

                    while (ifs.good()){
                        if (ifs>>v1>>v2>>v3>>v4){
                        int height = v2;
                        int width = v3;
                        if (v1!="P6") {
                            cout<<"Unsupported type of ppm file(not P6)"<<endl;
                            ifs.close();
                            exit(0);
                        }

                        if (v2<1){
                            cout<<"Unsupported width"<<endl;
                            ifs.close();
                            exit(0);
                        }
                        if (v3<1){
                            cout<<"Unsupported height"<<endl;
                            ifs.close();
                            exit(0);
                        }

                        if (v4<1 || v4>255){
                            cout<<"Unsupported number of bits"<<endl;
                            ifs.close();
                            exit(0);
                        }

                        //cout<<v1<<endl<<height<<endl<<width;

                        } 

                    }
            }


            else if (!ifs.is_open()) {
                    cout<<"The file with name: "<<filename<<" does not exist";
                    exit(EXIT_FAILURE);
                }
            ifs.close(); 
            // end of reading the file

             //start making the image

            Image image = new Image(v2,v3);
            return image;

}

我写了这段代码来读取和保存图像数据 . 问题是我想将图像数据提取为component_t类型(浮点数) .
char* data = new char[3*height*width]; int bytesPerLn = width*3; for (int i=0;i<height;i++){ char* tmp = new char[bytesPerLn]; ifs.getline(tmp,bytesPerLn); cout<<tmp; strcat(data,tmp); } cout<<data;