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/color"
"image/jpeg"
"log"
"math"
"os"
"runtime"
@ -14,10 +13,10 @@ import (
// job passed to goroutines. blend color from img1 and img2 at position (x, y)
type blendColorJob struct {
X int
Y int
Img1 image.Image
Img2 image.Image
X int
Y int
Img1 image.Image
Img2 image.Image
}
// 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) {
log.Println("Blending the images...")
// dimensions := getMaxDimensions(img1, img2)
dimensions := getDimensions(img1, img2)
// output image, ready to receive pixel values
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
outImg := image.NewRGBA(dimensions)
// TODO: use a worker pool for those operations ?
// resize image
img1Resized := resize(img1)
img2Resized := resize(img2)
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
p.WorkerWG.Wait()
@ -113,9 +103,9 @@ func encodeImage(imgData *image.RGBA) {
outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg")
out, _ := os.Create(outputFile)
defer out.Close()
log.Println("Encoding the new image...")
fmt.Print("Encoding the image...")
jpeg.Encode(out, imgData, nil)
log.Println("Done.")
fmt.Println(" Done.")
}
// 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
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
size1 := img1.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}
}
// 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
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type Config struct {
Method string `json:method`
OutputDir string `json:outputdir`
InputDir string `json:inputdir`
BaseImage string `json:baseimage`
Dimensions string `json:dimensions`
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
Method string
OutputDir string
}
// 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

15
fs.go
View File

@ -23,14 +23,13 @@ func loadImage(filename string) image.Image {
log.Fatal(err)
}
log.Println("Loading :", filename)
return imgData
}
// Walk through a folder recursively and returns a list of image paths
func getImagesList(path string) []string {
var imgs []string
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
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
func getRandomImage() string {
func getRandomImages(number int) []string {
rand.Seed(time.Now().UnixNano())
dir := getImagesList(ConfigRegister.InputDir)
index := rand.Intn(len(dir))
return dir[index]
var images []string
dir := getImagesList("/home/gator/Photos/")
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 (
"flag"
"image"
_ "image/jpeg"
_ "image/png"
)
@ -11,23 +10,12 @@ func main() {
// command line arguments
flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, fuckyfun")
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()
// set output's width and height
ConfigRegister.SetOutputDimensions()
// get two random images and load them
var img1 image.Image
if ConfigRegister.BaseImage != "" {
img1 = loadImage(ConfigRegister.BaseImage)
} else {
img1 = loadImage(getRandomImage())
}
img2 := loadImage(getRandomImage())
imgs := getRandomImages(2)
img1 := loadImage(imgs[0])
img2 := loadImage(imgs[1])
// pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool

View File

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