|
|
|
@ -56,8 +56,8 @@ func generateAST(path string) {
|
|
|
|
|
|
|
|
|
|
func validateInput(path string) {
|
|
|
|
|
generateAST(path)
|
|
|
|
|
if len(tree.Loops) > 0 {
|
|
|
|
|
fmt.Printf("File has %d procedures\n", len(tree.Loops))
|
|
|
|
|
if len(tree.Procedures) > 0 {
|
|
|
|
|
fmt.Printf("File has %d procedures\n", len(tree.Procedures))
|
|
|
|
|
fmt.Println("File is valid")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("Invalid or empty input file")
|
|
|
|
@ -67,12 +67,11 @@ func validateInput(path string) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func filterImage(path, fname string) {
|
|
|
|
|
fmt.Print("Loading image \r")
|
|
|
|
|
fmt.Print("Loading image... ")
|
|
|
|
|
im, imFormat = loadImage(path)
|
|
|
|
|
imSize := im.Bounds().Size()
|
|
|
|
|
variables["WID"] = imSize.X
|
|
|
|
|
variables["HIG"] = imSize.Y
|
|
|
|
|
variables["REG"] = 0
|
|
|
|
|
variables["RG1"] = 0
|
|
|
|
|
variables["RG2"] = 0
|
|
|
|
|
fil.location.maxX = imSize.X
|
|
|
|
|
fil.location.maxY = imSize.Y
|
|
|
|
|
|
|
|
|
@ -85,42 +84,70 @@ func filterImage(path, fname string) {
|
|
|
|
|
filter{3,0,0},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Print("Filtering image \r")
|
|
|
|
|
for _, loop := range tree.Loops {
|
|
|
|
|
for ;loop.Counter > 0; loop.Counter-- {
|
|
|
|
|
for _, proc := range loop.Procedures {
|
|
|
|
|
procedure(proc)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Print("Filtering image... ")
|
|
|
|
|
for _, proc := range tree.Procedures {
|
|
|
|
|
procedure(proc)
|
|
|
|
|
}
|
|
|
|
|
fmt.Print("Saving image \n")
|
|
|
|
|
fmt.Print("Saving image... \n")
|
|
|
|
|
saveImage(path, fname, imFormat, im)
|
|
|
|
|
fmt.Print("Image saved. Done. \n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateRegister(exp parser.Expression) {
|
|
|
|
|
func updateRegister(reg string, exp parser.Expression) {
|
|
|
|
|
var err error
|
|
|
|
|
opand := exp.Opperand
|
|
|
|
|
if exp.Variable != "" {
|
|
|
|
|
opand = variables[exp.Variable]
|
|
|
|
|
opand, err = retrieveVariableValue(exp.Variable)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Runtime error: Unknown variable %q encountered", exp.Variable))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
switch exp.Opperator {
|
|
|
|
|
case '+':
|
|
|
|
|
variables["REG"] += opand
|
|
|
|
|
variables[reg] += opand
|
|
|
|
|
case '-':
|
|
|
|
|
variables["REG"] -= opand
|
|
|
|
|
variables[reg] -= opand
|
|
|
|
|
case '*':
|
|
|
|
|
variables["REG"] *= opand
|
|
|
|
|
variables[reg] *= opand
|
|
|
|
|
case '/':
|
|
|
|
|
if opand == 0 {
|
|
|
|
|
variables["REG"] = 1
|
|
|
|
|
variables[reg] = 1
|
|
|
|
|
} else {
|
|
|
|
|
variables["REG"] /= opand
|
|
|
|
|
variables[reg] /= opand
|
|
|
|
|
}
|
|
|
|
|
case '=':
|
|
|
|
|
variables["REG"] = opand
|
|
|
|
|
variables[reg] = opand
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func retrieveVariableValue(ident string) (int, error) {
|
|
|
|
|
var val int
|
|
|
|
|
switch ident {
|
|
|
|
|
case "RG1", "RG2":
|
|
|
|
|
val = variables[ident]
|
|
|
|
|
case "WID":
|
|
|
|
|
val = fil.location.maxX
|
|
|
|
|
case "HIG":
|
|
|
|
|
val = fil.location.maxY
|
|
|
|
|
case "RED":
|
|
|
|
|
val = fil.red.val
|
|
|
|
|
case "GRN":
|
|
|
|
|
val = fil.green.val
|
|
|
|
|
case "BLU":
|
|
|
|
|
val = fil.blue.val
|
|
|
|
|
case "APH":
|
|
|
|
|
val = fil.alpha.val
|
|
|
|
|
case "LOX":
|
|
|
|
|
val = fil.location.x
|
|
|
|
|
case "LOY":
|
|
|
|
|
val = fil.location.y
|
|
|
|
|
default:
|
|
|
|
|
return 0, fmt.Errorf("Unknown variable %q sent to retrieveVariableValue", ident)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func procedure(p parser.Procedure) {
|
|
|
|
|
switch p.Kind {
|
|
|
|
|
case "LOC":
|
|
|
|
@ -131,9 +158,9 @@ func procedure(p parser.Procedure) {
|
|
|
|
|
if len(p.Expressions) >= 1 {
|
|
|
|
|
fil.mode.update(p.Expressions[0])
|
|
|
|
|
}
|
|
|
|
|
case "REG":
|
|
|
|
|
case "RG1", "RG2":
|
|
|
|
|
if len(p.Expressions) >= 1 {
|
|
|
|
|
updateRegister(p.Expressions[0])
|
|
|
|
|
updateRegister(p.Kind, p.Expressions[0])
|
|
|
|
|
}
|
|
|
|
|
case "APY":
|
|
|
|
|
applyToImage(p.Expressions)
|
|
|
|
@ -164,6 +191,7 @@ func procedure(p parser.Procedure) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func applyToImage(exps []parser.Expression) {
|
|
|
|
|
var err error
|
|
|
|
|
if len(exps) == 0 {
|
|
|
|
|
im.SetRGBA(
|
|
|
|
|
fil.location.x,
|
|
|
|
@ -178,14 +206,20 @@ func applyToImage(exps []parser.Expression) {
|
|
|
|
|
endX := exps[0].Opperand
|
|
|
|
|
endY := exps[1].Opperand
|
|
|
|
|
if exps[0].Variable != "" {
|
|
|
|
|
endX = variables[exps[0].Variable]
|
|
|
|
|
endX, err = retrieveVariableValue(exps[0].Variable)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Runtime error: Unknown variable %q encountered", exps[0].Variable))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if exps[1].Variable != "" {
|
|
|
|
|
endY = variables[exps[1].Variable]
|
|
|
|
|
endY, err = retrieveVariableValue(exps[1].Variable)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(fmt.Sprintf("Runtime error: Unknown variable %q encountered", exps[1].Variable))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
endX = getBoxBound(fil.location.x, endX, variables["WID"], exps[0].Opperator)
|
|
|
|
|
endY = getBoxBound(fil.location.y, endY, variables["HIG"], exps[1].Opperator)
|
|
|
|
|
endX = getBoxBound(fil.location.x, endX, fil.location.maxX, exps[0].Opperator)
|
|
|
|
|
endY = getBoxBound(fil.location.y, endY, fil.location.maxY, exps[1].Opperator)
|
|
|
|
|
|
|
|
|
|
var r,g,b,a int
|
|
|
|
|
var col color.RGBA
|
|
|
|
|