Example #1
13
func thumbnailHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		filename, err := url.QueryUnescape(r.URL.String())
		checkErr(err)

		fmt.Println("handling request for", filename)

		file, err := os.Open(filename)
		checkErr(err)
		defer file.Close()

		img, filetype, err := image.Decode(file)
		checkErr(err)

		var newImage image.Image

		if thumbnailSize < img.Bounds().Dx() || thumbnailSize < img.Bounds().Dy() {
			if img.Bounds().Dx() > img.Bounds().Dy() {
				newImage = resize.Resize(uint(thumbnailSize), 0, img, resize.Lanczos3)
			} else {
				newImage = resize.Resize(0, uint(thumbnailSize), img, resize.Lanczos3)
			}
		} else {
			newImage = img
		}

		switch filetype {
		case "png":
			err = png.Encode(w, newImage)
		case "gif":
			err = gif.Encode(w, newImage, nil)
		case "jpeg", "jpg":
			err = jpeg.Encode(w, newImage, nil)
		case "bmp":
			err = bmp.Encode(w, newImage)
		case "tiff":
			err = tiff.Encode(w, newImage, nil)
		default:
			// not sure how you got here but what are we going to do with you?
			fmt.Println("Unknown image type: ", filetype)
			io.Copy(w, file)
		}
		checkErr(err)
		fmt.Println("done handling request")

	})
}
Example #2
0
func main() {
	var md5sum string
	var rip = "10.242.87.153"
	var lastImg *image.RGBA
	http.HandleFunc("/connect", func(w http.ResponseWriter, r *http.Request) {
		serialNo := r.URL.Query().Get("serialno")
		fmt.Println(strings.Split(r.RemoteAddr, ":")[0], serialNo)
		rip = strings.Split(r.RemoteAddr, ":")[0]

		io.WriteString(w, "connected")
	})
	http.HandleFunc("/screen.png", func(w http.ResponseWriter, r *http.Request) {
		resp, err := http.Get(fmt.Sprintf("http://%s:21000/patch.snappy?md5sum="+md5sum, rip))
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		defer resp.Body.Close()
		if resp.StatusCode == http.StatusNotModified {
			png.Encode(w, lastImg)
			return
		}
		data, _ := ioutil.ReadAll(resp.Body)
		rawPixes, _ := snappy.Decode(nil, data)
		_ = rawPixes
		fmt.Println(resp.Header)

		hdr := resp.Header
		md5sum = hdr.Get("X-Md5sum")
		var isPatch = hdr.Get("X-Patch") == "true"
		var width, height int
		fmt.Sscanf(hdr.Get("X-Width")+" "+hdr.Get("X-Height"), "%d %d", &width, &height)

		img := image.NewRGBA(image.Rectangle{image.ZP, image.Point{width, height}})
		img.Pix = rawPixes
		if isPatch {
			lastImg, _ = pngdiff.Patch(lastImg, img)
			start := time.Now()
			bmp.Encode(w, lastImg)
			//png.Encode(w, lastImg)
			fmt.Println("patch:", time.Now().Sub(start))
			return
		} else {
			lastImg = img
			//png.Encode(w, img)
			bmp.Encode(w, lastImg)
		}
	})
	log.Fatal(http.ListenAndServe(":9000", nil))
}
Example #3
0
File: io.go Project: crhym3/imgdiff
func writeImage(dst string, mf string, m image.Image) {
	var err error
	w := os.Stdout
	if dst != "-" {
		w, err = os.Create(dst)
		if err != nil {
			log.Fatal(err)
		}
	}
	if ext := filepath.Ext(dst); mf == "" && ext != "" {
		mf = ext[1:]
	}
	switch mf {
	default:
		err = png.Encode(w, m)
	case "jpg", "jpeg":
		err = jpeg.Encode(w, m, nil)
	case "gif":
		err = gif.Encode(w, m, nil)
	case "tif", "tiff":
		err = tiff.Encode(w, m, nil)
	case "bmp":
		err = bmp.Encode(w, m)
	}
	if err != nil {
		log.Fatal(err)
	}
}
Example #4
0
func encode(w io.Writer, img image.Image, format imaging.Format, quality int) error {
	var err error
	switch format {
	case imaging.JPEG:
		var rgba *image.RGBA
		if nrgba, ok := img.(*image.NRGBA); ok {
			if nrgba.Opaque() {
				rgba = &image.RGBA{
					Pix:    nrgba.Pix,
					Stride: nrgba.Stride,
					Rect:   nrgba.Rect,
				}
			}
		}
		if rgba != nil {
			err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality})
		} else {
			err = jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
		}

	case imaging.PNG:
		err = png.Encode(w, img)
	case imaging.GIF:
		err = gif.Encode(w, img, &gif.Options{NumColors: 256})
	case imaging.TIFF:
		err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	case imaging.BMP:
		err = bmp.Encode(w, img)
	default:
		err = imaging.ErrUnsupportedFormat
	}
	return err
}
Example #5
0
func saveTemp(i image.Image) {
	p, err := os.Create("temp.bmp")
	if err != nil {
		panic(err)
	}
	err = bmp.Encode(p, i)
	if err != nil {
		panic(err)
	}
}
Example #6
0
// WriteBMP writes the image data to a BMP file specified by imgPath.
// WriteBMP creates the named file using mode 0666 (before umask), truncating
// it if it already exists
func WriteBMP(imgPath string, img image.Image) (err error) {
	fw, err := os.Create(imgPath)
	if err != nil {
		return err
	}
	defer fw.Close()
	if err := bmp.Encode(fw, img); err != nil {
		return err
	}
	return nil
}
Example #7
0
func Fuzz(data []byte) int {
	cfg, err := bmp.DecodeConfig(bytes.NewReader(data))
	if err != nil {
		return 0
	}
	if cfg.Width*cfg.Height > 1e6 {
		return 0
	}
	if cfg.Width*cfg.Height == 0 {
		// Workaround for a know bug.
		return 0
	}
	img, err := bmp.Decode(bytes.NewReader(data))
	if err != nil {
		return 0
	}
	var w bytes.Buffer
	err = bmp.Encode(&w, img)
	if err != nil {
		panic(err)
	}
	enc := w.Bytes()
	img1, err := bmp.Decode(&w)
	if err != nil {
		panic(err)
	}
	var w1 bytes.Buffer
	err = bmp.Encode(&w1, img1)
	if err != nil {
		panic(err)
	}
	enc1 := w1.Bytes()
	if !bytes.Equal(enc, enc1) {
		fmt.Printf("image1: [%v]%v\n", len(enc), hex.EncodeToString(enc))
		fmt.Printf("image2: [%v]%v\n", len(enc1), hex.EncodeToString(enc1))
		panic("not equal")
	}
	return 1
}
Example #8
0
/*
Close finishes encoding data into the image. This will cause the image data to
be written to the new_img file provided upon creation.
*/
func (img *StegoImgWriter) Close() error {

	// calculate the total size of the file
	var size uint32 = uint32(len(img.data) - 4)

	// set the size into the data
	binary.BigEndian.PutUint32(img.data[:4], size)

	// the data byte to write
	pos := 0

	// for each row in the image
	for y := 0; y < img.orig_img.Bounds().Max.Y; y++ {

		// for each column in the row
		for x := 0; x < img.orig_img.Bounds().Max.X; x++ {

			// get the color
			col := img.orig_img.At(x, y)

			//get the values
			var vals [3]uint32
			vals[0], vals[1], vals[2], _ = col.RGBA()

			// for each color value in the pixel
			for v := 0; v < 3; v++ {

				// check if there's data to encode
				if pos < len(img.data) && pos < len(img.data) {

					// Encode the byte into the value
					vals[v] = vals[v] & 0xFF00
					vals[v] = vals[v] | uint32(img.data[pos])
					pos++

				}

				// else no data to encode, nothing to do

			} // end of each pixel

			// write the pixel to the new image
			img.new_img.SetNRGBA64(x, y, color.NRGBA64{uint16(vals[0]), uint16(vals[1]), uint16(vals[2]), 0xFFFF})

		} // end of each column

	} // end of each row

	img.is_open = false

	// determine the format to encode into
	switch img.format {
	case "png":
		png.Encode(img.output, img.new_img)
	case "jpeg":
		jpeg.Encode(img.output, img.new_img, &jpeg.Options{100})
	case "gif":
		gif.Encode(img.output, img.new_img, &gif.Options{256, nil, nil})
	case "bmp":
		bmp.Encode(img.output, img.new_img)
	default:
		return errors.New("Somehow got an unaccepted format of " + img.format)
	}

	return nil

}
Example #9
0
// Encode implements Encoder.
func (enc *Encoder) Encode(w io.Writer, nim image.Image, params imageserver.Params) error {
	return bmp.Encode(w, nim)
}