Fixes math error for negative numbers during point update

This commit is contained in:
sloumdrone 2019-07-19 21:44:26 -07:00
parent 9a16ed2b66
commit 784a473fa1
5 changed files with 110 additions and 18 deletions

68
main.go
View File

@ -24,6 +24,13 @@ type filter struct {
val int val int
} }
type col struct {
red filter
green filter
blue filter
alpha filter
}
type filterState struct { type filterState struct {
red filter red filter
green filter green filter
@ -31,6 +38,7 @@ type filterState struct {
alpha filter alpha filter
location point location point
mode filter mode filter
sample col
} }
var variables = map[string]int{} var variables = map[string]int{}
var tree parser.AST var tree parser.AST
@ -82,6 +90,12 @@ func filterImage(path, fname string) {
filter{255, 0,255}, filter{255, 0,255},
point{0,0,imSize.X,imSize.Y}, point{0,0,imSize.X,imSize.Y},
filter{3,0,0}, filter{3,0,0},
col{
filter{255, 0, 255},
filter{255, 0, 255},
filter{255, 0, 255},
filter{255, 0, 255},
},
} }
fmt.Print("Filtering image... ") fmt.Print("Filtering image... ")
@ -163,7 +177,7 @@ func procedure(p parser.Procedure) {
updateRegister(p.Kind, p.Expressions[0]) updateRegister(p.Kind, p.Expressions[0])
} }
case "APY": case "APY":
applyToImage(p.Expressions) applyToImage(p.Expressions, false)
case "RED": case "RED":
if len(p.Expressions) >= 1 { if len(p.Expressions) >= 1 {
fil.red.update(p.Expressions[0]) fil.red.update(p.Expressions[0])
@ -187,21 +201,38 @@ func procedure(p parser.Procedure) {
fil.blue.update(p.Expressions[2]) fil.blue.update(p.Expressions[2])
fil.alpha.update(p.Expressions[3]) fil.alpha.update(p.Expressions[3])
} }
case "GET":
fil.sample.update()
case "PUT":
applyToImage(p.Expressions, true)
} }
} }
func applyToImage(exps []parser.Expression) { func applyToImage(exps []parser.Expression, useSample bool) {
var err error var err error
if len(exps) == 0 { if len(exps) == 0 {
im.SetRGBA( if useSample {
fil.location.x, im.SetRGBA(
fil.location.y, fil.location.x,
color.RGBA{ fil.location.y,
uint8(fil.red.val), color.RGBA{
uint8(fil.green.val), uint8(fil.sample.red.val),
uint8(fil.blue.val), uint8(fil.sample.green.val),
uint8(fil.alpha.val), uint8(fil.sample.blue.val),
}) uint8(fil.sample.alpha.val),
})
} else {
im.SetRGBA(
fil.location.x,
fil.location.y,
color.RGBA{
uint8(fil.red.val),
uint8(fil.green.val),
uint8(fil.blue.val),
uint8(fil.alpha.val),
})
}
} else if len(exps) == 2 { } else if len(exps) == 2 {
endX := exps[0].Opperand endX := exps[0].Opperand
endY := exps[1].Opperand endY := exps[1].Opperand
@ -225,10 +256,17 @@ func applyToImage(exps []parser.Expression) {
var col color.RGBA var col color.RGBA
for y := fil.location.y; y <= endY; y++ { for y := fil.location.y; y <= endY; y++ {
for x := fil.location.x; x <= endX; x++ { for x := fil.location.x; x <= endX; x++ {
r = fil.red.val if useSample {
g = fil.green.val r = fil.sample.red.val
b = fil.blue.val g = fil.sample.green.val
a = fil.alpha.val b = fil.sample.blue.val
a = fil.sample.alpha.val
} else {
r = fil.red.val
g = fil.green.val
b = fil.blue.val
a = fil.alpha.val
}
if fil.mode.val != 0 { if fil.mode.val != 0 {
col = im.RGBAAt(x, y) col = im.RGBAAt(x, y)
// fmt.Printf("Orig: %d, New: %d, Avg: %d\n", col.R, r, avgColor(int(col.R), r)) // fmt.Printf("Orig: %d, New: %d, Avg: %d\n", col.R, r, avgColor(int(col.R), r))

View File

@ -13,14 +13,19 @@ RG2 /2
# Nest loop over y then x axis # Nest loop over y then x axis
BEG 10 BEG 10
BEG 10 BEG 10
COL +80:*6:-200:-4 COL +80:*6:-200:-4
APY +RG2:+RG1 APY +RG2:+RG1
LOC +RG2:+0 LOC +RG2:+0
LOC +RG2:+0 LOC +RG2:+0
END END
LOC +RG2:+RG1 LOC +RG2:+RG1
LOC +RG2:+RG1 LOC +0:+RG1
END END

View File

@ -221,7 +221,7 @@ func (p *Parser) Parse() AST {
func isValidProcedure(p string) bool { func isValidProcedure(p string) bool {
switch p { switch p {
case "RG1", "RG2", "LOC", "BEG", "END", "RED", case "RG1", "RG2", "LOC", "BEG", "END", "RED", "GET", "PUT",
"GRN", "BLU", "APH", "COL", "APY", "MOD", "LOX", "LOY": "GRN", "BLU", "APH", "COL", "APY", "MOD", "LOX", "LOY":
return true return true
} }

View File

@ -99,10 +99,20 @@ func (p *point) update(change []parser.Expression) {
} }
if *target < 0 { if *target < 0 {
*target = max - *target%max *target = max - (*target * -1 % max)
} }
} }
p.x = totalX p.x = totalX
p.y = totalY p.y = totalY
} }
func (c *col) update() {
imCol := im.RGBAAt(fil.location.x, fil.location.y)
fil.sample.red.val = int(imCol.R)
fil.sample.blue.val = int(imCol.B)
fil.sample.green.val = int(imCol.B)
fil.sample.alpha.val = int(imCol.A)
}

39
sample_test.frs Normal file
View File

@ -0,0 +1,39 @@
# This will test image sampling
MOD 0
RG2 HIG
RG2 /2
LOC 50:37
BEG 50
LOC +50:*73
BEG 2
LOC +1:+0
GET
PUT +0:+RG2
END
END
MOD 1
LOC 0:0
COL 240:50:50:240
RG1 HIG
RG1 /10
APY +WID:+RG1
LOC +5:+1
MOD 0
BEG 50
LOC *9:+2
GET
LOC +0:+RG1
BEG 50
LOC +37:+0
PUT +1:+1
END
LOC +0:-RG1
END