Skip to content
pi-c Artifact [4bc509c433]
Login

Artifact [4bc509c433]

Artifact 4bc509c433bfe2267a777aa5663d253b71c93b3538e0dfb51b0383f175f7c2c1:


/* Images as Pi takes them (image.c): which bytes are a supported image,
 * the inline size limit, and base64. No database and no I/O here, so tools
 * and the provider adapters share one set of rules. */
#ifndef PIC_IMAGE_H
#define PIC_IMAGE_H

#include <stddef.h>

/* Pi's DEFAULT_MAX_BYTES: 4.5MB of base64 payload, under the strictest provider's 5MB per image. */
#define PIC_IMAGE_MAX_BASE64 ((size_t)(4.5 * 1024 * 1024))

/* Pi's notes when an image cannot go inline (image-process.ts). pic does not
 * resize or convert, so an image over the limit, or a BMP, gets these. */
#define PIC_IMAGE_TOO_BIG "[Image omitted: could not be resized below the inline image size limit.]"
#define PIC_IMAGE_UNCONVERTIBLE "[Image omitted: could not be converted to a supported inline image format.]"

/* Pi's placeholders for a model without image input (transform-messages.ts). */
#define PIC_IMAGE_USER_PLACEHOLDER "(image omitted: model does not support images)"
#define PIC_IMAGE_TOOL_PLACEHOLDER "(tool image omitted: model does not support images)"
/* An image whose bytes retention pruned (pic prune). */
#define PIC_IMAGE_REMOVED "[image removed]"

#define PIC_IMAGE_MAX_SIDE 8000 /* pixels: what the providers take at all (the strictest one's limit) */

/* An image to attach: its bytes and MIME type; width and height (pixels)
 * once checked. */
typedef struct {
    const char *mime;
    const unsigned char *data;
    size_t len;
    long width, height;
} pic_image;

enum {
    PIC_IMAGE_OK,
    PIC_IMAGE_INVALID,     /* not an image, or its header is broken */
    PIC_IMAGE_MISMATCH,    /* the declared type is not what the data is */
    PIC_IMAGE_UNSUPPORTED, /* a valid BMP: Pi converts it, pic cannot */
    PIC_IMAGE_TOO_LARGE,   /* over 8000 px a side or the 4.5MB base64 limit */
};

/* Reads IMG's header without decoding it: PNG (IHDR and its CRC), JPEG
 * (the first frame header), GIF (the logical screen), WebP (VP8, VP8L or
 * VP8X). On PIC_IMAGE_OK mime is the sniffed type and width and height are
 * set. DECLARED, when not NULL, is a type the sender claimed, which must be
 * the sniffed one. Else why (when not NULL) says what is wrong. */
int pic_image_inspect(pic_image *img, const char *declared, char *why, size_t cap);
/* Makes an inspected IMG fit what providers take. Pi shrinks to 2000x2000
 * here; without a decoder pic only checks: PIC_IMAGE_TOO_LARGE past 8000 px
 * a side or the base64 limit. The seam for resizing. */
int pic_image_fit(pic_image *img, char *why, size_t cap);
/* pic_image_inspect, then pic_image_fit. */
int pic_image_check(pic_image *img, const char *declared, char *why, size_t cap);
/* Pi's note for an image that cannot go inline (the read tool's). */
const char *pic_image_note(int rc);

/* Pi's _normalizePromptImages for a message: checks IMAGES[0..*N) (each
 * mime the declared type, or NULL to take the sniffed one). A BMP becomes
 * Pi's note after the text, after a blank line; any other failure is an
 * error (NULL, err set) naming the image. The rest stay in order (*N their
 * count, each with its sniffed type and size). The text is malloc'd. */
char *pic_image_prepare(const char *text, pic_image *images, size_t *n, char *err, size_t errcap);

/* Pi's detectSupportedImageMimeType: image/jpeg, png (not animated), gif,
 * webp or bmp from the leading bytes, else NULL. */
const char *pic_image_sniff(const unsigned char *b, size_t n);

/* Pi's normalizeSupportedImageMimeType: the inline type a declared MIME type
 * names (image/png, image/jpeg, image/gif, image/webp), else NULL. */
const char *pic_image_inline_mime(const char *declared);

/* Standard base64 of N bytes into out (4 * ((n + 2) / 3) + 1 bytes, NUL
 * terminated); the length written. */
size_t pic_base64_encode(const unsigned char *in, size_t n, char *out);
/* Decodes standard or URL-safe base64 (padding optional, whitespace
 * skipped) into out (at least 3 * (n / 4) + 3 bytes). The length, or
 * (size_t)-1 when the text is not base64. */
size_t pic_base64_decode(const char *in, size_t n, unsigned char *out);

#endif