KrisLibrary  1.0.0
image.h
1 #ifndef IMAGE_IMAGE_H
2 #define IMAGE_IMAGE_H
3 
4 class File;
5 
6 class Image
7 {
8 public:
9  Image();
10  Image(const Image& img);
11  virtual ~Image();
12 
13  const Image& operator = (const Image&);
14 
15  enum PixelFormat {
16  None,
17  R8G8B8, //24 bit, 1 byte per channel
18  A8R8G8B8, //32 bit, 1 byte per channel
19  R5G6B5, //16 bit, green has 6 bits
20  X1R5G5B5, //15 bit, 1 padding
21  A8, //8 bits of alpha
22  FloatRGB, //floating point rgb
23  FloatRGBA, //floating point rgba
24  FloatA, //floating point alpha
25  };
26  static bool isValidFormat(PixelFormat);
27  static unsigned int pixelFormatSize(PixelFormat);
28 
29  //returns -1 on failure, returns size of image on success
30  virtual int initialize(int w, int h, PixelFormat format);
31  virtual void unload();
32 
33  bool Read(const char*);
34  bool Write(const char*) const;
35  virtual bool Read(File& f);
36  virtual bool Write(File& f) const;
37 
38  //fills in all pixels with the given color
39  void clear(int dat = 0);
40  //copies this image, with rect (sx,sy,w,h), onto the dest at dx, dy
41  void blit(Image& dest, int sx = 0, int sy = 0, int w = -1, int h = -1, int dx = 0, int dy = 0) const;
42 
43  inline unsigned int pixelSize() const { return pixelFormatSize(format); } //size in bytes
44  inline unsigned int pixelBPP() const { return pixelSize()<<3; }
45  inline unsigned int pitch() const;
46  unsigned char* getData(int x, int y) const;
47 
48  PixelFormat format; //format of image
49  unsigned short w,h; //width and height, in pixels
50 
51  //the image data
52  unsigned char* data;
53  unsigned int num_bytes;
54 };
55 
56 class ImageMipmapped : public Image
57 {
58 public:
60  explicit ImageMipmapped(const Image&);
61  virtual ~ImageMipmapped();
62 
63  const ImageMipmapped& operator = (const Image&);
64  const ImageMipmapped& operator = (const ImageMipmapped&);
65 
66  void createMipmaps(); //creates mipmaps from the first texture level
67  virtual void unload();
68 
69  virtual bool Read(File& f);
70 
71  //mipmap images, starting at 1 after the main image
72  unsigned char** mipmap_data;
73  unsigned int num_mipmap_levels;
74 };
75 
76 typedef unsigned char color_channel_t;
77 
78 struct Pixel {
79  void add(const Pixel& a, const Pixel& b);
80  void sub(const Pixel& a, const Pixel& b);
81  void mul(const Pixel& a, const Pixel& b);
82  void mix(const Pixel& a, const Pixel& b, float u);
83  color_channel_t r,g,b,a;
84 };
85 
87 {
88 public:
89  ImageOperator();
90  explicit ImageOperator(const Image&);
91  ~ImageOperator();
92 
93  void initialize(int w, int h);
94  void resize(int w, int h);
95  void unload();
96  void clear();
97  void clear(const Pixel&);
98 
99  const ImageOperator& operator = (const Image&);
100  const ImageOperator& operator = (const ImageOperator&);
101  void output(Image&, Image::PixelFormat fmt) const;
102 
103  void stretchBlit(ImageOperator& dest) const;
104  void stretchBlitBilinear(ImageOperator& dest) const;
105 
106  Pixel& getPixel(int x, int y);
107  const Pixel& getPixel(int x, int y) const;
108  void sampleNearest(float x, float y, Pixel& out) const;
109  void sampleLinear(float x, float y, Pixel& out) const;
110 
111  Pixel* pixels;
112  unsigned short w,h;
113  unsigned int num_pixels;
114 };
115 
116 #endif
Definition: image.h:86
Definition: image.h:56
Definition: image.h:6
Definition: image.h:78
A cross-platform class for reading/writing binary data.
Definition: File.h:47