Added libpng based png loading

This commit is contained in:
Mateusz Palkowski 2021-03-08 13:38:37 +01:00 committed by Hermet Park
parent fe32ca8de7
commit 832568e07a
9 changed files with 115 additions and 1 deletions

View file

@ -33,6 +33,10 @@ if get_option('log') == true
message('Enable Log')
endif
if get_option('png') == true
config_h.set10('THORVG_PNG_LOADER_SUPPORT', true)
endif
configure_file(
output: 'config.h',
configuration: config_h

View file

@ -33,6 +33,11 @@ option('examples',
value: false,
description: 'Enable building examples')
option('png',
type: 'boolean',
value: false,
description: 'Enable libpng based png loading')
option('test',
type: 'boolean',
value: false,

View file

@ -24,6 +24,11 @@
#ifdef THORVG_SVG_LOADER_SUPPORT
#include "tvgSvgLoader.h"
#endif
#ifdef THORVG_PNG_LOADER_SUPPORT
#include "tvgPngLoader.h"
#endif
#include "tvgRawLoader.h"
/************************************************************************/
@ -39,6 +44,12 @@ static Loader* _find(FileType type)
case FileType::Svg: {
#ifdef THORVG_SVG_LOADER_SUPPORT
return new SvgLoader;
#endif
break;
}
case FileType::Png: {
#ifdef THORVG_PNG_LOADER_SUPPORT
return new PngLoader;
#endif
break;
}
@ -58,6 +69,7 @@ static Loader* _find(const string& path)
{
auto ext = path.substr(path.find_last_of(".") + 1);
if (!ext.compare("svg")) return _find(FileType::Svg);
if (!ext.compare("png")) return _find(FileType::Png);
return nullptr;
}

View file

@ -24,7 +24,7 @@
#include "tvgLoader.h"
enum class FileType { Svg = 0, Raw, Unknown };
enum class FileType { Svg = 0, Raw, Png, Unknown };
struct LoaderMgr
{

View file

@ -5,6 +5,10 @@ if get_option('loaders').contains('svg') == true
message('Enable SVG Loader')
endif
if get_option('png') == true
subdir('png')
endif
subdir('raw')
loader_dep = declare_dependency(

9
src/loaders/png/meson.build Executable file
View file

@ -0,0 +1,9 @@
source_file = [
'tvgPngLoader.h',
'tvgPngLoader.cpp',
]
subloader_dep += [declare_dependency(
include_directories : include_directories('.'),
sources : source_file
)]

View file

@ -0,0 +1,52 @@
#include <fstream>
#include <string>
#include "tvgLoaderMgr.h"
#include "tvgPngLoader.h"
// #include "png.h"
PngLoader::~PngLoader()
{
if (content) {
free((void*)content);
content = nullptr;
}
}
bool PngLoader::open(const string& path)
{
png_image image;
image.version = PNG_IMAGE_VERSION;
image.opaque = NULL;
if (!png_image_begin_read_from_file(&image, path.c_str())) return false;
w = image.width;
h = image.height;
vw = w;
vh = h;
png_bytep buffer;
image.format = PNG_FORMAT_BGRA;
buffer = static_cast<png_bytep>(malloc(PNG_IMAGE_SIZE(image)));
if (!buffer) {
// out of memory, only time when libpng doesnt free its data
png_image_free(&image);
return false;
}
if (!png_image_finish_read(&image, NULL, buffer, 0, NULL)) return false;
content = reinterpret_cast<uint32_t*>(buffer);
return true;
}
bool PngLoader::read()
{
return true;
}
bool PngLoader::close()
{
return true;
}
const uint32_t* PngLoader::pixels()
{
return this->content;
}

22
src/loaders/png/tvgPngLoader.h Executable file
View file

@ -0,0 +1,22 @@
#ifndef _TVG_PNG_LOADER_H_
#define _TVG_PNG_LOADER_H_
#include <png.h>
class PngLoader : public Loader
{
public:
const uint32_t* content = nullptr;
~PngLoader();
using Loader::open;
bool open(const string& path) override;
bool read() override;
bool close() override;
const uint32_t* pixels() override;
};
#endif //_TVG_PNG_LOADER_H_

View file

@ -19,6 +19,12 @@ subdir('bindings')
thread_dep = meson.get_compiler('cpp').find_library('pthread')
thorvg_lib_dep = [common_dep, loader_dep, binding_dep, thread_dep]
if get_option('png') == true
message('Enable libpng based png loading')
png_dep = meson.get_compiler('cpp').find_library('libpng')
thorvg_lib_dep += png_dep
endif
thorvg_lib = library(
'thorvg',
include_directories : headers,