aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py2/libImaging/Matrix.c
blob: 5cc7795a4beb08249443c4a1b6c232096f0bd42b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * 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"


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


Imaging
ImagingConvertMatrix(Imaging im, const char *mode, float m[])
{
    Imaging imOut;
    int x, y;

    /* Assume there's enough data in the buffer */
    if (!im)
	return (Imaging) ImagingError_ModeError();

    if (strcmp(mode, "L") == 0 && im->bands == 3) {

	imOut = ImagingNewDirty("L", im->xsize, im->ysize);
	if (!imOut)
	    return NULL;

	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 v = m[0]*in[0] + m[1]*in[1] + m[2]*in[2] + m[3] + 0.5;
		out[x] = CLIPF(v);
		in += 4;
	    }
	}

    } else if (strlen(mode) == 3 && im->bands == 3) {

	imOut = ImagingNewDirty(mode, im->xsize, im->ysize);
	if (!imOut)
	    return NULL;

	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;
	    }
	}
    } else
	return (Imaging) ImagingError_ModeError();

    return imOut;
}