image_preprocessor.cpp

src/inference/image_preprocessor.cpp

@file image_preprocessor.cpp @brief Image preprocessing implementation.

Owners: @tvanfossen

bilinear_resize

void anonymous_namespace{image_preprocessor.cpp}::bilinear_resize(entropic::PreprocessedImage &img, int new_w, int new_h)

Bilinear interpolation downscale.

src/inference/image_preprocessor.cpp:99

Show on the map →

Called by resize_if_needed

Calls interpolate_pixel

Source · 25 lines
void bilinear_resize(entropic::PreprocessedImage& img,
                     int new_w, int new_h) {
    std::vector<uint8_t> out(
        static_cast<size_t>(new_w) * static_cast<size_t>(new_h) * 3);

    float x_ratio = static_cast<float>(img.width) / static_cast<float>(new_w);
    float y_ratio = static_cast<float>(img.height) / static_cast<float>(new_h);

    for (int y = 0; y < new_h; ++y) {
        float sy = (static_cast<float>(y) + 0.5f) * y_ratio - 0.5f;
        for (int x = 0; x < new_w; ++x) {
            float sx = (static_cast<float>(x) + 0.5f) * x_ratio - 0.5f;
            size_t dst_off = (static_cast<size_t>(y)
                              * static_cast<size_t>(new_w)
                              + static_cast<size_t>(x)) * 3;
            interpolate_pixel(img.pixel_data.data(),
                              img.width, img.height,
                              out.data() + dst_off, sx, sy);
        }
    }

    img.pixel_data = std::move(out);
    img.width = new_w;
    img.height = new_h;
}
Activity · 12 steps
lines 101–102std::vector<uint8_t> out( static_cast<size_t>(new_w) * static_cast<size_t>(new_h) * 3);lines 104–104float x_ratio = static_cast<float>(img.width) / static_cast<float>(new_w);lines 105–105float y_ratio = static_cast<float>(img.height) / static_cast<float>(new_h);lines 107–107◇ for (int y = 0; y < new_h; ++y)lines 108–108float sy = (static_cast<float>(y) + 0.5f) * y_ratio - 0.5f;lines 109–109◇ for (int x = 0; x < new_w; ++x)lines 110–110float sx = (static_cast<float>(x) + 0.5f) * x_ratio - 0.5f;lines 111–113size_t dst_off = (static_cast<size_t>(y) * static_cast<size_t>(new_w) + static_cast<size_t>(x)) * 3;lines 114–116interpolate_pixel(img.pixel_data.data(), img.width, img.height, out.data() + dst_off, sx, sy);lines 120–120img.pixel_data = std::move(out);lines 121–121img.width = new_w;lines 122–122img.height = new_h;

Requirements

REQ-INFER-025

decode

PreprocessedImage entropic::ImagePreprocessor::decode(const uint8_t *data, size_t len, const std::string &source)

Decode raw bytes into a PreprocessedImage .

src/inference/image_preprocessor.cpp:226

Source · 26 lines
PreprocessedImage ImagePreprocessor::decode(
    const uint8_t* data, size_t len,
    const std::string& source) {
    int w = 0;
    int h = 0;
    int ch = 0;
    auto* pixels = stbi_load_from_memory(
        data, static_cast<int>(len), &w, &h, &ch, 3);

    if (pixels == nullptr) {
        logger->error("Failed to decode image '{}': {}",
                      source, stbi_failure_reason());
        throw std::runtime_error(
            "Unsupported image format or decode error: " + source);
    }

    PreprocessedImage img;
    img.width = w;
    img.height = h;
    img.channels = 3;
    size_t data_size = static_cast<size_t>(w) * static_cast<size_t>(h) * 3;
    img.pixel_data.assign(pixels, pixels + data_size);
    stbi_image_free(pixels);

    return img;
}
Activity · 15 steps
lines 229–229int w = 0;lines 230–230int h = 0;lines 231–231int ch = 0;lines 232–233auto* pixels = stbi_load_from_memory( data, static_cast<int>(len), &w, &h, &ch, 3);lines 235–235◇ if (pixels == nullptr)lines 236–237logger->error("Failed to decode image '{}': {}", source, stbi_failure_reason());lines 238–239throw std::runtime_error( "Unsupported image format or decode error: " + source);lines 242–242PreprocessedImage img;lines 243–243img.width = w;lines 244–244img.height = h;lines 245–245img.channels = 3;lines 246–246size_t data_size = static_cast<size_t>(w) * static_cast<size_t>(h) * 3;lines 247–247img.pixel_data.assign(pixels, pixels + data_size);lines 248–248stbi_image_free(pixels);lines 250–250return img;

Requirements

REQ-INFER-025

preprocess_buffer

PreprocessedImage entropic::ImagePreprocessor::preprocess_buffer(const uint8_t *data, size_t len, const std::string &source_label)

Preprocess an image from memory buffer.

src/inference/image_preprocessor.cpp:204

Source · 12 lines
PreprocessedImage ImagePreprocessor::preprocess_buffer(
    const uint8_t* data,
    size_t len,
    const std::string& source_label) {
    auto img = decode(data, len, source_label);
    img.source_path = source_label;
    resize_if_needed(img);

    logger->info("Preprocessed buffer '{}': {}x{}",
                 source_label, img.width, img.height);
    return img;
}
Activity · 5 steps
lines 208–208auto img = decode(data, len, source_label);lines 209–209img.source_path = source_label;lines 210–210resize_if_needed(img);lines 212–213logger->info("Preprocessed buffer '{}': {}x{}", source_label, img.width, img.height);lines 214–214return img;

Requirements

REQ-INFER-025

preprocess_file

PreprocessedImage entropic::ImagePreprocessor::preprocess_file(const std::filesystem::path &path)

Preprocess an image from file path.

src/inference/image_preprocessor.cpp:181

Source · 12 lines
PreprocessedImage ImagePreprocessor::preprocess_file(
    const std::filesystem::path& path) {
    auto buf = read_image_bytes(path, config_.max_file_size);
    auto img = decode(buf.data(), buf.size(), path.string());
    img.source_path = path.string();
    resize_if_needed(img);

    logger->info("Preprocessed image {}: {}x{} ({} bytes)",
                 path.string(), img.width, img.height,
                 img.pixel_data.size());
    return img;
}
Activity · 6 steps
lines 183–183auto buf = read_image_bytes(path, config_.max_file_size);lines 184–184auto img = decode(buf.data(), buf.size(), path.string());lines 185–185img.source_path = path.string();lines 186–186resize_if_needed(img);lines 188–190logger->info("Preprocessed image {}: {}x{} ({} bytes)", path.string(), img.width, img.height, img.pixel_data.size());lines 191–191return img;

Requirements

REQ-INFER-025

read_image_bytes

static std::vector< uint8_t > entropic::read_image_bytes(const std::filesystem::path &path, size_t max_file_size)

Validate + read an image file fully into a byte buffer.

src/inference/image_preprocessor.cpp:146

Source · 24 lines
static std::vector<uint8_t> read_image_bytes(
    const std::filesystem::path& path, size_t max_file_size) {
    if (!std::filesystem::exists(path)) {
        logger->error("Image file not found: {}", path.string());
        throw std::runtime_error("Image file not found: " + path.string());
    }
    auto file_size = std::filesystem::file_size(path);
    if (file_size > max_file_size) {
        logger->error("Image file too large: {} bytes (max {})",
                      file_size, max_file_size);
        throw std::runtime_error(
            "Image exceeds max file size ("
            + std::to_string(file_size) + " > "
            + std::to_string(max_file_size) + ")");
    }
    std::ifstream file(path, std::ios::binary);
    if (!file) {
        throw std::runtime_error("Cannot open image file: " + path.string());
    }
    std::vector<uint8_t> buf(static_cast<size_t>(file_size));
    file.read(reinterpret_cast<char*>(buf.data()),
              static_cast<std::streamsize>(file_size));
    return buf;
}
Activity · 13 steps
lines 148–148◇ if (!std::filesystem::exists(path))lines 149–149logger->error("Image file not found: {}", path.string());lines 150–150throw std::runtime_error("Image file not found: " + path.string());lines 152–152auto file_size = std::filesystem::file_size(path);lines 153–153◇ if (file_size > max_file_size)lines 154–155logger->error("Image file too large: {} bytes (max {})", file_size, max_file_size);lines 156–159throw std::runtime_error( "Image exceeds max file size (" + std::to_string(file_size) + " > " + std::to_string(max_file_size) + ")");lines 161–161std::ifstream file(path, std::ios::binary);lines 162–162◇ if (!file)lines 163–163throw std::runtime_error("Cannot open image file: " + path.string());lines 165–165std::vector<uint8_t> buf(static_cast<size_t>(file_size));lines 166–167file.read(reinterpret_cast<char*>(buf.data()), static_cast<std::streamsize>(file_size));lines 168–168return buf;

Requirements

REQ-INFER-025

resize_if_needed

void entropic::ImagePreprocessor::resize_if_needed(PreprocessedImage &img)

Resize image if it exceeds max dimensions.

src/inference/image_preprocessor.cpp:266

Source · 30 lines
void ImagePreprocessor::resize_if_needed(PreprocessedImage& img) {
    if (img.width <= config_.max_width
        && img.height <= config_.max_height) {
        return;
    }

    float scale_w = static_cast<float>(config_.max_width)
                  / static_cast<float>(img.width);
    float scale_h = static_cast<float>(config_.max_height)
                  / static_cast<float>(img.height);

    float scale = config_.preserve_aspect
        ? std::min(scale_w, scale_h)
        : 1.0f; // unused, but satisfies compiler

    if (!config_.preserve_aspect) {
        scale_w = std::min(scale_w, 1.0f);
        scale_h = std::min(scale_h, 1.0f);
    } else {
        scale_w = scale;
        scale_h = scale;
    }

    int new_w = std::max(1, static_cast<int>(
        static_cast<float>(img.width) * scale_w));
    int new_h = std::max(1, static_cast<int>(
        static_cast<float>(img.height) * scale_h));

    bilinear_resize(img, new_w, new_h);
}
Activity · 14 steps
lines 267–268◇ if (img.width <= config_.max_width && img.height <= config_.max_height)lines 269–269return;lines 272–273float scale_w = static_cast<float>(config_.max_width) / static_cast<float>(img.width);lines 274–275float scale_h = static_cast<float>(config_.max_height) / static_cast<float>(img.height);lines 277–279float scale = config_.preserve_aspect ? std::min(scale_w, scale_h) : 1.0f;lines 281–281◇ if (!config_.preserve_aspect)lines 282–282scale_w = std::min(scale_w, 1.0f);lines 283–283scale_h = std::min(scale_h, 1.0f);lines 284–284elselines 285–285scale_w = scale;lines 286–286scale_h = scale;lines 289–290int new_w = std::max(1, static_cast<int>( static_cast<float>(img.width) * scale_w));lines 291–292int new_h = std::max(1, static_cast<int>( static_cast<float>(img.height) * scale_h));lines 294–294bilinear_resize(img, new_w, new_h);

Requirements

REQ-INFER-025