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

128
main.go
View File

@ -5,13 +5,14 @@ import "C"
import ( import (
"fmt" "fmt"
//"image" "image"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw" "github.com/go-gl/glfw/v3.1/glfw"
"github.com/gographics/imagick/imagick"
"github.com/kardianos/osext" "github.com/kardianos/osext"
"github.com/lordwelch/qml" "github.com/lordwelch/qml"
) )
@ -25,15 +26,18 @@ type cell struct {
type slide []cell type slide []cell
var ( var (
path string x, y int
textEdit qml.Object path string
cellQml qml.Object textEdit qml.Object
window *qml.Window cellQml qml.Object
win *glfw.Window window *qml.Window
slides slide win *glfw.Window
err error slides slide
monitors []*glfw.Monitor err error
drawSlide func() monitors []*glfw.Monitor
mw1 *imagick.MagickWand
tex1 uint32
//drawSlide func()
) )
func main() { func main() {
@ -54,6 +58,14 @@ func main() {
func run() error { func run() error {
var mainQml qml.Object var mainQml qml.Object
imagick.Initialize()
defer imagick.Terminate()
mw1 = imagick.NewMagickWand()
err = mw1.ReadImage("logo:")
if err != nil {
panic(err)
}
engine := qml.NewEngine() engine := qml.NewEngine()
path, err = osext.ExecutableFolder() path, err = osext.ExecutableFolder()
@ -81,15 +93,100 @@ func run() error {
}) })
window.Wait() window.Wait()
mw1.Destroy()
return nil 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() { func checkMon() {
monitors = glfw.GetMonitors() 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 { if i := len(monitors); i < 2 {
fmt.Println("You only have 1 monitor!!!!!!!!!!! :-P") fmt.Println("You only have 1 monitor!!!!!!!!!!! :-P")
win, err = glfw.CreateWindow(600, 800, "Cube", nil, nil)
if err != nil {
panic(err)
}
} else { } else {
fmt.Printf("You have %d monitors\n", i) 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() monitorInfo()
@ -109,24 +206,17 @@ func glInit() {
log.Fatalln("failed to initialize glfw:", err) log.Fatalln("failed to initialize glfw:", err)
} }
checkMon() 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() win.MakeContextCurrent()
if err := gl.Init(); err != nil { if err := gl.Init(); err != nil {
panic(err) panic(err)
} }
setupScene()
win.SetPos(monitors[1].GetPos())
gl.ClearColor(0.1, 0.5, 0.9, 0.0)
qml.Func1 = func() int { qml.Func1 = func() int {
if !win.ShouldClose() { if !win.ShouldClose() {
gl.Clear(gl.COLOR_BUFFER_BIT)
drawSlide() drawSlide()
win.SwapBuffers() win.SwapBuffers()
glfw.PollEvents() glfw.PollEvents()