Advertisement
Guest User

index.ts

a guest
Nov 27th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as RX from "https://cdn.skypack.dev/rambdax"
  2. import * as Path from "https://deno.land/std@0.116.0/path/mod.ts"
  3. import * as Canvas from "https://deno.land/x/canvas/mod.ts"
  4. import {ImageData} from "https://deno.land/x/canvas@v1.3.0/src/types.ts"
  5.  
  6. // Just run: deno run index.ts --allow-read --allow-write "C:/ANY_DIR/Screenshot.jpg"
  7. // Assemble: deno compile index.ts --allow-read --allow-write
  8.  
  9. interface PathInfo { root: string; dir: string; base: string; ext: string; name: string; path: string; }
  10. interface State { state: string; detail: string; }
  11. interface SizeMap { [key: string]: number[][]; }
  12.  
  13. const NUM_OF_CHANNEL = 4
  14.  
  15. const SIZE_MAP: SizeMap = {
  16.   "1600,900": [[1395, 876], [1560, 900]],
  17.   "1920,1080": [[1670, 1052], [1863, 1080]],
  18. }
  19.  
  20. const rename: (oldName: string) => string =
  21.   (oldName: string) => `${oldName}_erased`
  22.  
  23. const exists: (path: string) => Promise<boolean> =
  24.   RX.tryCatchAsync(RX.pipeAsync(Deno.stat, RX.T), RX.F)
  25.  
  26. function fillWithFirstLine(image: ImageData) {
  27.   const {width, height, data} = image
  28.   for (let y = 0; y < height; y++) {
  29.     for (let x = 0; x < width; x++) {
  30.       const point1 = (y * width + x) * NUM_OF_CHANNEL
  31.       const point2 = x * NUM_OF_CHANNEL
  32.       data[point1 + 0] = data[point2 + 0]
  33.       data[point1 + 1] = data[point2 + 1]
  34.       data[point1 + 2] = data[point2 + 2]
  35.       data[point1 + 3] = data[point2 + 3]
  36.     }
  37.   }
  38.   return image
  39. }
  40.  
  41. async function fillImageIdArea(arg: string): Promise<Uint8Array | null> {
  42.   const image: Canvas.Image = await Canvas.loadImage(arg)
  43.  
  44.   const [imageW, imageH] = [image.width(), image.height()]
  45.   const sizeSignature = [imageW, imageH].toString()
  46.   if (!(sizeSignature in SIZE_MAP)) return null
  47.  
  48.   const [[fromX, fromY], [toX, toY]] = SIZE_MAP[sizeSignature]
  49.   console.debug(`Filling ID area from: { x: ${fromX} y: ${fromY} } to: { x: ${toX} y:${toY} }`)
  50.  
  51.   // load image on canvas
  52.   const canvas: Canvas.EmulatedCanvas2D = Canvas.createCanvas(imageW, imageH)
  53.   const context: Canvas.CanvasRenderingContext2D = canvas.getContext("2d")
  54.   context.drawImage(image, 0, 0)
  55.  
  56.   // fill ID area
  57.   const [idAreaW, idAreaH] = [toX - fromX, toY - fromY]
  58.   const oldIdAreaImage: ImageData = context.getImageData(fromX, fromY, idAreaW, idAreaH)
  59.   const newIdAreaImage: ImageData = fillWithFirstLine(oldIdAreaImage)
  60.   context.putImageData(newIdAreaImage, fromX, fromY)
  61.  
  62.   return canvas.toBuffer()
  63. }
  64.  
  65. async function procPath(pathInfo: PathInfo): Promise<State> {
  66.   if (!(await exists(pathInfo.path))) {
  67.     throw new Error(`Cannot find the specified file: ${pathInfo.path}`)
  68.   }
  69.  
  70.   console.info(`Processing file ${pathInfo.base} ...`)
  71.   const buffer = await fillImageIdArea(pathInfo.path)
  72.   if (buffer === null) {
  73.     throw new Error("Failed to process the image.")
  74.   }
  75.  
  76.   const newFileName = `${rename(pathInfo.name)}${pathInfo.ext}`
  77.   const newPath = Path.resolve(pathInfo.dir, newFileName)
  78.   await Deno.writeFile(newPath, buffer)
  79.   console.debug(`Write file to ${newFileName}`)
  80.  
  81.   return {state: "success", detail: newPath}
  82. }
  83.  
  84. // parse paths
  85. const pathInfos = Deno.args
  86.   .filter(arg => !arg.startsWith("-"))
  87.   .map(path => ({path, ...Path.parse(path)}))
  88.  
  89. // proc files async
  90. for (const pathInfo of pathInfos) {
  91.   await procPath(pathInfo)
  92. }
  93.  
  94. // keep console screen
  95. prompt("Please enter input...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement