Skip to content

Commit

Permalink
* add max_sub option
Browse files Browse the repository at this point in the history
* update test code and perf
* update preview image
  • Loading branch information
xVan Turing authored and xVan Turing committed Feb 9, 2019
1 parent 320b4f3 commit 44d5625
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 168 deletions.
25 changes: 0 additions & 25 deletions .vscode/c_cpp_properties.json

This file was deleted.

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 2.8)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

project(cquant)
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
[![Build Status](https://travis-ci.org/xVanTuring/cquant.svg?branch=master)](https://travis-ci.org/xVanTuring/cquant)
## View Latest Doc on [Github](https://github.com/xVanTuring/cquant)
## Preview
<!-- ![Ori]() -->
<img src="./img/3.jpg" width="700px" style="margin:0 auto; display:block">
<img src="./img/result.png" width="500px" style="margin:0 auto; display:block">
![Screenshot from 2019-02-09 15-16-53.png](https://i.loli.net/2019/02/09/5c5e7e7b2d278.png)
![Screenshot from 2019-02-09 15-16-32.png](https://i.loli.net/2019/02/09/5c5e7e7b42cd2.png)

## Usage
> Current Supported Prebuild binary version: Node 6 | 8 | 10 | 11 \
Expand Down
6 changes: 3 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0. add test
1. add para for subsampling
0. ~~add test~~
1. ~~add para for subsampling~~
2. use typescript wrapper
3. add travis-ci
3. ~~add travis-ci~~
4. remove rgba or not
1 change: 1 addition & 0 deletions compile_commands.json
14 changes: 7 additions & 7 deletions cquant.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const addon = require('bindings')('cquant')
function paletteAsync (buffer, depth, maxColor = 5, callback = null) {
function paletteAsync(buffer, depth, maxColor = 5, maxSub = 0, callback = null) {
if (callback == null) {
if (depth !== 3 && depth !== 4) {
return Promise.reject(new Error('Wrong Depth'));
return Promise.reject(new Error('Wrong depth!'));
}
if (maxColor <= 1 || maxColor > 256) {
return Promise.reject(new Error('maxColor too small or too large'));
return Promise.reject(new Error('maxColor too small or too large!'));
}
return new Promise((res, rej) => {
addon.PaletteAsync(buffer, maxColor, depth, (err, val) => {
addon.PaletteAsync(buffer, maxColor, depth, maxSub, (err, val) => {
if (err) {
rej(err)
} else {
Expand All @@ -18,14 +18,14 @@ function paletteAsync (buffer, depth, maxColor = 5, callback = null) {
})
} else {
if (depth !== 3 && depth !== 4) {
callback(new Error('Wrong Depth'));
callback(new Error('Wrong depth!'));
return;
}
if (maxColor <= 1 || maxColor > 256) {
callback(new Error('maxColor too small or too large'))
callback(new Error('maxColor too small or too large!'))
return
}
addon.PaletteAsync(buffer, maxColor, depth, callback)
addon.PaletteAsync(buffer, maxColor, depth, maxSub, callback)
}
}
module.exports = {
Expand Down
File renamed without changes
23 changes: 23 additions & 0 deletions img/large.1.jpg.scaled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

<style>
.color {
width: 100px;
height: 100px;
float: left;
}
img{
width: 500px;
display: block;
}
</style>
<img src="large.1.jpg"/><div class="color" style="background-color:rgb(75,84,81)">

</div> <div class="color" style="background-color:rgb(223,208,201)">

</div> <div class="color" style="background-color:rgb(155,152,153)">

</div> <div class="color" style="background-color:rgb(174,182,188)">

</div> <div class="color" style="background-color:rgb(172,180,177)">

</div>
File renamed without changes
23 changes: 23 additions & 0 deletions img/large.1.png.full.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

<style>
.color {
width: 100px;
height: 100px;
float: left;
}
img{
width: 500px;
display: block;
}
</style>
<img src="large.1.png"/><div class="color" style="background-color:rgb(76,84,81)">

</div> <div class="color" style="background-color:rgb(223,208,201)">

</div> <div class="color" style="background-color:rgb(154,151,151)">

</div> <div class="color" style="background-color:rgb(170,175,182)">

</div> <div class="color" style="background-color:rgb(177,188,188)">

</div>
23 changes: 23 additions & 0 deletions img/large.1.png.scaled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

<style>
.color {
width: 100px;
height: 100px;
float: left;
}
img{
width: 500px;
display: block;
}
</style>
<img src="large.1.png"/><div class="color" style="background-color:rgb(76,84,81)">

</div> <div class="color" style="background-color:rgb(223,208,201)">

</div> <div class="color" style="background-color:rgb(154,151,151)">

</div> <div class="color" style="background-color:rgb(170,175,182)">

</div> <div class="color" style="background-color:rgb(177,188,188)">

</div>
File renamed without changes
4 changes: 2 additions & 2 deletions img/1.jpg.html → img/large.2.jpg.full.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
display: block;
}
</style>
<img src="1.jpg"/><div class="color" style="background-color:rgb(77,128,162)">
<img src="large.2.jpg"/><div class="color" style="background-color:rgb(77,128,162)">

</div> <div class="color" style="background-color:rgb(32,38,51)">

</div> <div class="color" style="background-color:rgb(49,76,105)">

</div> <div class="color" style="background-color:rgb(42,59,85)">

</div> <div class="color" style="background-color:rgb(36,86,124)">
</div> <div class="color" style="background-color:rgb(35,86,125)">

</div>
File renamed without changes
4 changes: 2 additions & 2 deletions img/3.jpg.html → img/normal.jpg.full.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
display: block;
}
</style>
<img src="3.jpg"/><div class="color" style="background-color:rgb(75,84,80)">
<img src="normal.jpg"/><div class="color" style="background-color:rgb(75,84,80)">

</div> <div class="color" style="background-color:rgb(223,207,201)">
</div> <div class="color" style="background-color:rgb(223,208,201)">

</div> <div class="color" style="background-color:rgb(156,153,154)">

Expand Down
Binary file removed img/result.png
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cquant",
"version": "0.0.19",
"version": "0.0.20",
"description": "A fast and native image palette generator",
"main": "cquant.js",
"scripts": {
Expand Down
133 changes: 67 additions & 66 deletions src/async.cc
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
#include <napi.h>
#include "async.h"
#include "colorquant.h"
#include <napi.h>
class PaletteWorker : public Napi::AsyncWorker {
public:
PaletteWorker(Napi::Function &callback, PIX *pix, int max_color)
: Napi::AsyncWorker(callback),
pix(pix), max_color(max_color),
counter(NULL), cmap(NULL) {
}
~PaletteWorker() {
if (counter) {
free(counter);
}
if (cmap) {
if (cmap->array) {
free(cmap->array);
}
free(cmap);
}
if (pix) {
free(pix);
}
}
void Execute() {
counter = (size_t *)malloc(sizeof(size_t) * max_color);
cmap = pix_median_cut_quant(pix, max_color, 5, 0, counter);
}
void OnOK() {
Napi::HandleScope scope(Env());
PaletteWorker(Napi::Function &callback, PIX *pix, int max_color,
int max_sub)
: Napi::AsyncWorker(callback), pix(pix), max_color(max_color),
counter(NULL), cmap(NULL), max_sub(max_sub) {}
~PaletteWorker() {
if (counter) {
free(counter);
}
if (cmap) {
if (cmap->array) {
free(cmap->array);
}
free(cmap);
}
if (pix) {
free(pix);
}
}
void Execute() {
counter = (size_t *)malloc(sizeof(size_t) * max_color);
cmap = pix_median_cut_quant(pix, max_color, 5, max_sub, counter);
}
void OnOK() {
Napi::HandleScope scope(Env());

if (cmap == NULL) {
auto err = Napi::Error::New(Env(), "Buffer corrupted or image too small or too less color");
Callback().Call({ err.Value(), Env().Undefined() });
return;
}
if (cmap == NULL) {
auto err = Napi::Error::New(
Env(), "Buffer corrupted or image too small or too less color");
Callback().Call({err.Value(), Env().Undefined()});
return;
}

Napi::Array result = Napi::Array::New(Env());
RGB_QUAD *quad = (RGB_QUAD *)cmap->array;
for (size_t i = 0; i < cmap->n; i++) {
Napi::Object item = Napi::Object::New(Env());
item.Set("R", quad[i].red);
item.Set("G", quad[i].green);
item.Set("B", quad[i].blue);
item.Set("count", counter[i]);
result.Set(i, item);
}
Callback().Call({ Env().Undefined(), result });
}
Napi::Array result = Napi::Array::New(Env());
RGB_QUAD *quad = (RGB_QUAD *)cmap->array;
for (size_t i = 0; i < cmap->n; i++) {
Napi::Object item = Napi::Object::New(Env());
item.Set("R", quad[i].red);
item.Set("G", quad[i].green);
item.Set("B", quad[i].blue);
item.Set("count", counter[i]);
result.Set(i, item);
}
Callback().Call({Env().Undefined(), result});
}

private:
PIX *pix;
int max_color;
PIXCMAP *cmap;
size_t *counter;
PIX *pix;
int max_color;
PIXCMAP *cmap;
size_t *counter;
int max_sub;
};
Napi::Value PaletteAsync(const Napi::CallbackInfo &info) {
auto depth = info[2].As<Napi::Number>().Int32Value();
auto max_color = info[1].As<Napi::Number>().Int32Value();
auto depth = info[2].As<Napi::Number>().Int32Value();
auto max_color = info[1].As<Napi::Number>().Int32Value();
auto max_sub = info[3].As<Napi::Number>().Int32Value();

Napi::Function callback;
callback = info[3].As<Napi::Function>();
Napi::Function callback;
callback = info[4].As<Napi::Function>();

PIX *pix = (PIX *)malloc(sizeof(PIX));
if (depth == 3) {
auto buffer = info[0].As<Napi::Buffer<RGB_QUAD>>();
pix->n = buffer.Length();
pix->pixs = buffer.Data();
}
else if (depth == 4) {
auto buffer = info[0].As<Napi::Buffer<RGBA_QUAD>>();
pix->n = buffer.Length();
pix->pixs = buffer.Data();
}
pix->depth = depth;
PaletteWorker *paletteWorker = new PaletteWorker(callback, pix, max_color);
paletteWorker->Queue();
return info.Env().Undefined();
PIX *pix = (PIX *)malloc(sizeof(PIX));
if (depth == 3) {
auto buffer = info[0].As<Napi::Buffer<RGB_QUAD>>();
pix->n = buffer.Length();
pix->pixs = buffer.Data();
} else if (depth == 4) {
auto buffer = info[0].As<Napi::Buffer<RGBA_QUAD>>();
pix->n = buffer.Length();
pix->pixs = buffer.Data();
}
pix->depth = depth;
PaletteWorker *paletteWorker = new PaletteWorker(callback, pix, max_color, max_sub);
paletteWorker->Queue();
return info.Env().Undefined();
}
Loading

0 comments on commit 44d5625

Please sign in to comment.