stuff-from-scratch/src/image/PngElements.h
2022-11-23 17:51:36 +00:00

88 lines
2.3 KiB
C++

#pragma once
#include "CyclicRedundancyChecker.h"
#include <string>
#include <sstream>
namespace Png
{
inline unsigned char getHighBitCheck()
{
return 0x89;
}
inline std::vector<unsigned char> getSignature()
{
return {13, 10, 26, 10};
}
inline std::string getName()
{
return "PNG";
}
struct IHDRChunk
{
uint32_t width{0};
uint32_t height{0};
unsigned char bitDepth{0};
unsigned char colorType{0};
unsigned char compressionMethod{0};
unsigned char filterMethod{0};
unsigned char interlaceMethod{0};
std::string name{"IHDR"};
std::vector<unsigned char> mData;
std::string toString() const
{
std::stringstream sstr;
sstr << "width: " << width << "\n";
sstr << "height: " << height << "\n";
sstr << "bitDepth: " << (int)bitDepth << "\n";
sstr << "colorType: " << (int)colorType << "\n";
sstr << "compressionMethod: " << (int)compressionMethod << "\n";
sstr << "filterMethod: " << (int)filterMethod << "\n";
sstr << "interlaceMethod: " << (int)interlaceMethod << "\n";
return sstr.str();
}
uint32_t getLength() const
{
return 13;
}
void updateData()
{
mData.clear();
unsigned num_bytes = sizeof(uint32_t);
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(width, idx));
}
for(unsigned idx=0; idx<num_bytes;idx++)
{
mData.push_back(ByteUtils::getByteN(height, idx));
}
mData.push_back(bitDepth);
mData.push_back(colorType);
mData.push_back(compressionMethod);
mData.push_back(filterMethod);
mData.push_back(interlaceMethod);
}
uint32_t getCrc() const
{
CyclicRedundancyChecker crc_check;
std::vector<unsigned char> char_data = StringUtils::toBytes(name);
std::copy(mData.begin(), mData.end(), std::back_inserter(char_data));
auto result = crc_check.doCrc(char_data.data(), char_data.size());
return result;
}
};
}