/**
 * @file qdx_preprocess.h
 * @brief Zero-Copy Image Preprocessing for Thermal Imaging
 *
 * Includes Sliding Window Maximum/Average ROI search,
 * Temperature filtering, and Data extraction.
 */

#ifndef QDX_PREPROCESS_H
#define QDX_PREPROCESS_H

#include "qdx_protocol.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Initialize static memory resources for sliding window calculations.
 *
 * Called once during system boot. Pre-allocates column accumulation arrays
 * to prevent dynamic allocation during runtime.
 *
 * @param maxWidth The maximum supported width of the 2D input matrix
 * @param maxHeight The maximum supported height of the 2D input matrix
 * @return 0 on success, < 0 on failure.
 */
int8_t Preprocess_Init(uint16_t maxWidth, uint16_t maxHeight);

/**
 * @brief Execute the preprocessing pipeline on a raw frame.
 *
 * High-performance processing logic that reads `input`, applies thresholds,
 * finds the best `TargetWidth x TargetHeight` ROI using sliding window arrays,
 * and extracts that region into `out_buffer` starting precisely at
 * `HeadOffset`.
 *
 * @param input Raw thermal image array
 * @param out_buffer Pre-allocated transmission buffer wrapper
 * @param output_meta Struct to fill with process results/metadata
 * @return 0 if successful, < 0 if aborted (e.g., config invalid)
 */
int8_t Preprocess_Execute(const RawImageBuffer_t *input,
                          TcpTxBuffer_t *out_buffer,
                          PreprocessResult_t *output_meta);

/**
 * @brief Safely update internal preprocessing shadow parameters.
 *
 * @param newConfig2D Partial Configuration Update
 * @param newConfig1D Partial Configuration Update
 * @param newCommon   General System Setup
 * @return 0 on success.
 */
int8_t Preprocess_Settings_Change(const Config2D_t *newConfig2D,
                                  const Config1D_t *newConfig1D,
                                  const ConfigCommon_t *newCommon);

/**
 * @brief Check if the current frame triggers the internal 2D capture condition
 *
 * Evaluates the specified TriggerRoi region using either Max or Average
 * temperature against the TriggerTemperatureThreshold.
 *
 * @param input Raw thermal image array
 * @return 1 if trigger condition is met, 0 if not met, < 0 on error.
 */
int8_t Preprocess_CheckInternalTrigger2D(const RawImageBuffer_t *input);

#ifdef __cplusplus
}
#endif

#endif /* QDX_PREPROCESS_H */
