Skip to content

Commit

Permalink
[adamchalmers#2] Larger images need to be resampled
Browse files Browse the repository at this point in the history
  • Loading branch information
sssilver committed Jan 16, 2018
1 parent 0b7e77d commit e29e989
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
12 changes: 5 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ $(function() {
i = 0;
$("#start").on("click", function() {
// Load the target and approximation images.
var targetPrefix = "/remote/img?url=";
var approxPrefix = "/approx/img?url=";
var imgUrl = $("#imgUrl").val();
const targetPrefix = "/remote/img?url=";
const approxPrefix = "/approx/img?url=";
const imgUrl = $("#imgUrl").val();
$("#target-image").attr("src", targetPrefix + imgUrl).css("height", "auto").css("width", "auto");
$("#approx-image").attr("src", approxPrefix + imgUrl + "&" + i++).css("height", "auto").css("width", "auto");
console.log("Rendering", imgUrl);
var startTime = (new Date()).getTime();
const startTime = (new Date()).getTime();


// Once the target has loaded, we can check it's within the size limits.
// Once the target has loaded, we can display it
$("#target-image").on("load", function() {
// If it is, show the images and stats.
$("#error").text("");
$(this).show();
$("#approx-image").show();
Expand All @@ -55,5 +54,4 @@ $("#start").on("click", function() {
console.log("Took " + Math.round(((new Date()).getTime()-startTime)/1000) + " seconds.");
});
});

});
37 changes: 31 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/nfnt/resize"
"image"
_ "image/gif"
_ "image/jpeg"
Expand Down Expand Up @@ -61,13 +62,22 @@ func urlParam(r *http.Request) (string, error) {
// This allows us to get around CORS issues.
func remoteHandler(w http.ResponseWriter, r *http.Request) {
img, err := getImg(r)

if err != nil {
log.Println(err)
w.Write([]byte(err.Error()))
return
}

defer img.Close()
io.Copy(w, img)

_target, _, err := image.Decode(img)

ci := make(chan image.Image)
go preprocessImage(_target, ci)
preprocessedImage := <-ci

png.Encode(w, preprocessedImage)
}

// Serves the image generated by approximate() on the URL in the request.
Expand Down Expand Up @@ -98,12 +108,12 @@ func approxHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(err)
return
}
if dx, dy := _target.Bounds().Dx(), _target.Bounds().Dy(); dx > MAXSIZE || dy > MAXSIZE {
msg := fmt.Sprintf("Image of size %v.%v is too large (max size is %v)", dx, dy, MAXSIZE)
io.Copy(w, strings.NewReader(msg))
}

target := toRGBA(_target)
ci := make(chan image.Image)
go preprocessImage(_target, ci)
preprocessedTarget := <-ci

target := toRGBA(preprocessedTarget)

approximation, score := approximate(
target,
Expand All @@ -120,6 +130,21 @@ func approxHandler(w http.ResponseWriter, r *http.Request) {
}}
}

func preprocessImage(image image.Image, cr chan image.Image) {
if dx, dy := image.Bounds().Dx(), image.Bounds().Dy(); dx > MAXSIZE || dy > MAXSIZE {
// Resample the image down to MAXSIZE
msg := fmt.Sprintf("Image of size %v.%v is too large (max size is %v). Downsampling.", dx, dy, MAXSIZE)
log.Println(msg)

cr <- resize.Thumbnail(MAXSIZE,
MAXSIZE,
image,
resize.NearestNeighbor)
} else {
cr <- image
}
}

// Serve a static file.
func fileHandler(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Expand Down

0 comments on commit e29e989

Please sign in to comment.