Add .clang_format
Also applies clang-format to the current code base, using command:
`find . -regex '.*\.\(c\|h\)' -exec clang-format -style=file -i {} \;`
This commit is contained in:
@@ -26,13 +26,12 @@
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
Imaging image;
|
||||
Imaging image;
|
||||
} ImagingObject;
|
||||
|
||||
inline Imaging
|
||||
imaging_python_to_c(PyObject *obj)
|
||||
{
|
||||
PyObject *im;
|
||||
imaging_python_to_c(PyObject* obj) {
|
||||
PyObject* im;
|
||||
Imaging image;
|
||||
|
||||
/* first, get the 'im' attribute */
|
||||
@@ -48,7 +47,7 @@ imaging_python_to_c(PyObject *obj)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
image = ((ImagingObject *)im)->image;
|
||||
image = ((ImagingObject*)im)->image;
|
||||
Py_DECREF(im);
|
||||
return image;
|
||||
}
|
||||
@@ -57,14 +56,13 @@ imaging_python_to_c(PyObject *obj)
|
||||
in these composite functions -- even handles auto-sizing to src! */
|
||||
static inline void
|
||||
setup_source_destination(Imaging src, Imaging dest,
|
||||
int *sx, int *sy, int *dx, int *dy, int *xsize, int *ysize)
|
||||
{
|
||||
/* handle negative/zero sizes appropriately */
|
||||
int* sx, int* sy, int* dx, int* dy, int* xsize, int* ysize) {
|
||||
/* handle negative/zero sizes appropriately */
|
||||
if (*xsize <= 0 || *ysize <= 0) {
|
||||
*xsize = src->xsize;
|
||||
*ysize = src->ysize;
|
||||
}
|
||||
|
||||
|
||||
/* set up the source position, size and destination position */
|
||||
/* handle negative dest pos */
|
||||
if (*dx < 0) {
|
||||
@@ -93,7 +91,7 @@ setup_source_destination(Imaging src, Imaging dest,
|
||||
}
|
||||
|
||||
/* convenience alpha_over with 1.0 as overall_alpha */
|
||||
inline PyObject* alpha_over(PyObject *dest, PyObject *src, PyObject *mask,
|
||||
inline PyObject* alpha_over(PyObject* dest, PyObject* src, PyObject* mask,
|
||||
int dx, int dy, int xsize, int ysize) {
|
||||
return alpha_over_full(dest, src, mask, 1.0f, dx, dy, xsize, ysize);
|
||||
}
|
||||
@@ -103,8 +101,8 @@ inline PyObject* alpha_over(PyObject *dest, PyObject *src, PyObject *mask,
|
||||
* if xsize, ysize are negative, they are instead set to the size of the image in src
|
||||
* returns NULL on error, dest on success. You do NOT need to decref the return!
|
||||
*/
|
||||
inline PyObject *
|
||||
alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alpha,
|
||||
inline PyObject*
|
||||
alpha_over_full(PyObject* dest, PyObject* src, PyObject* mask, float overall_alpha,
|
||||
int dx, int dy, int xsize, int ysize) {
|
||||
/* libImaging handles */
|
||||
Imaging imDest, imSrc, imMask;
|
||||
@@ -118,7 +116,7 @@ alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alp
|
||||
int tmp1, tmp2, tmp3;
|
||||
/* integer [0, 255] version of overall_alpha */
|
||||
UINT8 overall_alpha_int = 255 * overall_alpha;
|
||||
|
||||
|
||||
/* short-circuit this whole thing if overall_alpha is zero */
|
||||
if (overall_alpha_int == 0)
|
||||
return dest;
|
||||
@@ -173,21 +171,21 @@ alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alp
|
||||
}
|
||||
|
||||
for (y = 0; y < ysize; y++) {
|
||||
UINT8 *out = (UINT8 *)imDest->image[dy + y] + dx * 4;
|
||||
UINT8 *outmask = (UINT8 *)imDest->image[dy + y] + dx * 4 + 3;
|
||||
UINT8 *in = (UINT8 *)imSrc->image[sy + y] + sx * (imSrc->pixelsize);
|
||||
UINT8 *inmask = (UINT8 *)imMask->image[sy + y] + sx * mask_stride + mask_offset;
|
||||
UINT8* out = (UINT8*)imDest->image[dy + y] + dx * 4;
|
||||
UINT8* outmask = (UINT8*)imDest->image[dy + y] + dx * 4 + 3;
|
||||
UINT8* in = (UINT8*)imSrc->image[sy + y] + sx * (imSrc->pixelsize);
|
||||
UINT8* inmask = (UINT8*)imMask->image[sy + y] + sx * mask_stride + mask_offset;
|
||||
|
||||
for (x = 0; x < xsize; x++) {
|
||||
UINT8 in_alpha;
|
||||
|
||||
|
||||
/* apply overall_alpha */
|
||||
if (overall_alpha_int != 255 && *inmask != 0) {
|
||||
in_alpha = OV_MULDIV255(*inmask, overall_alpha_int, tmp1);
|
||||
} else {
|
||||
in_alpha = *inmask;
|
||||
}
|
||||
|
||||
|
||||
/* special cases */
|
||||
if (in_alpha == 255 || (*outmask == 0 && in_alpha > 0)) {
|
||||
*outmask = in_alpha;
|
||||
@@ -208,8 +206,8 @@ alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alp
|
||||
for (i = 0; i < 3; i++) {
|
||||
/* general case */
|
||||
*out = OV_MULDIV255(*in, in_alpha, tmp1) +
|
||||
OV_MULDIV255(OV_MULDIV255(*out, *outmask, tmp2), 255 - in_alpha, tmp3);
|
||||
|
||||
OV_MULDIV255(OV_MULDIV255(*out, *outmask, tmp2), 255 - in_alpha, tmp3);
|
||||
|
||||
*out = (*out * 255) / alpha;
|
||||
out++, in++;
|
||||
}
|
||||
@@ -230,22 +228,21 @@ alpha_over_full(PyObject *dest, PyObject *src, PyObject *mask, float overall_alp
|
||||
|
||||
/* wraps alpha_over so it can be called directly from python */
|
||||
/* properly refs the return value when needed: you DO need to decref the return */
|
||||
PyObject *
|
||||
alpha_over_wrap(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject*
|
||||
alpha_over_wrap(PyObject* self, PyObject* args) {
|
||||
/* raw input python variables */
|
||||
PyObject *dest, *src, *pos = NULL, *mask = NULL;
|
||||
/* destination position and size */
|
||||
int dx, dy, xsize, ysize;
|
||||
/* return value: dest image on success */
|
||||
PyObject *ret;
|
||||
PyObject* ret;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OO|OO", &dest, &src, &pos, &mask))
|
||||
return NULL;
|
||||
|
||||
|
||||
if (mask == NULL)
|
||||
mask = src;
|
||||
|
||||
|
||||
/* destination position read */
|
||||
if (pos == NULL) {
|
||||
xsize = 0;
|
||||
@@ -277,10 +274,10 @@ alpha_over_wrap(PyObject *self, PyObject *args)
|
||||
/* like alpha_over, but instead of src image it takes a source color
|
||||
* also, it multiplies instead of doing an over operation
|
||||
*/
|
||||
PyObject *
|
||||
tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
|
||||
PyObject*
|
||||
tint_with_mask(PyObject* dest, unsigned char sr, unsigned char sg,
|
||||
unsigned char sb, unsigned char sa,
|
||||
PyObject *mask, int dx, int dy, int xsize, int ysize) {
|
||||
PyObject* mask, int dx, int dy, int xsize, int ysize) {
|
||||
/* libImaging handles */
|
||||
Imaging imDest, imMask;
|
||||
/* cached blend properties */
|
||||
@@ -326,8 +323,8 @@ tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
|
||||
}
|
||||
|
||||
for (y = 0; y < ysize; y++) {
|
||||
UINT8 *out = (UINT8 *)imDest->image[dy + y] + dx * 4;
|
||||
UINT8 *inmask = (UINT8 *)imMask->image[sy + y] + sx * mask_stride + mask_offset;
|
||||
UINT8* out = (UINT8*)imDest->image[dy + y] + dx * 4;
|
||||
UINT8* inmask = (UINT8*)imMask->image[sy + y] + sx * mask_stride + mask_offset;
|
||||
|
||||
for (x = 0; x < xsize; x++) {
|
||||
/* special cases */
|
||||
@@ -345,7 +342,7 @@ tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
|
||||
out += 4;
|
||||
} else {
|
||||
/* general case */
|
||||
|
||||
|
||||
/* TODO work out general case */
|
||||
*out = OV_MULDIV255(*out, (255 - *inmask) + OV_MULDIV255(sr, *inmask, tmp1), tmp2);
|
||||
out++;
|
||||
@@ -373,16 +370,16 @@ tint_with_mask(PyObject *dest, unsigned char sr, unsigned char sg,
|
||||
* (or at least, the version poorly reproduced here:
|
||||
* http://www.gidforums.com/t-20838.html )
|
||||
*/
|
||||
PyObject *
|
||||
draw_triangle(PyObject *dest, int inclusive,
|
||||
PyObject*
|
||||
draw_triangle(PyObject* dest, int inclusive,
|
||||
int x0, int y0,
|
||||
unsigned char r0, unsigned char g0, unsigned char b0,
|
||||
int x1, int y1,
|
||||
unsigned char r1, unsigned char g1, unsigned char b1,
|
||||
int x2, int y2,
|
||||
unsigned char r2, unsigned char g2, unsigned char b2,
|
||||
int tux, int tuy, int *touchups, unsigned int num_touchups) {
|
||||
|
||||
int tux, int tuy, int* touchups, unsigned int num_touchups) {
|
||||
|
||||
/* destination image */
|
||||
Imaging imDest;
|
||||
/* ranges of pixels that are affected */
|
||||
@@ -397,7 +394,7 @@ draw_triangle(PyObject *dest, int inclusive,
|
||||
int tmp;
|
||||
/* iteration variables */
|
||||
int x, y;
|
||||
|
||||
|
||||
imDest = imaging_python_to_c(dest);
|
||||
if (!imDest)
|
||||
return NULL;
|
||||
@@ -408,48 +405,57 @@ draw_triangle(PyObject *dest, int inclusive,
|
||||
"given destination image does not have mode \"RGBA\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* set up draw ranges */
|
||||
xmin = OV_MIN(x0, OV_MIN(x1, x2));
|
||||
ymin = OV_MIN(y0, OV_MIN(y1, y2));
|
||||
xmax = OV_MAX(x0, OV_MAX(x1, x2)) + 1;
|
||||
ymax = OV_MAX(y0, OV_MAX(y1, y2)) + 1;
|
||||
|
||||
|
||||
xmin = OV_MAX(xmin, 0);
|
||||
ymin = OV_MAX(ymin, 0);
|
||||
xmax = OV_MIN(xmax, imDest->xsize);
|
||||
ymax = OV_MIN(ymax, imDest->ysize);
|
||||
|
||||
|
||||
/* setup coefficients */
|
||||
a12 = y1 - y2; b12 = x2 - x1; c12 = (x1 * y2) - (x2 * y1);
|
||||
a20 = y2 - y0; b20 = x0 - x2; c20 = (x2 * y0) - (x0 * y2);
|
||||
a01 = y0 - y1; b01 = x1 - x0; c01 = (x0 * y1) - (x1 * y0);
|
||||
|
||||
a12 = y1 - y2;
|
||||
b12 = x2 - x1;
|
||||
c12 = (x1 * y2) - (x2 * y1);
|
||||
a20 = y2 - y0;
|
||||
b20 = x0 - x2;
|
||||
c20 = (x2 * y0) - (x0 * y2);
|
||||
a01 = y0 - y1;
|
||||
b01 = x1 - x0;
|
||||
c01 = (x0 * y1) - (x1 * y0);
|
||||
|
||||
/* setup normalizers */
|
||||
alpha_norm = 1.0f / ((a12 * x0) + (b12 * y0) + c12);
|
||||
beta_norm = 1.0f / ((a20 * x1) + (b20 * y1) + c20);
|
||||
beta_norm = 1.0f / ((a20 * x1) + (b20 * y1) + c20);
|
||||
gamma_norm = 1.0f / ((a01 * x2) + (b01 * y2) + c01);
|
||||
|
||||
|
||||
/* iterate over the destination rect */
|
||||
for (y = ymin; y < ymax; y++) {
|
||||
UINT8 *out = (UINT8 *)imDest->image[y] + xmin * 4;
|
||||
|
||||
UINT8* out = (UINT8*)imDest->image[y] + xmin * 4;
|
||||
|
||||
for (x = xmin; x < xmax; x++) {
|
||||
float alpha, beta, gamma;
|
||||
alpha = alpha_norm * ((a12 * x) + (b12 * y) + c12);
|
||||
beta = beta_norm * ((a20 * x) + (b20 * y) + c20);
|
||||
beta = beta_norm * ((a20 * x) + (b20 * y) + c20);
|
||||
gamma = gamma_norm * ((a01 * x) + (b01 * y) + c01);
|
||||
|
||||
|
||||
if (alpha >= 0 && beta >= 0 && gamma >= 0 &&
|
||||
(inclusive || (alpha * beta * gamma > 0))) {
|
||||
unsigned int r = alpha * r0 + beta * r1 + gamma * r2;
|
||||
unsigned int g = alpha * g0 + beta * g1 + gamma * g2;
|
||||
unsigned int b = alpha * b0 + beta * b1 + gamma * b2;
|
||||
|
||||
*out = OV_MULDIV255(*out, r, tmp); out++;
|
||||
*out = OV_MULDIV255(*out, g, tmp); out++;
|
||||
*out = OV_MULDIV255(*out, b, tmp); out++;
|
||||
|
||||
|
||||
*out = OV_MULDIV255(*out, r, tmp);
|
||||
out++;
|
||||
*out = OV_MULDIV255(*out, g, tmp);
|
||||
out++;
|
||||
*out = OV_MULDIV255(*out, b, tmp);
|
||||
out++;
|
||||
|
||||
/* keep alpha the same */
|
||||
out++;
|
||||
} else {
|
||||
@@ -458,42 +464,45 @@ draw_triangle(PyObject *dest, int inclusive,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
while (num_touchups > 0) {
|
||||
float alpha, beta, gamma;
|
||||
unsigned int r, g, b;
|
||||
UINT8 *out;
|
||||
|
||||
UINT8* out;
|
||||
|
||||
x = touchups[0] + tux;
|
||||
y = touchups[1] + tuy;
|
||||
touchups += 2;
|
||||
num_touchups--;
|
||||
|
||||
|
||||
if (x < 0 || x >= imDest->xsize || y < 0 || y >= imDest->ysize)
|
||||
continue;
|
||||
|
||||
out = (UINT8 *)imDest->image[y] + x * 4;
|
||||
out = (UINT8*)imDest->image[y] + x * 4;
|
||||
|
||||
alpha = alpha_norm * ((a12 * x) + (b12 * y) + c12);
|
||||
beta = beta_norm * ((a20 * x) + (b20 * y) + c20);
|
||||
beta = beta_norm * ((a20 * x) + (b20 * y) + c20);
|
||||
gamma = gamma_norm * ((a01 * x) + (b01 * y) + c01);
|
||||
|
||||
|
||||
r = alpha * r0 + beta * r1 + gamma * r2;
|
||||
g = alpha * g0 + beta * g1 + gamma * g2;
|
||||
b = alpha * b0 + beta * b1 + gamma * b2;
|
||||
|
||||
*out = OV_MULDIV255(*out, r, tmp); out++;
|
||||
*out = OV_MULDIV255(*out, g, tmp); out++;
|
||||
*out = OV_MULDIV255(*out, b, tmp); out++;
|
||||
|
||||
*out = OV_MULDIV255(*out, r, tmp);
|
||||
out++;
|
||||
*out = OV_MULDIV255(*out, g, tmp);
|
||||
out++;
|
||||
*out = OV_MULDIV255(*out, b, tmp);
|
||||
out++;
|
||||
}
|
||||
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* scales the image to half size
|
||||
*/
|
||||
inline PyObject *
|
||||
resize_half(PyObject *dest, PyObject *src) {
|
||||
inline PyObject*
|
||||
resize_half(PyObject* dest, PyObject* src) {
|
||||
/* libImaging handles */
|
||||
Imaging imDest, imSrc;
|
||||
/* alpha properties */
|
||||
@@ -501,138 +510,134 @@ resize_half(PyObject *dest, PyObject *src) {
|
||||
/* iteration variables */
|
||||
unsigned int x, y;
|
||||
/* temp color variables */
|
||||
unsigned int r, g, b, a;
|
||||
unsigned int r, g, b, a;
|
||||
/* size values for source and destination */
|
||||
int src_width, src_height, dest_width, dest_height;
|
||||
|
||||
|
||||
imDest = imaging_python_to_c(dest);
|
||||
imSrc = imaging_python_to_c(src);
|
||||
|
||||
|
||||
if (!imDest || !imSrc)
|
||||
return NULL;
|
||||
|
||||
|
||||
/* check the various image modes, make sure they make sense */
|
||||
if (strcmp(imDest->mode, "RGBA") != 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"given destination image does not have mode \"RGBA\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
if (strcmp(imSrc->mode, "RGBA") != 0 && strcmp(imSrc->mode, "RGB") != 0) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"given source image does not have mode \"RGBA\" or \"RGB\"");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
src_width = imSrc->xsize;
|
||||
src_height = imSrc->ysize;
|
||||
dest_width = imDest->xsize;
|
||||
dest_height = imDest->ysize;
|
||||
|
||||
|
||||
/* make sure destination size is 1/2 src size */
|
||||
if (src_width / 2 != dest_width || src_height / 2 != dest_height) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"destination image size is not one-half source image size");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* set up flags for the src/mask type */
|
||||
src_has_alpha = (imSrc->pixelsize == 4 ? 1 : 0);
|
||||
dest_has_alpha = (imDest->pixelsize == 4 ? 1 : 0);
|
||||
|
||||
|
||||
/* check that there remains anything to resize */
|
||||
if (dest_width <= 0 || dest_height <= 0) {
|
||||
/* nothing to do, return */
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
/* set to fully opaque if source has no alpha channel */
|
||||
if(!src_has_alpha)
|
||||
if (!src_has_alpha)
|
||||
a = 0xFF << 2;
|
||||
|
||||
|
||||
for (y = 0; y < dest_height; y++) {
|
||||
|
||||
UINT8 *out = (UINT8 *)imDest->image[y];
|
||||
UINT8 *in_row1 = (UINT8 *)imSrc->image[y * 2];
|
||||
UINT8 *in_row2 = (UINT8 *)imSrc->image[y * 2 + 1];
|
||||
|
||||
|
||||
UINT8* out = (UINT8*)imDest->image[y];
|
||||
UINT8* in_row1 = (UINT8*)imSrc->image[y * 2];
|
||||
UINT8* in_row2 = (UINT8*)imSrc->image[y * 2 + 1];
|
||||
|
||||
for (x = 0; x < dest_width; x++) {
|
||||
|
||||
|
||||
// read first column
|
||||
r = *in_row1;
|
||||
r = *in_row1;
|
||||
r += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
g = *in_row1;
|
||||
g = *in_row1;
|
||||
g += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
b = *in_row1;
|
||||
b = *in_row1;
|
||||
b += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
|
||||
if (src_has_alpha)
|
||||
{
|
||||
a = *in_row1;
|
||||
in_row2++;
|
||||
|
||||
if (src_has_alpha) {
|
||||
a = *in_row1;
|
||||
a += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
}
|
||||
|
||||
// read second column
|
||||
r += *in_row1;
|
||||
|
||||
// read second column
|
||||
r += *in_row1;
|
||||
r += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
g += *in_row1;
|
||||
g += *in_row1;
|
||||
g += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
b += *in_row1;
|
||||
b += *in_row1;
|
||||
b += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
|
||||
if (src_has_alpha)
|
||||
{
|
||||
a += *in_row1;
|
||||
|
||||
if (src_has_alpha) {
|
||||
a += *in_row1;
|
||||
a += *in_row2;
|
||||
in_row1++;
|
||||
in_row2++;
|
||||
}
|
||||
|
||||
// write blended color
|
||||
|
||||
// write blended color
|
||||
*out = (UINT8)(r >> 2);
|
||||
out++;
|
||||
*out = (UINT8)(g >> 2);
|
||||
out++;
|
||||
*out = (UINT8)(b >> 2);
|
||||
out++;
|
||||
|
||||
if (dest_has_alpha)
|
||||
{
|
||||
|
||||
if (dest_has_alpha) {
|
||||
*out = (UINT8)(a >> 2);
|
||||
out++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* wraps resize_half so it can be called directly from python */
|
||||
PyObject *
|
||||
resize_half_wrap(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject*
|
||||
resize_half_wrap(PyObject* self, PyObject* args) {
|
||||
/* raw input python variables */
|
||||
PyObject *dest, *src;
|
||||
/* return value: dest image on success */
|
||||
PyObject *ret;
|
||||
|
||||
PyObject* ret;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "OO", &dest, &src))
|
||||
return NULL;
|
||||
|
||||
|
||||
ret = resize_half(dest, src);
|
||||
if (ret == dest) {
|
||||
/* Python needs us to own our return value */
|
||||
|
||||
Reference in New Issue
Block a user