Ejemplo n.º 1
0
func loadSize(data []byte, max int) *image.RGBA {
	i, _, err := image.Decode(bytes.NewBuffer(data))
	if err != nil {
		panic(err)
	}
	//	switch i := i.(type) {
	//	case *image.RGBA64:
	//		i = i.RGBA()
	//	}

	b := i.Bounds()
	dx, dy := max, max
	if b.Dx() > b.Dy() {
		dy = b.Dy() * dx / b.Dx()
	} else {
		dx = b.Dx() * dy / b.Dy()
	}
	var irgba *image.RGBA
	switch i := i.(type) {
	case *image.RGBA:
		irgba = qr.ResizeRGBA(i, i.Bounds(), dx, dy)
	case *image.NRGBA:
		irgba = qr.ResizeNRGBA(i, i.Bounds(), dx, dy)
	}
	return irgba
}
Ejemplo n.º 2
0
// convert2PNG convert any format to PNG
func convert2PNG(i image.Image) bytes.Buffer {
	// Convert image to 128x128 gray+alpha.
	//	i := grayScale(i)
	b := i.Bounds()

	const max = 128
	// If it's gigantic, it's more efficient to downsample first
	// and then resize; resizing will smooth out the roughness.
	var i1 *image.RGBA
	if b.Dx() > 4*max || b.Dy() > 4*max {
		w, h := 2*max, 2*max
		if b.Dx() > b.Dy() {
			h = b.Dy() * h / b.Dx()
		} else {
			w = b.Dx() * w / b.Dy()
		}
		i1 = qr.Resample(i, b, w, h)
	} else {
		// "Resample" to same size, just to convert to RGBA.
		i1 = qr.Resample(i, b, b.Dx(), b.Dy())
	}
	b = i1.Bounds()

	// Encode to PNG.
	dx, dy := 128, 128
	if b.Dx() > b.Dy() {
		dy = b.Dy() * dx / b.Dx()
	} else {
		dx = b.Dx() * dy / b.Dy()
	}
	i128 := qr.ResizeRGBA(i1, i1.Bounds(), dx, dy)

	var buf bytes.Buffer
	if err := png.Encode(&buf, i128); err != nil {
		panic(err)
	}
	return buf
}