"""
Author: Ismael Seidel (ismael.seidel@ufsc.br)
Affiliation: Embedded Computing Lab (ECL), Federal University of Santa Catarina (UFSC)
Description:
This module defines the LightFieldFactory class, in which there are static methods to
return raw Light Field instances in different formats (e.g., PPM, YUV...).
"""
from pathlib import Path
from typing import Union
from lfc_toolkit.src.configuration.configuration_reader import \
ConfigurationReader
from lfc_toolkit.src.data_handlers.lightfield import (
LightField, RAW_BT709_FR_PGX_LightField_Data,
RAW_BT709_FR_YUV444p10le_LightField_Data, RAW_RGB_PNG_LightField_Data,
RAW_RGB_PPM_LightField_Data, RAWLightFieldData)
[docs]
class LightFieldFactory:
[docs]
@staticmethod
def get_lightfield(configuration: dict, after_preprocessing: bool = True) -> LightField:
"""Static method to return the LightField instance from defined configuration
:param configuration: JSON configuration file in which sizes, number of rows and columns are defined
:type configuration: dict
:param after_preprocessing: Boolean to set if the Light Field raw files were pre-processed
:type after_preprocessing: bool, optional
:return: LightField instance, according to its JSON configuration file
:rtype: LightField
"""
if after_preprocessing:
lf_dimensions = configuration["encode-size"]
else:
lf_dimensions = configuration["original-size"]
return LightField(
name=configuration["name"],
view_width=lf_dimensions["view-width"],
view_height=lf_dimensions["view-height"],
n_views_width=lf_dimensions["number-of-columns"],
n_views_height=lf_dimensions["number-of-rows"],
)
[docs]
@staticmethod
def get_raw_ppm_lightfield(
configuration: dict, raw_path: Union[str, Path], after_preprocessing=True, bpp_for_naming=None
) -> RAW_RGB_PPM_LightField_Data:
"""Static method to return a RAW_RGB_PPM_LightField_Data instance based on the provided configuration and raw path.
:param configuration: JSON configuration file in which sizes, number of rows and columns are defined
:type configuration: dict
:param raw_path: Path to the raw PPM light field data
:type raw_path: Union[str, Path]
:param after_preprocessing: Boolean to set if the Light Field raw files were pre-processed
:type after_preprocessing: bool, optional
:param bpp_for_naming: Bits per pixel for naming purposes
:type bpp_for_naming: optional
:return: RAW_RGB_PPM_LightField_Data instance
:rtype: RAW_RGB_PPM_LightField_Data
"""
kwargs = dict()
pre_processing = configuration.get("pre-processing", None)
if not after_preprocessing and pre_processing:
pre_processing_args = pre_processing.get("resize", dict())
for key, value in pre_processing_args.items():
kwargs[key.replace("-", "_")] = value
return RAW_RGB_PPM_LightField_Data(
lightfield=LightFieldFactory.get_lightfield(configuration=configuration, after_preprocessing=after_preprocessing),
ppm_path=raw_path,
bpp_for_naming=bpp_for_naming,
**kwargs,
)
[docs]
@staticmethod
def get_raw_yuv_lightfield(configuration: dict, raw_path: Union[str, Path], bpp_for_naming=None) -> RAW_BT709_FR_YUV444p10le_LightField_Data:
"""Static method to return a RAW_BT709_FR_YUV444p10le_LightField_Data instance based on the provided configuration and raw path.
:param configuration: JSON configuration file in which sizes, number of rows and columns are defined
:type configuration: dict
:param raw_path: Path to the raw YUV light field data
:type raw_path: Union[str, Path]
:param bpp_for_naming: Bits per pixel for naming purposes
:type bpp_for_naming: optional
:return: RAW_BT709_FR_YUV444p10le_LightField_Data instance
:rtype: RAW_BT709_FR_YUV444p10le_LightField_Data
"""
return RAW_BT709_FR_YUV444p10le_LightField_Data(
lightfield=LightFieldFactory.get_lightfield(configuration=configuration), yuv_path=raw_path, bpp_for_naming=bpp_for_naming
)
[docs]
@staticmethod
def get_raw_pgx_lightfield(configuration: dict, raw_path: Union[str, Path], bpp_for_naming=None) -> RAW_BT709_FR_PGX_LightField_Data:
"""Static method to return a RAW_BT709_FR_PGX_LightField_Data instance based on the provided configuration and raw path.
:param configuration: JSON configuration file in which sizes, number of rows and columns are defined
:type configuration: dict
:param raw_path: Path to the raw PGX light field data
:type raw_path: Union[str, Path]
:param bpp_for_naming: Bits per pixel for naming purposes
:type bpp_for_naming: optional
:return: RAW_BT709_FR_PGX_LightField_Data instance
:rtype: RAW_BT709_FR_PGX_LightField_Data
"""
return RAW_BT709_FR_PGX_LightField_Data(
lightfield=LightFieldFactory.get_lightfield(configuration=configuration), pgx_path=raw_path, bpp_for_naming=bpp_for_naming
)
[docs]
@staticmethod
def get_raw_png_lightfield(configuration: dict, raw_path: Union[str, Path], bpp_for_naming=None) -> RAW_RGB_PNG_LightField_Data:
"""Static method to return a RAW_RGB_PNG_LightField_Data instance based on the provided configuration and raw path.
:param configuration: JSON configuration file in which sizes, number of rows and columns are defined
:type configuration: dict
:param raw_path: Path to the raw PNG light field data
:type raw_path: Union[str, Path]
:param bpp_for_naming: Bits per pixel for naming purposes
:type bpp_for_naming: optional
:return: RAW_RGB_PNG_LightField_Data instance
:rtype: RAW_RGB_PNG_LightField_Data
"""
return RAW_RGB_PNG_LightField_Data(
lightfield=LightFieldFactory.get_lightfield(configuration=configuration), png_path=raw_path, bpp_for_naming=bpp_for_naming
)
[docs]
@staticmethod
def get_raw_lightfield(
configuration: ConfigurationReader,
lightfield_name: str,
raw_type: str,
after_preprocessing=True,
decoded_base_path: Union[str, Path] = None,
bpp_for_naming=None,
raw_path=None,
) -> RAWLightFieldData:
"""Static method to return a RAWLightFieldData instance based on the provided configuration, lightfield name, and raw type.
:param configuration: ConfigurationReader instance containing lightfield configurations
:type configuration: ConfigurationReader
:param lightfield_name: Name of the lightfield
:type lightfield_name: str
:param raw_type: Type of the raw lightfield data (e.g., "pgx", "yuv", "ppm", "png")
:type raw_type: str
:param after_preprocessing: Boolean to set if the Light Field raw files were pre-processed
:type after_preprocessing: bool, optional
:param decoded_base_path: Base path for decoded files
:type decoded_base_path: Union[str, Path], optional
:param bpp_for_naming: Bits per pixel for naming purposes
:type bpp_for_naming: optional
:param raw_path: Path to the raw lightfield data
:type raw_path: optional
:return: RAWLightFieldData instance
:rtype: RAWLightFieldData
"""
if not raw_path:
raw_path = configuration["raw_paths"][raw_type.lower()]
if bpp_for_naming and decoded_base_path:
raw_path = Path(decoded_base_path) / (lightfield_name) / "decoded" / raw_type.lower()
if raw_type.lower() == "pgx":
return LightFieldFactory.get_raw_pgx_lightfield(
configuration=configuration.lightfield_configurations[lightfield_name], raw_path=raw_path, bpp_for_naming=bpp_for_naming
)
if raw_type.lower() == "yuv":
return LightFieldFactory.get_raw_yuv_lightfield(
configuration=configuration.lightfield_configurations[lightfield_name], raw_path=raw_path, bpp_for_naming=bpp_for_naming
)
if raw_type.lower() == "ppm":
if not after_preprocessing:
raw_path = configuration["raw_paths"]["ppm-download"]
return LightFieldFactory.get_raw_ppm_lightfield(
configuration=configuration.lightfield_configurations[lightfield_name],
raw_path=raw_path,
after_preprocessing=after_preprocessing,
bpp_for_naming=bpp_for_naming,
)
if raw_type.lower() == "png":
return LightFieldFactory.get_raw_png_lightfield(
configuration=configuration.lightfield_configurations[lightfield_name], raw_path=raw_path, bpp_for_naming=bpp_for_naming
)
raise Exception("Unknown raw_type: " + raw_type)