gen-preview.cjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const { fromFile } = require('geotiff')
  2. const sharp = require('sharp')
  3. const fs = require('fs')
  4. const path = require('path')
  5. async function main() {
  6. const tiffPath = path.resolve(__dirname, '../de99928f1689454084d1af33ec3cc155.tif')
  7. const outPath = path.resolve(__dirname, '../public/de99928f-preview.png')
  8. if (!fs.existsSync(tiffPath)) {
  9. console.error('TIFF not found:', tiffPath)
  10. process.exit(1)
  11. }
  12. console.log('Reading TIFF...')
  13. const tiff = await fromFile(tiffPath)
  14. const image = await tiff.getImage()
  15. const w = image.getWidth()
  16. const h = image.getHeight()
  17. console.log('TIFF size:', w, 'x', h)
  18. // Target size matching current preview
  19. const targetW = 6181
  20. const targetH = 8192
  21. console.log('Reading rasters at target size...')
  22. const data = await image.readRasters({ width: targetW, height: targetH, samples: [0, 1, 2] })
  23. const r = data[0]
  24. const g = data[1]
  25. const b = data[2]
  26. console.log('Building RGBA with alpha mask...')
  27. const rgba = Buffer.alloc(targetW * targetH * 4)
  28. let blackCount = 0
  29. for (let i = 0; i < targetW * targetH; i++) {
  30. const isBlack = r[i] < 10 && g[i] < 10 && b[i] < 10
  31. rgba[i * 4] = r[i]
  32. rgba[i * 4 + 1] = g[i]
  33. rgba[i * 4 + 2] = b[i]
  34. rgba[i * 4 + 3] = isBlack ? 0 : 255
  35. if (isBlack) blackCount++
  36. }
  37. console.log('Black pixels masked:', blackCount)
  38. await sharp(rgba, { raw: { width: targetW, height: targetH, channels: 4 } })
  39. .png({ compressionLevel: 6 })
  40. .toFile(outPath)
  41. console.log('Saved:', outPath)
  42. }
  43. main().catch(e => {
  44. console.error(e)
  45. process.exit(1)
  46. })