X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=image.hpp;h=dc53ec990ee3a19f931f5e9160269c7e0c875169;hb=447f4a55a01e82b0265f44212b8d439fa83750d7;hp=f46fb31e69712f4cebe322dea04b71c13e48a686;hpb=aa0a7822338b32e0cca2a984759cda0e8c846457;p=apitrace diff --git a/image.hpp b/image.hpp index f46fb31..dc53ec9 100644 --- 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,11 +49,12 @@ 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() { @@ -60,18 +62,38 @@ public: } inline unsigned char *start(void) { - return flipped ? pixels + (height - 1)*width*4 : pixels; + 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*4 : pixels + height*width*4; + 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*4 : width*4; + return flipped ? -width*channels : width*channels; } bool writeBMP(const char *filename) const; + + void writePNM(std::ostream &os, const char *comment = NULL) const; + + inline bool writePNM(const char *filename, const char *comment = NULL) const { + std::ofstream os(filename, std::ofstream::binary); + if (!os) { + return false; + } + writePNM(os, comment); + return true; + } + bool writePNG(const char *filename) const; double compare(Image &ref);