stuff-from-scratch/src/media/image/png/PngHeader.cpp
2024-01-02 16:14:23 +00:00

117 lines
2.2 KiB
C++

#include "PngHeader.h"
#include "Bits.h"
#include "StringUtils.h"
#include "CyclicRedundancyChecker.h"
String PngHeader::toString() const
{
Stringstream sstr;
sstr << "PngHeader" << "\n";
sstr << "width: " << mWidth << "\n";
sstr << "height: " << mHeight << "\n";
sstr << "bitDepth: " << static_cast<int>(mBitDepth) << "\n";
sstr << "cached CRC: " << mCachedCrc << "\n";
sstr << mPngInfo.toString();
return sstr.str();
}
DWord PngHeader::getWidth() const
{
return mWidth;
}
DWord PngHeader::getHeight() const
{
return mHeight;
}
Byte PngHeader::getBitDepth() const
{
return mBitDepth;
}
const PngInfo& PngHeader::getPngInfo() const
{
return mPngInfo;
}
DWord PngHeader::getLength() const
{
return 13;
}
Byte PngHeader::getHighBitCheck() const
{
return 0x89;
}
void PngHeader::getSignature(VecBytes& sig) const
{
sig = {13, 10, 26, 10};
}
String PngHeader::getFileName() const
{
return "PNG";
}
const String& PngHeader::getChunkName() const
{
return mName;
}
const VecBytes& PngHeader::getData() const
{
return mData;
}
void PngHeader::updateData()
{
mData.clear();
const auto num_bytes = sizeof(DWord);
for(size_t idx=0; idx<num_bytes;idx++)
{
mData.push_back(Bits::getByteN(mWidth, idx));
}
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(Bits::getByteN(mHeight, idx));
}
mData.push_back(mBitDepth);
mData.push_back(static_cast<Byte>(mPngInfo.mColorType));
mData.push_back(static_cast<Byte>(mPngInfo.mCompressionMethod));
mData.push_back(static_cast<Byte>(mPngInfo.mFilterMethod));
mData.push_back(static_cast<Byte>(mPngInfo.mInterlaceMethod));
}
DWord PngHeader::getCrc()
{
CyclicRedundancyChecker crc_check;
Vector<unsigned char> char_data = StringUtils::toBytes(mName);
for (auto c : char_data)
{
crc_check.addValue(c);
}
for (auto entry : mData)
{
crc_check.addValue(entry);
}
mCachedCrc = crc_check.getChecksum();
return mCachedCrc;
}
void PngHeader::setPngInfo(const PngInfo& info)
{
mPngInfo = info;
}
void PngHeader::setImageData(uint32_t width, uint32_t height, unsigned char bitDepth)
{
mWidth = width;
mHeight = height;
mBitDepth = bitDepth;
}