75 lines
1.5 KiB
Odin
75 lines
1.5 KiB
Odin
package main
|
|
|
|
import "core:fmt"
|
|
import "core:image"
|
|
import "core:image/qoi"
|
|
import "core:image/png"
|
|
import "core:slice"
|
|
import "core:os/os2"
|
|
|
|
import blurhash "../"
|
|
|
|
decode_example :: proc() {
|
|
if len(os2.args) < 3 {
|
|
fmt.eprintln("Please the blurhash as an arg")
|
|
return
|
|
}
|
|
|
|
width := 400
|
|
height := 240
|
|
pixels, err := blurhash.decode(os2.args[2], width, height, 1.2)
|
|
defer delete(pixels)
|
|
if err != nil {
|
|
fmt.eprintln(err)
|
|
return
|
|
}
|
|
|
|
img, ok := image.pixels_to_image(slice.reinterpret([]image.RGB_Pixel, pixels), width, height)
|
|
if !ok {
|
|
fmt.eprintln("Couldn't create an image")
|
|
return
|
|
}
|
|
|
|
save_err := qoi.save_to_file("yup.qoi", &img)
|
|
if save_err != nil {
|
|
fmt.eprintln(save_err)
|
|
return
|
|
}
|
|
}
|
|
|
|
encode_example :: proc() {
|
|
if len(os2.args) < 3 {
|
|
fmt.eprintln("Please provide a path to a png")
|
|
return
|
|
}
|
|
|
|
img, err := image.load_from_file(os2.args[2])
|
|
if err != nil {
|
|
fmt.eprintln(err)
|
|
return
|
|
}
|
|
|
|
bhash, hash_err := blurhash.encode(img.pixels.buf[:], 5, 4, img.width, img.height, img.width*img.channels)
|
|
if hash_err != nil {
|
|
fmt.eprintln(hash_err)
|
|
return
|
|
}
|
|
|
|
fmt.println(bhash)
|
|
}
|
|
|
|
main :: proc() {
|
|
if len(os2.args) < 2 {
|
|
fmt.eprintln("Usage: program \"decode|encode\" <arg>")
|
|
return
|
|
}
|
|
|
|
if os2.args[1] == "decode" {
|
|
decode_example()
|
|
} else if os2.args[1] == "encode" {
|
|
encode_example()
|
|
} else {
|
|
fmt.eprintln("Unknown command")
|
|
}
|
|
}
|