Project

General

Profile

Files » config_reader.py

KAUNG Zin Thu, 12/25/2025 02:44 PM

 
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3

    
4
## @file config_reader.py
5
## @brief Reads and provides configuration values for the alpha-blending system.
6
## @details
7
## This module loads a JSON configuration file (`config.json`) and exposes
8
## accessor methods that return typed values for physical projector parameters,
9
## image metadata, blend settings, and runtime options.
10

    
11
import json
12
import os
13

    
14

    
15
class ConfigReader(object):
16
    """
17
    @brief A helper class for reading configuration values from config.json.
18
    @details
19
    The class loads parameters only once during initialization and provides
20
    getters which will be used by the MainAlphaBlender class.
21
    @see main_alpha_blender.py
22
    """
23

    
24
    CONFIG_FILENAME = "config.json"
25

    
26
    def __init__(self):
27
        """
28
        @brief This is the constructor and this loads config.json into memory if it exists.
29
        @details
30
        If the JSON file does not exist, default values will be used.
31
        """
32
        self.__config = {}
33
        self._load_config()
34

    
35
    def _load_config(self):
36
        """
37
        @brief Internal function to load JSON configuration.
38
        @details
39
        This function opens config.json (if present) and loads settings into a dictionary.
40
        For the missing files, this will not raise any error but defaults will be used instead.
41
        """
42
        if not os.path.exists(self.CONFIG_FILENAME):
43
            print(f"Warning: {self.CONFIG_FILENAME} not found. Using defaults.")
44
            return
45

    
46
        with open(self.CONFIG_FILENAME, 'r') as f:
47
            self.__config = json.load(f)
48

    
49
    def getProjectedImageWidth(self):
50
        """
51
        @brief This returns the physical width of the projected image.
52
        @details This is the width of *one* projector's image in centimeters.
53
        @return return  Projected image width in float or 0 if not found or invalid.
54
        @see main_alpha_blender.py
55
        """
56
        return float(self.__config.get("projected_image_width", 0))
57

    
58
    def getDistanceBetweenProjectors(self):
59
        """
60
        @brief This returns the physical distance between projector lenses in centimeters.
61
        @details this is used to compute percentage of overlap between the two images.
62
        @return return  Distance between projectors in float or 0 in case of wrong format.
63
        @see main_alpha_blender.py
64
        """
65
        return float(self.__config.get("distance_between_projectors", 0))
66

    
67
    def getImageWidth(self):
68
        """
69
        @brief this returns the pixel width of the input image.
70
        @return return the Pixel width (default 1920) in int.
71
        @see main_alpha_blender.py
72
        """
73
        return int(self.__config.get("image_width", 1920))
74

    
75
    def getImageHeight(self):
76
        """
77
        @brief this returns the pixel height of the input image.
78
        @return return the Pixel height (default 1080) in int.
79
        @see main_alpha_blender.py
80
        """
81
        return int(self.__config.get("image_height", 1080))
82

    
83
    def getImageName(self):
84
        """
85
        @brief this returns the file name of the image to process.
86
        @return return Input image path in string.
87
        @see main_alpha_blender.py
88
        """
89
        return self.__config.get("image_name", "")
90

    
91
    def getSide(self):
92
        """
93
        @brief This returns which projector side this instance represents.
94
        @details Valid values are `"left"` or `"right"`. Defaults to `"left"`.
95
        @return return the  Blend side in lowercase in string i.e "left" or "right".
96
        @see main_alpha_blender.py
97
        """
98
        return self.__config.get("side", "left").lower()
99

    
100
    def getGamma(self):
101
        """
102
        @brief This returns the gamma value used for mask correction.
103
        @details Typical projector gamma is around 2.2.
104
        @return return Gamma value in float.
105
        @see main_alpha_blender.py
106
        """
107
        return float(self.__config.get("gamma", 2.2))
(2-2/4)