aboutsummaryrefslogtreecommitdiffstats
path: root/libglitter_colour_model_convert_rasters_double.c
blob: 143cd4e09f2a4548e55a4e6ae652dee67e8dc145 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


static void
multiply_33(double **outputs, size_t opos, const double **inputs, size_t ipos, const double matrix[3][3])
{
	double i0 = inputs[0][ipos], i1 = inputs[1][ipos], i2 = inputs[2][ipos];
	outputs[0][opos] = i0 * matrix[0][0] + i1 * matrix[0][1] + i2 * matrix[0][2];
	outputs[1][opos] = i0 * matrix[1][0] + i1 * matrix[1][1] + i2 * matrix[1][2];
	outputs[2][opos] = i0 * matrix[2][0] + i1 * matrix[2][1] + i2 * matrix[2][2];
}


static void
multiply_nn(size_t n, double **outputs, size_t opos, const double **inputs, size_t ipos,
            const double matrix[n][n], double buffer[n])
{
	size_t i, j;
	for (j = 0; j < n; j++)
		buffer[j] = inputs[j][ipos];
	for (i = 0; i < n; i++) {
		outputs[i][opos] = buffer[0] * matrix[i][0];
		for (j = 1; j < n; j++)
			outputs[i][opos] += buffer[j] * matrix[i][j];
	}
}


static void
multiply_nm(size_t n, size_t m, double **outputs /* m */, size_t opos, const double **inputs /* n */,
            size_t ipos, const double matrix[m][n], double buffer[n])
{
	size_t i, j;
	for (j = 0; j < n; j++)
		buffer[j] = inputs[j][ipos];
	for (i = 0; i < m; i++) {
		outputs[i][opos] = buffer[0] * matrix[i][0];
		for (j = 1; j < n; j++)
			outputs[i][opos] += buffer[j] * matrix[i][j];
	}
}


void
libglitter_colour_model_convert_rasters_double(size_t n, size_t m, double **outputs /* m */, const double **inputs /* n */,
                                               size_t output_rowsize, size_t output_cellsize, size_t input_rowsize,
                                               size_t input_cellsize, size_t width, size_t height, const double matrix[m][n])
{
	size_t y, x, output_i, input_i, output_blanking, input_blanking;
	double *buffer;

	output_blanking = (output_rowsize - width) * output_cellsize;
	input_blanking = (input_rowsize - width) * input_cellsize;
	output_i = 0;
	input_i = 0;

	if (n == 3 && m == 3) {
		for (y = 0; y < height; y++, output_i += output_blanking, input_i += input_blanking)
			for (x = 0; x < width; x++, output_i += output_cellsize, input_i += input_cellsize)
				multiply_33(outputs, output_i, inputs, input_i, matrix);

	} else if (n == m) {
		buffer = alloca(n * sizeof(*buffer));
		for (y = 0; y < height; y++, output_i += output_blanking, input_i += input_blanking)
			for (x = 0; x < width; x++, output_i += output_cellsize, input_i += input_cellsize)
				multiply_nn(n, outputs, output_i, inputs, input_i, matrix, buffer);

	} else {
		buffer = alloca(n * sizeof(*buffer));
		for (y = 0; y < height; y++, output_i += output_blanking, input_i += input_blanking)
			for (x = 0; x < width; x++, output_i += output_cellsize, input_i += input_cellsize)
				multiply_nm(n, m, outputs, output_i, inputs, input_i, matrix, buffer);
	}
}


#else


int
main(void)
{
	return 0; /* TODO add test */
}


#endif