oil 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Copyright (c) 2014-2019 Timothy Elliott
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to deal
5
+ * in the Software without restriction, including without limitation the rights
6
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ * copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ * THE SOFTWARE.
20
+ */
21
+
22
+ #ifndef OIL_RESAMPLE_H
23
+ #define OIL_RESAMPLE_H
24
+
25
+ /**
26
+ * Color spaces currently supported by oil.
27
+ */
28
+ enum oil_colorspace {
29
+ // error
30
+ OIL_CS_UNKNOWN = 0,
31
+
32
+ // greyscale - no sRGB gamma space conversions
33
+ OIL_CS_G = 0x0001,
34
+
35
+ // greyscale w/ alpha - uses premultiplied alpha
36
+ OIL_CS_GA = 0x0002,
37
+
38
+ // sRGB - input will be converted to linear RGB during processing
39
+ OIL_CS_RGB = 0x0003,
40
+
41
+ // sRGB w/ alpha - sRGB to linear conversion and premultiplied alpha
42
+ OIL_CS_RGBA = 0x0104,
43
+
44
+ // sRGB w/ alpha - alpha first, then sRGB to linear conversion and premultiplied alpha
45
+ OIL_CS_ARGB = 0x0304,
46
+
47
+ // sRGB w/o alpha - 4 bytes per pixel, 4th byte (X) is ignored
48
+ OIL_CS_RGBX = 0x0404,
49
+
50
+ // no color space conversions
51
+ OIL_CS_CMYK = 0x0204,
52
+ };
53
+
54
+ /**
55
+ * Macro to get the number of components from an oil color space.
56
+ */
57
+ #define OIL_CMP(x) ((x)&0xFF)
58
+
59
+ /**
60
+ * Struct to hold state for scaling. Changing these will produce unpredictable
61
+ * results.
62
+ */
63
+ struct oil_scale {
64
+ int in_height; // input image height.
65
+ int out_height; // output image height.
66
+ int in_width; // input image width.
67
+ int out_width; // output image height.
68
+ enum oil_colorspace cs; // color space of input & output.
69
+ int in_pos; // current row of input image.
70
+ int out_pos; // current row of output image.
71
+ float *coeffs_y; // buffer for holding temporary y-coefficients.
72
+ float *coeffs_x; // buffer for holding precalculated coefficients.
73
+ int *borders_x; // holds precalculated coefficient rotation points.
74
+ int *borders_y; // coefficient rotation points for y-scaling.
75
+ float *sums_y; // buffer of intermediate sums for y-scaling.
76
+ float *rb; // ring buffer holding scanlines.
77
+ float *tmp_coeffs; // temporary buffer for calculating coeffs.
78
+ void *buf; // single backing allocation for all buffers above.
79
+ };
80
+
81
+ /**
82
+ * Initialize static, pre-calculated tables. This only needs to be called once.
83
+ * A call to oil_scale_init() will initialize these tables if not already done,
84
+ * so explicityly calling oil_global_init() is only needed if there are
85
+ * concurrency concerns.
86
+ */
87
+ void oil_global_init(void);
88
+
89
+ /**
90
+ * Calculate the buffer size needed for an oil scaler struct.
91
+ * @in_height: Height, in pixels, of the input image.
92
+ * @out_height: Height, in pixels, of the output image.
93
+ * @in_width: Width, in pixels, of the input image.
94
+ * @out_width: Width, in pixels, of the output image.
95
+ * @cs: Color space of the input/output images.
96
+ *
97
+ * Returns the required buffer size in bytes.
98
+ */
99
+ int oil_scale_alloc_size(int in_height, int out_height, int in_width,
100
+ int out_width, enum oil_colorspace cs);
101
+
102
+ /**
103
+ * Initialize an oil scaler struct with a pre-allocated buffer.
104
+ * @os: Pointer to the scaler struct to be initialized.
105
+ * @in_height: Height, in pixels, of the input image.
106
+ * @out_height: Height, in pixels, of the output image.
107
+ * @in_width: Width, in pixels, of the input image.
108
+ * @out_width: Width, in pixels, of the output image.
109
+ * @cs: Color space of the input/output images.
110
+ * @buf: Pre-allocated buffer for internal use.
111
+ *
112
+ * Returns 0 on success.
113
+ * Returns -1 if an argument is bad.
114
+ */
115
+ int oil_scale_init_allocated(struct oil_scale *os, int in_height,
116
+ int out_height, int in_width, int out_width, enum oil_colorspace cs,
117
+ void *buf);
118
+
119
+ /**
120
+ * Initialize an oil scaler struct.
121
+ * @os: Pointer to the scaler struct to be initialized.
122
+ * @in_height: Height, in pixels, of the input image.
123
+ * @out_height: Height, in pixels, of the output image.
124
+ * @in_width: Width, in pixels, of the input image.
125
+ * @out_width: Width, in pixels, of the output image.
126
+ * @cs: Color space of the input/output images.
127
+ *
128
+ * Returns 0 on success.
129
+ * Returns -1 if an argument is bad.
130
+ * Returns -2 if unable to allocate memory.
131
+ */
132
+ int oil_scale_init(struct oil_scale *os, int in_height, int out_height,
133
+ int in_width, int out_width, enum oil_colorspace cs);
134
+
135
+ /**
136
+ * Reset rows counters in an oil scaler struct.
137
+ * @os: Pointer to the scaler struct to be reseted.
138
+ */
139
+ void oil_scale_restart(struct oil_scale *);
140
+
141
+ /**
142
+ * Free heap allocations associated with an oil scaler struct.
143
+ * @os: Pointer to the scaler struct to be freed.
144
+ */
145
+ void oil_scale_free(struct oil_scale *os);
146
+
147
+ /**
148
+ * Return the number of input scanlines needed before the next output scanline
149
+ * can be produced.
150
+ * @os: Pointer to the oil scaler struct.
151
+ *
152
+ * Returns 0 if no more input lines are needed to produce the next output line.
153
+ * Otherwise, returns the number of input lines that are needed.
154
+ */
155
+ int oil_scale_slots(struct oil_scale *os);
156
+
157
+ /**
158
+ * Ingest & buffer an input scanline. Input is unsigned chars.
159
+ * @os: Pointer to the scaler struct.
160
+ * @in: Pointer to the input buffer containing a scanline.
161
+ */
162
+ void oil_scale_in(struct oil_scale *os, unsigned char *in);
163
+
164
+ /**
165
+ * Scale previously ingested & buffered contents to produce the next scaled output
166
+ * scanline.
167
+ * @os: Pointer to the scaler struct.
168
+ * @out: Pointer to the buffer where the output scanline will be written.
169
+ */
170
+ void oil_scale_out(struct oil_scale *os, unsigned char *out);
171
+
172
+ /**
173
+ * Calculate an output ratio that preserves the input aspect ratio.
174
+ * @src_width: Width, in pixels, of the input image.
175
+ * @src_height: Height, in pixels, of the input image.
176
+ * @out_width: Width, in pixels, of the output bounding box.
177
+ * @out_height: Height, in pixels, of the output bounding box.
178
+ *
179
+ * The out_width and out_height parameters will be modified, if necessary, to
180
+ * maintain the input aspect ratio while staying within the given bounding box.
181
+ *
182
+ * Returns 0 on success.
183
+ * Returns -1 if an argument is bad.
184
+ * Returns -3 if an adjusted dimension would be out of range.
185
+ */
186
+ int oil_fix_ratio(int src_width, int src_height, int *out_width,
187
+ int *out_height);
188
+
189
+ #endif
@@ -0,0 +1,59 @@
1
+ #ifndef OIL_RESAMPLE_INTERNAL_H
2
+ #define OIL_RESAMPLE_INTERNAL_H
3
+
4
+ #if defined(__x86_64__) && !defined(OIL_NO_SIMD)
5
+ #define OIL_USE_SSE2
6
+ #endif
7
+
8
+ /* Lookup tables shared between oil_resample.c and arch-specific files. */
9
+ extern float s2l_map[256];
10
+ extern float i2f_map[256];
11
+ extern unsigned char *l2s_map;
12
+ extern int l2s_len;
13
+
14
+ /* SSE2-optimized functions (oil_resample_sse2.c) */
15
+ #if defined(OIL_USE_SSE2)
16
+ void oil_shift_left_f_sse2(float *f);
17
+ void oil_yscale_out_nonlinear_sse2(float *sums, int len, unsigned char *out);
18
+ void oil_yscale_out_linear_sse2(float *sums, int len, unsigned char *out);
19
+ void oil_scale_down_g_sse2(unsigned char *in, float *sums_y, int out_width,
20
+ float *coeffs_x, int *border_buf, float *coeffs_y);
21
+ void oil_scale_down_ga_sse2(unsigned char *in, float *sums_y, int out_width,
22
+ float *coeffs_x, int *border_buf, float *coeffs_y);
23
+ void oil_scale_down_rgb_sse2(unsigned char *in, float *sums_y, int out_width,
24
+ float *coeffs_x, int *border_buf, float *coeffs_y);
25
+ void oil_scale_down_rgbx_sse2(unsigned char *in, float *sums_y, int out_width,
26
+ float *coeffs_x, int *border_buf, float *coeffs_y);
27
+ void oil_scale_down_rgba_sse2(unsigned char *in, float *sums_y, int out_width,
28
+ float *coeffs_x, int *border_buf, float *coeffs_y);
29
+ void oil_xscale_up_g_sse2(unsigned char *in, int width_in, float *out,
30
+ float *coeff_buf, int *border_buf);
31
+ void oil_xscale_up_ga_sse2(unsigned char *in, int width_in, float *out,
32
+ float *coeff_buf, int *border_buf);
33
+ void oil_yscale_up_g_cmyk_sse2(float **in, int len, float *coeffs,
34
+ unsigned char *out);
35
+ void oil_yscale_up_rgb_sse2(float **in, int len, float *coeffs,
36
+ unsigned char *out);
37
+ void oil_xscale_up_rgb_sse2(unsigned char *in, int width_in, float *out,
38
+ float *coeff_buf, int *border_buf);
39
+ void oil_xscale_up_rgbx_sse2(unsigned char *in, int width_in, float *out,
40
+ float *coeff_buf, int *border_buf);
41
+ void oil_yscale_up_ga_sse2(float **in, int len, float *coeffs,
42
+ unsigned char *out);
43
+ void oil_yscale_out_ga_sse2(float *sums, int width, unsigned char *out);
44
+ void oil_yscale_out_rgbx_sse2(float *sums, int width, unsigned char *out);
45
+ void oil_yscale_up_rgbx_sse2(float **in, int len, float *coeffs,
46
+ unsigned char *out);
47
+ void oil_yscale_out_rgba_sse2(float *sums, int width, unsigned char *out);
48
+ void oil_yscale_up_rgba_sse2(float **in, int len, float *coeffs,
49
+ unsigned char *out);
50
+ void oil_xscale_up_rgba_sse2(unsigned char *in, int width_in, float *out,
51
+ float *coeff_buf, int *border_buf);
52
+ void oil_xscale_up_cmyk_sse2(unsigned char *in, int width_in, float *out,
53
+ float *coeff_buf, int *border_buf);
54
+ void oil_scale_down_cmyk_sse2(unsigned char *in, float *sums_y, int out_width,
55
+ float *coeffs_x, int *border_buf, float *coeffs_y);
56
+ void oil_yscale_out_cmyk_sse2(float *sums, int len, unsigned char *out);
57
+ #endif
58
+
59
+ #endif