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
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
Requirements
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
Requirements
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
Requirements
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
Requirements
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
Requirements
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);
}