132 lines
3.7 KiB
Go
132 lines
3.7 KiB
Go
/*
|
|
Copyright © 2021 Zoraiz Hassan <hzoraiz8@gmail.com>
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package aic_package
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
_ "embed"
|
|
|
|
imgManip "github.com/TheZoraiz/ascii-image-converter/image_manipulation"
|
|
"github.com/fogleman/gg"
|
|
"github.com/golang/freetype/truetype"
|
|
)
|
|
|
|
//go:embed RobotoMono-Bold.ttf
|
|
var embeddedFontFile []byte
|
|
|
|
var tempFont *truetype.Font
|
|
|
|
// Load embedded font
|
|
func init() {
|
|
// Error not handled because the same font file will always be used
|
|
tempFont, _ = truetype.Parse(embeddedFontFile)
|
|
}
|
|
|
|
/*
|
|
Unlike createGifFrameToSave(), this function is altered to ignore execution time and has a fixed font size.
|
|
This creates maximum quality ascii art, although the resulting image will not have the same dimensions
|
|
as the original image, but the ascii art quality will be maintained. This is required, since smaller provided
|
|
images will considerably decrease ascii art quality because of smaller font size.
|
|
|
|
Size of resulting image may also be considerably larger than original image.
|
|
*/
|
|
func createImageToSave(asciiArt [][]imgManip.AsciiChar, colored bool, saveImagePath, imagePath, urlImgName string) error {
|
|
|
|
constant := 14.0
|
|
|
|
x := len(asciiArt[0])
|
|
y := len(asciiArt)
|
|
|
|
// Multipying resulting image dimensions with respect to constant
|
|
x = int(constant * float64(x))
|
|
|
|
y = int(constant * float64(y))
|
|
y = y * 2
|
|
|
|
// 10 extra pixels on both x and y-axis to have 5 pixels of padding on each side
|
|
y += 10
|
|
x += 10
|
|
|
|
tempImg := image.NewRGBA(image.Rect(0, 0, x, y))
|
|
|
|
imgWidth := tempImg.Bounds().Dx()
|
|
imgHeight := tempImg.Bounds().Dy()
|
|
|
|
dc := gg.NewContext(imgWidth, imgHeight)
|
|
|
|
// Set image background as black
|
|
dc.SetRGB(0, 0, 0)
|
|
dc.Clear()
|
|
|
|
dc.DrawImage(tempImg, 0, 0)
|
|
|
|
robotoBoldFontFace := truetype.NewFace(tempFont, &truetype.Options{Size: constant * 1.5})
|
|
dc.SetFontFace(robotoBoldFontFace)
|
|
|
|
// Font color of text on picture is white by default
|
|
dc.SetColor(color.White)
|
|
|
|
// Pointer to track y-axis on the image frame
|
|
yImgPointer := 5.0
|
|
|
|
// These nested loops print each character in asciArt 2D slice separately
|
|
// so that their RGB colors can be maintained in the resulting image
|
|
for _, line := range asciiArt {
|
|
|
|
// Pointer to track x-axis on the image frame
|
|
xImgPointer := 5.0
|
|
|
|
for _, char := range line {
|
|
|
|
r := uint8(char.RgbValue[0])
|
|
g := uint8(char.RgbValue[1])
|
|
b := uint8(char.RgbValue[2])
|
|
|
|
if colored {
|
|
// Simply put, dc.SetColor() sets color for EACH character before printing it
|
|
dc.SetColor(color.RGBA{r, g, b, 255})
|
|
}
|
|
|
|
dc.DrawStringWrapped(char.Simple, xImgPointer, yImgPointer, 0, 0, float64(x), 1.8, gg.AlignLeft)
|
|
|
|
// Incremet x-axis pointer character so new one can be printed after it
|
|
// Set to the same constant as in line
|
|
xImgPointer += float64(constant)
|
|
}
|
|
|
|
dc.DrawStringWrapped("\n", xImgPointer, yImgPointer, 0, 0, float64(x), 1.8, gg.AlignLeft)
|
|
|
|
// Incremet pointer for y axis after every line printed, so
|
|
// new line can start at below the previous one on next iteration
|
|
yImgPointer += float64(constant * 2)
|
|
}
|
|
|
|
imageName, err := createSaveFileName(imagePath, urlImgName, "-ascii-art.png")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fullPathName, err := getFullSavePath(imageName, saveImagePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return dc.SavePNG(fullPathName)
|
|
}
|