save(char* filename)

Discussion in 'C++' started by George, Sep 7, 2005.

  1. George

    George Guest

    This program need to draw the some triangles into a 512 × 512 buffer
    (in memory). Or save it to a file.

    #include "project3.h"


    Image::Image(int xres, int yres): xres(xres), yres(yres)
    {
    image =new Color*[yres];
    for (int i=0;i<yres;i++)
    image = new Color[xres];
    }


    Image::~Image()
    {
    if(image)
    {
    for (int i=0;i<yres;i++)
    delete[] image;

    delete[] image;
    }
    }


    void Image::save(char* filename)
    {

    }



    #ifndef IMAGE_H
    #define IMAGE_H 1

    /* A Color is a RGB float.
    **
    ** R, G, and B are all in the range [0..1]
    **
    ** This class allows you to add, subtract, and multiply colors,
    ** It also allows you to get the separate components (e.g.,
    myColor.red() ),
    ** use constructions like "myColor += yourColor;"
    */
    class Color
    {
    float r,g,b;
    public:
    inline Color(): r(0), g(0), b(0) {}
    inline Color(float r, float g, float b) : r(r), g(g), b(b){}
    inline ~Color() {}
    inline Color operator*(const Color& c) const
    {
    return Color(r*c.r, g*c.g, b*c.b);
    }
    inline Color operator+(const Color& c) const
    {
    return Color(r+c.r, g+c.g, b+c.b);
    }
    inline Color operator-(const Color& c) const
    {
    return Color(r-c.r, g-c.g, b-c.b);
    }
    inline Color operator*(float s) const
    {
    return Color(r*s, g*s, b*s);
    }
    inline Color& operator+=(const Color& c)
    {
    r+=c.r;
    g+=c.g;
    b+=c.b;
    return *this;
    }
    inline float red() const
    {
    return r;
    }
    inline float green() const
    {
    return g;
    }
    inline float blue() const
    {
    return b;
    }
    inline float luminance() const
    {
    return (float)(0.3*g + 0.6*r + 0.1*b);
    }

    inline float max_component() const
    {
    float temp = (g > r? g : r);
    return (b > temp? b : temp);
    }
    };


    /* An image is a collection of xres*yres Colors.
    **
    ** You can write to a pixel by saying "myImage(x,y) = Color(1, 0.5,
    0);"
    **
    ** You can save the entire image to a PPM file by calling
    myImage.save("output.ppm");
    */
    class Image
    {
    float* buf;
    Color** image;
    int xres, yres;
    public:
    Image(int xres, int yres);
    ~Image();
    inline int getXRes() const
    {
    return xres;
    }
    inline int getYRes() const
    {
    return yres;
    }
    inline Color& operator()(int x, int y)
    {
    return image[y][x];
    }
    void save(char* file);
    };

    #endif
    George, Sep 7, 2005
    #1
    1. Advertising

  2. George wrote:
    > This program need to draw the some triangles into a 512 × 512 buffer
    > (in memory). Or save it to a file.


    If you're seeking comments, see below. If you're not, explain what it
    is you want next time.

    >
    > #include "project3.h"
    >
    >
    > Image::Image(int xres, int yres): xres(xres), yres(yres)
    > {
    > image =new Color*[yres];
    > for (int i=0;i<yres;i++)
    > image = new Color[xres];
    > }
    >
    >
    > Image::~Image()
    > {
    > if(image)
    > {
    > for (int i=0;i<yres;i++)
    > delete[] image;
    >
    > delete[] image;
    > }
    > }


    Read about "The Rule of Three".

    > void Image::save(char* filename)


    If you intend to provide the file name as a string literal (in double
    quotes), then I strongly recommend you use 'const char*' as your argument
    instead of a pointer to non-const char:

    void Image::save(const char* filename)

    Also it would be nice if it has either 'bool' or 'int' return value to
    indicate the success or failure or threw an exception...

    > {
    >
    > }
    >
    >
    >
    > #ifndef IMAGE_H
    > #define IMAGE_H 1
    >
    > /* A Color is a RGB float.
    > **
    > ** R, G, and B are all in the range [0..1]
    > **
    > ** This class allows you to add, subtract, and multiply colors,
    > ** It also allows you to get the separate components (e.g.,
    > myColor.red() ),
    > ** use constructions like "myColor += yourColor;"
    > */
    > class Color
    > {
    > float r,g,b;
    > public:
    > inline Color(): r(0), g(0), b(0) {}
    > inline Color(float r, float g, float b) : r(r), g(g), b(b){}
    > inline ~Color() {}
    > inline Color operator*(const Color& c) const
    > {
    > return Color(r*c.r, g*c.g, b*c.b);
    > }
    > inline Color operator+(const Color& c) const
    > {
    > return Color(r+c.r, g+c.g, b+c.b);


    Watch out for overflow. If this->r is 0.8 and c.r is 0.7, the resulting
    Color will have r == 1.5, which is definitely not in the range [0..1].

    > }
    > inline Color operator-(const Color& c) const
    > {
    > return Color(r-c.r, g-c.g, b-c.b);


    Same notion here. If 'c.r' is smaller than 'this->r', you can slip into
    the negative values...

    > }
    > inline Color operator*(float s) const
    > {
    > return Color(r*s, g*s, b*s);


    Same here. No checking apparently is done. You desperately need to make
    sure the results are in the range.

    > }
    > inline Color& operator+=(const Color& c)
    > {
    > r+=c.r;
    > g+=c.g;
    > b+=c.b;


    Same here.

    > return *this;
    > }
    > inline float red() const
    > {
    > return r;
    > }
    > inline float green() const
    > {
    > return g;
    > }
    > inline float blue() const
    > {
    > return b;
    > }
    > inline float luminance() const
    > {
    > return (float)(0.3*g + 0.6*r + 0.1*b);
    > }
    >
    > inline float max_component() const
    > {
    > float temp = (g > r? g : r);
    > return (b > temp? b : temp);
    > }
    > };
    >
    >
    > /* An image is a collection of xres*yres Colors.
    > **
    > ** You can write to a pixel by saying "myImage(x,y) = Color(1, 0.5,
    > 0);"
    > **
    > ** You can save the entire image to a PPM file by calling
    > myImage.save("output.ppm");
    > */
    > class Image
    > {
    > float* buf;

    ^^^^^^^^^^^
    This member variable doesn't seem to be used...

    > Color** image;
    > int xres, yres;
    > public:
    > Image(int xres, int yres);
    > ~Image();
    > inline int getXRes() const
    > {
    > return xres;
    > }
    > inline int getYRes() const
    > {
    > return yres;
    > }
    > inline Color& operator()(int x, int y)
    > {
    > return image[y][x];
    > }
    > void save(char* file);
    > };
    >
    > #endif
    >


    V
    Victor Bazarov, Sep 7, 2005
    #2
    1. Advertising

  3. George

    George Guest

    Sorry to be confusing but I was just wondering how I should go about
    creating the void save(char* file).
    George, Sep 7, 2005
    #3
  4. George

    Puppet_Sock Guest

    George wrote:
    > Sorry to be confusing but I was just wondering how I should go about
    > creating the void save(char* file).


    When I dream, I have a pony.
    Socks
    Puppet_Sock, Sep 7, 2005
    #4
  5. "George" <> writes:

    > Sorry to be confusing but I was just wondering how I should go about
    > creating the void save(char* file).


    Have a look at the "Thinking in C++" books, especially Vol. 2, Ch. 4, which
    discusses C++'s standard iostream classes.

    <http://www.mindview.net/Books>

    Also of interest is the group FAQ, esp. section 15:

    <http://www.parashift.com/c++-faq-lite/>

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Sep 7, 2005
    #5
  6. George wrote:
    > Sorry to be confusing but I was just wondering how I should go about
    > creating the void save(char* file).
    >


    You should open a file stream for output, then output the data the way
    you need it to be in the file, then close the stream. Then return from
    the function. Or just return without closing, it will close itself.

    V
    Victor Bazarov, Sep 7, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,019
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,467
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,871
    alok000707
    Jul 13, 2010
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
  5. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    745
    Jonathan N. Little
    May 30, 2008
Loading...

Share This Page