2016-12-07 37 views
-1

所以,我有一個ppm文件,並在ppmformat.cpp,我有imaging :: component_t * buffer = new imaging :: component_t [3 * numCol * numRow];。我用這個聲明什麼?我想要一個緩衝區(3 * numCol * numRow)的大小,但我不明白什麼component_t的使用?ppm文件,對象聲明到一個組件

class Color.h 
    #include <ostream> 
    #ifndef _COLOR 
    #define _COLOR 


    namespace imaging 
    { 
    /*! An alias for the floating point representation of color components (32bit per color channel). 
     * 
     * Each color channel goes from 0.0f (darkness) to 1.0f (full color brightness). 
     * For example, bright red is (1,0,0), white is (1,1,1), magenta is (1,0,1) etc. 
    */ 
    typedef float component_t; 

    /*! Represents a triplet of Red, Green, Blue (RGB) values. 
    */ 
    class Color 
    { 
    public: 
    // members 
    component_t r, //! The red color channel (component) 
     g, //! The green color channel (component) 
     b; //! The blue color channel (component) 

      // member functions 

      /*! This operator returns the index-th component of the image. 
      * 
      * For speed, no bounds for index values are checked. 
      * 
      * \param index is the index of the component to obtain. Values should be 0, 1 or 2. 
      * 
      * \return a reference to the respective color component. 
      */ 
     component_t & operator [] (size_t index) 
     { 
     return *(&r + index); 
     } 

     /*! Addition operator. 
     * 
     * Adds a color to the current one and returns the result. 
     * 
     * \param right is the right Color operand of the + sign. 
     * 
     * \return the resulting color after the component-wise addition of the two colors. 
     */ 
     Color operator + (Color & right) 
     { 
     Color left; 
     left.r = r + right.r; 
     left.g = g + right.g; 
     left.b = b + right.b; 
     return left; 
     } 

     // constructors 

    /*! Parameterized constructor. 
    * 
    * \param r is the red component of the color. 
    * \param g is the green component of the color. 
    * \param b is the blue component of the color. 
    */ 
    Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {} 

    /*! Default constructor. 
    * 
    * All components set to zero, i.e. a black color. 
    */ 
    Color() : r(0), g(0), b(0) {} 
    }; 
    } 

    #endif _COLOR 

回答

0

您可以看到component_t是float數據類型的別名。所以在聲明中使用float還是component_t沒有區別。

typedef float component_t; 

但我建議您使用component_t代表一個顏色組件,因此它使您的代碼更具可讀性。

+0

所以,就像使用float * buffer = new float [3 * numCol * numRow]一樣? 和緩衝區必須採取浮動值? – madrugadas25845

+0

是的,它是相同的,但使用component_t將使您的代碼更具可讀性。 –

+0

非常感謝 – madrugadas25845