11# CQuant
22[ ![ Build status] ( https://ci.appveyor.com/api/projects/status/gy8vrvnkhrh9tw1s?svg=true )] ( https://ci.appveyor.com/project/xVanTuring/cquant )
3- ## CQuant-Web is coming soon!!!!!
43[ ![ Build Status] ( https://travis-ci.org/xVanTuring/cquant.svg?branch=master )] ( https://travis-ci.org/xVanTuring/cquant )
4+ ## CQuant-Web is coming soon!!!!!
55## View Latest Doc on [ Github] ( https://github.com/xVanTuring/cquant )
66## Preview
77![ Screenshot from 2019-02-09 15-16-32.png] ( https://i.loli.net/2019/02/09/5c5e7e7b42cd2.png )
1515After running the install command make sure to use electron-rebuild to rebuild it for electron, usually it will just download the prebuild.
1616### Async!
1717This package is real async. You can run multiple task without blocking the main loop
18- ### API
19- ``` ts
20- interface Color {
21- R: number ;
22- G: number ;
23- B: number ;
24- count: number ;
25- }
26- declare type Palette = Color [];
27- type CallBackFunc = (err : Error | undefined | string , result : Palette ) => void ;
28-
29- function paletteAsync(buffer : Buffer , depth = 3 , maxColor = 5 , maxSub = 0 ): Promise <Palette >;
30- /**
31- *
32- * @param buffer Image Buffer(RGB/RGBA)
33- * @param depth 3 or 4 for RGB/RGBA
34- * @param maxColor Color Amout You want
35- * @param maxSub max subsample for image, 1 for no sub sample,0 for auto, by default it will scale to size of `1000x1000`
36- * @param callback callback with err and result
37- */
38- function paletteAsync(buffer : Buffer , depth = 3 , maxColor = 5 , maxSub = 0 , callback : CallBackFunc ): void ;
39-
40- ```
4118### Basic
4219``` js
4320const cquant = require (' cquant' )
44- // work best with sharp for converting image to RAW buffer
21+ // use sharp to conver image to RGB Buffer Array fast and clear
4522const sharp = require (' sharp' )
4623sharp (' path/to/image' )
47- .raw () // convert raw buffer like RGB RGB RGB RGB
48- .toBuffer ((err , buffer , info ) => {
49- if (! err) {
50- // you need to set the buffer and
51- // the depth(only 3 (for RGB) and 4 (for RGBA) are accepted )
52- // you can use callback, or leave it empty for promise
53- let iWantForColor = 4
54- cquant .paletteAsync (buffer, info .channels , iWantForColor).then (res => {
24+ .raw () // convert raw buffer like [RGB RGB RGB RGB]
25+ .toBuffer ((_err , buffer , info ) => {
26+ if (! _err) {
27+ let colorCount = 4
28+
29+ cquant .paletteAsync (buffer, info .channels , colorCount).then (res => {
30+
5531 console .log (res)
5632 }).catch (err => {
33+
5734 console .log (err)
5835 })
5936 }
6037 })
6138```
62- ### With ` async.queue `
63- > If you have lots of image to process, the best way to do it is using [ async] ( https://www.npmjs.com/package/async ) .queue for parallel, and controllable
64- ``` js
65- // test/example.js
66- const myQueue = async .queue (async (filePath ) => {
67- // note : i am using the `async` function, so the callback is not needed
68- const img = await sharp (filePath)
69- .raw () // to raw
70- .toBuffer ({ resolveWithObject: true })
71- const palette = await cquant .paletteAsync (img .data , img .info .channels , 5 )
72- console .log (palette)
73- }, os .cpus ().length - 1 )
39+ ### API
40+ ``` ts
41+ /**
42+ *
43+ * @param buffer Image Buffer(RGB/RGBA)
44+ * @param depth 3 or 4 for RGB/RGBA
45+ * @param maxColor Color Amout You want
46+ * @param maxSub max subsample for image, 1 for no sub sample,0 for auto, by default it will scale to size of `1000x1000`
47+ * @param callback callback with err and result
48+ */
49+ function paletteAsync(buffer : Buffer , depth = 3 , maxColor = 5 , maxSub = 0 , callback : CallBackFunc ): void ;
50+ interface Color {
51+ R: number ; /* red*/
52+ G: number ; /* green*/
53+ B: number ; /* blue*/
54+ count: number ; /* count*/
55+ }
56+ declare type Palette = Color [];
57+ type CallBackFunc = (err , result : Palette ) => void ;
58+ function paletteAsync(buffer : Buffer , depth = 3 , maxColor = 5 , maxSub = 0 ): Promise <Palette >;
59+
7460```
75-
7661## Perf
7762> test result will be diff based on your local machine
7863### JPG 5572 x 3715 (No SubSample)
@@ -88,6 +73,20 @@ const myQueue = async.queue(async (filePath) => {
8873| ---------------| :--------:|
8974| cquant | 12ms |
9075| image-palette | 950ms |
76+ ## Extra
77+ ### With ` async.queue `
78+ > If you have lots of image to process, the best way to do it is using [ async] ( https://www.npmjs.com/package/async ) .queue for parallel, and controllable
79+ ``` js
80+ // test/example.js
81+ const myQueue = async .queue (async (filePath ) => {
82+ // note : using the `async` function, so the callback is not needed
83+ const img = await sharp (filePath)
84+ .raw () // to raw
85+ .toBuffer ({ resolveWithObject: true })
86+ const palette = await cquant .paletteAsync (img .data , img .info .channels , 5 )
87+ console .log (palette)
88+ }, os .cpus ().length - 1 )
89+ ```
9190## Build Your Self
9291### CMake
9392You need to install [ CMake] ( https://cmake.org/download/ ) based on your System.
0 commit comments