Skip to content

Commit

Permalink
improvements except no improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
raluvy95 committed Jun 1, 2023
1 parent 96feb8c commit 4e42502
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/commands/tools/catppify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ async function generator(msg: Message, args: string[]) {
}
}
const init_time = DateTime.now()
client.sendChannelTyping(msg.channel.id)

await client.sendChannelTyping(msg.channel.id)

const generated = await catppify(link, palette as Palette, noise as Noise)

const final_time = DateTime.now()
const diff = final_time.diff(init_time, ["hours", "minutes", "seconds", "milliseconds"])

client.createMessage(msg.channel.id, `Here you go (Took ${ms(diff.toMillis())})`, {
file: generated,
name: "generated.png"
Expand Down
53 changes: 31 additions & 22 deletions src/utils/catppify.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import { CanvasRenderingContext2D, Image, createCanvas, loadImage } from "canvas"
import { Canvas, loadImage, CanvasRenderingContext2D, Image } from "canvas"
import { readFile } from "fs/promises"

export type Palette = "frappe" | "latte" | "machiatto" | "mocha" | "oled"

export type Noise = "0" | "1" | "2" | "3" | "4"

export async function catppify(url: string, palette: Palette = "mocha", noise: Noise = "4") {
const _img = await loadImage(url)
const out_img = createCanvas(_img.width, _img.height)
const ctx = out_img.getContext("2d")
ctx.drawImage(_img, 0, 0)
const clut_path = `./assets/palette/${palette}/noise_${noise}.png`
let _clut: Image
let ctxC: CanvasRenderingContext2D
try {
_clut = await loadImage(await readFile(clut_path))
const canv = createCanvas(_clut.width, _clut.height)
ctxC = canv.getContext('2d')
ctxC.drawImage(_clut, 0, 0)
} catch (e) {
throw e
}

for (let x = 0; x < _img.width; x++) {
for (let y = 0; y < _img.height; y++) {
async function imageProcessing(ctx: CanvasRenderingContext2D, ctxC: CanvasRenderingContext2D, img: Image) {
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
let chunk = ctx.getImageData(x, y, 1, 1).data

const r = chunk[0]
Expand All @@ -42,11 +26,36 @@ export async function catppify(url: string, palette: Palette = "mocha", noise: N
const c_pixel = ctxC.getImageData(c_x, c_y, 1, 1).data

const out_pixel = [...c_pixel, a]
const fillStyle = `rgba(${out_pixel[0]}, ${out_pixel[1]}, ${out_pixel[2]}, ${(a / 255).toFixed(5)})`
const fillStyle = `rgba(${out_pixel[0]}, ${out_pixel[1]}, ${out_pixel[2]}, ${(a / 255).toFixed(2)})`
ctx.fillStyle = fillStyle
ctx.fillRect(x, y, 1, 1)
}
}

return out_img.toBuffer()
return ctx
}

export async function catppify(url: string, palette: Palette = "mocha", noise: Noise = "4") {
const _img = await loadImage(url)

let img = new Canvas(_img.width, _img.height)
let ctx = img.getContext("2d")
ctx.drawImage(_img, 0, 0)

const clut_path = `./assets/palette/${palette}/noise_${noise}.png`
let _clut: Image
let ctxC: CanvasRenderingContext2D
try {
_clut = await loadImage(await readFile(clut_path))
const canv = new Canvas(_clut.width, _clut.height)
ctxC = canv.getContext('2d')
ctxC.drawImage(_clut, 0, 0)
} catch (e) {
throw e
}

// needs a way to stop blocking other tasks
ctx = await imageProcessing(ctx, ctxC, _img)

return img.toBuffer()
}

0 comments on commit 4e42502

Please sign in to comment.