JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q46852F
  • License MIT

A fast and native image palette generator

Package Exports

  • cquant

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cquant) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

CQuant

Build status Build Status

CQuant-Web is coming soon!!!!!

View Latest Doc on Github

Preview

Screenshot from 2019-02-09 15-16-32.png

Usage

Current Supported Prebuild binary version: Node 6 | 8 | 10 | 11
For Electron user: Prebuild Supported Version v3 and v4.0.4

Install

npm i cquant

Electron User

After running the install command make sure to use electron-rebuild to rebuild it for electron, usually it will just download the prebuild.

Async!

This package is real async. You can run multiple task without blocking the main loop

Basic

const cquant = require('cquant')
// use sharp to conver image to RGB Buffer Array fast and clear
const sharp = require('sharp')
sharp('path/to/image')
  .raw() // convert raw buffer like [RGB RGB RGB RGB]
  .toBuffer((_err, buffer, info) => {
    if (!_err) {
      let colorCount = 4

      cquant.paletteAsync(buffer, info.channels, colorCount).then(res => {

        console.log(res)
      }).catch(err => {

        console.log(err)
      })
    }
  })

API

  /**
   * 
   * @param buffer Image Buffer(RGB/RGBA)
   * @param depth 3 or 4 for RGB/RGBA
   * @param maxColor Color Amout You want
   * @param maxSub max subsample for image, 1 for no sub sample,0 for auto, by default it will scale to size of `1000x1000`
   * @param callback callback with err and result
   */
  function paletteAsync(buffer: Buffer, depth=3, maxColor=5, maxSub=0, callback:CallBackFunc): void;
  interface Color {
      R: number; /*red*/
      G: number; /*green*/
      B: number; /*blue*/
      count: number; /*count*/
  }
  declare type Palette = Color[];
  type CallBackFunc = (err, result: Palette) => void;
  function paletteAsync(buffer: Buffer, depth=3, maxColor=5, maxSub=0): Promise<Palette>;
  

Perf

test result will be diff based on your local machine

JPG 5572 x 3715 (No SubSample)

Program Time(ms)
cquant 60 ms
image-palette N/A

N/A: crashed

JPG 1920 x 1280 (No SubSample)

Program Time(ms)
cquant 12ms
image-palette 950ms

Extra

With async.queue

If you have lots of image to process, the best way to do it is using async.queue for parallel, and controllable

// test/example.js
const myQueue = async.queue(async (filePath) => {
  // note : using the `async` function, so the callback is not needed
  const img = await sharp(filePath)
    .raw() // to raw
    .toBuffer({ resolveWithObject: true })
  const palette = await cquant.paletteAsync(img.data, img.info.channels, 5)
  console.log(palette)
}, os.cpus().length - 1)

Build Your Self

CMake

You need to install CMake based on your System. ### Build Tool To be able to build from the source, you also need the standard build tool based on your OS. #### For Windows User * You can use this awesome app windows-build-tools to auto download all tools needed, if you don't have visual studio installed * If you have already installed a visual studio like vs2017, basicly you just need to enable c++ developement(it's gonna be huge). ### Build it > for more info you can view project cmake-js Basically you need run this command bash cmake-js -r electron -v 4.0.4 rebuild # for electron cmake-js -r node -v 10.0.0 rebuild # for node

xVan Turing 2019