thorvg/src/loaders/external_png/tvgPngLoader.cpp
Michal Maciola cd5116b053
loaders: Consider colorspaces (#838)
* common: added colorSpace() function

This patch introduces colorSpace() function for SW and GL engine.

* infra: change LoadModule:read() into LoadModule:read(uint32_t colorspace)

This patch changes LoadModule:read() into LoadModule:read(uint32_t colorspace)

* picture: implement passing colorspace into loader

This patch implements passing colorspace into loaders.
Loader->read is now called on the first update.

* external_jpg_loader: support colorspaces

* external_png_loader: support colorspaces
2021-11-01 16:10:22 +09:00

123 lines
3.4 KiB
C++

/*
* Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <memory.h>
#include "tvgLoader.h"
#include "tvgPngLoader.h"
/************************************************************************/
/* Internal Class Implementation */
/************************************************************************/
void PngLoader::clear()
{
if (freeData) free(data);
data = nullptr;
freeData = false;
}
/************************************************************************/
/* External Class Implementation */
/************************************************************************/
PngLoader::PngLoader()
{
image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
image->version = PNG_IMAGE_VERSION;
image->opaque = NULL;
}
PngLoader::~PngLoader()
{
if (freeData) free(data);
data = nullptr;
if (content) {
free((void*)content);
content = nullptr;
}
free(image);
}
bool PngLoader::open(const string& path)
{
clear();
image->opaque = NULL;
if (!png_image_begin_read_from_file(image, path.c_str())) return false;
w = (float)image->width;
h = (float)image->height;
return true;
}
bool PngLoader::open(const char* data, uint32_t size, bool copy)
{
clear();
image->opaque = NULL;
if (copy) {
this->data = (char *) malloc(size);
if (!this->data) return false;
memcpy(this->data, data, size);
freeData = true;
} else {
this->data = (char *) data;
}
if (!png_image_begin_read_from_memory(image, this->data, size)) return false;
w = (float)image->width;
h = (float)image->height;
return true;
}
bool PngLoader::read(uint32_t colorspace)
{
png_bytep buffer;
image->format = (colorspace == tvg::SwCanvas::ABGR8888) ? PNG_FORMAT_RGBA : 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::close()
{
png_image_free(image);
return true;
}
const uint32_t* PngLoader::pixels()
{
return this->content;
}