Compare commits

..

No commits in common. "bffd1d542b82cf7e20bdb64a66e1ead2fd6b2e33" and "09b64a7fad482c979c006f3dd9a314701955f882" have entirely different histories.

6 changed files with 24 additions and 109 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
*.jpg
*.png

View File

@ -5,7 +5,6 @@ import (
"image" "image"
"image/color" "image/color"
"image/jpeg" "image/jpeg"
"log"
"math" "math"
"os" "os"
"runtime" "runtime"
@ -14,10 +13,10 @@ import (
// job passed to goroutines. blend color from img1 and img2 at position (x, y) // job passed to goroutines. blend color from img1 and img2 at position (x, y)
type blendColorJob struct { type blendColorJob struct {
X int X int
Y int Y int
Img1 image.Image Img1 image.Image
Img2 image.Image Img2 image.Image
} }
// new color after blend, to apply at position (x, y) // new color after blend, to apply at position (x, y)
@ -83,21 +82,12 @@ func (p *blendWorkerPool) SetImageWorker(newImage *image.RGBA) {
} }
func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) { func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
log.Println("Blending the images...") dimensions := getDimensions(img1, img2)
// dimensions := getMaxDimensions(img1, img2)
// output image, ready to receive pixel values // output image, ready to receive pixel values
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
outImg := image.NewRGBA(dimensions) outImg := image.NewRGBA(dimensions)
// TODO: use a worker pool for those operations ?
// resize image
img1Resized := resize(img1)
img2Resized := resize(img2)
p.RunWorkers(outImg) p.RunWorkers(outImg)
p.SendBlendJobs(dimensions, img1Resized, img2Resized) p.SendBlendJobs(dimensions, img1, img2)
// first waitgroup to wait for the results to be ready before closing the channel // first waitgroup to wait for the results to be ready before closing the channel
p.WorkerWG.Wait() p.WorkerWG.Wait()
@ -113,9 +103,9 @@ func encodeImage(imgData *image.RGBA) {
outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg") outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg")
out, _ := os.Create(outputFile) out, _ := os.Create(outputFile)
defer out.Close() defer out.Close()
log.Println("Encoding the new image...") fmt.Print("Encoding the image...")
jpeg.Encode(out, imgData, nil) jpeg.Encode(out, imgData, nil)
log.Println("Done.") fmt.Println(" Done.")
} }
// convert RGBA pixel to grayscale // convert RGBA pixel to grayscale
@ -173,7 +163,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
} }
// creates a new rectangle with the min height and width from both images // creates a new rectangle with the min height and width from both images
func getMaxDimensions(img1 image.Image, img2 image.Image) image.Rectangle { func getDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
// get dimensions for both images // get dimensions for both images
size1 := img1.Bounds().Size() size1 := img1.Bounds().Size()
size2 := img2.Bounds().Size() size2 := img2.Bounds().Size()
@ -188,30 +178,3 @@ func getMaxDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
return image.Rectangle{upLeft, lowRight} return image.Rectangle{upLeft, lowRight}
} }
// resizes image using dimensions from config register (nearest neighbour interpolation)
func resize(img image.Image) (resized *image.RGBA) {
imgSize := img.Bounds().Size()
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
xscale := float64(imgSize.X) / float64(dimensions.Max.X)
yscale := float64(imgSize.Y) / float64(dimensions.Max.Y)
// creates new rescaled image based on a given image dimensions
resized = image.NewRGBA(dimensions)
// get pixels from the original image
for x := 0; x < dimensions.Max.X; x++ {
for y := 0; y < dimensions.Max.Y; y++ {
xp := int(math.Floor(float64(x) * xscale))
yp := int(math.Floor(float64(y) * yscale))
pixel := img.At(xp, yp)
resized.Set(x, y, pixel)
}
}
return
}
// returns a Rectangle of dimensions <width>x<height>
func dimensionsToRectangle(width int, height int) image.Rectangle {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
return image.Rectangle{upLeft, lowRight}
}

View File

@ -1,45 +1,8 @@
package main package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type Config struct { type Config struct {
Method string `json:method` Method string
OutputDir string `json:outputdir` OutputDir string
InputDir string `json:inputdir`
BaseImage string `json:baseimage`
Dimensions string `json:dimensions`
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
} }
// loads configuration values from json file
func (c *Config) LoadConfigFromFile(filePath string) {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Config{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Method)
fmt.Println(configuration.OutputDir)
}
// Set output file dimensions using string symbol (<width>x<height>)
func (c *Config) SetOutputDimensions() {
split := strings.Split(c.Dimensions, "x")
width, _ := strconv.Atoi(split[0])
height, _ := strconv.Atoi(split[1])
c.OutputWidth = width
c.OutputHeight = height
}
// global register to acccess configuration values
var ConfigRegister Config var ConfigRegister Config

15
fs.go
View File

@ -23,14 +23,13 @@ func loadImage(filename string) image.Image {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Loading :", filename)
return imgData return imgData
} }
// Walk through a folder recursively and returns a list of image paths // Walk through a folder recursively and returns a list of image paths
func getImagesList(path string) []string { func getImagesList(path string) []string {
var imgs []string var imgs []string
err := filepath.Walk(path, err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error { func(path string, info os.FileInfo, err error) error {
ext := strings.ToLower(filepath.Ext(path)) ext := strings.ToLower(filepath.Ext(path))
@ -51,9 +50,13 @@ func getImagesList(path string) []string {
} }
// Randomly choose x number of image from a given folder // Randomly choose x number of image from a given folder
func getRandomImage() string { func getRandomImages(number int) []string {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
dir := getImagesList(ConfigRegister.InputDir) var images []string
index := rand.Intn(len(dir)) dir := getImagesList("/home/gator/Photos/")
return dir[index] for i := 0; i < number; i++ {
index := rand.Intn(len(dir))
images = append(images, dir[index])
}
return images
} }

18
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"image"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
) )
@ -11,23 +10,12 @@ func main() {
// command line arguments // command line arguments
flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, fuckyfun") flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, fuckyfun")
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory") flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory")
flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from")
flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "800x600", "Out image dimensions. <width>x<height>")
flag.Parse() flag.Parse()
// set output's width and height
ConfigRegister.SetOutputDimensions()
// get two random images and load them // get two random images and load them
var img1 image.Image imgs := getRandomImages(2)
if ConfigRegister.BaseImage != "" { img1 := loadImage(imgs[0])
img1 = loadImage(ConfigRegister.BaseImage) img2 := loadImage(imgs[1])
} else {
img1 = loadImage(getRandomImage())
}
img2 := loadImage(getRandomImage())
// pool of workers unionizing, ready to blend a new picture using the power of friendship // pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool var pool blendWorkerPool

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"testing" "testing"
) )
func TestMain(t *testing.T) { func TestMain(t *testing.T) {