Parent directory

main.go

2272 bytes
  1//
  2// main.go
  3// Copyright (C) 2019 pavle <pavle.portic@tilda.center>
  4//
  5// Distributed under terms of the BSD-3-Clause license.
  6//
  7
  8package main
  9
 10import (
 11	"fmt"
 12	"image"
 13	"image/color"
 14	_ "image/jpeg"
 15	"image/png"
 16	"log"
 17	// "math"
 18	"os"
 19	"time"
 20)
 21
 22func elapsed() func() {
 23	start := time.Now()
 24	return func() {
 25		fmt.Printf("%v\n", time.Since(start))
 26	}
 27}
 28
 29const width uint = 512
 30
 31func main() {
 32	defer elapsed()()
 33
 34	imgfile, err := os.Open("in.jpg")
 35	if err != nil {
 36		log.Fatal(err)
 37	}
 38	defer imgfile.Close()
 39
 40	img, _, err := image.Decode(imgfile)
 41	if err != nil {
 42		log.Fatal(err)
 43	}
 44
 45	bounds := img.Bounds()
 46	scale := float64(width) / float64(bounds.Max.X - bounds.Min.X)
 47	coeff := uint8(scale * 4)
 48	if coeff == 0 {
 49		coeff = 1
 50	}
 51	fmt.Printf("scale: %v, coeff: %v\n", scale, coeff)
 52
 53	scope := image.NewRGBA(image.Rect(0, 0, int(width), 256))
 54	scope_bounds := scope.Bounds()
 55
 56	for y := scope_bounds.Min.Y; y < scope_bounds.Max.Y; y++ {
 57		for x := scope_bounds.Min.X; x < scope_bounds.Max.X; x++ {
 58			scope.SetRGBA(x, y, color.RGBA{0, 0, 0, 255})
 59		}
 60	}
 61
 62	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
 63		for x := bounds.Min.X; x < bounds.Max.X; x++ {
 64			r, g, b, _:= img.At(x, y).RGBA()
 65			ir := uint8(r >> 8)
 66			ig := uint8(g >> 8)
 67			ib := uint8(b >> 8)
 68
 69			tr, tg, tb, _ := scope.At(int(float64(x) * scale), 255 - int(ir)).RGBA()
 70			sr := uint8(tr >> 8)
 71			sg := uint8(tg >> 8)
 72			sb := uint8(tb >> 8)
 73			if sr > 255 - coeff {
 74				sr = 255
 75			} else {
 76				sr += coeff
 77			}
 78			scope.SetRGBA(int(float64(x) * scale), 255 - int(ir), color.RGBA{sr, sg, sb, 255})
 79
 80			tr, tg, tb, _ = scope.At(int(float64(x) * scale), 255 - int(ig)).RGBA()
 81			sr = uint8(tr >> 8)
 82			sg = uint8(tg >> 8)
 83			sb = uint8(tb >> 8)
 84			if sg > 255 - coeff {
 85				sg = 255
 86			} else {
 87				sg += coeff
 88			}
 89			scope.SetRGBA(int(float64(x) * scale), 255 - int(ig), color.RGBA{sr, sg, sb, 255})
 90
 91			tr, tg, tb, _ = scope.At(int(float64(x) * scale), 255 - int(ib)).RGBA()
 92			sr = uint8(tr >> 8)
 93			sg = uint8(tg >> 8)
 94			sb = uint8(tb >> 8)
 95			if sb > 255 - coeff {
 96				sb = 255
 97			} else {
 98				sb += coeff
 99			}
100			scope.SetRGBA(int(float64(x) * scale), 255 - int(ib), color.RGBA{sr, sg, sb, 255})
101		}
102	}
103
104	outFile, _ := os.Create("output.png")
105	defer outFile.Close()
106	png.Encode(outFile, scope)
107}
108