6.7 KiB
6.7 KiB
Bit Depth Terminology Refactoring Plan
1. Background
Currently, there's an inconsistency in how bit depth rules and settings are defined and used across the project:
config/file_type_definitions.json: Uses"bit_depth_rule"with values like"force_8bit"and"respect".config/app_settings.json: (WithinMAP_MERGE_RULES) uses"output_bit_depth"with values like"respect_inputs".processing/utils/image_saving_utils.py: Contains logic that attempts to handle"respect_inputs"but is currently unreachable, and the"respect"rule effectively defaults to 8-bit.
This plan aims to unify the terminology and correct the processing logic.
2. Proposed Unified Terminology
A new configuration key and a clear set of values will be adopted:
- New Key:
bit_depth_policy- This key will replace
"bit_depth_rule"infile_type_definitions.json. - This key will replace
"output_bit_depth"inapp_settings.json(forMAP_MERGE_RULES).
- This key will replace
- Values for
bit_depth_policy:"force_8bit": Always output 8-bit."force_16bit": Always output 16-bit."preserve": If any source image (or any input to a merge operation) has a bit depth greater than 8-bit, the output will be 16-bit. Otherwise, the output will be 8-bit.""(empty string ornull): No specific bit depth policy applies (e.g., for non-image files like models or text files).
3. Refactoring Plan Details
Phase 1: Configuration File Updates
config/file_type_definitions.json:- Rename all instances of the key
"bit_depth_rule"to"bit_depth_policy". - Update values:
"force_8bit"remains"force_8bit"."respect"changes to"preserve".""(empty string) remains"".
- Rename all instances of the key
config/app_settings.json:- Within each rule in the
MAP_MERGE_RULESarray, rename the key"output_bit_depth"to"bit_depth_policy". - Update the value:
"respect_inputs"changes to"preserve".
- Within each rule in the
Phase 2: Code Update - configuration.py
- Modify the
Configurationclass:- Rename the method
get_bit_depth_rule()toget_bit_depth_policy(). - Update this method to read the new
"bit_depth_policy"key from the loaded file type definitions. - Ensure it correctly handles and returns the new policy values (
"force_8bit","force_16bit","preserve",""). - The method should continue to provide a sensible default if a map type is not found or has an invalid policy.
- Rename the method
Phase 3: Code Update - processing/utils/image_saving_utils.py
- Refactor the
save_image_variantsfunction:- It will receive the
bit_depth_policy(e.g.,"preserve","force_8bit") via itsfile_type_defsargument (which originates from theConfigurationobject). - Correct the internal logic for determining
target_bit_depthbased on thebit_depth_policyargument:- If
bit_depth_policy == "force_8bit", thentarget_bit_depth = 8. - If
bit_depth_policy == "force_16bit", thentarget_bit_depth = 16. - If
bit_depth_policy == "preserve":- Examine the
source_bit_depth_infoargument (list of bit depths of input images). - If any source bit depth in
source_bit_depth_infois greater than 8, thentarget_bit_depth = 16. - Otherwise (all source bit depths are 8 or less, or list is empty/all None),
target_bit_depth = 8.
- Examine the
- If
bit_depth_policy == ""or isnull(or any other unhandled value), a clear default behavior should be established (e.g., log a warning and default to"preserve"or skip bit depth adjustments if appropriate for the file type).
- If
- It will receive the
Phase 4: Code Update - processing/pipeline/stages/merged_task_processor.py
- This stage is largely unaffected in its core logic for collecting
input_source_bit_depths. - The
ProcessedMergedMapDataobject it produces will continue to carry thesesource_bit_depths. - When this data is later passed to the
SaveVariantsStage(and subsequently tosave_image_variants), theinternal_map_typeof the merged map (e.g., "MAP_NRMRGH") will be used. TheConfigurationobject will provide itsbit_depth_policy(which, after refactoringfile_type_definitions.json, should be"preserve"for relevant merged maps). - The refactored
save_image_variantswill then use this"preserve"policy along with thesource_bit_depth_info(derived from the merge inputs) to correctly determine the output bit depth for the merged map.
Phase 5: Review Other Code & Potential Impacts
- Conduct a codebase search for any remaining direct usages of the old keys (
"bit_depth_rule","output_bit_depth") or their values. - Update these locations to use the new
Configuration.get_bit_depth_policy()method and the new"bit_depth_policy"key and values. - Pay special attention to any prediction logic (e.g., in
gui/prediction_handler.pyorgui/llm_prediction_handler.py) if it currently considers or tries to infer bit depth rules.
4. Backward Compatibility & Migration
- This is a breaking change for existing user-customized configuration files (
file_type_definitions.json,app_settings.json, and any custom presets). - Recommended Approach: Implement migration logic within the
Configurationclass's loading methods.- When loading
file_type_definitions.json: If"bit_depth_rule"is found, convert its value (e.g.,"respect"to"preserve") and store it under the new"bit_depth_policy"key. Log a warning. - When loading
app_settings.json(specificallyMAP_MERGE_RULES): If"output_bit_depth"is found, convert its value (e.g.,"respect_inputs"to"preserve") and store it under"bit_depth_policy". Log a warning. - This ensures the application can still function with older user configs while guiding users to update.
- When loading
5. Visualized Logic for save_image_variants (Post-Refactor)
graph TD
A[Start save_image_variants] --> B{Get bit_depth_policy for base_map_type};
B --> C{Policy is "force_8bit"?};
C -- Yes --> D[target_bit_depth = 8];
C -- No --> E{Policy is "force_16bit"?};
E -- Yes --> F[target_bit_depth = 16];
E -- No --> G{Policy is "preserve"?};
G -- Yes --> H{Any source_bit_depth_info > 8?};
H -- Yes --> I[target_bit_depth = 16];
H -- No --> J[target_bit_depth = 8];
G -- No --> K[Log warning: Unknown policy or "" policy, default to 8-bit or handle as per type];
K --> D;
D --> L[Proceed to save with 8-bit];
F --> M[Proceed to save with 16-bit];
I --> M;
J --> L;
L --> Z[End];
M --> Z;
This plan aims to create a more consistent, understandable, and correctly functioning system for handling bit depth across the application.