67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#pragma once
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
|
|
class Image
|
|
{
|
|
private:
|
|
|
|
ssize_t bufferSize = -1;
|
|
|
|
public:
|
|
|
|
enum {
|
|
FORMAT_RGB,
|
|
FORMAT_JPEG,
|
|
FORMAT_YUYV
|
|
} typedef format_t;
|
|
|
|
std::shared_ptr<unsigned char> 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<unsigned char> 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<unsigned char>(new unsigned char[filelength]);
|
|
infile.read((char*)data.get(), filelength);
|
|
|
|
infile.close();
|
|
return true;
|
|
}
|
|
};
|