]> git.cworth.org Git - apitrace/blobdiff - image/image.hpp
image: Add floating point support.
[apitrace] / image / image.hpp
index cfdf875f3228a95bc95bc1b74c32c76ef893400a..e36bdd0a2c174560dca1643da738adae1c3cdcbd 100644 (file)
 namespace image {
 
 
+enum ChannelType {
+    TYPE_UNORM8 = 0,
+    TYPE_FLOAT
+};
+
+
 class Image {
 public:
     unsigned width;
     unsigned height;
     unsigned channels;
+    ChannelType channelType;
+    unsigned bytesPerPixel;
 
     // Flipped vertically or not
     bool flipped;
@@ -49,10 +57,12 @@ public:
     // Pixels in RGBA format
     unsigned char *pixels;
 
-    inline Image(unsigned w, unsigned h, unsigned c = 4, bool f = false) : 
+    inline Image(unsigned w, unsigned h, unsigned c = 4, bool f = false, ChannelType t = TYPE_UNORM8) :
         width(w),
         height(h),
         channels(c),
+        channelType(t),
+        bytesPerPixel(channels * (t == TYPE_FLOAT ? 4 : 1)),
         flipped(f),
         pixels(new unsigned char[h*w*c])
     {}
@@ -61,24 +71,30 @@ public:
         delete [] pixels;
     }
 
+    // Absolute stride
+    inline unsigned
+    _stride() const {
+        return width*bytesPerPixel;
+    }
+
     inline unsigned char *start(void) {
-        return flipped ? pixels + (height - 1)*width*channels : pixels;
+        return flipped ? pixels + (height - 1)*_stride() : pixels;
     }
 
     inline const unsigned char *start(void) const {
-        return flipped ? pixels + (height - 1)*width*channels : pixels;
+        return flipped ? pixels + (height - 1)*_stride() : pixels;
     }
 
     inline unsigned char *end(void) {
-        return flipped ? pixels - width*channels : pixels + height*width*channels;
+        return flipped ? pixels - _stride() : pixels + height*_stride();
     }
 
     inline const unsigned char *end(void) const {
-        return flipped ? pixels - width*channels : pixels + height*width*channels;
+        return flipped ? pixels - _stride() : pixels + height*_stride();
     }
 
     inline signed stride(void) const {
-        return flipped ? -(signed)(width*channels) : width*channels;
+        return flipped ? -(signed)_stride() : _stride();
     }
 
     bool