#pragma once #include #include #include #include class Image { private: ssize_t bufferSize = -1; public: enum { FORMAT_RGB, FORMAT_JPEG, FORMAT_YUYV } typedef format_t; std::shared_ptr data = nullptr; uint32_t width; uint32_t height; uint8_t channels; format_t format = FORMAT_RGB; const size_t size() const { return bufferSize < 0 ? width*height*channels : bufferSize; } void setSize(size_t size) { bufferSize = size; } void setBuffer(std::shared_ptr buffer, size_t size, format_t inFormat = FORMAT_RGB) { data = buffer; bufferSize = size; format = inFormat; } void save(const char* filename) const { std::ofstream outfile(filename); outfile.write((char *) data.get(), size()); outfile.close(); } bool open(const char* filename, const uint32_t widthI, const uint32_t heightI, const uint32_t channelsI) { height = heightI; width = widthI; channels = channelsI; std::ifstream infile( filename, std::ios::binary); if(!infile.is_open() || infile.bad()) return false; infile.seekg (0, infile.end); uint32_t filelength = infile.tellg(); infile.seekg (0, infile.beg); if(filelength != size()) return false; data = std::shared_ptr(new unsigned char[filelength]); infile.read((char*)data.get(), filelength); infile.close(); return true; } };