From ab3b3971398ce9ed4fef8dbe39277b79cfa1374f Mon Sep 17 00:00:00 2001 From: Terence Boldt Date: Fri, 13 Jan 2023 23:03:43 -0500 Subject: [PATCH] Add image import (#17) * Add image import * Update version --- go.mod | 2 + go.sum | 26 ++++++ main.go | 2 +- prodos/host.go | 4 + prodos/image.go | 219 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 prodos/image.go diff --git a/go.mod b/go.mod index b0fc0b9..5120922 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/tjboldt/ProDOS-Utilities go 1.16 + +require golang.org/x/image v0.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c9d07e6 --- /dev/null +++ b/go.sum @@ -0,0 +1,26 @@ +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg= +golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 2836a45..c6e58fb 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( "github.com/tjboldt/ProDOS-Utilities/prodos" ) -const version = "0.4.2" +const version = "0.4.3" func main() { var fileName string diff --git a/prodos/host.go b/prodos/host.go index d05e90a..dac130a 100644 --- a/prodos/host.go +++ b/prodos/host.go @@ -122,6 +122,10 @@ func convertFileByType(inFileName string, inFile []byte) (int, int, []byte, erro inFile = []byte(strings.ReplaceAll(strings.ReplaceAll(string(inFile), "\r\n", "r"), "\n", "\r")) fileType = 0x04 auxType = 0x0000 + case ".JPG", ".PNG": + inFile = ConvertImageToHiResMonochrome(inFile) + fileType = 0x06 + auxType = 0x2000 } } diff --git a/prodos/image.go b/prodos/image.go new file mode 100644 index 0000000..70a2d92 --- /dev/null +++ b/prodos/image.go @@ -0,0 +1,219 @@ +// Copyright Terence J. Boldt (c)2021-2023 +// Use of this source code is governed by an MIT +// license that can be found in the LICENSE file. + +// This file provides access to read, write and delete +// files on a ProDOS drive image + +package prodos + +import ( + "bytes" + "fmt" + "image" + "image/color" + _ "image/jpeg" + _ "image/png" + + "golang.org/x/image/draw" +) + +// 10 PRINT CHR$ (4)"open offsets" +// 20 PRINT CHR$ (4)"write offsets" +// 30 PRINT "offsets := [192]int{"; +// 40 FOR Y = 0 TO 191 +// 50 HPLOT 0,Y +// 60 PRINT ( PEEK (39) * 256 + PEEK (38)) - 8192; +// 70 IF Y < 191 THEN PRINT ", "; +// 80 NEXT +// 90 PRINT "}" +// 100 PRINT CHR$ (4)"close offsets" +var offsets []int = []int{0, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 128, 1152, 2176, 3200, 4224, 5248, 6272, 7296, 256, 1280, 2304, 3328, 4352, 5376, 6400, 7424, 384, 1408, 2432, 3456, 4480, 5504, 6528, 7552, 512, 1536, 2560, 3584, 4608, 5632, 6656, 7680, 640, 1664, 2688, 3712, 4736, 5760, 6784, 7808, 768, 1792, 2816, 3840, 4864, 5888, 6912, 7936, 896, 1920, 2944, 3968, 4992, 6016, 7040, 8064, 40, 1064, 2088, 3112, 4136, 5160, 6184, 7208, 168, 1192, 2216, 3240, 4264, 5288, 6312, 7336, 296, 1320, 2344, 3368, 4392, 5416, 6440, 7464, 424, 1448, 2472, 3496, 4520, 5544, 6568, 7592, 552, 1576, 2600, 3624, 4648, 5672, 6696, 7720, 680, 1704, 2728, 3752, 4776, 5800, 6824, 7848, 808, 1832, 2856, 3880, 4904, 5928, 6952, 7976, 936, 1960, 2984, 4008, 5032, 6056, 7080, 8104, 80, 1104, 2128, 3152, 4176, 5200, 6224, 7248, 208, 1232, 2256, 3280, 4304, 5328, 6352, 7376, 336, 1360, 2384, 3408, 4432, 5456, 6480, 7504, 464, 1488, 2512, 3536, 4560, 5584, 6608, 7632, 592, 1616, 2640, 3664, 4688, 5712, 6736, 7760, 720, 1744, 2768, 3792, 4816, 5840, 6864, 7888, 848, 1872, 2896, 3920, 4944, 5968, 6992, 8016, 976, 2000, 3024, 4048, 5072, 6096, 7120, 8144} +var pixel []byte = []byte{1, 2, 4, 8, 16, 32, 64} + +func ConvertImageToHiResMonochrome(imageBytes []byte) []byte { + + img, _, err := image.Decode(bytes.NewReader(imageBytes)) + + if err != nil { + fmt.Printf("%s\n", err) + return nil + } + + a2imgSize := image.Rect(0, 0, 280, 192) + + a2img := image.NewPaletted(a2imgSize, []color.Color{ + color.Black, + color.White, + }) + + a2monoImgSize := image.Rect(0, 0, 280, 192) + + scaledImg := image.NewRGBA(a2monoImgSize) + draw.BiLinear.Scale(scaledImg, a2monoImgSize, img, img.Bounds(), draw.Over, nil) + draw.FloydSteinberg.Draw(a2img, a2monoImgSize, scaledImg, image.Point{}) + + hires := make([]byte, 8192) + + for y := a2img.Bounds().Min.Y; y < a2img.Bounds().Max.Y; y++ { + for x := a2img.Bounds().Min.X; x < a2img.Bounds().Max.X; x++ { + if a2img.At(x, y) == color.White { + hires[offsets[y]+x/7] |= pixel[x%7] | 128 + } + } + } + + return hires +} + +func ConvertImageToHiResColour(imageBytes []byte) []byte { + + img, _, err := image.Decode(bytes.NewReader(imageBytes)) + + if err != nil { + fmt.Printf("%s\n", err) + return nil + } + + a2colourImgSize := image.Rect(0, 0, 140, 192) + + black := color.NRGBA{0, 0, 0, 255} + green := color.NRGBA{20, 245, 60, 255} + purple := color.NRGBA{255, 68, 253, 255} + white := color.NRGBA{255, 255, 255, 255} + orange := color.NRGBA{255, 106, 60, 255} + blue := color.NRGBA{20, 207, 253, 255} + + a2img := image.NewPaletted(a2colourImgSize, []color.Color{ + black, + green, + purple, + white, + orange, + blue, + }) + + scaledImg := image.NewRGBA(a2colourImgSize) + draw.BiLinear.Scale(scaledImg, a2colourImgSize, img, img.Bounds(), draw.Over, nil) + draw.FloydSteinberg.Draw(a2img, a2colourImgSize, scaledImg, image.Point{}) + + hires := make([]byte, 8192) + + for y := a2img.Bounds().Min.Y; y < a2img.Bounds().Max.Y; y++ { + for x7 := a2img.Bounds().Min.X; x7 < a2img.Bounds().Max.X; x7 += 7 { + switch a2img.At(x7, y) { + case green: + hires[offsets[y]+x7*2/7] = 2 + case purple: + hires[offsets[y]+x7*2/7] = 1 + case orange: + hires[offsets[y]+x7*2/7] = 2 + hires[offsets[y]+x7*2/7] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7] = 1 + hires[offsets[y]+x7*2/7] |= 0x80 + case white: + hires[offsets[y]+x7*2/7] = 3 + } + switch a2img.At(x7+1, y) { + case green: + hires[offsets[y]+x7*2/7] |= 8 + hires[offsets[y]+x7*2/7] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7] |= 4 + hires[offsets[y]+x7*2/7] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7] |= 8 + hires[offsets[y]+x7*2/7] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7] |= 4 + hires[offsets[y]+x7*2/7] |= 0x80 + case white: + hires[offsets[y]+x7*2/7] |= 12 + } + switch a2img.At(x7+2, y) { + case green: + hires[offsets[y]+x7*2/7] |= 32 + hires[offsets[y]+x7*2/7] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7] |= 16 + hires[offsets[y]+x7*2/7] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7] |= 32 + hires[offsets[y]+x7*2/7] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7] |= 16 + hires[offsets[y]+x7*2/7] |= 0x80 + case white: + hires[offsets[y]+x7*2/7] |= 48 + } + switch a2img.At(x7+3, y) { + case green: + hires[offsets[y]+x7*2/7+1] |= 1 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7] |= 64 + hires[offsets[y]+x7*2/7] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7+1] |= 1 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7] |= 64 + hires[offsets[y]+x7*2/7] |= 0x80 + case white: + hires[offsets[y]+x7*2/7] |= 64 + hires[offsets[y]+x7*2/7+1] |= 1 + } + switch a2img.At(x7+4, y) { + case green: + hires[offsets[y]+x7*2/7+1] |= 4 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7+1] |= 2 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7+1] |= 4 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7+1] |= 2 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case white: + hires[offsets[y]+x7*2/7+1] |= 6 + } + switch a2img.At(x7+5, y) { + case green: + hires[offsets[y]+x7*2/7+1] |= 16 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7+1] |= 8 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7+1] |= 16 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7+1] |= 8 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case white: + hires[offsets[y]+x7*2/7+1] |= 24 + } + switch a2img.At(x7+6, y) { + case green: + hires[offsets[y]+x7*2/7+1] |= 64 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case purple: + hires[offsets[y]+x7*2/7+1] |= 32 + hires[offsets[y]+x7*2/7+1] &= 0x7F + case orange: + hires[offsets[y]+x7*2/7+1] |= 64 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case blue: + hires[offsets[y]+x7*2/7+1] |= 32 + hires[offsets[y]+x7*2/7+1] |= 0x80 + case white: + hires[offsets[y]+x7*2/7+1] |= 96 + } + } + } + + return hires +}