]> git.cworth.org Git - apitrace/blobdiff - image.hpp
PNM output support.
[apitrace] / image.hpp
index 92749c6752d511e2b23c08b874ac39c3bd03321c..44fc4f4352252b148699097c08cef57afa16d523 100644 (file)
--- a/image.hpp
+++ b/image.hpp
@@ -41,6 +41,7 @@ class Image {
 public:
     unsigned width;
     unsigned height;
+    unsigned channels;
 
     // Flipped vertically or not
     bool flipped;
@@ -48,20 +49,65 @@ public:
     // Pixels in RGBA format
     unsigned char *pixels;
 
-    inline Image(unsigned w, unsigned h, bool f = false) : 
+    inline Image(unsigned w, unsigned h, unsigned c = 4, bool f = false) : 
         width(w),
         height(h),
+        channels(c),
         flipped(f),
-        pixels(new unsigned char[h*w*4])
+        pixels(new unsigned char[h*w*c])
     {}
 
     inline ~Image() {
         delete [] pixels;
     }
 
-    void writeBMP(const char *filename) const;
+    inline unsigned char *start(void) {
+        return flipped ? pixels + (height - 1)*width*channels : pixels;
+    }
+
+    inline const unsigned char *start(void) const {
+        return flipped ? pixels + (height - 1)*width*channels : pixels;
+    }
+
+    inline unsigned char *end(void) {
+        return flipped ? pixels - width*channels : pixels + height*width*channels;
+    }
+
+    inline const unsigned char *end(void) const {
+        return flipped ? pixels - width*channels : pixels + height*width*channels;
+    }
+
+    inline signed stride(void) const {
+        return flipped ? -width*channels : width*channels;
+    }
+
+    bool writeBMP(const char *filename) const;
+
+    void writePNM(std::ostream &os) const;
+
+    inline bool writePNM(const char *filename) const {
+        std::ofstream os(filename, std::ofstream::binary);
+        if (!os) {
+            return false;
+        }
+        writePNM(os);
+        return true;
+    }
+
+    bool writePNG(const char *filename) const;
+
+    double compare(Image &ref);
 };
 
+bool writePixelsToBuffer(unsigned char *pixels,
+                         unsigned w, unsigned h, unsigned numChannels,
+                         bool flipped,
+                         char **buffer,
+                         int *size);
+
+Image *
+readPNG(const char *filename);
+
 
 } /* namespace Image */