Пример #1
0
// Sets skin.Processed to an isometric render of the head from a top-left angle (showing 3 sides).
func (skin *mcSkin) GetCube(width int) error {
	// Crop out the top of the head
	topFlat := imaging.Crop(skin.Image, image.Rect(8, 0, 16, 8))
	// Resize appropriately, so that it fills the `width` when rotated 45 def.
	topFlat = imaging.Resize(topFlat, int(float64(width)*math.Sqrt(2)/3+1), 0, imaging.NearestNeighbor)
	// Create the Gift filter
	filter := gift.New(
		gift.Rotate(45, color.Transparent, gift.LinearInterpolation),
	)
	bounds := filter.Bounds(topFlat.Bounds())
	top := image.NewNRGBA(bounds)
	// Draw it on the filter, then smush it!
	filter.Draw(top, topFlat)
	top = imaging.Resize(top, width+2, width/3, imaging.NearestNeighbor)
	// Skew the front and sides at 15 degree angles to match up with the
	// head that has been smushed
	front := skin.cropHead(skin.Image).(*image.NRGBA)
	side := imaging.Crop(skin.Image, image.Rect(0, 8, 8, 16))
	front = imaging.Resize(front, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
	side = imaging.Resize(side, width/2, int(float64(width)/1.75), imaging.NearestNeighbor)
	front = skewVertical(front, math.Pi/12)
	side = skewVertical(imaging.FlipH(side), math.Pi/-12)

	// Create a new image to assemble upon
	skin.Processed = image.NewNRGBA(image.Rect(0, 0, width, width))
	// Draw each side
	draw.Draw(skin.Processed.(draw.Image), image.Rect(0, width/6, width/2, width), side, image.Pt(0, 0), draw.Src)
	draw.Draw(skin.Processed.(draw.Image), image.Rect(width/2, width/6, width, width), front, image.Pt(0, 0), draw.Src)
	// Draw the top we created
	draw.Draw(skin.Processed.(draw.Image), image.Rect(-1, 0, width+1, width/3), top, image.Pt(0, 0), draw.Over)

	return nil
}
Пример #2
0
func (ipo *ImageProcessorOptions) ProcessImage(source *Image) (*Image, error) {
	dest := &Image{format: source.format}

	var wip image.Image

	if ipo.ScaleX == -1 {
		wip = imaging.FlipH(source.image)
	} else {
		wip = source.image
	}

	if ipo.X != 0 || ipo.Y != 0 || ipo.Width != source.Width() || ipo.Height != source.Height() {
		r := image.Rectangle{image.Point{int(ipo.X), int(ipo.Y)}, image.Point{int(ipo.X + ipo.Width), int(ipo.Y + ipo.Height)}}
		wip = imaging.Crop(wip, r)
	}

	var err error
	switch dest.format {
	case JPEG:
		err = jpeg.Encode(&dest.buffer, wip, &jpeg.Options{int(ipo.Quality)})
	case PNG:
		err = png.Encode(&dest.buffer, wip)
	default:
		return nil, errors.New("attempt to encode to unsupported image format")
	}

	return dest, err
}
Пример #3
0
// Returns the legs.
func (skin *mcSkin) renderLowerBody() *image.NRGBA {
	// This will be the base.
	lowerBodyImg := image.NewNRGBA(image.Rect(0, 0, LlWidth+RlWidth, LlHeight))

	rlImg := imaging.Crop(skin.Image, image.Rect(RlX, RlY, RlX+RlWidth, RlY+RlHeight))

	// If it's an old skin, they don't have a Left Leg, so we'll just flip their right.
	var llImg image.Image
	if skin.is18Skin() {
		llImg = imaging.Crop(skin.Image, image.Rect(LlX, LlY, LlX+LlWidth, LlY+LlHeight))
	} else {
		llImg = imaging.FlipH(rlImg)
	}

	return skin.drawLower(lowerBodyImg, rlImg, llImg.(*image.NRGBA))
}
Пример #4
0
func Extract(img image.Image, name string, X, Y, W, H float32, rotated bool) error {
	dst := imaging.Crop(img, image.Rect(int(X), int(Y), int(X+W), int(Y+H)))
	if rotated {
		log.Fatal("图片被旋转过")
	}
	return imgutil.SaveImg(name, dst)
}
Пример #5
0
func ResizeHandler(w http.ResponseWriter, r *http.Request) {
	// parse url vars
	v := mux.Vars(r)
	width, _ := v["width"]
	height, _ := v["height"]
	x, _ := strconv.Atoi(width)
	y, _ := strconv.Atoi(height)

	imageUrl, _ := v["imageUrl"]
	m := getImg(imageUrl)

	cropBox, _ := getFillCrop(m, float32(x)/float32(y))
	cropRect := image.Rect(cropBox.X, cropBox.Y, cropBox.X+cropBox.Width, cropBox.Y+cropBox.Height)
	croppedImg := imaging.Crop(m, cropRect)

	// convert to opencv image
	//srcImage := opencv.FromImage(m)
	//if srcImage == nil {
	//  fmt.Printf("Couldn't create opencv Image")
	//}
	//defer srcImage.Release()
	//croppedImage := opencv.Crop(srcImage, cropBox.X, cropBox.Y, cropBox.Width, cropBox.Height)
	//resizedImage := opencv.Resize(croppedImage, x, y, opencv.CV_INTER_LINEAR)
	resizedImage := imaging.Resize(croppedImg, x, y, imaging.CatmullRom)
	jpeg.Encode(w, resizedImage, &jpeg.Options{Quality: 90})
}
Пример #6
0
// Returns the head of the skin image overlayed with the helm.
func (skin *mcSkin) cropHelm(img image.Image) image.Image {
	headImg := skin.cropHead(img)
	helmImg := imaging.Crop(img, image.Rect(HelmX, HelmY, HelmX+HeadWidth, HelmY+HeadHeight))
	skin.removeAlpha(helmImg)
	fastDraw(headImg.(*image.NRGBA), helmImg, 0, 0)

	return headImg
}
Пример #7
0
// Returns the legs but with any armor which the user has.
func (skin *mcSkin) renderLowerArmor() *image.NRGBA {
	// This will be the base.
	lowerArmorBodyImg := skin.renderLowerBody()

	// If it's an old skin, they don't have armor here.
	if skin.is18Skin() {
		// Get the armor layers from the skin and remove the Alpha.
		ll2Img := imaging.Crop(skin.Image, image.Rect(Ll2X, Ll2Y, Ll2X+LlWidth, Ll2Y+LlHeight))
		skin.removeAlpha(ll2Img)

		rl2Img := imaging.Crop(skin.Image, image.Rect(Rl2X, Rl2Y, Rl2X+RlWidth, Rl2Y+RlHeight))
		skin.removeAlpha(rl2Img)

		return skin.drawLower(lowerArmorBodyImg, ll2Img, rl2Img)
	}
	return lowerArmorBodyImg
}
Пример #8
0
// Returns the torso and arms.
func (skin *mcSkin) renderUpperBody() *image.NRGBA {
	// This will be the base.
	upperBodyImg := image.NewNRGBA(image.Rect(0, 0, LaWidth+TorsoWidth+RaWidth, TorsoHeight))

	torsoImg := imaging.Crop(skin.Image, image.Rect(TorsoX, TorsoY, TorsoX+TorsoWidth, TorsoY+TorsoHeight))
	raImg := imaging.Crop(skin.Image, image.Rect(RaX, RaY, RaX+RaWidth, RaY+TorsoHeight))

	// If it's an old skin, they don't have a Left Arm, so we'll just flip their right.
	var laImg image.Image
	if skin.is18Skin() {
		laImg = imaging.Crop(skin.Image, image.Rect(LaX, LaY, LaX+LaWidth, LaY+TorsoHeight))
	} else {
		laImg = imaging.FlipH(raImg)
	}

	return skin.drawUpper(upperBodyImg, torsoImg, raImg, laImg.(*image.NRGBA))
}
Пример #9
0
func resizeThumbnail(from *bytes.Buffer, spec *ThumbnailSpec) (to io.Reader, w int, h int, err error) {

	src, name, err := image.Decode(from)
	if err != nil {
		return
	}
	srcB := src.Bounds()

	var dst image.Image
	if spec.CropToSquare && srcB.Dx() != srcB.Dy() {
		var rect image.Rectangle
		if srcB.Dx() > srcB.Dy() {
			x1 := (srcB.Dx() - srcB.Dy()) / 2
			x2 := srcB.Dx() - x1
			rect = image.Rect(x1, 0, x2, srcB.Dy())

		} else {
			rect = image.Rect(0, 0, srcB.Dx(), srcB.Dx())
		}
		w = spec.Height
		if (spec.Height > spec.Width && spec.Width != 0) || spec.Height == 0 {
			w = spec.Width
		}
		h = w

		cropedImg := imaging.Crop(src, rect)
		rect = cropedImg.Bounds()
		dst = resize.Resize(cropedImg, rect, w, h)

	} else {
		w, h = spec.CalculateRect(srcB, spec.CropToSquare)

		if w >= srcB.Dx() || h >= srcB.Dy() {
			w, h = srcB.Dx(), srcB.Dy()
		}

		rect := image.Rect(0, 0, srcB.Dx(), srcB.Dy())
		dst = resize.Resize(src, rect, w, h)
	}

	var buf bytes.Buffer
	switch name {
	case "jpeg":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	case "png":
		png.Encode(&buf, dst)
	case "gif":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	case "bmp":
		jpeg.Encode(&buf, dst, &jpeg.Options{95})
	}

	to = &buf
	return
}
Пример #10
0
// Returns the torso and arms but with any armor which the user has.
func (skin *mcSkin) renderUpperArmor() *image.NRGBA {
	// This will be the base.
	upperArmorBodyImg := skin.renderUpperBody()

	// If it's an old skin, they don't have armor here.
	if skin.is18Skin() {
		// Get the armor layers from the skin and remove the Alpha.
		torso2Img := imaging.Crop(skin.Image, image.Rect(Torso2X, Torso2Y, Torso2X+TorsoWidth, Torso2Y+TorsoHeight))
		skin.removeAlpha(torso2Img)

		la2Img := imaging.Crop(skin.Image, image.Rect(La2X, La2Y, La2X+LaWidth, La2Y+TorsoHeight))
		skin.removeAlpha(la2Img)

		ra2Img := imaging.Crop(skin.Image, image.Rect(Ra2X, Ra2Y, Ra2X+RaWidth, Ra2Y+TorsoHeight))
		skin.removeAlpha(ra2Img)

		return skin.drawUpper(upperArmorBodyImg, torso2Img, ra2Img, la2Img)
	}
	return upperArmorBodyImg
}
Пример #11
0
func (imageHandler) Handle(media MediaLibrary, file multipart.File, option *Option) error {
	if err := media.Store(media.URL("original"), option, file); err == nil {
		file.Seek(0, 0)

		if img, err := imaging.Decode(file); err == nil {
			if format, err := getImageFormat(media.URL()); err == nil {
				if cropOption := media.GetCropOption("original"); cropOption != nil {
					img = imaging.Crop(img, *cropOption)
				}

				// Save default image
				var buffer bytes.Buffer
				imaging.Encode(&buffer, img, *format)
				media.Store(media.URL(), option, &buffer)

				for key, size := range media.GetSizes() {
					newImage := img
					if cropOption := media.GetCropOption(key); cropOption != nil {
						newImage = imaging.Crop(newImage, *cropOption)
					}

					dst := imaging.Thumbnail(newImage, size.Width, size.Height, imaging.Lanczos)
					var buffer bytes.Buffer
					imaging.Encode(&buffer, dst, *format)
					media.Store(media.URL(key), option, &buffer)
				}
				return nil
			} else {
				return err
			}
		} else {
			return err
		}
	} else {
		return err
	}
}
Пример #12
0
func Create(c *gin.Context) {
	file, fileHeader, err := c.Request.FormFile("file")
	if err != nil {
		c.Error(err)
		return
	}

	_, h := parseVal("h", c)
	_, w := parseVal("w", c)
	_, x := parseVal("x", c)
	_, y := parseVal("y", c)

	img, _, err := image.Decode(file)
	if err != nil {
		c.Error(err)
		return
	}

	log.Debug("Crop w:%v h:%v x:%v y:%v", w, h, x, y)

	rect := convertToRectangle(int(w), int(h), int(x), int(y))

	log.Debug("Rect", rect)

	croppedImg := imaging.Crop(img, rect)

	db := utils.GetDb(c)
	gridFile, err := db.GridFS("fs").Create(fileHeader.Filename)
	if err != nil {
		c.Error(err)
		return
	}

	buffer := new(bytes.Buffer)
	jpeg.Encode(buffer, croppedImg, nil)

	gridFile.SetName(fileHeader.Filename)
	gridFile.SetContentType(fileHeader.Header.Get("Content-Type"))
	_, err = io.Copy(gridFile, buffer)
	if err != nil {
		c.Error(err)
		return
	}

	file.Close()
	gridFile.Close()

	c.JSON(http.StatusOK, gin.H{"_id": gridFile.Id()})
}
Пример #13
0
func fitCropScale(i image.Image, r image.Rectangle) image.Image {
	wantRatio := float64(r.Bounds().Dx()) / float64(r.Bounds().Dy())
	haveRatio := float64(i.Bounds().Dx()) / float64(i.Bounds().Dy())

	sliceRect := image.Rectangle{}

	if haveRatio > wantRatio {
		wantwidth := wantRatio * float64(i.Bounds().Dy())
		sliceRect = image.Rect(i.Bounds().Dx()/2-int(wantwidth/2), 0, i.Bounds().Dx()/2+int(wantwidth/2), i.Bounds().Dy())
	} else {
		wantheight := float64(i.Bounds().Dx()) / wantRatio
		sliceRect = image.Rect(0, i.Bounds().Dy()/2-int(wantheight/2), i.Bounds().Dx(), i.Bounds().Dy()/2+int(wantheight/2))
	}

	return imaging.Resize(imaging.Crop(i, sliceRect), r.Dx(), r.Dy(), imaging.Lanczos)
}
Пример #14
0
func (i *ImgNet) cut(p image.Point, img *image.Image) []float64 {
	rect := image.Rect(p.X, p.Y, p.X+i.w, p.Y+i.h)
	croppedImg := imaging.Crop(*img, rect)

	rgb := make([]float64, i.w*i.h*3)

	for y := 0; y < croppedImg.Bounds().Dy(); y++ {
		for x := 0; x < croppedImg.Bounds().Dx(); x++ {
			r, g, b, _ := croppedImg.At(x, y).RGBA()
			index := x + y*i.w*3
			rgb[index] = float64(r) / 65535
			rgb[index+1] = float64(g) / 65535
			rgb[index+2] = float64(b) / 65535
		}
	}
	return rgb
}
Пример #15
0
func NewCubemap(name string, baseImage image.Image, lod bool) *CubeMap {
	cubeMap := new(CubeMap)

	x := baseImage.Bounds().Max.X
	y := baseImage.Bounds().Max.Y

	cubeMap.Name = name
	cubeMap.Lod = lod
	cubeMap.Right = imaging.Crop(baseImage, image.Rect(x/2, y/3, 3*x/4, 2*y/3))
	cubeMap.Left = imaging.Crop(baseImage, image.Rect(0, y/3, x/4, 2*y/3))
	cubeMap.Top = imaging.Crop(baseImage, image.Rect(x/4, 0, x/2, y/3))
	cubeMap.Bottom = imaging.Crop(baseImage, image.Rect(x/4, 2*y/3, x/2, y))
	cubeMap.Back = imaging.Crop(baseImage, image.Rect(3*x/4, y/3, x, 2*y/3))
	cubeMap.Front = imaging.Crop(baseImage, image.Rect(x/4, y/3, x/2, 2*y/3))

	return cubeMap
}
Пример #16
0
func MakeImages(rect *image.Rectangle, file io.Reader) ([]*bytes.Buffer, error) {
	runtime.GOMAXPROCS(runtime.NumCPU())

	ar := aspectRatio(rect)

	if ar < RATIO {
		return nil, fmt.Errorf("makeImages: aspect ratio is too small")
	} else if ar > RATIO {
		cropRatio(rect)
	}

	img, _, err := image.Decode(file)
	if err != nil {
		return nil, err
	}
	img = imaging.Clone(img)

	top := rect.Min.Y
	square := squareHeight(rect)
	space := spaceHeight(rect)

	buffers := make([]*bytes.Buffer, 5, 5)

	for i := 0; i < 5; i++ {
		new_rect := image.Rect(rect.Min.X, top, rect.Max.X, top+square)
		new_img := imaging.Crop(img, new_rect)

		buffers[i] = bytes.NewBuffer(make([]byte, 0))
		err = png.Encode(buffers[i], new_img)
		if err != nil {
			panic(err)
		}

		top = top + square + space
	}

	return buffers, nil
}
Пример #17
0
// Process begins the process of loading the source image, partitioning it, and
// determining all of the unique images.  Once complete, it produces a report
// which is written to a file called report.html in the current directory.
func (p *ImageSet) Process() {
	sourceImage := p.getSourceImage()
	if sourceImage == nil {
		return
	}

	bounds := sourceImage.Bounds()
	numX, numY := calculateNumberImages(bounds)

	p.stride = numX
	p.images = make([]uint64, numX*numY)
	p.orientations = make([]byte, numX*numY)

	for y := 0; y < numY; y++ {
		for x := 0; x < numX; x++ {
			bounds := image.Rect(x*128, y*128, (x+1)*128, (y+1)*128)
			p.AddImage(imaging.Crop(sourceImage, bounds), x, y)
		}
	}

	p.WriteImageFiles()
	p.WriteReport(bounds)
}
Пример #18
0
// Do performs the resize
func Do(r io.Reader, w io.Writer, crop *image.Rectangle, width, height int) error {
	// read it
	img, err := imaging.Decode(r)
	if err != nil {
		return err
	}

	// if crop isn't nil
	if crop != nil {
		// then crop it
		img = imaging.Crop(img, *crop)
	}

	// resize it
	img = imaging.Resize(img, width, height, imaging.MitchellNetravali)

	// write it
	if err := imaging.Encode(w, img, imaging.PNG); err != nil {
		return err
	}

	return nil
}
Пример #19
0
func SaveAndCropImage(isCreate bool) func(scope *gorm.Scope) {
	return func(scope *gorm.Scope) {
		for _, field := range scope.Fields() {
			if media, ok := field.Field.Addr().Interface().(MediaLibrary); ok {
				option := parseTagOption(field.Tag.Get("media_library"))
				if media.GetFileHeader() != nil || media.NeedCrop() {
					var file multipart.File
					var err error
					if fileHeader := media.GetFileHeader(); fileHeader != nil {
						file, err = media.GetFileHeader().Open()
					} else {
						file, err = media.Retrieve(media.URL("original"))
					}

					if scope.Err(err) != nil {
						return
					}

					if url := media.GetURL(option, scope, field, media); url == "" {
						scope.Err(errors.New("invalid URL"))
					} else {
						result, _ := json.Marshal(map[string]string{"Url": url})
						media.Scan(string(result))
					}

					if isCreate && !scope.HasError() {
						if value, err := media.Value(); err == nil {
							gorm.Update(scope.New(scope.Value).InstanceSet("gorm:update_attrs", map[string]interface{}{field.DBName: value}))
						}
					}

					if file != nil {
						defer file.Close()

						if media.IsImage() {
							// Save Original Image
							if scope.Err(media.Store(media.URL("original"), option, file)) == nil {
								file.Seek(0, 0)

								// Crop & Resize
								if img, err := imaging.Decode(file); scope.Err(err) == nil {
									if format, err := getImageFormat(media.URL()); scope.Err(err) == nil {
										if cropOption := media.GetCropOption("original"); cropOption != nil {
											img = imaging.Crop(img, *cropOption)
										}

										// Save default image
										var buffer bytes.Buffer
										imaging.Encode(&buffer, img, *format)
										media.Store(media.URL(), option, &buffer)

										for key, size := range media.GetSizes() {
											newImage := img
											if cropOption := media.GetCropOption(key); cropOption != nil {
												newImage = imaging.Crop(newImage, *cropOption)
											}

											dst := imaging.Thumbnail(newImage, size.Width, size.Height, imaging.Lanczos)
											var buffer bytes.Buffer
											imaging.Encode(&buffer, dst, *format)
											media.Store(media.URL(key), option, &buffer)
										}
									}
								}
							}
						} else {
							// Save File
							scope.Err(media.Store(media.URL(), option, file))
						}
					}
				}
			}
		}
	}
}
Пример #20
0
// Returns the head of the skin image.
func (skin *mcSkin) cropHead(img image.Image) image.Image {
	return imaging.Crop(img, image.Rect(HeadX, HeadY, HeadX+HeadWidth, HeadY+HeadHeight))
}