Skip to content

Commit 2b25169

Browse files
committed
Document the internal image distinctness constraints added in the optimization PRs
1 parent 8f86212 commit 2b25169

11 files changed

Lines changed: 142 additions & 0 deletions

File tree

src/libImaging/AlphaComposite.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ typedef struct {
1919
UINT8 a;
2020
} rgba8;
2121

22+
/**
23+
* Alpha-composite imSrc over imDst, returning a newly allocated result.
24+
* Contract: imDst and imSrc are read-only and may alias each other.
25+
*/
2226
Imaging
2327
ImagingAlphaComposite(Imaging imDst, Imaging imSrc) {
2428
Imaging imOut;

src/libImaging/Bands.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
#include "Imaging.h"
1919

20+
/**
21+
* Extract a single band from imIn into a newly allocated single-band image.
22+
* Contract: imIn is read-only and the returned image is a distinct allocation.
23+
*/
2024
Imaging
2125
ImagingGetBand(Imaging imIn, int band) {
2226
Imaging imOut;
@@ -68,6 +72,14 @@ ImagingGetBand(Imaging imIn, int band) {
6872
return imOut;
6973
}
7074

75+
/**
76+
* Split imIn into its component bands.
77+
* The caller must provide an array of 4 Imaging pointers,
78+
* which will be allocated and filled with the individual bands.
79+
* The number of bands returned is the number of bands in imIn.
80+
*
81+
* Contract: imIn is read-only.
82+
*/
7183
int
7284
ImagingSplit(Imaging imIn, Imaging bands[4]) {
7385
int i, j, x, y;
@@ -173,6 +185,11 @@ ImagingSplit(Imaging imIn, Imaging bands[4]) {
173185
return imIn->bands;
174186
}
175187

188+
/**
189+
* Insert single-band imIn into `band` of imOut, in place.
190+
*
191+
* Contract: imIn and imOut MUST be distinct images and not alias.
192+
*/
176193
Imaging
177194
ImagingPutBand(Imaging imOut, Imaging imIn, int band) {
178195
int x, y;
@@ -219,6 +236,9 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) {
219236
return imOut;
220237
}
221238

239+
/**
240+
* Fill a single band of imOut with a constant colour, in place.
241+
*/
222242
Imaging
223243
ImagingFillBand(Imaging imOut, int band, int color) {
224244
int x, y;
@@ -253,6 +273,11 @@ ImagingFillBand(Imaging imOut, int band, int color) {
253273
return imOut;
254274
}
255275

276+
/**
277+
* Merge the caller-supplied bands[] into a newly allocated multi-band image.
278+
*
279+
* Contract: the bands[] inputs are read-only, and the output is new.
280+
*/
256281
Imaging
257282
ImagingMerge(const ModeID mode, Imaging bands[4]) {
258283
int i, x, y;

src/libImaging/Blend.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
#include "Imaging.h"
1919

20+
/**
21+
* Interpolate (or, for alpha outside [0, 1], extrapolate) between imIn1 and
22+
* imIn2 by `alpha`, returning a newly allocated result.
23+
*
24+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
25+
*/
2026
Imaging
2127
ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha) {
2228
Imaging imOut;

src/libImaging/Chops.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,66 +84,124 @@ create(Imaging im1, Imaging im2, const ModeID mode) {
8484
return ImagingNewDirty(im1->mode, xsize, ysize);
8585
}
8686

87+
/**
88+
* Return a newly allocated image containing the lighter pixels of the two images.
89+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
90+
*/
8791
Imaging
8892
ImagingChopLighter(Imaging imIn1, Imaging imIn2) {
8993
CHOP((in1[x] > in2[x]) ? in1[x] : in2[x]);
9094
}
9195

96+
/**
97+
* Return a newly allocated image containing the darker pixels of the two images.
98+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
99+
*/
92100
Imaging
93101
ImagingChopDarker(Imaging imIn1, Imaging imIn2) {
94102
CHOP((in1[x] < in2[x]) ? in1[x] : in2[x]);
95103
}
96104

105+
/**
106+
* Return a newly allocated image containing the absolute per-pixel difference of the
107+
* two images.
108+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
109+
*/
97110
Imaging
98111
ImagingChopDifference(Imaging imIn1, Imaging imIn2) {
99112
CHOP(abs((int)in1[x] - (int)in2[x]));
100113
}
101114

115+
/**
116+
* Return a newly allocated image containing the per-pixel product (scaled to 0-255) of
117+
* the two images.
118+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
119+
*/
102120
Imaging
103121
ImagingChopMultiply(Imaging imIn1, Imaging imIn2) {
104122
CHOP((int)in1[x] * (int)in2[x] / 255);
105123
}
106124

125+
/**
126+
* Return a newly allocated image containing the screen blend of the two images.
127+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
128+
*/
107129
Imaging
108130
ImagingChopScreen(Imaging imIn1, Imaging imIn2) {
109131
CHOP(255 - ((int)(255 - in1[x]) * (int)(255 - in2[x])) / 255);
110132
}
111133

134+
/**
135+
* Return a newly allocated image containing the per-pixel sum, divided by `scale` and
136+
* shifted by `offset`, clipped to 0-255.
137+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
138+
*/
112139
Imaging
113140
ImagingChopAdd(Imaging imIn1, Imaging imIn2, float scale, int offset) {
114141
CHOP(((int)in1[x] + (int)in2[x]) / scale + offset);
115142
}
116143

144+
/**
145+
* Return a newly allocated image containing the per-pixel difference, divided by
146+
* `scale` and shifted by `offset`, clipped to 0-255.
147+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
148+
*/
117149
Imaging
118150
ImagingChopSubtract(Imaging imIn1, Imaging imIn2, float scale, int offset) {
119151
CHOP(((int)in1[x] - (int)in2[x]) / scale + offset);
120152
}
121153

154+
/**
155+
* Return a newly allocated "1" image that is the logical AND of the two images.
156+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
157+
*/
122158
Imaging
123159
ImagingChopAnd(Imaging imIn1, Imaging imIn2) {
124160
CHOP2((in1[x] && in2[x]) ? 255 : 0, IMAGING_MODE_1);
125161
}
126162

163+
/**
164+
* Return a newly allocated "1" image that is the logical OR of the two images.
165+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
166+
*/
127167
Imaging
128168
ImagingChopOr(Imaging imIn1, Imaging imIn2) {
129169
CHOP2((in1[x] || in2[x]) ? 255 : 0, IMAGING_MODE_1);
130170
}
131171

172+
/**
173+
* Return a newly allocated "1" image that is the logical XOR of the two images.
174+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
175+
*/
132176
Imaging
133177
ImagingChopXor(Imaging imIn1, Imaging imIn2) {
134178
CHOP2(((in1[x] != 0) ^ (in2[x] != 0)) ? 255 : 0, IMAGING_MODE_1);
135179
}
136180

181+
/**
182+
* Return a newly allocated image containing the per-pixel sum of the two images,
183+
* wrapping on overflow (no clipping).
184+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
185+
*/
137186
Imaging
138187
ImagingChopAddModulo(Imaging imIn1, Imaging imIn2) {
139188
CHOP2(in1[x] + in2[x], IMAGING_MODE_UNKNOWN);
140189
}
141190

191+
/**
192+
* Return a newly allocated image containing the per-pixel difference of the two images,
193+
* wrapping on underflow (no clipping).
194+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
195+
*/
142196
Imaging
143197
ImagingChopSubtractModulo(Imaging imIn1, Imaging imIn2) {
144198
CHOP2(in1[x] - in2[x], IMAGING_MODE_UNKNOWN);
145199
}
146200

201+
/**
202+
* Return a newly allocated image containing the soft-light blend of the two images.
203+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
204+
*/
147205
Imaging
148206
ImagingChopSoftLight(Imaging imIn1, Imaging imIn2) {
149207
CHOP2(
@@ -153,6 +211,10 @@ ImagingChopSoftLight(Imaging imIn1, Imaging imIn2) {
153211
);
154212
}
155213

214+
/**
215+
* Return a newly allocated image containing the hard-light blend of the two images.
216+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
217+
*/
156218
Imaging
157219
ImagingChopHardLight(Imaging imIn1, Imaging imIn2) {
158220
CHOP2(
@@ -162,6 +224,10 @@ ImagingChopHardLight(Imaging imIn1, Imaging imIn2) {
162224
);
163225
}
164226

227+
/**
228+
* Return a newly allocated image containing the overlay blend of the two images.
229+
* Contract: imIn1 and imIn2 are read-only and may alias each other.
230+
*/
165231
Imaging
166232
ImagingOverlay(Imaging imIn1, Imaging imIn2) {
167233
CHOP2(

src/libImaging/ColorLUT.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ table_index3D(int index1D, int index2D, int index3D, int size1D, int size1D_2D)
5555
Each element is signed 16-bit int where 0 is lowest output value
5656
and 255 << PRECISION_BITS (16320) is highest value.
5757
*/
58+
/**
59+
* Apply a 3D colour lookup table to imIn, writing the result to imOut.
60+
*
61+
* Contract: unlike the other point-style loops in libImaging,
62+
* imIn and imOut MAY be the same image to support running in place.
63+
*/
5864
Imaging
5965
ImagingColorLUT3D_linear(
6066
Imaging imOut,

src/libImaging/Fill.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
#include "math.h"
2121

22+
/**
23+
* Fill an entire image with a constant colour, in place.
24+
*/
2225
Imaging
2326
ImagingFill(Imaging im, const void *colour) {
2427
ImagingSectionCookie cookie;

src/libImaging/Filter.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ kernel_i16(int size, UINT8 *in0, int x, const float *kernel, int bigendian) {
127127
return result;
128128
}
129129

130+
/**
131+
* Convolve `im` with a 3x3 kernel, writing the result to imOut.
132+
*
133+
* Contract: imOut and im *MUST* be distinct images.
134+
*/
130135
static void
131136
ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
132137
#define KERNEL1x3(in0, x, kernel, d) \
@@ -279,6 +284,11 @@ ImagingFilter3x3(Imaging imOut, Imaging im, const float *kernel, float offset) {
279284
memcpy(imOut->image[y], im->image[y], im->linesize);
280285
}
281286

287+
/**
288+
* Convolve `im` with a 5x5 kernel, writing the result to imOut.
289+
*
290+
* Contract: imOut and im *MUST* be distinct images.
291+
*/
282292
static void
283293
ImagingFilter5x5(Imaging imOut, Imaging im, const float *kernel, float offset) {
284294
#define KERNEL1x5(in0, x, kernel, d) \

src/libImaging/Histo.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ ImagingHistogramNew(Imaging im) {
5656
return h;
5757
}
5858

59+
/**
60+
* Accumulate a histogram over `im`, optionally restricted to imMask.
61+
*
62+
* Contract: Both im and imMask are read-only.
63+
*/
5964
ImagingHistogram
6065
ImagingGetHistogram(Imaging im, Imaging imMask, void *minmax) {
6166
ImagingSectionCookie cookie;

src/libImaging/Matrix.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
#define CLIPF(v) ((v <= 0.0) ? 0 : (v >= 255.0F) ? 255 : (UINT8)v)
1919

20+
/**
21+
* Convert `im` to `mode` by applying the colour matrix `m`,
22+
* returning a newly allocated result.
23+
*
24+
* Contract: im is read-only.
25+
*/
2026
Imaging
2127
ImagingConvertMatrix(Imaging im, const ModeID mode, const float m[12]) {
2228
Imaging imOut;

src/libImaging/Negative.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
#include "Imaging.h"
2020

21+
/**
22+
* Invert every byte of `im`, returning a newly allocated result.
23+
*
24+
* Contract: im is read-only.
25+
*/
2126
Imaging
2227
ImagingNegative(Imaging im) {
2328
Imaging imOut;

0 commit comments

Comments
 (0)