Add PDF writer.
This commit is contained in:
parent
c05b7b6315
commit
9c116b1efd
72 changed files with 1819 additions and 114 deletions
21
src/image/CMakeLists.txt
Normal file
21
src/image/CMakeLists.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
list(APPEND image_HEADERS
|
||||
Image.h
|
||||
PngWriter.h
|
||||
)
|
||||
|
||||
list(APPEND image_LIB_INCLUDES
|
||||
Image.cpp
|
||||
PngWriter.cpp
|
||||
)
|
||||
|
||||
add_library(image SHARED ${image_LIB_INCLUDES} ${image_HEADERS})
|
||||
|
||||
target_include_directories(image PUBLIC
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
)
|
||||
set_target_properties( image PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
|
||||
|
||||
find_package(PNG REQUIRED)
|
||||
target_link_libraries( image PUBLIC PNG::PNG)
|
||||
|
||||
set_property(TARGET image PROPERTY FOLDER src)
|
69
src/image/Image.cpp
Normal file
69
src/image/Image.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "Image.h"
|
||||
|
||||
Image::Image(unsigned width, unsigned height)
|
||||
: mWidth(width),
|
||||
mHeight(height)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<Image> Image::Create(unsigned width, unsigned height)
|
||||
{
|
||||
return std::make_unique<Image>(width, height);
|
||||
}
|
||||
|
||||
unsigned Image::GetBytesPerRow() const
|
||||
{
|
||||
const auto bitsPerEntry = mBitDepth <= 8 ? 1 : 2;
|
||||
return mWidth * mNumChannels *bitsPerEntry;
|
||||
}
|
||||
|
||||
unsigned Image::GetWidth() const
|
||||
{
|
||||
return mWidth;
|
||||
}
|
||||
|
||||
unsigned Image::GetHeight() const
|
||||
{
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
unsigned Image::GetBitDepth() const
|
||||
{
|
||||
return mBitDepth;
|
||||
}
|
||||
|
||||
unsigned char Image::GetByte(unsigned idx, unsigned jdx) const
|
||||
{
|
||||
return mData[jdx*GetBytesPerRow() + idx];
|
||||
}
|
||||
|
||||
unsigned Image::GetNumChannels() const
|
||||
{
|
||||
return mNumChannels;
|
||||
}
|
||||
|
||||
void Image::SetData(const std::vector<unsigned char>& data)
|
||||
{
|
||||
mData = data;
|
||||
}
|
||||
|
||||
void Image::SetWidth(unsigned width)
|
||||
{
|
||||
mWidth = width;
|
||||
}
|
||||
|
||||
void Image::SetHeight(unsigned height)
|
||||
{
|
||||
mHeight = height;
|
||||
}
|
||||
|
||||
void Image::SetBitDepth(unsigned bitDepth)
|
||||
{
|
||||
mBitDepth = bitDepth;
|
||||
}
|
||||
|
||||
void Image::SetNumChannels(unsigned numChannels)
|
||||
{
|
||||
mNumChannels = numChannels;
|
||||
}
|
35
src/image/Image.h
Normal file
35
src/image/Image.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
|
||||
class Image
|
||||
{
|
||||
public:
|
||||
|
||||
Image(unsigned width, unsigned height);
|
||||
static std::unique_ptr<Image> Create(unsigned width, unsigned height);
|
||||
|
||||
unsigned GetBytesPerRow() const;
|
||||
unsigned GetWidth() const;
|
||||
unsigned GetHeight() const;
|
||||
unsigned GetBitDepth() const;
|
||||
unsigned GetNumChannels() const;
|
||||
unsigned char 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);
|
||||
|
||||
private:
|
||||
unsigned mWidth{1};
|
||||
unsigned mHeight{1};
|
||||
unsigned mBitDepth{8};
|
||||
unsigned mNumChannels{1};
|
||||
std::vector<unsigned char> mData;
|
||||
};
|
||||
|
||||
using ImagePtr = std::unique_ptr<Image>;
|
74
src/image/PngWriter.cpp
Normal file
74
src/image/PngWriter.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include "PngWriter.h"
|
||||
|
||||
#include "Image.h"
|
||||
|
||||
#include <png.h>
|
||||
#include <stdio.h>
|
||||
|
||||
std::unique_ptr<PngWriter> PngWriter::Create()
|
||||
{
|
||||
return std::make_unique<PngWriter>();
|
||||
}
|
||||
|
||||
void PngWriter::SetPath(const std::string& path)
|
||||
{
|
||||
mPath = path;
|
||||
}
|
||||
|
||||
void PngWriter::Write(const std::unique_ptr<Image>& image) const
|
||||
{
|
||||
auto fp = fopen(mPath.c_str(), "wb");
|
||||
|
||||
auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
auto info_ptr = png_create_info_struct(png_ptr);
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
png_init_io(png_ptr, fp);
|
||||
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
auto color_type = PNG_COLOR_TYPE_RGB;
|
||||
png_set_IHDR(png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
|
||||
image->GetBitDepth(), color_type, PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * image->GetHeight());
|
||||
auto row_size = image->GetBytesPerRow();
|
||||
for(unsigned jdx=0;jdx<image->GetHeight();jdx++)
|
||||
{
|
||||
row_pointers[jdx]=(png_byte*)malloc(sizeof(png_byte)*row_size);
|
||||
for(unsigned idx=0;idx<row_size;idx++)
|
||||
{
|
||||
row_pointers[jdx][idx] = image->GetByte(idx, jdx);
|
||||
}
|
||||
}
|
||||
png_write_image(png_ptr, row_pointers);
|
||||
if (setjmp(png_jmpbuf(png_ptr)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
png_write_end(png_ptr, nullptr);
|
||||
|
||||
for (unsigned y=0; y<image->GetHeight(); y++)
|
||||
{
|
||||
free(row_pointers[y]);
|
||||
}
|
||||
free(row_pointers);
|
||||
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
22
src/image/PngWriter.h
Normal file
22
src/image/PngWriter.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
class Image;
|
||||
|
||||
class PngWriter
|
||||
{
|
||||
public:
|
||||
static std::unique_ptr<PngWriter> Create();
|
||||
|
||||
void SetPath(const std::string& path);
|
||||
|
||||
void Write(const std::unique_ptr<Image>& image) const;
|
||||
|
||||
private:
|
||||
|
||||
std::string mPath;
|
||||
};
|
||||
|
||||
using PngWriterPtr = std::unique_ptr<PngWriter>;
|
Loading…
Add table
Add a link
Reference in a new issue