aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
blob: bf6c0256092939ae7ec4c0b43b049a3e879dc7a7 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * render.c
 *
 * Render a high dynamic range buffer in some sensible way
 *
 * (c) 2008-2009 Thomas White <taw27@cam.ac.uk>
 *
 *  hdfileview - view Ditabis Micron images
 *
 */

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

#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <math.h>
#include <stdint.h>

#include "hdf5-file.h"

/* Set to 1 to measure mean intensity in a rectangle */
#define MEASURE_INT 0

#define RENDER_RGB							       \
									       \
	int s;								       \
	float p;							       \
									       \
	s = val / (max/6);						       \
	p = fmod(val, max/6);						       \
	p /= (max/6);							       \
									       \
	r = 0;	g = 0;	b = 0;						       \
									       \
	switch ( s ) {							       \
		case 0 : {	/* Black to blue */			       \
			r = 0;		g = 0;			b = p*255;     \
			break;						       \
		}							       \
		case 1 : {	/* Blue to green */			       \
			r = 0;		g = 255*p;		b = (1-p)*255; \
			break;						       \
		}							       \
		case 2 : {	/* Green to red */			       \
			r =p*255;	g = (1-p)*255;		b = 0;	       \
			break;						       \
		}							       \
		case 3 : {	/* Red to Orange */			       \
			r = 255;	g = 127*p;		b = 0;	       \
			break;						       \
		}							       \
		case 4 : {	/* Orange to Yellow */			       \
			r = 255;	g = 127 + 127*p;	b = 0;	       \
			break;						       \
		}							       \
		case 5 : {	/* Yellow to White */			       \
			r = 255;	g = 255;		b = 255*p;     \
			break;						       \
		}							       \
		case 6 : {	/* Pixel has hit the maximum value */	       \
			r = 255;	g = 255;		b = 255;       \
			break;						       \
		}							       \
		default : {	/* Above saturation */			       \
			r = 255;	g = 255;		b = 255;       \
			break;						       \
		}							       \
	}

#define RENDER_MONO							       \
	float p;							       \
	p = (float)val / (float)max;					       \
	r = 255.0*p;	g = 255.0*p;	b = 255.0*p;

/* NB This function is shared between render_get_image() and
 * render_get_colour_scale() */
static void render_free_data(guchar *data, gpointer p)
{
	free(data);
}

/* Return a pixbuf containing a rendered version of the image after binning.
 * This pixbuf might be scaled later - hopefully mostly in a downward
 * direction. */
GdkPixbuf *render_get_image(struct hdfile *hdfile, int binning, int boostint,
			    int monochrome)
{
	int mw, mh, w, h;
	guchar *data;
	int16_t *hdr;
	size_t x, y;
	int16_t max;

	mw = hdfile_get_width(hdfile);
	mh = hdfile_get_height(hdfile);
	w = mw / binning;
	h = mh / binning;

	/* High dynamic range version */
	hdr = hdfile_get_image_binned(hdfile, binning, &max);
	if ( hdr == NULL ) return NULL;

	/* Rendered (colourful) version */
	data = malloc(3*w*h);
	if ( data == NULL ) {
		free(hdr);
		return NULL;
	}

	max /= boostint;
	if ( max <= 6 ) { max = 10; }
	/* These x,y coordinates are measured relative to the bottom-left
	 * corner */
	for ( y=0; y<h; y++ ) {
	for ( x=0; x<w; x++ ) {

		int val;
		guchar r, g, b;

		val = hdr[x+w*y];
		if ( !monochrome ) {
			RENDER_RGB
		} else {
			RENDER_MONO
		}

		/* Stuff inside square brackets makes this pixel go to
		 * the expected location in the pixbuf (which measures
		 * from the top-left corner */
		data[3*( x+w*(h-1-y) )+0] = r;
		data[3*( x+w*(h-1-y) )+1] = g;
		data[3*( x+w*(h-1-y) )+2] = b;

	}
	}

	/* Finished with this */
	free(hdr);

	/* Create the pixbuf from the 8-bit display data */
	return gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, FALSE, 8,
					w, h, w*3, render_free_data, NULL);
}

GdkPixbuf *render_get_colour_scale(size_t w, size_t h, int monochrome)
{
	guchar *data;
	size_t x, y;
	int max;

	data = malloc(3*w*h);
	if ( data == NULL ) return NULL;

	max = h;

	for ( y=0; y<h; y++ ) {

		guchar r, g, b;
		int val;

		val = y;
		if ( !monochrome ) {
			RENDER_RGB
		} else {
			RENDER_MONO
		}

		data[3*( 0+w*(h-1-y) )+0] = 0;
		data[3*( 0+w*(h-1-y) )+1] = 0;
		data[3*( 0+w*(h-1-y) )+2] = 0;
		for ( x=1; x<w; x++ ) {
			data[3*( x+w*(h-1-y) )+0] = r;
			data[3*( x+w*(h-1-y) )+1] = g;
			data[3*( x+w*(h-1-y) )+2] = b;
		}

	}

	return gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, FALSE, 8,
					w, h, w*3, render_free_data, NULL);
}