Refactor: Image - Printer - Draw

Hi,

I just came from C, and I would like how to refactor this case for learning purposes.

What I don't like from my code:

- the way I choose if writing to stdout or file.
- the way I select the type of printer (2 constructors, isStdout because filename is always not null)
- writing and reading every pixel by a function (looks horrible thinking in performance).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>

#include <fstream>

class Image {
public:
  Image(int w, int h) :
    w_(w), h_(h), colorByte_(3)
  {
    size_ = w * h * colorByte_;
    data_ = new unsigned char[size_];
  }
  ~Image(){
    delete[] data_;
  }
  void setPixel(int x, int y, int r, int g, int b){
    int pos = y * w_ * colorByte_ + x * colorByte_;
    data_[pos] = r;
    data_[pos+1] = g;
    data_[pos+2] = b;
  }
  void getPixel(int x, int y, int* r, int* g, int* b) const {
    int pos = y * w_ * colorByte_ + x * colorByte_;
    *r = data_[pos];
    *g = data_[pos+1];
    *b = data_[pos+2];
  }
  int width() const { return w_; }
  int height() const { return h_; }

private:
  unsigned char * data_;
  int colorByte_;
  int w_;
  int h_;
  int size_;
};

class Printer {
public:
  Printer()
    : isStdout_(true)
  {

  }
  Printer(std::string filename)
    : filename_(filename), isStdout_(false)
  {

  }

  void
  print(Image & img)
  {
    std::ostream* sout;
    std::ofstream fileout;
    if (isStdout_){
      sout = &std::cout;
    }else{
      fileout.open(filename_, std::ios::out);
      sout = &fileout;
    }

    int w = img.width();
    int h = img.height();
    int lasti = w - 1;
    *sout << "P3\n" << w << " " << h << "\n255\n";
    for (int j=0; j<h; j++){
      for (int i=0; i<w; i++){
        int ri, gi, bi;
        img.getPixel(i, j, &ri, &gi, &bi);
        *sout << ri << " " << gi << " " << bi;
        if (i == lasti){
          *sout << "\n";
        }else{
          *sout << " ";
        }
      }
    }
    if (!isStdout_){
      fileout.close();
    }
  }
private:
  std::string filename_;
  bool isStdout_;
};


void
draw(Image & img)
{
  int w = img.width();
  int h = img.height();
  bool first = true;
  for (int j=0; j<h; j++){
    for (int i=0; i<w; i++){
      float r = float(i)/float(w);
      float g = float(j)/float(h);
      float b = 0.2;
      int ri = int(255.99f * r);
      int gi = int(255.99f * g);
      int bi = int(255.99f * b);
      img.setPixel(i, j, ri, gi, bi);
    }
  }
}

void
print(Image & img)
{
  int w = img.width();
  int h = img.height();
  int lasti = w - 1;
  std::cout << "P3\n" << w << " " << h << "\n255\n";
  for (int j=0; j<h; j++){
    for (int i=0; i<w; i++){
      int ri, gi, bi;
      img.getPixel(i, j, &ri, &gi, &bi);
      std::cout << ri << " " << gi << " " << bi;
      if (i == lasti){
        std::cout << "\n";
      }else{
        std::cout << " ";
      }
    }
  }
}

void
printer()
{
  Image img(200, 100);

  draw(img);

  // image
  Printer printer("image.ppm");
  printer.print(img);

  // terminal
  Printer printer;
  printer.print(img);
}
Last edited on
Topic archived. No new replies allowed.