X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=image%2Fimage.hpp;h=857e0621c1386e5d9a72eb9dba6e9ff467a35322;hb=beda4440ab82ed4e8f7568fd5a19d8d595b748a3;hp=7e13dd6f490cc97e40e24918e98073b405dcc8c6;hpb=72d2ba9c2b5ba2e2516c0959bf66eafd7b054892;p=apitrace diff --git a/image/image.hpp b/image/image.hpp index 7e13dd6..857e062 100644 --- a/image/image.hpp +++ b/image/image.hpp @@ -31,17 +31,25 @@ #define _IMAGE_HPP_ -#include +#include 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,83 +57,89 @@ 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]) + pixels(new unsigned char[h*w*bytesPerPixel]) {} inline ~Image() { 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 writeBMP(const char *filename) const; + bool + writeBMP(const char *filename) const; - void writePNM(std::ostream &os, const char *comment = NULL) 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 + writePNM(const char *filename, const char *comment = NULL) const; bool writePNG(std::ostream &os) const; - inline bool - writePNG(const char *filename) const { - std::ofstream os(filename, std::ofstream::binary); - if (!os) { - return false; - } - return writePNG(os); - } + bool + writePNG(const char *filename) const; void writeRAW(std::ostream &os) const; - inline bool - writeRAW(const char *filename) const { - std::ofstream os(filename, std::ofstream::binary); - if (!os) { - return false; - } - writeRAW(os); - return true; - } + bool + writeRAW(const char *filename) const; }; +Image * +readPNG(std::istream &is); + Image * readPNG(const char *filename); + +struct PNMInfo +{ + unsigned width; + unsigned height; + unsigned channels; + ChannelType channelType; +}; + const char * -readPNMHeader(const char *buffer, size_t size, unsigned *channels, unsigned *width, unsigned *height); +readPNMHeader(const char *buffer, size_t size, PNMInfo &info); + +Image * +readPNM(const char *buffer, size_t bufferSize); } /* namespace image */