From a244345b54388fcecc01350cb80de60bf126ffc8 Mon Sep 17 00:00:00 2001 From: lordwelch Date: Wed, 3 Feb 2016 16:14:11 -0800 Subject: [PATCH] fix typos add routine for resizing an image using imagick --- magickwand.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 3 ++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 magickwand.go diff --git a/magickwand.go b/magickwand.go new file mode 100644 index 0000000..9f4e685 --- /dev/null +++ b/magickwand.go @@ -0,0 +1,54 @@ +// magickwand.go +package main + +import ( + "math" + + "github.com/gographics/imagick/imagick" +) + +func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepExactSize, center bool) (resmw *imagick.MagickWand) { + var ( + width, height int + ) + origHeight := int(mw.GetImageHeight()) + origWidth := int(mw.GetImageWidth()) + + if (origHeight != newHeight) || (origWidth != newWidth) { + if (round((origHeight / origWidth) * newWidth)) <= newHeight { + height = round((float64(origHeight) / float64(origWidth))) * newWidth + width = newWidth + } else { + width = round((float64(origWidth) / float64(origHeight))) * newHeight + height = newHeight + } + } else { + height = newHeight + width = newWidth + } + + if !keepExactSize { + resmw.SetSize(width, height) + center = false + } else { + resmw.SetSize(newWidth, newHeight) + if center { + err = mw.ResizeImage(width, height, imagick.FILTER_LANCZOS, 1) + if err != nil { + panic(err) + } + resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, uint((width-newWidth)/2), uint((height-newHeight)/2)) + } else { + resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, 0, 0) + } + } + mw.Destroy() + return +} + +func round(a float64) int { + if a < 0 { + return int(math.Ceil(a - 0.5)) + } + return int(math.Floor(a + 0.5)) +} diff --git a/main.go b/main.go index 83bf84e..f8afbe5 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ func main() { }*/ func run() error { + var mainQml qml.Object engine := qml.NewEngine() path, err = osext.ExecutableFolder() @@ -142,7 +143,7 @@ func glInit() { func setSignals() { textEdit.ObjectByName("textEdit1").On("focusChanged", func(focus bool) { var ( - tr string + str string cel cell )