aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/render.c
blob: 17318782a32d2d97dad6399536e431aec00801f1 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * render.c
 *
 * Render a high dynamic range buffer in some sensible way
 *
 * Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2009-2012,2014 Thomas White <taw@physics.org>
 *
 * This file is part of CrystFEL.
 *
 * CrystFEL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CrystFEL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrystFEL.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif


#include <stdlib.h>
#include <math.h>
#include <stdint.h>


#include "hdf5-file.h"
#include "render.h"
#include "peaks.h"
#include "filters.h"
#include "utils.h"


static void render_rgb(double val, double max,
                       double *rp, double *gp, double *bp)
{
	int s;
	double p;
	double r, g, b;

	s = val / (max/6);
	p = fmod(val, max/6.0);
	p /= (max/6.0);

	r = 0.0;  g = 0.0;  b = 0.0;

	if ( (val < 0.0) ) {
		p = fabs(val) / (max/6.0);
		*rp = 0.0;
		*gp = 0.5*p;
		*bp = 0.0;
		return;
	}
	if ( (val > max) ) {
		s = 6;
	}
	switch ( s ) {

		case 0 :	/* Black to blue */
		r = 0;  g = 0;  b = p;
		break;

		case 1 :	/* Blue to pink */
		r = p;  g = 0;  b = 1.0;
		break;

		case 2 :	/* Pink to red */
		r = 1.0;  g = 0;  b = (1.0-p)*1.0;
		break;

		case 3 :	/* Red to Orange */
		r = 1.0;  g = 0.5*p;  b = 0;
		break;

		case 4 :	/* Orange to Yellow */
		r = 1.0;  g = 0.5 + 0.5*p;  b = 0;
		break;

		case 5 :	/* Yellow to White */
		r = 1.0;  g = 1.0;  b = 1.0*p;
		break;

		case 6 :	/* Pixel has hit the maximum value */
		r = 1.0;  g = 1.0;  b = 1.0;
		break;

	}

	*rp = r;
	*gp = g;
	*bp = b;
}


static void render_ratio(double val, double max,
                         double *rp, double *gp, double *bp)
{
	if ( val <= 1.0 ) {
		render_rgb(val, 2.0, rp, gp, bp);
	} else {
		/* Your homework is to simplify this expression */
		val = ((val-1.0)/(max-1.0)) * (max/2.0) + max/2.0;
		render_rgb(val, max, rp, gp, bp);
	}
}


static void render_mono(double val, double max,
                        double *rp, double *gp, double *bp)
{
	double p;
	p = val / max;
	if ( val < 0.0 ) p = 0.0;
	if ( val > max ) p = 1.0;
	*rp = p;
	*gp = p;
	*bp = p;
}


static void render_invmono(double val, double max,
                           double *rp, double *gp, double *bp)
{
	double p;
	p = val / max;
	p = 1.0 - p;
	if ( val < 0.0 ) p = 1.0;
	if ( val > max ) p = 0.0;
	*rp = p;
	*gp = p;
	*bp = p;
}


void render_scale(double val, double max, int scale,
                  double *rp, double *gp, double *bp)
{
	switch ( scale ) {

		case SCALE_COLOUR :
		render_rgb(val, max, rp, gp, bp);
		break;

		case SCALE_MONO :
		render_mono(val, max, rp, gp, bp);
		break;

		case SCALE_INVMONO :
		render_invmono(val, max, rp, gp, bp);
		break;

		case SCALE_RATIO :
		render_ratio(val, max, rp, gp, bp);
		break;
	}
}