~jaspervdg/+junk/diffuselib

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
#ifndef JASPERS_PPM_H
#define JASPERS_PPM_H

struct RGBTriplet {
  float red, green, blue;
};

class PPMImage {
private:
  unsigned int width, height;
  unsigned int maxval;
  unsigned char* data;

public:
  PPMImage(const unsigned int width, const unsigned int height, const unsigned int maxval, const unsigned char* const data=0);
  PPMImage(const char* const filename);
  ~PPMImage();

  void saveTo(const char* const filename);

  unsigned int getWidth() const { return width; }
  unsigned int getHeight() const { return height; }
  unsigned int getMaximum() const { return maxval; }

  unsigned int getColor(unsigned int colorIndex, unsigned int x, unsigned int y) const {
    unsigned int index = (y*width+x)*3+colorIndex;
    if ( maxval > 255 ) {
      return reinterpret_cast<unsigned short*>(data)[index];
    }
    else {
      return data[index];
    }
  }
  unsigned int getRed(unsigned int x, unsigned int y) const { return getColor(0, x, y); }
  unsigned int getGreen(unsigned int x, unsigned int y) const { return getColor(1, x, y); }
  unsigned int getBlue(unsigned int x, unsigned int y) const { return getColor(2, x, y); }

  void setColor(unsigned int colorIndex, unsigned int x, unsigned int y, unsigned int value) {
    unsigned int index = (y*width+x)*3+colorIndex;
    if ( maxval > 255 ) {
      reinterpret_cast<unsigned short*>(data)[index] = static_cast<unsigned short>(value);
    }
    else {
      data[index] = static_cast<unsigned char>(value);
    }
  }
  void setRed(unsigned int x, unsigned int y, unsigned int value) { setColor(0, x, y, value); }
  void setGreen(unsigned int x, unsigned int y, unsigned int value) { setColor(1, x, y, value); }
  void setBlue(unsigned int x, unsigned int y, unsigned int value) { setColor(2, x, y, value); }
};

#endif//JASPERS_PPM_H