Pillow/src/libImaging/Matrix.c

79 lines
2.2 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* colour and luminance matrix transforms
*
* history:
* 1996-05-18 fl: created (brute force implementation)
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2021-01-03 06:17:51 +03:00
#define CLIPF(v) ((v <= 0.0) ? 0 : (v >= 255.0F) ? 255 : (UINT8)v)
2010-07-31 06:52:47 +04:00
Imaging
2021-01-03 06:17:51 +03:00
ImagingConvertMatrix(Imaging im, const char *mode, float m[]) {
2010-07-31 06:52:47 +04:00
Imaging imOut;
int x, y;
ImagingSectionCookie cookie;
2010-07-31 06:52:47 +04:00
/* Assume there's enough data in the buffer */
2020-05-10 12:56:36 +03:00
if (!im) {
2021-01-03 06:17:51 +03:00
return (Imaging)ImagingError_ModeError();
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
if (strcmp(mode, "L") == 0 && im->bands == 3) {
2020-05-01 15:08:57 +03:00
imOut = ImagingNewDirty("L", im->xsize, im->ysize);
2020-05-10 12:56:36 +03:00
if (!imOut) {
2020-05-01 15:08:57 +03:00
return NULL;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
ImagingSectionEnter(&cookie);
2020-05-01 15:08:57 +03:00
for (y = 0; y < im->ysize; y++) {
2021-01-03 06:17:51 +03:00
UINT8 *in = (UINT8 *)im->image[y];
UINT8 *out = (UINT8 *)imOut->image[y];
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
for (x = 0; x < im->xsize; x++) {
2021-01-03 06:17:51 +03:00
float v = m[0] * in[0] + m[1] * in[1] + m[2] * in[2] + m[3] + 0.5;
2020-05-01 15:08:57 +03:00
out[x] = CLIPF(v);
in += 4;
}
}
ImagingSectionLeave(&cookie);
2010-07-31 06:52:47 +04:00
} else if (strlen(mode) == 3 && im->bands == 3) {
2020-05-01 15:08:57 +03:00
imOut = ImagingNewDirty(mode, im->xsize, im->ysize);
2020-05-10 12:56:36 +03:00
if (!imOut) {
2020-05-01 15:08:57 +03:00
return NULL;
2020-05-10 12:56:36 +03:00
}
2020-05-01 15:08:57 +03:00
for (y = 0; y < im->ysize; y++) {
2021-01-03 06:17:51 +03:00
UINT8 *in = (UINT8 *)im->image[y];
UINT8 *out = (UINT8 *)imOut->image[y];
2020-05-01 15:08:57 +03:00
ImagingSectionEnter(&cookie);
2020-05-01 15:08:57 +03:00
for (x = 0; x < im->xsize; x++) {
2021-01-03 06:17:51 +03:00
float v0 = m[0] * in[0] + m[1] * in[1] + m[2] * in[2] + m[3] + 0.5;
float v1 = m[4] * in[0] + m[5] * in[1] + m[6] * in[2] + m[7] + 0.5;
float v2 = m[8] * in[0] + m[9] * in[1] + m[10] * in[2] + m[11] + 0.5;
2020-05-01 15:08:57 +03:00
out[0] = CLIPF(v0);
out[1] = CLIPF(v1);
out[2] = CLIPF(v2);
2021-01-03 06:17:51 +03:00
in += 4;
out += 4;
2020-05-01 15:08:57 +03:00
}
ImagingSectionLeave(&cookie);
2020-05-01 15:08:57 +03:00
}
2020-05-10 12:56:36 +03:00
} else {
2021-01-03 06:17:51 +03:00
return (Imaging)ImagingError_ModeError();
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
return imOut;
}