Example #1
0
func worker(q <-chan *job) {
	for {
		j := <-q
		j.updateStatus("Prepping data", "downloading source image")
		resp, err := http.Get(j.mainImgUrl)
		if err != nil {
			j.updateStatus("Done", "error downloading image")
			log.Println(err)
			continue
		}
		img, _, err := image.Decode(resp.Body)
		if err != nil {
			j.updateStatus("Done", "error decoding image")
			log.Println(err)
			continue
		}
		j.updateStatus(j.status.Status, "verifying library")
		lib, ok := collections[j.gallery]
		if !ok {
			j.updateStatus("Done", "error: gallery does not exist")
			continue
		}
		j.updateStatus("Building mosaic", "")
		reporter := make(chan float64)
		done := make(chan struct{})
		var mosaic image.Image
		go func() {
			mosaic = mosaics.BuildMosaicFromLibrary(img, lib.library, reporter)
			close(done)
		}()
	Loop:
		for {
			select {
			case pct := <-reporter:
				j.updateStatus("Building mosaic", fmt.Sprintf("%.2f%% percent complete", pct))
			case <-done:
				break Loop
			}
		}
		j.updateStatus("Encoding image", "")
		buf := &bytes.Buffer{}
		jpeg.Encode(buf, mosaic, &jpeg.Options{30})
		j.updateStatus("Done", fmt.Sprintf("<img width=1000px src='data:image/jpg;base64,%s'/><br/>Right click to save.<br/><a href='/options'>Build another!</a>", base64.StdEncoding.EncodeToString(buf.Bytes())))
	}
}
Example #2
0
func main() {
	flag.Parse()
	var comparision mosaics.Evaluator
	if *grid == 0 {
		comparision = mosaics.AveragingEvaluator()
	} else {
		comparision = mosaics.GridEvaluator(*grid)
	}
	lib := mosaics.NewLibrary(comparision)
	files, err := ioutil.ReadDir(*thumbDir)
	if err != nil {
		check(err)
	}
	log.Println("Loading thumbnails...")
	for _, file := range files {
		img := parseFile(*thumbDir, file.Name())
		lib.AddImage(img)
	}
	log.Println("Loading target image...")
	var r io.Reader
	if (*img)[0:4] == "http" {
		resp, err := http.Get(*img)
		check(err)
		r = resp.Body
	} else {
		f, err := os.Open(*img)
		check(err)
		defer f.Close()
		r = f
	}

	progress := make(chan float64)
	go func() {
		for pct := range progress {
			log.Printf("%.2f percent done\n", pct)
		}
	}()

	if !*doGif {
		target, _, err := image.Decode(r)
		check(err)
		log.Println("Building mosaic...")
		mos := mosaics.BuildMosaicFromLibrary(target, lib, progress)
		output, err := os.Create(*outputFile)
		check(err)
		defer output.Close()
		err = jpeg.Encode(output, mos, &jpeg.Options{20})
		check(err)
	} else {
		g, err := gif.DecodeAll(r)
		check(err)
		log.Println("Building mosaic...")
		mos, err := mosaics.BuildGifzaic(g, lib, progress)
		check(err)
		output, err := os.Create(*outputFile)
		check(err)
		defer output.Close()
		err = gif.EncodeAll(output, mos)
		check(err)
	}
}