Clean image types
This commit is contained in:
parent
e7683cd94e
commit
c6d03f16d0
18 changed files with 169 additions and 107 deletions
|
@ -1,91 +1,112 @@
|
|||
#include "Image.h"
|
||||
|
||||
Image::Image(unsigned width, unsigned height)
|
||||
#include "Color.h"
|
||||
|
||||
template<typename T>
|
||||
Image<T>::Image(unsigned width, unsigned height)
|
||||
: mWidth(width),
|
||||
mHeight(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Image::~Image()
|
||||
template<typename T>
|
||||
Image<T>::~Image()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Image::Initialize()
|
||||
template<typename T>
|
||||
void Image<T>::initialize()
|
||||
{
|
||||
mData = std::vector<unsigned char>(GetBytesPerRow()*mHeight, 0);
|
||||
mData = std::vector<T>(getBytesPerRow()*mHeight, 0);
|
||||
}
|
||||
|
||||
void Image::SetPixelValue(unsigned idx, unsigned jdx, const Color& color)
|
||||
template<typename T>
|
||||
void Image<T>::setPixelValue(unsigned idx, unsigned jdx, const Color& color)
|
||||
{
|
||||
if (mData.empty())
|
||||
{
|
||||
Initialize();
|
||||
initialize();
|
||||
}
|
||||
|
||||
mData[jdx*GetBytesPerRow() + idx*3] = static_cast<unsigned char>(color.GetR());
|
||||
mData[jdx*GetBytesPerRow() + idx*3 + 1] = static_cast<unsigned char>(color.GetG());
|
||||
mData[jdx*GetBytesPerRow() + idx*3 + 2] = static_cast<unsigned char>(color.GetB());
|
||||
mData[jdx*getBytesPerRow() + idx*3] = static_cast<T>(color.GetR());
|
||||
mData[jdx*getBytesPerRow() + idx*3 + 1] = static_cast<T>(color.GetG());
|
||||
mData[jdx*getBytesPerRow() + idx*3 + 2] = static_cast<T>(color.GetB());
|
||||
}
|
||||
|
||||
std::unique_ptr<Image> Image::Create(unsigned width, unsigned height)
|
||||
template<typename T>
|
||||
std::unique_ptr<Image<T> > Image<T>::Create(unsigned width, unsigned height)
|
||||
{
|
||||
return std::make_unique<Image>(width, height);
|
||||
return std::make_unique<Image<T> >(width, height);
|
||||
}
|
||||
|
||||
unsigned Image::GetBytesPerRow() const
|
||||
template<typename T>
|
||||
unsigned Image<T>::getBytesPerRow() const
|
||||
{
|
||||
const auto bitsPerEntry = mBitDepth <= 8 ? 1 : 2;
|
||||
return mWidth * mNumChannels *bitsPerEntry;
|
||||
}
|
||||
|
||||
unsigned Image::GetWidth() const
|
||||
template<typename T>
|
||||
unsigned Image<T>::getWidth() const
|
||||
{
|
||||
return mWidth;
|
||||
}
|
||||
|
||||
unsigned Image::GetHeight() const
|
||||
template<typename T>
|
||||
unsigned Image<T>::getHeight() const
|
||||
{
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
unsigned Image::GetBitDepth() const
|
||||
template<typename T>
|
||||
unsigned Image<T>::getBitDepth() const
|
||||
{
|
||||
return mBitDepth;
|
||||
}
|
||||
|
||||
unsigned char Image::GetByte(unsigned idx, unsigned jdx) const
|
||||
template<typename T>
|
||||
T Image<T>::getByte(unsigned idx, unsigned jdx) const
|
||||
{
|
||||
return mData[jdx*GetBytesPerRow() + idx];
|
||||
return mData[jdx*getBytesPerRow() + idx];
|
||||
}
|
||||
|
||||
unsigned Image::GetNumChannels() const
|
||||
template<typename T>
|
||||
unsigned Image<T>::getNumChannels() const
|
||||
{
|
||||
return mNumChannels;
|
||||
}
|
||||
|
||||
void Image::SetData(const std::vector<unsigned char>& data)
|
||||
template<typename T>
|
||||
void Image<T>::setData(const std::vector<T>& data)
|
||||
{
|
||||
mData = data;
|
||||
}
|
||||
|
||||
void Image::SetWidth(unsigned width)
|
||||
template<typename T>
|
||||
void Image<T>::setWidth(unsigned width)
|
||||
{
|
||||
mWidth = width;
|
||||
}
|
||||
|
||||
void Image::SetHeight(unsigned height)
|
||||
template<typename T>
|
||||
void Image<T>::setHeight(unsigned height)
|
||||
{
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
void Image::SetBitDepth(unsigned bitDepth)
|
||||
template<typename T>
|
||||
void Image<T>::setBitDepth(unsigned bitDepth)
|
||||
{
|
||||
mBitDepth = bitDepth;
|
||||
}
|
||||
|
||||
void Image::SetNumChannels(unsigned numChannels)
|
||||
template<typename T>
|
||||
void Image<T>::setNumChannels(unsigned numChannels)
|
||||
{
|
||||
mNumChannels = numChannels;
|
||||
}
|
||||
|
||||
template class Image<unsigned char>;
|
||||
//template class Image<uint8_t>;
|
||||
|
|
|
@ -1,43 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include "Color.h"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class Color;
|
||||
|
||||
template<typename T>
|
||||
class Image
|
||||
{
|
||||
public:
|
||||
|
||||
Image(unsigned width, unsigned height);
|
||||
~Image();
|
||||
static std::unique_ptr<Image> Create(unsigned width, unsigned height);
|
||||
static std::unique_ptr<Image<T> > Create(unsigned width, unsigned height);
|
||||
|
||||
unsigned GetBytesPerRow() const;
|
||||
unsigned GetWidth() const;
|
||||
unsigned GetHeight() const;
|
||||
unsigned GetBitDepth() const;
|
||||
unsigned GetNumChannels() const;
|
||||
unsigned getBytesPerRow() const;
|
||||
unsigned getWidth() const;
|
||||
unsigned getHeight() const;
|
||||
unsigned getBitDepth() const;
|
||||
unsigned getNumChannels() const;
|
||||
|
||||
void SetPixelValue(unsigned idx, unsigned jdx, const Color& color);
|
||||
void setPixelValue(unsigned idx, unsigned jdx, const Color& color);
|
||||
|
||||
unsigned char GetByte(unsigned idx, unsigned jdx) const;
|
||||
T getByte(unsigned idx, unsigned jdx) const;
|
||||
|
||||
void SetData(const std::vector<unsigned char>& data);
|
||||
void SetWidth(unsigned width);
|
||||
void SetHeight(unsigned height);
|
||||
void SetBitDepth(unsigned bitDepth);
|
||||
void SetNumChannels(unsigned numChannels);
|
||||
void setData(const std::vector<T>& data);
|
||||
void setWidth(unsigned width);
|
||||
void setHeight(unsigned height);
|
||||
void setBitDepth(unsigned bitDepth);
|
||||
void setNumChannels(unsigned numChannels);
|
||||
|
||||
private:
|
||||
void Initialize();
|
||||
void initialize();
|
||||
|
||||
unsigned mWidth{1};
|
||||
unsigned mHeight{1};
|
||||
unsigned mBitDepth{8};
|
||||
unsigned mNumChannels{1};
|
||||
std::vector<unsigned char> mData;
|
||||
std::vector<T> mData;
|
||||
};
|
||||
|
||||
using ImagePtr = std::unique_ptr<Image>;
|
||||
|
|
26
src/image/ImagePrimitives.h
Normal file
26
src/image/ImagePrimitives.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
class ImagePrimitives
|
||||
{
|
||||
public:
|
||||
static void drawCheckerboard(uint8_t* data, int width, int height, int offset = 0)
|
||||
{
|
||||
auto pixels = (uint32_t *)&(data)[offset];
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
for (int x = 0; x < width; ++x)
|
||||
{
|
||||
if ((x + y / 8 * 8) % 16 < 8)
|
||||
{
|
||||
pixels[y * width + x] = 0xFF666666;
|
||||
}
|
||||
else
|
||||
{
|
||||
pixels[y * width + x] = 0xFFEEEEEE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
|
@ -108,9 +108,9 @@ void PngReader::logHeader()
|
|||
std::cout << sstr.str() << std::endl;
|
||||
}
|
||||
|
||||
std::unique_ptr<Image> PngReader::read()
|
||||
std::unique_ptr<Image<unsigned char> > PngReader::read()
|
||||
{
|
||||
auto image = std::make_unique<Image>(5, 5);
|
||||
auto image = std::make_unique<Image<unsigned char> >(5, 5);
|
||||
|
||||
mFile = std::make_unique<File>(mPath);
|
||||
mFile->Open(true);
|
||||
|
|
|
@ -5,8 +5,7 @@
|
|||
#include <vector>
|
||||
|
||||
#include "File.h"
|
||||
|
||||
class Image;
|
||||
#include "Image.h"
|
||||
|
||||
class PngReader
|
||||
{
|
||||
|
@ -14,19 +13,19 @@ public:
|
|||
~PngReader();
|
||||
void setPath(const std::string& path);
|
||||
|
||||
std::unique_ptr<Image> read();
|
||||
std::unique_ptr<Image<unsigned char> > read();
|
||||
|
||||
private:
|
||||
|
||||
struct IHDRChunk
|
||||
{
|
||||
unsigned width{0};
|
||||
unsigned height{0};
|
||||
char bitDepth{0};
|
||||
char colorType{0};
|
||||
char compressionMethod{0};
|
||||
char filterMethod{0};
|
||||
char interlaceMethod{0};
|
||||
unsigned width{0};
|
||||
unsigned height{0};
|
||||
char bitDepth{0};
|
||||
char colorType{0};
|
||||
char compressionMethod{0};
|
||||
char filterMethod{0};
|
||||
char interlaceMethod{0};
|
||||
};
|
||||
|
||||
bool readChunk();
|
||||
|
@ -39,7 +38,7 @@ private:
|
|||
|
||||
IHDRChunk mIHDRChunk;
|
||||
|
||||
std::unique_ptr<Image> mWorkingImage;
|
||||
std::unique_ptr<Image<unsigned char> > mWorkingImage;
|
||||
std::unique_ptr<File> mFile;
|
||||
std::string mPath;
|
||||
};
|
||||
|
|
|
@ -29,7 +29,7 @@ void PngWriter::SetPath(const std::string& path)
|
|||
mImpl->setPath(path);
|
||||
}
|
||||
|
||||
void PngWriter::Write(const std::unique_ptr<Image>& image) const
|
||||
void PngWriter::Write(const std::unique_ptr<Image<unsigned char> >& image) const
|
||||
{
|
||||
mImpl->write(image);
|
||||
//auto fp = fopen(mPath.c_str(), "wb");
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Image;
|
||||
#include "Image.h"
|
||||
|
||||
class PngWriterImpl;
|
||||
|
||||
class PngWriter
|
||||
|
@ -15,7 +16,7 @@ public:
|
|||
|
||||
void SetPath(const std::string& path);
|
||||
|
||||
void Write(const std::unique_ptr<Image>& image) const;
|
||||
void Write(const std::unique_ptr<Image<unsigned char> >& image) const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<PngWriterImpl> mImpl;
|
||||
|
|
|
@ -5,7 +5,7 @@ void PngWriterBasic::setPath(const std::string& path)
|
|||
|
||||
}
|
||||
|
||||
void PngWriterBasic::write(const std::unique_ptr<Image>& image) const
|
||||
void PngWriterBasic::write(const std::unique_ptr<Image<unsigned char> >& image) const
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ class PngWriterBasic : public PngWriterImpl
|
|||
public:
|
||||
void setPath(const std::string& path) override;
|
||||
|
||||
void write(const std::unique_ptr<Image>& image) const override;
|
||||
void write(const std::unique_ptr<Image<unsigned char>>& image) const override;
|
||||
|
||||
private:
|
||||
std::string mPath;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class Image;
|
||||
#include "Image.h"
|
||||
|
||||
class PngWriterImpl
|
||||
{
|
||||
|
@ -12,5 +12,5 @@ public:
|
|||
|
||||
virtual void setPath(const std::string& path) = 0;
|
||||
|
||||
virtual void write(const std::unique_ptr<Image>& image) const = 0;
|
||||
virtual void write(const std::unique_ptr<Image<unsigned char> >& image) const = 0;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue