Skip to content

Configuration

pngx provides extensive configuration options for both encoding and decoding PNG images.

Encoding Options

Configure PNG encoding with these options:

typescript
import { PNG } from 'pngx'

const png = new PNG({
  // Image dimensions
  width: 100,
  height: 100,

  // Color settings
  colorType: 6,        // 0=grayscale, 2=RGB, 4=grayscale+alpha, 6=RGBA
  bitDepth: 8,         // 8 or 16 bits per channel
  inputColorType: 6,   // Input data color type
  inputHasAlpha: true, // Input data has alpha channel

  // Compression settings
  deflateLevel: 9,     // 0-9 (0=none, 9=best)
  deflateStrategy: 3,  // 0-4 (zlib strategies)
  deflateChunkSize: 32 * 1024, // Chunk size for compression

  // Other options
  filterType: -1,      // Filter type (-1=auto, 0-4=specific)
})

Color Types

PNG supports several color types:

typescript
// Color type constants
const ColorType = {
  GRAYSCALE: 0,       // Grayscale (1 channel)
  RGB: 2,             // RGB (3 channels)
  INDEXED: 3,         // Palette-based
  GRAYSCALE_ALPHA: 4, // Grayscale + Alpha (2 channels)
  RGBA: 6,            // RGBA (4 channels)
}

// Examples
const grayscale = new PNG({ width: 100, height: 100, colorType: 0 })
const rgb = new PNG({ width: 100, height: 100, colorType: 2 })
const rgba = new PNG({ width: 100, height: 100, colorType: 6 })

Bit Depth

Control the precision of color values:

typescript
// 8-bit (0-255 per channel) - default
const png8 = new PNG({
  width: 100,
  height: 100,
  bitDepth: 8,
})

// 16-bit (0-65535 per channel) - higher precision
const png16 = new PNG({
  width: 100,
  height: 100,
  bitDepth: 16,
})

Compression Settings

Fine-tune compression for size vs. speed:

typescript
// Maximum compression (smallest file, slowest)
const maxCompression = new PNG({
  width: 100,
  height: 100,
  deflateLevel: 9,
  deflateStrategy: 3,
})

// No compression (largest file, fastest)
const noCompression = new PNG({
  width: 100,
  height: 100,
  deflateLevel: 0,
})

// Balanced compression
const balanced = new PNG({
  width: 100,
  height: 100,
  deflateLevel: 6,
  deflateStrategy: 1,
})

Deflate Levels

LevelDescription
0No compression
1Fastest compression
6Default compression
9Best compression

Deflate Strategies

StrategyDescriptionBest For
0DefaultGeneral use
1FilteredFiltered data
2Huffman OnlyFast compression
3RLERun-length encoding
4FixedFixed Huffman codes

Decoding Options

Configure PNG decoding:

typescript
import { PNG } from 'pngx'

// With CRC checking (default)
const png = PNG.sync.read(buffer, {
  checkCRC: true,
})

// Skip CRC checking (faster)
const pngFast = PNG.sync.read(buffer, {
  checkCRC: false,
})

Filter Types

PNG uses row filters for better compression:

typescript
// Auto-select best filter per row
const autoFilter = new PNG({
  width: 100,
  height: 100,
  filterType: -1, // Auto
})

// Force specific filter
const subFilter = new PNG({
  width: 100,
  height: 100,
  filterType: 1, // Sub
})

Filter Options

FilterValueDescription
Auto-1Select best per row
None0No filtering
Sub1Difference from left
Up2Difference from above
Average3Average of left and above
Paeth4Paeth predictor

Complete Configuration Example

typescript
import { PNG } from 'pngx'

// Production configuration for web images
const webConfig = {
  colorType: 6,          // RGBA for transparency support
  bitDepth: 8,           // Standard 8-bit color
  deflateLevel: 9,       // Maximum compression
  deflateStrategy: 3,    // RLE for typical images
  filterType: -1,        // Auto-select filters
}

// High-quality configuration for editing
const editConfig = {
  colorType: 6,
  bitDepth: 16,          // 16-bit for precision
  deflateLevel: 6,       // Balanced compression
  filterType: -1,
}

// Fast processing configuration
const fastConfig = {
  colorType: 6,
  bitDepth: 8,
  deflateLevel: 1,       // Minimal compression
  deflateStrategy: 2,    // Huffman only
  filterType: 0,         // No filtering
}

function createPNG(width: number, height: number, profile: 'web' | 'edit' | 'fast') {
  const configs = { web: webConfig, edit: editConfig, fast: fastConfig }
  return new PNG({ width, height, ...configs[profile] })
}

Environment-Based Configuration

typescript
const isProd = process.env.NODE_ENV === 'production'

const config = {
  deflateLevel: isProd ? 9 : 1,
  deflateStrategy: isProd ? 3 : 0,
}

Next Steps

Released under the MIT License.