193 lines
6.6 KiB
Python
193 lines
6.6 KiB
Python
import logging
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from PIL import Image, UnidentifiedImageError
|
|
|
|
from glsl_glyphs.sentences import Sentence
|
|
|
|
from .glyph import Glyph
|
|
from .set import GlyphSet
|
|
|
|
if typing.TYPE_CHECKING:
|
|
from .params import Parameters
|
|
|
|
MAX_GLYPHS = 256
|
|
|
|
|
|
class Reader:
|
|
def __init__(self, params: Parameters) -> None:
|
|
self.source_image_path = (
|
|
pathlib.Path(params.source_image_path) if params.source_image_path else None
|
|
)
|
|
self.source_names_path = (
|
|
pathlib.Path(params.source_names_path) if params.source_names_path else None
|
|
)
|
|
self.source_sets_path = (
|
|
pathlib.Path(params.source_sets_path) if params.source_sets_path else None
|
|
)
|
|
self.source_sentences_path = (
|
|
pathlib.Path(params.source_sentences_path)
|
|
if params.source_sentences_path
|
|
else None
|
|
)
|
|
self.add_empty = params.add_empty
|
|
self.add_all = params.add_all
|
|
self.log = logging.getLogger(__name__)
|
|
|
|
def read(self) -> tuple[list[Glyph], list[GlyphSet], list[Sentence]]:
|
|
glyphs = self.__read_image()
|
|
names = self.__read_names()
|
|
sets = self.__read_sets()
|
|
|
|
for glyph, name in zip(glyphs, names, strict=False):
|
|
glyph.name = name
|
|
|
|
if self.add_all:
|
|
sets.insert(0, GlyphSet(index=0, length=len(glyphs), name="ALL"))
|
|
|
|
if self.add_empty and "EMPTY" not in names:
|
|
if len(glyphs) >= MAX_GLYPHS:
|
|
self.log.error(
|
|
"Cannot add missing EMPTY glyph in full %d glyphmap", MAX_GLYPHS
|
|
)
|
|
sys.exit(1)
|
|
glyphs.insert(0, Glyph(data=[0] * 4, name="EMPTY"))
|
|
names.insert(0, "EMPTY")
|
|
for glyphset in sets:
|
|
glyphset.index += 1
|
|
|
|
sentences = self.__read_sentences(names)
|
|
|
|
return glyphs, sets, sentences
|
|
|
|
def read_sentences(self) -> list[Sentence]:
|
|
names = self.__read_names()
|
|
return self.__read_sentences(names)
|
|
|
|
def __read_image(self) -> list[Glyph]:
|
|
if not self.source_image_path:
|
|
self.log.error(
|
|
"No input image provided"
|
|
)
|
|
sys.exit(1)
|
|
try:
|
|
img = Image.open(self.source_image_path).convert("1")
|
|
if img.width % 8 != 0 or img.height % 8 != 0:
|
|
self.log.error(
|
|
"Input image is not an 8x8 multiple (%dx%d)", img.width, img.height
|
|
)
|
|
sys.exit(1)
|
|
if img.width * img.height > MAX_GLYPHS * 8 * 8:
|
|
self.log.error(
|
|
"Input image too big, it must have at maximum %d tiles (currently: %d)",
|
|
MAX_GLYPHS,
|
|
(img.width * img.height) // (8 * 8),
|
|
)
|
|
sys.exit(1)
|
|
glyphs = []
|
|
for y in range(img.height // 8):
|
|
for x in range(img.width // 8):
|
|
glyphs += [self.__get_img_glyph(img, x, y)]
|
|
self.log.info(
|
|
"Loaded %d glyphs from %s", len(glyphs), self.source_image_path
|
|
)
|
|
except (OSError, UnidentifiedImageError) as e:
|
|
self.log.error(
|
|
"Could not read input image at: %s (%s)", self.source_image_path, e
|
|
)
|
|
sys.exit(1)
|
|
return glyphs
|
|
|
|
def __get_img_glyph(self, img: Image.Image, glyph_x: int, glyph_y: int) -> Glyph:
|
|
data = [
|
|
self.__get_img_glyph_sub_square(img, glyph_x, glyph_y, square)
|
|
for square in range(4)
|
|
]
|
|
return Glyph(data=data)
|
|
|
|
def __get_img_glyph_sub_square(
|
|
self, img: Image.Image, glyph_x: int, glyph_y: int, square: int
|
|
) -> int:
|
|
data = 0
|
|
for y in range(4):
|
|
for x in range(4):
|
|
pixel_on = (
|
|
img.getpixel(
|
|
(
|
|
glyph_x * 8 + (square % 2) * 4 + x,
|
|
glyph_y * 8 + (square // 2) * 4 + y,
|
|
)
|
|
)
|
|
!= 0
|
|
)
|
|
if pixel_on:
|
|
data += pow(2, y * 4 + x)
|
|
return data
|
|
|
|
def __read_sets(self) -> list[GlyphSet]:
|
|
sets = []
|
|
if self.source_sets_path:
|
|
try:
|
|
for line in self.source_sets_path.read_text().splitlines()[1:]:
|
|
index, length, name = line.split(",")
|
|
sets += [GlyphSet(index=int(index), length=int(length), name=name)]
|
|
self.log.info(
|
|
"Extracted %d sets from %s", len(sets), self.source_sets_path
|
|
)
|
|
except ValueError, TypeError:
|
|
self.log.error(
|
|
"Could not read sets file at: %s (invalid format)",
|
|
self.source_sets_path,
|
|
)
|
|
except OSError as e:
|
|
self.log.warning(
|
|
"Could not read sets file at: %s (%s)", self.source_sets_path, e
|
|
)
|
|
else:
|
|
self.log.info("No sets file provided")
|
|
return sets
|
|
|
|
def __read_names(self) -> list[str]:
|
|
names = []
|
|
if self.source_names_path:
|
|
try:
|
|
names = self.source_names_path.read_text().splitlines()
|
|
self.log.info(
|
|
"Extracted %d names from %s", len(names), self.source_names_path
|
|
)
|
|
except OSError as e:
|
|
self.log.warning(
|
|
"Could not to read names file at: %s (%s)",
|
|
self.source_names_path,
|
|
e,
|
|
)
|
|
else:
|
|
self.log.info("No names file provided")
|
|
return names
|
|
|
|
def __read_sentences(self, names: list[str]) -> list[Sentence]:
|
|
sentences = []
|
|
|
|
if self.source_sentences_path:
|
|
try:
|
|
lines = self.source_sentences_path.read_text().splitlines()
|
|
if len(lines) and len(names) == 0:
|
|
self.log.error("Cannot extract sentences without names")
|
|
sys.exit(1)
|
|
for line in lines:
|
|
sentences += [Sentence.from_text(line, names)]
|
|
self.log.info(
|
|
"Extracted %d sentences from %s",
|
|
len(sentences),
|
|
self.source_names_path,
|
|
)
|
|
except OSError as e:
|
|
self.log.warning(
|
|
"Could not to read names file at: %s (%s)",
|
|
self.source_names_path,
|
|
e,
|
|
)
|
|
return sentences
|