Files
mtusi2026lab5/src/image_util.hpp
T
2026-11-16 09:28:38 +03:00

42 lines
1.4 KiB
C++

#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
struct ImageLimits {
std::int64_t max_bytes = 10'485'760; // 10 MiB
std::int64_t max_pixels = 40'000'000; // decoded width*height
};
enum class ImageReject {
unsupported_media_type, // → 415
payload_too_large, // → 413 (bytes or pixels)
invalid_image, // → 415 (corrupt / undecodable)
};
struct ImageInfo {
std::string content_type; // image/png | image/jpeg
int width = 0;
int height = 0;
std::int64_t pixel_count = 0;
};
// Magic sniff only (does not decode). Returns nullopt if neither PNG nor JPEG.
std::optional<std::string> sniff_content_type(const std::uint8_t* data,
std::size_t len);
// Full validation pipeline from contracts:
// sniff → stbi_info (dims) → pixel limit → stbi_load (integrity) → free.
// On success fills `out`. On failure returns ImageReject; decoded pixels are
// never retained after return.
std::optional<ImageReject> validate_image(const std::uint8_t* data,
std::size_t len,
const ImageLimits& limits,
ImageInfo& out);
// Human-readable error_code for Attachment failed path / API body.
std::string_view image_reject_code(ImageReject r);