code: function generateScaledMegatiles(tileset, pixelsPerMega) {
    const cached = tileset.scaledMegatileCache[pixelsPerMega]
    if (cached !== undefined) {
      return cached
    }
    const megatileCount = tileset.megatiles.length / 0x20
    const out = new Buffer(pixelsPerMega * pixelsPerMega * megatileCount * 3)
    let outPos = 0
    const pixelsPerScaled = 32 / pixelsPerMega
    const centeringOffset = pixelsPerScaled / 4
    for (let i = 0; i < megatileCount; i++) {
      let top = centeringOffset
      let bottom = pixelsPerScaled - centeringOffset
      for (let y = 0; y < pixelsPerMega; y++) {
        let left = centeringOffset
        let right = pixelsPerScaled - centeringOffset
        for (let x = 0; x < pixelsPerMega; x++) {
          const tl = colorAtMega(tileset, i, left, top)
          const tr = colorAtMega(tileset, i, right, top)
          const bl = colorAtMega(tileset, i, left, bottom)
          const br = colorAtMega(tileset, i, right, bottom)
          out[outPos + 0] = (tl[0] + tr[0] + bl[0] + br[0]) / 4
          out[outPos + 1] = (tl[1] + tr[1] + bl[1] + br[1]) / 4
          out[outPos + 2] = (tl[2] + tr[2] + bl[2] + br[2]) / 4
          outPos = outPos + 3
          left = left + pixelsPerScaled
          right = right + pixelsPerScaled
        }
        top = top + pixelsPerScaled
        bottom = bottom + pixelsPerScaled
      }
    }
  
    tileset.scaledMegatileCache[pixelsPerMega] = out
    return out
  }
docs: Creates an array of megatiles, where each megatile has 3 * pixelsPerMega bytes, which are interpolated from all colors by simple average (or you could call it specialized bilinear :p) algorithm. pixelsPerMega must be power of 2. Scaling upwards doesn't generate anything sensible.