Esempio n. 1
0
func crop(mw *imagick.MagickWand, x, y int, cols, rows uint) error {
	var result error
	result = nil

	imCols := mw.GetImageWidth()
	imRows := mw.GetImageHeight()

	if x < 0 {
		x = 0
	}
	if y < 0 {
		y = 0
	}

	if uint(x) >= imCols || uint(y) >= imRows {
		result = fmt.Errorf("x, y more than image cols, rows")
		return result
	}

	if cols == 0 || imCols < uint(x)+cols {
		cols = imCols - uint(x)
	}

	if rows == 0 || imRows < uint(y)+rows {
		rows = imRows - uint(y)
	}

	fmt.Print(fmt.Sprintf("wi_crop(im, %d, %d, %d, %d)\n", x, y, cols, rows))

	result = mw.CropImage(cols, rows, x, y)

	return result
}
Esempio n. 2
0
func crop(mw *imagick.MagickWand, geo btcdn.GeoBox) (err error) {
	offsetX, offsetY, err := calculateGravityOffsets(int(mw.GetImageWidth()), int(mw.GetImageHeight()), geo)
	if err != nil {
		return
	}
	err = mw.CropImage(uint(geo.Width()), uint(geo.Height()), offsetX, offsetY)
	if err != nil {
		return
	}

	return
}
Esempio n. 3
0
func calcDnR(image *imagick.MagickWand) (d uint, r float64) {
	width := image.GetImageWidth()
	height := image.GetImageHeight()

	if height > width {
		d = width / minChunksPerSide
	} else {
		d = height / minChunksPerSide
	}
	r = float64(d) / 2
	return
}
Esempio n. 4
0
func (i *Image) resize(wand *imagick.MagickWand, w, h uint) {
	ow := wand.GetImageWidth()
	oh := wand.GetImageHeight()

	if (float64(ow) / float64(oh)) < (float64(w) / float64(h)) {
		h = oh * w / ow
	} else {
		w = ow * h / oh
	}

	wand.SetImageInterpolateMethod(imagick.INTERPOLATE_PIXEL_BICUBIC)
	wand.ResizeImage(w, h, imagick.FILTER_LANCZOS2_SHARP, 1)
}
Esempio n. 5
0
func resizeImage(wand *imagick.MagickWand, size int, mozaic bool) *imagick.MagickWand {
	width := float32(wand.GetImageWidth())
	height := float32(wand.GetImageHeight())
	var rate float32
	if width > height {
		rate = float32(size) / width
	} else {
		rate = float32(size) / height
	}
	if mozaic {
		wand.ResizeImage(uint(width*rate/20), uint(height*rate/20), imagick.FILTER_LANCZOS, 1)
		wand.ResizeImage(uint(width*rate), uint(height*rate), imagick.FILTER_POINT, 1)
	} else {
		wand.ResizeImage(uint(width*rate), uint(height*rate), imagick.FILTER_LANCZOS, 1)
	}
	return wand.GetImage()
}
Esempio n. 6
0
func speedLine(mw *imagick.MagickWand, aw *imagick.MagickWand) error {
	cols, rows := mw.GetImageHeight(), mw.GetImageWidth()

	dw := imagick.NewDrawingWand()
	defer dw.Destroy()
	cw := imagick.NewPixelWand()
	cw.SetColor(*color)
	dw.SetFillColor(cw)

	center := []float64{float64(rows) / 2.0, float64(cols) / 2.0}
	const radiusCenter float64 = 0.75
	const step float64 = 0.02
	const bold float64 = 1.0
	var theeta float64

	for theeta < math.Pi*2 {
		stepNoise := rand.Float64() + 0.5
		theeta += step * stepNoise
		radiusCenterNoise := rand.Float64()*0.3 + 1.0
		boldNoise := rand.Float64() + 0.7 + 0.3

		point0 := imagick.PointInfo{
			X: math.Sin(theeta)*center[0]*radiusCenter*radiusCenterNoise + center[0],
			Y: math.Cos(theeta)*center[1]*radiusCenter*radiusCenterNoise + center[1],
		}
		point1 := imagick.PointInfo{
			X: math.Sin(theeta)*center[0]*2 + center[0],
			Y: math.Cos(theeta)*center[1]*2 + center[1],
		}
		point2 := imagick.PointInfo{
			X: math.Sin(theeta+step*bold*boldNoise)*center[0]*2 + center[0],
			Y: math.Cos(theeta+step*bold*boldNoise)*center[1]*2 + center[1],
		}

		dw.Polygon([]imagick.PointInfo{point0, point1, point2})
	}

	if err := aw.DrawImage(dw); err != nil {
		return err
	}

	return nil
}
Esempio n. 7
0
func tileLineImage(wand *imagick.MagickWand) {
	it := wand.NewPixelIterator()
	it.SetLastIteratorRow()
	width := float32(wand.GetImageWidth())
	height := float32(wand.GetImageHeight())
	heightRemain := float32(int(height / 20))
	if heightRemain != 0 {
		heightRemain = height/float32(heightRemain) - 20
	}
	widthRemain := float32(int(width / 20))
	if widthRemain != 0 {
		widthRemain = width/float32(widthRemain) - 20
	}
	cnt := it.GetIteratorRow()
	it.SetFirstIteratorRow()
	heightCount := 0
	nextHeight := 21
	hLineCount := 0
	hRemainCount := heightRemain
	widthCount := 0
	nextWidth := 21
	wLineCount := 0
	wRemainCount := widthRemain
	for i := 0; i <= cnt; i++ {
		heightCount++
		it.SetIteratorRow(i)
		pws := it.GetCurrentIteratorRow()
		if nextHeight-heightCount < 2 {
			for j := 0; j < len(pws); j++ {
				pws[j].SetColor("#ffffff")
			}
			if nextHeight == heightCount {
				heightCount = 0
				hLineCount++
				hRemainCount += heightRemain
				if hRemainCount > 1 {
					hRemainCount -= 1
					nextHeight = 21
				} else {
					nextHeight = 20
				}
			}
		} else {
			for j := 0; j < len(pws); j++ {
				widthCount++
				if nextWidth-widthCount < 2 {
					pws[j].SetColor("#ffffff")
					if nextWidth == widthCount {
						widthCount = 0
						wLineCount++
						wRemainCount += widthRemain
						if wRemainCount > 1 {
							wRemainCount -= 1
							nextWidth = 21
						} else {
							nextWidth = 20
						}
					}
				}
				// if j % 20 < 2 {
				// 	pws[j].SetColor("#ffffff")
				// }
			}
		}
		it.SyncIterator()
	}
}
Esempio n. 8
0
func proportion(mw *imagick.MagickWand, proportion int, cols uint, rows uint) error {
	var result error
	result = nil

	imCols := mw.GetImageWidth()
	imRows := mw.GetImageHeight()

	if proportion == 0 {
		fmt.Sprintf("p=0, wi_scale(im, %d, %d)\n", cols, rows)
		result = mw.ResizeImage(cols, rows, imagick.FILTER_UNDEFINED, 1.0)

	} else if proportion == 1 {

		if cols == 0 || rows == 0 {
			if cols > 0 {
				rows = uint(round(float64((cols / imCols) * imRows)))
			} else {
				cols = uint(round(float64((rows / imRows) * imCols)))
			}
			fmt.Sprintf("p=1, wi_scale(im, %d, %d)\n", cols, rows)
			result = mw.ResizeImage(cols, rows, imagick.FILTER_UNDEFINED, 1.0)
		} else {
			var x, y, sCols, sRows uint
			x, y = 0, 0

			colsRate := cols / imCols
			rowsRate := rows / imRows

			if colsRate > rowsRate {
				sCols = cols
				sRows = uint(round(float64(colsRate * imRows)))
				y = uint(math.Floor(float64((sRows - rows) / 2.0)))
			} else {
				sCols = uint(round(float64(rowsRate * imCols)))
				sRows = rows
				x = uint(math.Floor(float64((sCols - cols) / 2.0)))
			}

			fmt.Sprintf("p=2, wi_scale(im, %d, %d)\n", sCols, sRows)
			result = mw.ResizeImage(sCols, sRows, imagick.FILTER_UNDEFINED, 1.0)

			fmt.Sprintf("p=2, wi_crop(im, %d, %d, %d, %d)\n", x, y, cols, rows)
			result = mw.CropImage(cols, rows, int(x), int(y))
		}

	} else if proportion == 2 {
		x := int(math.Floor(float64((imCols - cols) / 2.0)))
		y := int(math.Floor(float64((imRows - rows) / 2.0)))
		fmt.Sprintf("p=3, wi_crop(im, %d, %d, %d, %d)\n", x, y, cols, rows)
		result = mw.CropImage(cols, rows, x, y)

	} else if proportion == 3 {
		if cols == 0 || rows == 0 {
			var rate uint
			if cols > 0 {
				rate = cols
			} else {
				rate = rows
			}
			rows = uint(round(float64(imRows * rate / 100)))
			cols = uint(round(float64(imCols * rate / 100)))
			fmt.Sprintf("p=3, wi_scale(im, %d, %d)\n", cols, rows)
			result = mw.ResizeImage(cols, rows, imagick.FILTER_UNDEFINED, 1.0)
		} else {
			rows = uint(round(float64(imRows * rows / 100)))
			cols = uint(round(float64(imCols * cols / 100)))
			fmt.Sprintf("p=3, wi_scale(im, %d, %d)\n", cols, rows)
			result = mw.ResizeImage(cols, rows, imagick.FILTER_UNDEFINED, 1.0)
		}

	} else if proportion == 4 {
		var rate float64
		rate = 1.0
		if cols == 0 || rows == 0 {
			if cols > 0 {
				rate = float64(cols / imCols)
			} else {
				rate = float64(rows / imRows)
			}
		} else {
			rateCol := cols / imCols
			rateRow := rows / imRows
			if rateCol < rateRow {
				rate = float64(rateCol)
			} else {
				rate = float64(rateRow)
			}
		}

		cols = uint(round(float64(float64(imCols) * rate)))
		rows = uint(round(float64(float64(imRows) * rate)))
		fmt.Sprintf("p=4, wi_scale(im, %d, %d)\n", cols, rows)
		result = mw.ResizeImage(cols, rows, imagick.FILTER_UNDEFINED, 1.0)
	}

	return result

}
Esempio n. 9
0
// Returns an the width and height of magick wand
func getDimensions(wand *imagick.MagickWand) (w, h uint) {
	h = wand.GetImageHeight()
	w = wand.GetImageWidth()
	return
}
Esempio n. 10
0
func heightToWidthRatio(im *imagick.MagickWand) float64 {
	return (float64)(im.GetImageHeight()) / (float64)(im.GetImageWidth())
}
Esempio n. 11
0
func resizeRatio(im *imagick.MagickWand, width, height int) float64 {
	return math.Abs((float64)(width*height) / (float64)(im.GetImageWidth()*im.GetImageHeight()))
}