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"
|
|
|
|
|
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
#define CLIPF(v) ((v <= 0.0) ? 0 : (v >= 255.0F) ? 255 : (UINT8) v)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
Imaging
|
|
|
|
ImagingConvertMatrix(Imaging im, const char *mode, float m[])
|
|
|
|
{
|
|
|
|
Imaging imOut;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
/* Assume there's enough data in the buffer */
|
2020-05-10 12:56:36 +03:00
|
|
|
if (!im) {
|
2020-05-01 15:08:57 +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
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
for (y = 0; y < im->ysize; y++) {
|
|
|
|
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++) {
|
|
|
|
float v = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5;
|
|
|
|
out[x] = CLIPF(v);
|
|
|
|
in += 4;
|
|
|
|
}
|
|
|
|
}
|
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++) {
|
|
|
|
UINT8* in = (UINT8*) im->image[y];
|
|
|
|
UINT8* out = (UINT8*) imOut->image[y];
|
|
|
|
|
|
|
|
for (x = 0; x < im->xsize; x++) {
|
|
|
|
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;
|
|
|
|
out[0] = CLIPF(v0);
|
|
|
|
out[1] = CLIPF(v1);
|
|
|
|
out[2] = CLIPF(v2);
|
|
|
|
in += 4; out += 4;
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 12:56:36 +03:00
|
|
|
} else {
|
2020-05-01 15:08:57 +03:00
|
|
|
return (Imaging) ImagingError_ModeError();
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return imOut;
|
|
|
|
}
|