#define _IMAGE_HPP_
-#include <fstream>
+#include <iostream>
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;
// 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])
{}
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);