tinyimg 1.0.0
Lightweight image manipulation library for Cloudflare Workers
Loading...
Searching...
No Matches
plan.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <stddef.h>
14#include <stdint.h>
15
16#include "tinyimg/image.h"
17#include "tinyimg/tinyimg.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
30#define TINYIMG_PLAN_MAX_OPS 32
31
32#pragma region the ir
33
82
131
181
222
248
255typedef struct {
258
260 union {
261 struct {
264 uint32_t x;
266 uint32_t y;
268 uint32_t width;
270 uint32_t height;
271 } crop;
272
273 struct {
275 uint32_t width;
277 uint32_t height;
280 } resize;
281
282 struct {
284 uint32_t width;
286 uint32_t height;
302 float focus_x;
304 float focus_y;
306 uint8_t focused;
309 } fit;
310
311 struct {
313 uint32_t turns;
314 } rotate;
315
316 struct {
318 float amount;
320 uint8_t gaussian;
321 } blur;
322
323 struct {
331 float m[12];
332 } matrix;
333
334 struct {
338 float p[5];
346 uint8_t channels;
347 } curve;
348
349 struct {
353 float p[4];
355 uint32_t x;
357 uint32_t y;
359 uint32_t width;
361 uint32_t height;
362 } effect;
363
364 struct {
366 float value;
367 } scalar;
368
372 };
373} TinyPlanOp;
374
387typedef struct {
389 const uint8_t* buffer;
391 size_t size;
394
396 uint32_t source_width;
403
405 uint8_t fusion;
407 uint8_t effort;
409 uint8_t background[4];
410
412 uint32_t count;
415} TinyPlan;
416
417#pragma endregion
418
419#pragma region resolution
420
449
538
546typedef struct {
557 int32_t matrix[12];
559 uint8_t lut[3][256];
561
562#pragma endregion
563
564#pragma region building
565
572uint32_t tiny_plan_sizeof(void);
573
580
594int tiny_plan_init(TinyPlan* plan, const uint8_t* buffer, size_t size);
595
608int tiny_plan_init_image(TinyPlan* plan, const TinyImage* image);
609
627int tiny_plan_set_fusion(TinyPlan* plan, int enabled);
628
646int tiny_plan_set_effort(TinyPlan* plan, uint8_t effort);
647
656int tiny_plan_background(TinyPlan* plan, const uint8_t* color);
657
673 TinyPlan* plan, uint32_t x, uint32_t y, uint32_t width, uint32_t height
674);
675
685int tiny_plan_resize(TinyPlan* plan, uint32_t width, uint32_t height);
686
698 TinyPlan* plan, uint32_t width, uint32_t height, TinyResampleFilter filter
699);
700
714 TinyPlan* plan, uint32_t width, uint32_t height, TinyImageFit mode,
715 TinyImageGravity gravity
716);
717
738 TinyPlan* plan, uint32_t width, uint32_t height, TinyImageFit mode,
740);
741
749
757
767int tiny_plan_rotate(TinyPlan* plan, int32_t degrees);
768
777int tiny_plan_brightness(TinyPlan* plan, float factor);
778
787int tiny_plan_contrast(TinyPlan* plan, float factor);
788
798int tiny_plan_saturation(TinyPlan* plan, float factor);
799
808int tiny_plan_hue(TinyPlan* plan, float degrees);
809
820
828
837int tiny_plan_gamma(TinyPlan* plan, float gamma);
838
847int tiny_plan_blur(TinyPlan* plan, float radius);
848
861int tiny_plan_gaussian_blur(TinyPlan* plan, float sigma);
862
876int tiny_plan_matrix(TinyPlan* plan, const float* matrix);
877
894 TinyPlan* plan, TinyCurveKind kind, const float* params, uint8_t channels
895);
896
906int tiny_plan_effect(TinyPlan* plan, TinyEffectKind kind, const float* params);
907
922 TinyPlan* plan, TinyEffectKind kind, const float* params, uint32_t x,
923 uint32_t y, uint32_t width, uint32_t height
924);
925
926#pragma endregion
927
928#pragma region inspecting
929
936uint32_t tiny_plan_count(const TinyPlan* plan);
937
946int tiny_plan_op_at(const TinyPlan* plan, uint32_t index, TinyPlanOp* op);
947
956
979uint32_t tiny_plan_cost(const TinyPlan* plan);
980
995 TinyImageFormat format, uint32_t width, uint32_t height
996);
997
1043
1056 const TinyPlanResolution* resolution, TinyPlanField field
1057);
1058
1076int tiny_plan_resolve(const TinyPlan* plan, TinyPlanResolution* resolution);
1077
1093 const TinyPlanResolution* resolution, TinyColorStage* stages,
1094 uint32_t capacity, uint32_t* count
1095);
1096
1111
1125
1137int tiny_plan_blur_box(TinyImage* image, uint32_t radius);
1138
1139#pragma endregion
1140
1141#pragma region running
1142
1150int tiny_plan_run(const TinyPlan* plan, TinyImage* out);
1151
1162 const TinyPlan* plan, TinyImageFormat format, const TinyEncodeOpts* opts,
1163 TinyWriter* writer
1164);
1165
1166#pragma endregion
1167
1168#ifdef __cplusplus
1169}
1170#endif
Image processing library utilities.
TinyImageFormat
Image formats supported by TinyImage for conversion and saving.
Definition image.h:32
TinyImageFit
How an image is made to fit a target width and height.
Definition image.h:1702
TinyImageGravity
Which part of an image a crop keeps, or where a pad puts it.
Definition image.h:1795
int tiny_plan_op_at(const TinyPlan *plan, uint32_t index, TinyPlanOp *op)
Reads one appended operation out.
int tiny_plan_fit_with(TinyPlan *plan, uint32_t width, uint32_t height, TinyImageFit mode, TinyImageGravity gravity, TinyResampleFilter filter)
Appends a fit that samples through a named filter.
int tiny_plan_contrast(TinyPlan *plan, float factor)
Appends a contrast change.
uint32_t tiny_plan_sizeof(void)
Size of a TinyPlan, for a host allocating one across the wasm boundary.
int tiny_plan_hue(TinyPlan *plan, float degrees)
Appends a hue rotation.
int tiny_plan_blur(TinyPlan *plan, float radius)
Appends a box blur.
uint32_t tiny_encode_cost(TinyImageFormat format, uint32_t width, uint32_t height)
What encoding an image of this extent is expected to cost.
TinyPlanOpClass tiny_plan_op_class(TinyPlanOpKind kind)
What class the planner puts an operation in.
int tiny_plan_curve(TinyPlan *plan, TinyCurveKind kind, const float *params, uint8_t channels)
Appends a tone curve.
TinyPlanKernel
Special cases the planner took, reported as a bitmask.
Definition plan.h:429
@ TINYIMG_KERNEL_SCALED
Definition plan.h:433
@ TINYIMG_KERNEL_ORIENT
Definition plan.h:439
@ TINYIMG_KERNEL_COPY
Definition plan.h:435
@ TINYIMG_KERNEL_REGION
Definition plan.h:431
@ TINYIMG_KERNEL_RESAMPLE
Definition plan.h:437
@ TINYIMG_KERNEL_PAD
Definition plan.h:443
@ TINYIMG_KERNEL_GRAY_DECODE
Definition plan.h:445
@ TINYIMG_KERNEL_COLOR
Definition plan.h:441
@ TINYIMG_KERNEL_NEIGHBORHOOD
Definition plan.h:447
#define TINYIMG_PLAN_MAX_OPS
How many operations one plan holds.
Definition plan.h:30
int tiny_plan_effect(TinyPlan *plan, TinyEffectKind kind, const float *params)
Appends a neighborhood effect.
TinyPlanOpClass
What the planner needs to know about an operation to place it.
Definition plan.h:191
@ TINYIMG_OP_CLASS_GEOMETRY
Moves pixels without changing their values.
Definition plan.h:198
@ TINYIMG_OP_CLASS_NEIGHBORHOOD
Reads a neighborhood around each pixel.
Definition plan.h:220
@ TINYIMG_OP_CLASS_COLOR_MATRIX
Reads one pixel and is affine in its channels.
Definition plan.h:205
@ TINYIMG_OP_CLASS_COLOR_LUT
Reads one pixel and is not affine in its channels.
Definition plan.h:212
uint32_t tiny_plan_resolution_sizeof(void)
Size of a TinyPlanResolution, for the same reason.
int tiny_plan_effect_rect(TinyPlan *plan, TinyEffectKind kind, const float *params, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
Appends a neighborhood effect confined to a rectangle.
uint32_t tiny_plan_field(const TinyPlanResolution *resolution, TinyPlanField field)
Reads one named field of a resolution.
int tiny_plan_set_fusion(TinyPlan *plan, int enabled)
Chooses whether the plan fuses.
int tiny_plan_background(TinyPlan *plan, const uint8_t *color)
Sets what padding is filled with.
int tiny_plan_resolve(const TinyPlan *plan, TinyPlanResolution *resolution)
Runs the rewrites and the propagation, touching no pixels.
int tiny_plan_brightness(TinyPlan *plan, float factor)
Appends a brightness change.
int tiny_plan_gaussian_blur(TinyPlan *plan, float sigma)
Appends a gaussian blur.
int tiny_plan_invert(TinyPlan *plan)
Appends an inversion.
int tiny_plan_flip_vertical(TinyPlan *plan)
Appends a vertical flip.
TinyPlanOpKind
The operations a plan can hold.
Definition plan.h:41
@ TINYIMG_OP_MATRIX
Definition plan.h:76
@ TINYIMG_OP_CURVE
Definition plan.h:78
@ TINYIMG_OP_CONTRAST
Definition plan.h:62
@ TINYIMG_OP_FIT
Definition plan.h:52
@ TINYIMG_OP_CROP
Definition plan.h:48
@ TINYIMG_OP_HUE
Definition plan.h:66
@ TINYIMG_OP_GAMMA
Definition plan.h:72
@ TINYIMG_OP_GRAYSCALE
Definition plan.h:68
@ TINYIMG_OP_ROTATE
Definition plan.h:58
@ TINYIMG_OP_FLIP_V
Definition plan.h:56
@ TINYIMG_OP_BLUR
Definition plan.h:74
@ TINYIMG_OP_FLIP_H
Definition plan.h:54
@ TINYIMG_OP_BRIGHTNESS
Definition plan.h:60
@ TINYIMG_OP_SATURATION
Definition plan.h:64
@ TINYIMG_OP_RESIZE
Definition plan.h:50
@ TINYIMG_OP_NONE
An empty slot, which a rewrite leaves behind and the resolved op list never contains.
Definition plan.h:46
@ TINYIMG_OP_EFFECT
Definition plan.h:80
@ TINYIMG_OP_INVERT
Definition plan.h:70
int tiny_plan_crop(TinyPlan *plan, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
Appends a crop.
int tiny_plan_run(const TinyPlan *plan, TinyImage *out)
Runs the plan and produces the output image.
TinyResampleFilter
Which weights the resampler reads a source pixel through.
Definition plan.h:227
@ TINYIMG_FILTER_NEAREST
Definition plan.h:240
@ TINYIMG_FILTER_BOX
Definition plan.h:244
@ TINYIMG_FILTER_BILINEAR
Definition plan.h:242
@ TINYIMG_FILTER_CATMULL_ROM
Definition plan.h:246
@ TINYIMG_FILTER_AUTO
Box when the axis is being reduced, Catmull-Rom when it is being enlarged, chosen per axis.
Definition plan.h:238
int tiny_plan_saturation(TinyPlan *plan, float factor)
Appends a saturation change.
int tiny_plan_matrix(TinyPlan *plan, const float *matrix)
Appends a color matrix.
int tiny_plan_set_effort(TinyPlan *plan, uint8_t effort)
Chooses how much work the plan's decode may spend.
int tiny_plan_resize_with(TinyPlan *plan, uint32_t width, uint32_t height, TinyResampleFilter filter)
Appends a resize through a named filter.
int tiny_plan_grayscale(TinyPlan *plan)
Appends a conversion to luminance.
TinyEffectKind
The neighborhood effects TINYIMG_OP_EFFECT can carry.
Definition plan.h:141
@ TINYIMG_FX_SCANLINES
Definition plan.h:179
@ TINYIMG_FX_UNSHARP
Definition plan.h:143
@ TINYIMG_FX_DITHER
Definition plan.h:175
@ TINYIMG_FX_HALFTONE
Definition plan.h:177
@ TINYIMG_FX_SOBEL
Definition plan.h:147
@ TINYIMG_FX_PIXELATE
Definition plan.h:151
@ TINYIMG_FX_ERODE
Definition plan.h:157
@ TINYIMG_FX_OUTLINE
Definition plan.h:159
@ TINYIMG_FX_ZOOM_BLUR
Definition plan.h:165
@ TINYIMG_FX_RADIAL_BLUR
Definition plan.h:163
@ TINYIMG_FX_MEDIAN
Definition plan.h:153
@ TINYIMG_FX_CHROMATIC
Definition plan.h:173
@ TINYIMG_FX_PIXELATE_REGION
Definition plan.h:171
@ TINYIMG_FX_DILATE
Definition plan.h:155
@ TINYIMG_FX_TILT_SHIFT
Definition plan.h:167
@ TINYIMG_FX_BLUR_REGION
Definition plan.h:169
@ TINYIMG_FX_CLARITY
Definition plan.h:145
@ TINYIMG_FX_EMBOSS
Definition plan.h:149
@ TINYIMG_FX_MOTION_BLUR
Definition plan.h:161
int tiny_plan_encode(const TinyPlan *plan, TinyImageFormat format, const TinyEncodeOpts *opts, TinyWriter *writer)
Runs the plan and encodes the result in one call.
int tiny_plan_rotate(TinyPlan *plan, int32_t degrees)
Appends a turn.
int tiny_plan_resize(TinyPlan *plan, uint32_t width, uint32_t height)
Appends a resize with the filter the planner would choose.
TinyPlanField
A field of a TinyPlanResolution, named rather than offset.
Definition plan.h:1009
@ TINYIMG_FIELD_PASSES
Definition plan.h:1039
@ TINYIMG_FIELD_COLOR_STAGES
Definition plan.h:1037
@ TINYIMG_FIELD_SCALE
Definition plan.h:1019
@ TINYIMG_FIELD_ELIMINATED
Definition plan.h:1033
@ TINYIMG_FIELD_DECODE_HEIGHT
Definition plan.h:1023
@ TINYIMG_FIELD_COLLAPSED
Definition plan.h:1035
@ TINYIMG_FIELD_WIDTH
Definition plan.h:1025
@ TINYIMG_FIELD_REGION_X
Definition plan.h:1011
@ TINYIMG_FIELD_REGION_HEIGHT
Definition plan.h:1017
@ TINYIMG_FIELD_HEIGHT
Definition plan.h:1027
@ TINYIMG_FIELD_DECODE_WIDTH
Definition plan.h:1021
@ TINYIMG_FIELD_REGION_Y
Definition plan.h:1013
@ TINYIMG_FIELD_KERNELS
Definition plan.h:1041
@ TINYIMG_FIELD_REGION_WIDTH
Definition plan.h:1015
@ TINYIMG_FIELD_CHANNELS
Definition plan.h:1029
@ TINYIMG_FIELD_OPS
Definition plan.h:1031
int tiny_effect_apply(TinyImage *image, const TinyPlanOp *op)
Runs one TINYIMG_OP_EFFECT operation over a materialized image.
int tiny_plan_blur_box(TinyImage *image, uint32_t radius)
Blurs an image with one box pass of the given radius.
int tiny_plan_init_image(TinyPlan *plan, const TinyImage *image)
Starts a plan over pixels that are already decoded.
uint32_t tiny_plan_count(const TinyPlan *plan)
How many operations a plan holds, before any rewrite.
uint32_t tiny_plan_cost(const TinyPlan *plan)
What running this plan is expected to cost, in microseconds.
int tiny_plan_gamma(TinyPlan *plan, float gamma)
Appends a gamma correction.
int tiny_plan_color_stages(const TinyPlanResolution *resolution, TinyColorStage *stages, uint32_t capacity, uint32_t *count)
Collapses a resolved plan's color operations into stages.
int tiny_plan_init(TinyPlan *plan, const uint8_t *buffer, size_t size)
Starts a plan over encoded bytes.
int tiny_plan_fit(TinyPlan *plan, uint32_t width, uint32_t height, TinyImageFit mode, TinyImageGravity gravity)
Appends a fit, which resolves to a scale and a crop or a pad.
TinyCurveKind
The tone curves TINYIMG_OP_CURVE can carry.
Definition plan.h:93
@ TINYIMG_CURVE_SIGMOID
Definition plan.h:116
@ TINYIMG_CURVE_GAMMA
Definition plan.h:95
@ TINYIMG_CURVE_EXPOSURE
Definition plan.h:103
@ TINYIMG_CURVE_GAIN
Definition plan.h:114
@ TINYIMG_CURVE_LEVELS
Map p[0]..p[1] onto p[3]..p[4] through gamma p[2].
Definition plan.h:110
@ TINYIMG_CURVE_SOLARIZE
Definition plan.h:101
@ TINYIMG_CURVE_FILL_LIGHT
Definition plan.h:112
@ TINYIMG_CURVE_THRESHOLD
Definition plan.h:99
@ TINYIMG_CURVE_SRGB
Definition plan.h:120
@ TINYIMG_CURVE_NEGATE
Definition plan.h:118
@ TINYIMG_CURVE_POSTERIZE
Definition plan.h:97
@ TINYIMG_CURVE_BALANCE
Shift the shadows by p[0], the midtones by p[1] and the highlights by p[2], each weighted by how much...
Definition plan.h:129
int tiny_plan_flip_horizontal(TinyPlan *plan)
Appends a horizontal flip.
int tiny_plan_replace(TinyImage *image, TinyPlan *plan)
Runs a plan over an image and puts the result in its place.
One collapsed color operation.
Definition plan.h:546
int32_t matrix[12]
Row major 3x4 in 16.16 fixed point, applied to RGB.
Definition plan.h:557
uint8_t lut[3][256]
Definition plan.h:559
TinyPlanOpClass kind
TINYIMG_OP_CLASS_COLOR_MATRIX or TINYIMG_OP_CLASS_COLOR_LUT.
Definition plan.h:550
Options a decoder honors when only part of an image is wanted.
Definition image.h:151
Options an encoder honors.
Definition image.h:224
Represents an image with its width, height, and pixel data.
Definition image.h:90
uint8_t channels
Which channels it applies to, bit 0 red through bit 2 blue.
float focus_x
Where the interesting part is, 0 through 1 across each axis.
One operation and its operands.
Definition plan.h:255
TinyPlanOpKind kind
Definition plan.h:257
What the planner decided, before any pixel is touched.
Definition plan.h:457
double source_y
Definition plan.h:476
TinyPlanOp op[TINYIMG_PLAN_MAX_OPS]
Definition plan.h:514
uint32_t color_stages_before
How many of those run on the source samples rather than the output ones.
Definition plan.h:530
double source_x
The window of the decoded image the resample reads, in decoded pixels.
Definition plan.h:474
uint8_t channels
Definition plan.h:505
double source_width
Definition plan.h:478
uint32_t sample_width
Definition plan.h:483
uint32_t eliminated
Definition plan.h:516
TinyResampleFilter filter_x
Definition plan.h:487
TinyDecodeOpts decode
Definition plan.h:459
uint32_t ops
Definition plan.h:512
uint32_t sample_height
Definition plan.h:485
uint32_t consumed
Definition plan.h:532
uint32_t offset_x
Definition plan.h:507
TinyResampleFilter filter_y
Definition plan.h:489
uint32_t decode_width
Definition plan.h:461
int8_t orientation[4]
The composed flip and quarter turn, as a signed permutation.
Definition plan.h:498
uint32_t kernels
Definition plan.h:536
uint32_t decode_height
Definition plan.h:463
uint32_t passes
Definition plan.h:534
uint32_t color_stages
Definition plan.h:520
uint32_t height
Definition plan.h:503
double source_height
Definition plan.h:480
uint32_t width
Definition plan.h:501
uint32_t collapsed
Definition plan.h:518
uint32_t offset_y
Definition plan.h:509
A sequence of operations over one source, and the source itself.
Definition plan.h:387
uint8_t background[4]
Definition plan.h:409
TinyImageFormat source_format
Definition plan.h:402
const uint8_t * buffer
Definition plan.h:389
uint8_t fusion
Definition plan.h:405
const TinyImage * image
Definition plan.h:393
uint32_t source_height
Definition plan.h:398
TinyPlanOp ops[TINYIMG_PLAN_MAX_OPS]
Definition plan.h:414
size_t size
Definition plan.h:391
uint8_t effort
Definition plan.h:407
uint8_t source_channels
Definition plan.h:400
uint32_t count
Definition plan.h:412
uint32_t source_width
Definition plan.h:396
A grow on demand byte sink.
Definition util.h:389
Umbrella header, version and error codes.
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@166254360075117043312123362303367067327240315243 curve
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@017000337133230177212043256265206222055221045063 scalar
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@316342044130103376150025253271232142115076152334 matrix
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@374333223277304076201121103302217150336110177144 fit
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@012264134113027310277220155302265350046074071203 rotate
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@206106373216176301302316134274273271120133120043 crop
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@260170342026261376217021373354227142227227360352 effect
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@101175010012034003231370371246362173337133306014 blur
struct TinyPlanOp::@233230225100226155165050354365162313070343334030::@234262012327153113164132170267211064356332166317 resize