mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
bin svg2png: revise abe7187f5b
revise previous patch code. 1. turned off it in default. 2. + copyright declaration. 3. fix coding convention.
This commit is contained in:
parent
abe7187f5b
commit
64c958cd2c
3 changed files with 40 additions and 30 deletions
|
@ -22,8 +22,8 @@ option('bindings',
|
||||||
value: ['capi'],
|
value: ['capi'],
|
||||||
description: 'Enable C API binding')
|
description: 'Enable C API binding')
|
||||||
|
|
||||||
option('utility',
|
option('utilities',
|
||||||
type: 'array',
|
type: 'array',
|
||||||
choices: ['', 'svg2png'],
|
choices: ['', 'svg2png'],
|
||||||
value: ['svg2png'],
|
value: [''],
|
||||||
description: 'Enable building utilities')
|
description: 'Enable building utilities')
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
if get_option('utility').contains('svg2png') == true
|
if get_option('utilities').contains('svg2png') == true
|
||||||
subdir('svg2png')
|
subdir('svg2png')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 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 <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <thorvg.h>
|
#include <thorvg.h>
|
||||||
|
@ -7,19 +29,14 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
class PngBuilder {
|
struct PngBuilder {
|
||||||
public:
|
|
||||||
~PngBuilder()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
void build(const std::string &fileName , const uint32_t width, const uint32_t height, uint32_t *buffer)
|
void build(const std::string &fileName , const uint32_t width, const uint32_t height, uint32_t *buffer)
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> image;
|
std::vector<unsigned char> image;
|
||||||
image.resize(width * height * 4);
|
image.resize(width * height * 4);
|
||||||
for(unsigned y = 0; y < height; y++)
|
for(unsigned y = 0; y < height; y++) {
|
||||||
{
|
for(unsigned x = 0; x < width; x++) {
|
||||||
for(unsigned x = 0; x < width; x++)
|
|
||||||
{
|
|
||||||
uint32_t n = buffer[ y * width + x ];
|
uint32_t n = buffer[ y * width + x ];
|
||||||
image[4 * width * y + 4 * x + 0] = ( n >> 16 ) & 0xff;
|
image[4 * width * y + 4 * x + 0] = ( n >> 16 ) & 0xff;
|
||||||
image[4 * width * y + 4 * x + 1] = ( n >> 8 ) & 0xff;
|
image[4 * width * y + 4 * x + 1] = ( n >> 8 ) & 0xff;
|
||||||
|
@ -35,8 +52,8 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class App {
|
struct App {
|
||||||
public:
|
|
||||||
void tvgDrawCmds(tvg::Canvas* canvas, bool useSvgSize)
|
void tvgDrawCmds(tvg::Canvas* canvas, bool useSvgSize)
|
||||||
{
|
{
|
||||||
if (!canvas) return;
|
if (!canvas) return;
|
||||||
|
@ -47,19 +64,16 @@ public:
|
||||||
|
|
||||||
picture->viewbox(nullptr, nullptr, &fw, &fh);
|
picture->viewbox(nullptr, nullptr, &fw, &fh);
|
||||||
|
|
||||||
if (useSvgSize)
|
if (useSvgSize) {
|
||||||
{
|
|
||||||
width = fw;
|
width = fw;
|
||||||
height = fh;
|
height = fh;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
tvg::Matrix m = {width / fw, 0, 0, 0, height / fh, 0, 0, 0, 1};
|
tvg::Matrix m = {width / fw, 0, 0, 0, height / fh, 0, 0, 0, 1};
|
||||||
picture->transform(m);
|
picture->transform(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bgColor != 0xffffffff)
|
if (bgColor != 0xffffffff) {
|
||||||
{
|
|
||||||
uint8_t bgColorR = (uint8_t) ((bgColor & 0xff0000) >> 16);
|
uint8_t bgColorR = (uint8_t) ((bgColor & 0xff0000) >> 16);
|
||||||
uint8_t bgColorG = (uint8_t) ((bgColor & 0x00ff00) >> 8);
|
uint8_t bgColorG = (uint8_t) ((bgColor & 0x00ff00) >> 8);
|
||||||
uint8_t bgColorB = (uint8_t) ((bgColor & 0x0000ff));
|
uint8_t bgColorB = (uint8_t) ((bgColor & 0x0000ff));
|
||||||
|
@ -90,20 +104,18 @@ public:
|
||||||
if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
|
if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) {
|
||||||
|
|
||||||
//Create a Canvas
|
//Create a Canvas
|
||||||
unique_ptr<tvg::SwCanvas> swCanvas = tvg::SwCanvas::gen();
|
auto swCanvas = tvg::SwCanvas::gen();
|
||||||
bool useSvgSize = false;
|
bool useSvgSize = false;
|
||||||
if (w == 0 && h == 0)
|
if (w == 0 && h == 0) {
|
||||||
{
|
|
||||||
w = h = 200; // Set temporary size
|
w = h = 200; // Set temporary size
|
||||||
useSvgSize = true;
|
useSvgSize = true;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
width = w;
|
width = w;
|
||||||
height = h;
|
height = h;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t *buffer = (uint32_t*)malloc(sizeof(uint32_t) * w * h);
|
auto buffer = (uint32_t*)malloc(sizeof(uint32_t) * w * h);
|
||||||
swCanvas->target(buffer, w, w, h, tvg::SwCanvas::ARGB8888);
|
swCanvas->target(buffer, w, w, h, tvg::SwCanvas::ARGB8888);
|
||||||
/* Push the shape into the Canvas drawing list
|
/* Push the shape into the Canvas drawing list
|
||||||
When this shape is into the canvas list, the shape could update & prepare
|
When this shape is into the canvas list, the shape could update & prepare
|
||||||
|
@ -111,8 +123,7 @@ public:
|
||||||
Canvas keeps this shape node unless user call canvas->clear() */
|
Canvas keeps this shape node unless user call canvas->clear() */
|
||||||
tvgDrawCmds(swCanvas.get(), useSvgSize);
|
tvgDrawCmds(swCanvas.get(), useSvgSize);
|
||||||
|
|
||||||
if (useSvgSize)
|
if (useSvgSize) {
|
||||||
{
|
|
||||||
//Resize target buffer
|
//Resize target buffer
|
||||||
buffer = (uint32_t*)realloc(buffer, sizeof(uint32_t) * width * height);
|
buffer = (uint32_t*)realloc(buffer, sizeof(uint32_t) * width * height);
|
||||||
swCanvas->target(buffer, width, width, height, tvg::SwCanvas::ARGB8888);
|
swCanvas->target(buffer, width, width, height, tvg::SwCanvas::ARGB8888);
|
||||||
|
@ -185,8 +196,7 @@ private:
|
||||||
|
|
||||||
bool svgFile() {
|
bool svgFile() {
|
||||||
std::string extn = ".svg";
|
std::string extn = ".svg";
|
||||||
if ( fileName.size() <= extn.size() ||
|
if (fileName.size() <= extn.size() || fileName.substr(fileName.size() - extn.size()) != extn)
|
||||||
fileName.substr(fileName.size()- extn.size()) != extn )
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
Loading…
Add table
Reference in a new issue