-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoansi.go
65 lines (55 loc) · 2.22 KB
/
goansi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// package goansi provides library-level access to ANSi file formats and SAUCE records
package goansi
import (
"bytes"
"image"
"github.com/nfnt/resize"
)
// Parse takes a buffer of ANSi data and returns an Image.image
func Parse(inputFileBuffer []byte, inputFileSize int64, fontName string, bits int, columns int, mode string, icecolors bool, fext string, scaleFactor float32) image.Image {
var outputImg image.Image
adjustedSize := inputFileSize
buf := bytes.NewReader(inputFileBuffer)
record := readRecord(buf)
// if we find a SAUCE record, update bool flag
fileHasSAUCE := (record != nil && string(record.Sauceinf.ID[:]) == SauceID)
// adjust the file size if file contains a SAUCE record
if fileHasSAUCE {
adjustedSize -= 129
if record.Sauceinf.Comments > 0 {
adjustedSize -= int64(5 + 64*record.Sauceinf.Comments)
}
}
// create the output file by invoking the appropiate function
if fext == ".pcb" {
// params: input, output, font, bits
outputImg = pcboard(inputFileBuffer, adjustedSize, fontName, bits, icecolors)
} else if fext == ".bin" {
// params: input, output, columns, font, bits, icecolors
outputImg = binfile(inputFileBuffer, adjustedSize, columns, fontName, bits, icecolors)
} else if fext == ".adf" {
// params: input, output, bits
outputImg = artworx(inputFileBuffer, adjustedSize)
} else if fext == ".idf" {
// params: input, output, bits
outputImg = icedraw(inputFileBuffer, adjustedSize)
} else if fext == ".tnd" {
outputImg = tundra(inputFileBuffer, adjustedSize, columns, fontName, bits)
} else if fext == ".xb" {
// params: input, output, bits
outputImg = xbin(inputFileBuffer, adjustedSize)
} else {
// params: input, output, font, bits, icecolors, fext
outputImg = ansi(inputFileBuffer, adjustedSize, fontName, bits, mode, icecolors, fext)
}
if scaleFactor != 1.0 && outputImg != nil {
scaledHeight := float32(outputImg.Bounds().Max.Y) * scaleFactor
scaledWidth := float32(outputImg.Bounds().Max.X) * scaleFactor
outputImg = resize.Resize(uint(scaledWidth), uint(scaledHeight), outputImg, resize.NearestNeighbor)
}
return outputImg
}
// GetSauce returns a sauce record for a given file if it exists
func GetSauce(fileName string) Sauce {
return *readFileName(fileName)
}