Skip to content

Commit

Permalink
update color-quant
Browse files Browse the repository at this point in the history
format code
  • Loading branch information
xVan Turing authored and xVan Turing committed Feb 14, 2019
1 parent afa41f6 commit ab07d17
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 27 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[![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
![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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "cquant",
"version": "0.0.22",
"version": "0.0.23",
"description": "A fast and native image palette generator",
"main": "cquant.js",
"types": "./cquant.d.ts",
"scripts": {
"install": "prebuild-install || cmake-js rebuild",
"rebuild": "cmake-js rebuild",
"rebuild": "cmake-js configure && cmake-js build",
"test": "node ./test/test.js && node ./test/api-test.js && node ./test/perf.js && node ./script/build-deploy.js",
"build": "tsc --build"
"tsc": "tsc --build"
},
"keywords": [
"image-palette",
Expand Down
12 changes: 6 additions & 6 deletions src/addon.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <napi.h>
#include <iostream>
#include "colorquant.h"
#include "async.h"
#include "colorquant.h"
#include <iostream>
#include <napi.h>
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "PaletteAsync"),
Napi::Function::New(env, PaletteAsync));
return exports;
exports.Set(Napi::String::New(env, "PaletteAsync"),
Napi::Function::New(env, PaletteAsync));
return exports;
}
NODE_API_MODULE(cquant, Init)
23 changes: 8 additions & 15 deletions src/async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
#include <napi.h>
class PaletteWorker : public Napi::AsyncWorker {
public:
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(Napi::Function &callback, PIX *pix, int max_color, int max_sub)
: Napi::AsyncWorker(callback), pix(pix), max_color(max_color), cmap(NULL),
max_sub(max_sub) {}
~PaletteWorker() {
if (counter) {
free(counter);
}
if (cmap) {
if (cmap->array) {
free(cmap->array);
Expand All @@ -21,10 +17,7 @@ class PaletteWorker : public Napi::AsyncWorker {
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 Execute() { cmap = pix_median_cut_quant(pix, max_color, 5, max_sub); }
void OnOK() {
Napi::HandleScope scope(Env());

Expand All @@ -36,13 +29,13 @@ class PaletteWorker : public Napi::AsyncWorker {
}

Napi::Array result = Napi::Array::New(Env());
RGB_QUAD *quad = (RGB_QUAD *)cmap->array;
RGBC_QUAD *quad = (RGBC_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]);
item.Set("count", quad[i].count);
result.Set(i, item);
}
Callback().Call({Env().Undefined(), result});
Expand All @@ -52,7 +45,6 @@ class PaletteWorker : public Napi::AsyncWorker {
PIX *pix;
int max_color;
PIXCMAP *cmap;
size_t *counter;
int max_sub;
};
Napi::Value PaletteAsync(const Napi::CallbackInfo &info) {
Expand All @@ -74,7 +66,8 @@ Napi::Value PaletteAsync(const Napi::CallbackInfo &info) {
pix->pixs = buffer.Data();
}
pix->depth = depth;
PaletteWorker *paletteWorker = new PaletteWorker(callback, pix, max_color, max_sub);
PaletteWorker *paletteWorker =
new PaletteWorker(callback, pix, max_color, max_sub);
paletteWorker->Queue();
return info.Env().Undefined();
}
2 changes: 1 addition & 1 deletion test/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var pixels = require('image-pixels');

// (async () => {
// let start1 = Date.now();
// let pixs = await pixels('./img/0.jpg')
// let pixs = await pixels('./img/large.1.jpg')
// var { ids, colors } = palette(pixs)
// let time = Date.now() - start1
// console.log(time)
Expand Down

0 comments on commit ab07d17

Please sign in to comment.