add imagick

add gl routines
fix resizing routine
This commit is contained in:
lordwelch 2016-02-09 21:55:57 -08:00
parent a244345b54
commit 0461807287
2 changed files with 129 additions and 34 deletions

View File

@ -2,48 +2,53 @@
package main
import (
"fmt"
"math"
"github.com/gographics/imagick/imagick"
)
func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepExactSize, center bool) (resmw *imagick.MagickWand) {
func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepSpecSize, center bool) (resmw *imagick.MagickWand) {
var (
width, height int
width, height, origHeight, origWidth int
)
origHeight := int(mw.GetImageHeight())
origWidth := int(mw.GetImageWidth())
origHeight = int(mw.GetImageHeight())
fmt.Println("hahahahahah :-P")
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
if (round((float64(origWidth) / float64(origHeight)) * float64(newHeight))) <= newWidth {
width = round((float64(origWidth) / float64(origHeight)) * float64(newHeight))
height = newHeight
} else {
height = round((float64(origHeight) / float64(origWidth)) * float64(newWidth))
width = newWidth
}
} else {
height = newHeight
width = newWidth
}
if !keepExactSize {
resmw.SetSize(width, height)
resmw = imagick.NewMagickWand()
if !keepSpecSize {
resmw.NewImage(uint(width), uint(height), imagick.NewPixelWand())
center = false
} else {
resmw.SetSize(newWidth, newHeight)
resmw.NewImage(uint(newWidth), uint(newHeight), imagick.NewPixelWand())
fmt.Println(resmw.GetImageHeight(), resmw.GetImageWidth())
if center {
err = mw.ResizeImage(width, height, imagick.FILTER_LANCZOS, 1)
err = mw.ResizeImage(uint(width), uint(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))
resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, round(float64(newWidth-width)/float64(2)), round(float64(newHeight-height)/float64(2)))
} else {
resmw.CompositeImage(mw, imagick.COMPOSITE_OP_SRC_OVER, 0, 0)
}
}
mw.Destroy()
return
return resmw
}
func round(a float64) int {

128
main.go
View File

@ -5,13 +5,14 @@ import "C"
import (
"fmt"
//"image"
"image"
"log"
"os"
"path/filepath"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/gographics/imagick/imagick"
"github.com/kardianos/osext"
"github.com/lordwelch/qml"
)
@ -25,15 +26,18 @@ type cell struct {
type slide []cell
var (
path string
textEdit qml.Object
cellQml qml.Object
window *qml.Window
win *glfw.Window
slides slide
err error
monitors []*glfw.Monitor
drawSlide func()
x, y int
path string
textEdit qml.Object
cellQml qml.Object
window *qml.Window
win *glfw.Window
slides slide
err error
monitors []*glfw.Monitor
mw1 *imagick.MagickWand
tex1 uint32
//drawSlide func()
)
func main() {
@ -54,6 +58,14 @@ func main() {
func run() error {
var mainQml qml.Object
imagick.Initialize()
defer imagick.Terminate()
mw1 = imagick.NewMagickWand()
err = mw1.ReadImage("logo:")
if err != nil {
panic(err)
}
engine := qml.NewEngine()
path, err = osext.ExecutableFolder()
@ -81,15 +93,100 @@ func run() error {
})
window.Wait()
mw1.Destroy()
return nil
}
func setupScene() {
gl.ClearColor(0.1, 0.5, 0.9, 0.0)
mw2 := resizeImage(mw1, x, y, true, true)
tex1 = newTexture(*mw2)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(-1, 1, -1, 1, 1.0, 10.0)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
}
func drawSlide() {
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Translatef(0, 0, -3.0)
gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 0)
gl.Vertex3f(-1, 1, 0)
gl.TexCoord2f(1, 0)
gl.Vertex3f(1, 1, 0)
gl.TexCoord2f(1, 1)
gl.Vertex3f(1, -1, 0)
gl.TexCoord2f(0, 1)
gl.Vertex3f(-1, -1, 0)
gl.End()
}
func newTexture(mw imagick.MagickWand) uint32 {
x1 := mw.GetImageWidth()
y1 := mw.GetImageHeight()
rgba := image.NewRGBA(image.Rect(0, 0, int(x1), int(y1)))
if rgba.Stride != rgba.Rect.Size().X*4 {
panic("unsupported stride")
}
TPix, _ := mw.ExportImagePixels(0, 0, x1, y1, "RGBA", imagick.PIXEL_CHAR)
rgba.Pix = TPix.([]uint8)
var texture1 uint32
gl.Enable(gl.TEXTURE_2D)
gl.GenTextures(1, &texture1)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix))
return texture1
}
func checkMon() {
monitors = glfw.GetMonitors()
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.AutoIconify, glfw.False)
glfw.WindowHint(glfw.Decorated, glfw.False)
if i := len(monitors); i < 2 {
fmt.Println("You only have 1 monitor!!!!!!!!!!! :-P")
win, err = glfw.CreateWindow(600, 800, "Cube", nil, nil)
if err != nil {
panic(err)
}
} else {
fmt.Printf("You have %d monitors\n", i)
x = monitors[1].GetVideoMode().Width
y = monitors[1].GetVideoMode().Height
win, err = glfw.CreateWindow(x, y, "Cube", nil, nil)
fmt.Println(win.GetPos())
win.SetPos(monitors[1].GetPos())
fmt.Println(x, y)
if err != nil {
panic(err)
}
}
monitorInfo()
@ -109,24 +206,17 @@ func glInit() {
log.Fatalln("failed to initialize glfw:", err)
}
checkMon()
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
win, err = glfw.CreateWindow(800, 600, "Cube", nil, nil)
if err != nil {
panic(err)
}
win.MakeContextCurrent()
if err := gl.Init(); err != nil {
panic(err)
}
setupScene()
win.SetPos(monitors[1].GetPos())
gl.ClearColor(0.1, 0.5, 0.9, 0.0)
qml.Func1 = func() int {
if !win.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT)
drawSlide()
win.SwapBuffers()
glfw.PollEvents()