All files / src/render gif-image.render.js

98.14% Statements 212/216
90% Branches 18/20
100% Functions 12/12
98.14% Lines 212/216

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3363x 3363x 3363x 3363x 3061x 3363x 302x 302x 302x 302x 6x 6x 302x 302x 302x 302x 302x 302x 3363x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 305x     305x 283x 283x 283x 283x 283x 283x 305x 305x 305x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 308x     308x 308x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { ByteArrayOutputStream } from '../utils/bite-array-output-stream'
 
// ---------------------------------------------------------------------
// gifImage (B/W)
// ---------------------------------------------------------------------
 
/**
 *
 * @param {number} width - image width
 * @param {number} height - image height
 * @returns {BlackWhiteGIFImage} Black and white GIF image
 */
export function gifImage (width, height) {
  const _width = width
  const _height = height
  const _data = new Array(width * height).fill(1)
 
  return {
    setPixel: (x, y, pixel) => { _data[y * _width + x] = pixel },
    write (out) {
      // ---------------------------------
      // GIF Signature
 
      out.writeString('GIF87a')
 
      // ---------------------------------
      // Screen Descriptor
 
      out.writeShort(_width)
      out.writeShort(_height)
 
      out.writeByte(0x80) // 2bit
      out.writeByte(0)
      out.writeByte(0)
 
      // ---------------------------------
      // Global Color Map
 
      // black
      out.writeByte(0x00)
      out.writeByte(0x00)
      out.writeByte(0x00)
 
      // white
      out.writeByte(0xff)
      out.writeByte(0xff)
      out.writeByte(0xff)
 
      // ---------------------------------
      // Image Descriptor
 
      out.writeString(',')
      out.writeShort(0)
      out.writeShort(0)
      out.writeShort(_width)
      out.writeShort(_height)
      out.writeByte(0)
 
      // ---------------------------------
      // Local Color Map
 
      // ---------------------------------
      // Raster Data
 
      const lzwMinCodeSize = 2
      const raster = getLZWRaster(_data, lzwMinCodeSize)
 
      out.writeByte(lzwMinCodeSize)
 
      let offset = 0
 
      while (raster.length - offset > 255) {
        out.writeByte(255)
        out.writeBytes(raster, offset, 255)
        offset += 255
      }
 
      out.writeByte(raster.length - offset)
      out.writeBytes(raster, offset, raster.length - offset)
      out.writeByte(0x00)
 
      // ---------------------------------
      // GIF Terminator
      out.writeString(';')
    },
  }
};
 
/**
 * @param {number[]} data - gif pixel data
 * @param {number} lzwMinCodeSize - lzw minimum code size
 */
function getLZWRaster (data, lzwMinCodeSize) {
  const clearCode = 1 << lzwMinCodeSize
  const endCode = (1 << lzwMinCodeSize) + 1
  let bitLength = lzwMinCodeSize + 1
 
  // Setup LZWTable
  const table = lzwTable()
 
  for (let i = 0; i < clearCode; i += 1) {
    table.add(String.fromCodePoint(i))
  }
  table.add(String.fromCodePoint(clearCode))
  table.add(String.fromCodePoint(endCode))
 
  const byteOut = new ByteArrayOutputStream()
  const bitOut = bitOutputStream(byteOut)
 
  // clear code
  bitOut.write(clearCode, bitLength)
 
  let dataIndex = 0
 
  let s = String.fromCodePoint(data[dataIndex])
  dataIndex += 1
 
  while (dataIndex < data.length) {
    const c = String.fromCodePoint(data[dataIndex])
    dataIndex += 1
 
    if (table.contains(s + c)) {
      s = s + c
    } else {
      bitOut.write(table.indexOf(s), bitLength)
 
      if (table.size < 0xfff) {
        if (table.size === (1 << bitLength)) {
          bitLength += 1
        }
 
        table.add(s + c)
      }
 
      s = c
    }
  }
 
  bitOut.write(table.indexOf(s), bitLength)
 
  // end code
  bitOut.write(endCode, bitLength)
 
  bitOut.flush()
 
  return byteOut.toByteArray()
}
 
/** @param {import('../utils/bite-array-output-stream').ByteArrayOutputStream} out - output stream */
function bitOutputStream (out) {
  const _out = out
  let _bitLength = 0
  let _bitBuffer = 0
 
  return {
  /**
   * @param {number} data - numeric data
   * @param {number} length - data bit length
   */
    write (data, length) {
      if ((data >>> length) !== 0) {
        throw Error('length over')
      }
      while (_bitLength + length >= 8) {
        _out.writeByte(0xff & ((data << _bitLength) | _bitBuffer))
        length -= (8 - _bitLength)
        data >>>= (8 - _bitLength)
        _bitBuffer = 0
        _bitLength = 0
      }
 
      _bitBuffer = (data << _bitLength) | _bitBuffer
      _bitLength = _bitLength + length
    },
    flush: () => { _bitLength > 0 && _out.writeByte(_bitBuffer) },
  }
}
 
/**
 * @returns {LzwTable} created LZW table
 */
function lzwTable () {
  /** @type {Record<string,number>} */
  const _map = {}
  let _size = 0
 
  const _this = /**  @type {LzwTable} */ ({
    add: (key) => {
      if (_this.contains(key)) {
        throw Error(`dup key: ${key}`)
      }
      _map[key] = _size
      _size += 1
    },
    get size () { return _size },
    indexOf: (key) => _map[key],
    contains: (key) => _map[key] != null,
  })
 
  return _this
}
 
/**
 * @typedef {object} LzwTable
 * @property {(key: string) => void} add - add key to table
 * @property {number} size - table size
 * @property {(key: string) => number} indexOf - get index of key
 * @property {(key: string) => boolean} contains - check if key is already in table
 */
 
/**
 * @typedef {object} BlackWhiteGIFImage
 * Black and white GIF image
 * @property {(x: number, y: number, pixel: number) => void} setPixel - set pixel in position
 * @property {(out: import('../utils/bite-array-output-stream').ByteArrayOutputStream) => void} write - write to output stream
 */