[Odin] Modulo magic[Odin] Modulo magic
🖼️
Image Watermark
Week 27, 2026
package main
import "core:fmt"
import "core:image"
import "core:image/bmp"
import "core:image/png"
_ :: png
main :: proc () {
watermark(input_filename="bliss.png", output_filename="result.bmp")
}
watermark :: proc (input_filename, output_filename: string, limit := 100) {
fmt.println("Loading", input_filename)
img, err := image.load_from_file(input_filename)
assert(err == nil)
fmt.println(img)
defer image.destroy(img)
assert(img.width * img.height == len(img.pixels.buf) / 4)
pixels := transmute ([][4] u8) img.pixels.buf[:len(img.pixels.buf)/4]
total := img.width * img.height
stride := total / limit
i, n: int
for i < total {
o := pixels[i]
c := &pixels[i]
switch {
case n==0 : /**/
case n%2==0 : c.b = byte(round_up_to(int(c.b), 4) % 256)
case : c.g = byte(round_up_to(int(c.g), 3) % 256)
}
fmt.printfln(
"num %2d (%d) | idx %8d | pos %4d,%4d | rgb %2x -> %2x",
n, n%3, i, i%img.width, i/img.width, o.rgb, c.rgb,
)
i += stride
n += 1
}
fmt.println("Saving", output_filename)
err = bmp.save(output_filename, img)
assert(err == nil)
fmt.println("All done.")
}
round_up_to :: proc (value, up: int) -> int {
for d in 0..