44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
#pragma once
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
|
|
class Image
|
|
{
|
|
public:
|
|
std::shared_ptr<unsigned char> data = nullptr;
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint8_t channels;
|
|
const uint32_t size()
|
|
{
|
|
return width*height*channels;
|
|
}
|
|
void save(const char* filename)
|
|
{
|
|
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;
|
|
}
|
|
};
|