Source code for src.data_handlers.formatters
"""
Author: Ismael Seidel (ismael.seidel@ufsc.br)
Affiliation: Embedded Computing Lab (ECL), Federal University of Santa Catarina (UFSC)
Description:
This module contains helpers to deal with filenames for light fields.
"""
import datetime
import os
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Union
if TYPE_CHECKING:
from lfc_toolkit.src.data_handlers.lightfield import (LightField,
RAWLightFieldData)
[docs]
def get_modified_date(filename: Union[str, Path]) -> str:
"""Gets the last modified date of a file.
:param filename: Path to the file
:type filename: Union[str, Path]
:return: Last modified date as a string
:rtype: str
"""
t = os.path.getmtime(filename)
return str(datetime.datetime.fromtimestamp(t))
[docs]
def get_bpp_str(bpp: float) -> str:
"""Formats the bits per pixel (bpp) value into a string.
:param bpp: Bits per pixel value
:type bpp: float
:return: Formatted bpp string
:rtype: str
"""
splitted_bpp = str(bpp).split(".")
if bpp < 1.0:
return splitted_bpp[1]
return f"{splitted_bpp[0]}_{splitted_bpp[1]}"
[docs]
def get_formatted_filename(
path: Union[str, Path, None],
pix_fmt: str,
lf_name: str,
view_width: int,
view_height: int,
n_views_width: int,
n_views_height: int,
file_extension: str = "yuv",
bpp: Optional[float] = None,
extra: str = "",
extra_end: str = "",
) -> str:
"""Generates a formatted filename for a Light Field file.
:param path: Path to the directory
:type path: Union[str, Path, None]
:param pix_fmt: Pixel format
:type pix_fmt: str
:param lf_name: Name of the Light Field
:type lf_name: str
:param view_width: Width of a single view
:type view_width: int
:param view_height: Height of a single view
:type view_height: int
:param n_views_width: Number of views in width
:type n_views_width: int
:param n_views_height: Number of views in height
:type n_views_height: int
:param file_extension: File extension, defaults to "yuv"
:type file_extension: str, optional
:param bpp: Bits per pixel, defaults to None
:type bpp: Optional[float], optional
:param extra: Extra string to append, defaults to ""
:type extra: str, optional
:param extra_end: Extra string to append at the end, defaults to ""
:type extra_end: str, optional
:return: Formatted filename
:rtype: str
"""
filename = lf_name
if bpp:
filename += "_" + get_bpp_str(bpp)
if extra:
filename += f"_{extra}"
filename += (
f"_{n_views_width}_{n_views_height}_{view_width}x{view_height}_{pix_fmt}"
)
if extra_end:
filename += f"_{extra_end}"
filename += f".{file_extension}"
if path is not None:
return Path(path) / filename
return filename
[docs]
def get_formatted_pgx_or_ppm_path(
path: Optional[Union[str, Path]], lightfield: "LightField", bpp: Optional[float] = None
) -> Path:
"""Gets the formatted path for PGX or PPM light field files.
:param path: Path to the directory, or None
:type path: Optional[Union[str, Path]]
:param lightfield: Light Field data
:type lightfield: LightField
:param bpp: Bits per pixel, defaults to None
:type bpp: Optional[float], optional
:return: Path to the formatted filename
:rtype: Path
"""
filename = lightfield.name
if bpp:
filename += f"_{get_bpp_str(bpp)}"
if path is not None:
return Path(path) / filename
return filename
[docs]
def get_formatted_filename_for_lf(
path: Union[str, Path, None],
lightfield: "RAWLightFieldData",
bpp: Optional[float] = None,
file_extension: str = "yuv",
extra: str = "",
extra_end: str = "",
) -> Path:
"""Generates a formatted filename for a Light Field file.
:param path: Path to the directory
:type path: Union[str, Path, None]
:param lightfield: Light Field data
:type lightfield: RAWLightFieldData
:param bpp: Bits per pixel, defaults to None
:type bpp: Optional[float], optional
:param file_extension: File extension, defaults to "yuv"
:type file_extension: str, optional
:param extra: Extra string to append, defaults to ""
:type extra: str, optional
:param extra_end: Extra string to append at the end, defaults to ""
:type extra_end: str, optional
:return: Formatted filename
:rtype: Path
"""
return get_formatted_filename(
path,
lightfield.pix_fmt,
lightfield.name,
lightfield.view_width,
lightfield.view_height,
lightfield.n_views_width,
lightfield.n_views_height,
file_extension,
bpp,
extra,
extra_end,
)
[docs]
def get_yuv_filename(
yuv_path: Union[str, Path],
pix_fmt: str,
lf_name: str,
view_width: int,
view_height: int,
n_views_width: int,
n_views_height: int,
) -> str:
"""Generates a formatted filename for a Light Field file.
:param yuv_path: Path to the directory
:type yuv_path: Union[str, Path]
:param pix_fmt: Pixel format
:type pix_fmt: str
:param lf_name: Name of the Light Field
:type lf_name: str
:param view_width: Width of a single view
:type view_width: int
:param view_height: Height of a single view
:type view_height: int
:param n_views_width: Number of views in width
:type n_views_width: int
:param n_views_height: Number of views in height
:type n_views_height: int
:return: Formatted filename
:rtype: str
"""
return get_formatted_filename(
path=yuv_path,
pix_fmt=pix_fmt,
lf_name=lf_name,
view_width=view_width,
view_height=view_height,
n_views_width=n_views_width,
n_views_height=n_views_height,
file_extension="yuv",
)
[docs]
def get_yuv_filename_for_lf(
yuv_path: Union[str, Path], pix_fmt: str, lightfield: "LightField"
):
"""Generates a formatted filename for a Light Field file.
:param yuv_path: Path to the directory
:type yuv_path: Union[str, Path]
:param pix_fmt: Pixel format
:type pix_fmt: str
:param lightfield: Light Field data
:type lightfield: LightField
:return: Formatted filename
:rtype: str
"""
return get_yuv_filename(
yuv_path,
pix_fmt,
lightfield.name,
lightfield.view_width,
lightfield.view_height,
lightfield.n_views_width,
lightfield.n_views_height,
)
[docs]
def get_view_name_with_leading_zeros(x: int, y: int) -> str:
"""Generates a view name with leading zeros based on coordinates.
:param x: Horizontal coordinate
:type x: int
:param y: Vertical coordinate
:type y: int
:return: Formatted view name
:rtype: str
"""
return f"{x:0>3d}_{y:0>3d}"
[docs]
def get_sequential_view_name_with_leading_zeros(i: int, leading_zeros: int = 3):
return f"{i:0{leading_zeros}d}"
[docs]
def get_formatted_filename_for_rd_report(lightfield_name: str, codec_name: str) -> str:
"""Generates the formatted filename for an RD report JSON file.
:param lightfield_name: Name of the light field
:type lightfield_name: str
:param codec_name: Name of the codec
:type codec_name: str
:return: Formatted filename for the RD report
:rtype: str
"""
return f"{lightfield_name}_{codec_name}_rd_report.json"