Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ppm文件,组件的对象声明_C++_File_Object_Ppm - Fatal编程技术网

C++ ppm文件,组件的对象声明

C++ ppm文件,组件的对象声明,c++,file,object,ppm,C++,File,Object,Ppm,因此,我有一个ppm文件,在ppmformat.cpp中,我有imaging::component_t*buffer=new imaging::component_t[3*numCol*numRow]。我用这个申报什么?我想要一个大小为(3*numCol*numRow)的缓冲区,但我不明白组件的用途是什么 class Color.h #include <ostream> #ifndef _COLOR #define _COLOR nam

因此,我有一个ppm文件,在ppmformat.cpp中,我有imaging::component_t*buffer=new imaging::component_t[3*numCol*numRow]。我用这个申报什么?我想要一个大小为(3*numCol*numRow)的缓冲区,但我不明白组件的用途是什么

  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
class Color.h
#包括
#ifndef\u颜色
#定义颜色
名称空间映像
{
/*!颜色组件的浮点表示形式的别名(每个颜色通道32位)。
*
*每个颜色通道从0.0f(黑暗)到1.0f(全色亮度)。
*例如,鲜红色是(1,0,0),白色是(1,1,1),品红色是(1,0,1)等等。
*/
类型定义浮动组件;
/*!表示红、绿、蓝(RGB)值的三元组。
*/
类颜色
{
公众:
//成员
组件\u t r,//!红色通道(组件)
g、 //!绿色通道(组件)
b、 //!蓝色通道(组件)
//成员函数
/*!此运算符返回图像的索引th组件。
*
*对于速度,不检查索引值的边界。
*
*\param index是要获取的组件的索引。值应为0、1或2。
*
*\返回对相应颜色组件的引用。
*/
组件和运算符[](大小索引)
{
返回*(&r+索引);
}
/*!加法运算符。
*
*将颜色添加到当前颜色并返回结果。
*
*\param right是+号的右颜色操作数。
*
*\按组件添加两种颜色后返回结果颜色。
*/
颜色运算符+(颜色和右侧)
{
色彩偏左;
左.r=r+右.r;
左.g=g+右.g;
左b=b+右b;
左转;
}
//建设者
/*!参数化构造函数。
*
*\param r是颜色的红色分量。
*\param g是颜色的绿色分量。
*\param b是颜色的蓝色分量。
*/
颜色(分量r,分量g,分量b):r(r),g(g),b(b){
/*!默认构造函数。
*
*所有组件设置为零,即黑色。
*/
Color():r(0),g(0),b(0){}
};
}
#endif_颜色

您可以看到组件是浮点数据类型的别名。所以在声明中使用float还是component没有区别

typedef float component_t;

但是我建议您使用表示颜色组件的组件,这样代码更可读。

所以,这与使用float buffer=new float[3*numColnumRow]是一样的?缓冲区必须采用浮点值?是的,它是相同的,但是使用组件将使代码更可读。