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
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* detector.c
*
* Detector properties
*
* (c) 2007-2009 Thomas White <thomas.white@desy.de>
*
* pattern_sim - Simulate diffraction patterns from small crystals
*
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "image.h"
#include "utils.h"
#include "diffraction.h"
/* Pulse energy density in J/m^2 */
#define PULSE_ENERGY_DENSITY (30.0e7)
/* Detector's quantum efficiency */
#define DQE (0.9)
/* Detector's saturation value */
#define SATURATION (60000)
/* Bleed excess intensity into neighbouring pixels */
static void bloom_values(double *tmp, int x, int y,
int width, int height, double val)
{
double overflow;
overflow = val - SATURATION;
/* Intensity which bleeds off the edge of the detector is lost */
if ( x > 0 ) {
tmp[x-1 + width*y] += overflow / 6.0;
if ( y > 0 ) {
tmp[x-1 + width*(y-1)] += overflow / 12.0;
}
if ( y < height-1 ) {
tmp[x-1 + width*(y+1)] += overflow / 12.0;
}
}
if ( x < width-1 ) {
tmp[x+1 + width*y] += overflow / 6.0;
if ( y > 0 ) {
tmp[x+1 + width*(y-1)] += overflow / 12.0;
}
if ( y < height-1 ) {
tmp[x+1 + width*(y+1)] += overflow / 12.0;
}
}
if ( y > 0 ) {
tmp[x + width*(y-1)] += overflow / 6.0;
}
if ( y < height-1 ) {
tmp[x + width*(y+1)] += overflow / 6.0;
}
}
static uint16_t *bloom(double *hdr_in, int width, int height)
{
int x, y;
uint16_t *data;
double *tmp;
double *hdr;
int did_something;
data = malloc(width * height * sizeof(uint16_t));
tmp = malloc(width * height * sizeof(double));
hdr = malloc(width * height * sizeof(double));
memcpy(hdr, hdr_in, width*height*sizeof(double));
/* Apply DQE (once only) */
for ( x=0; x<width; x++ ) {
for ( y=0; y<height; y++ ) {
hdr[x + width*y] *= DQE;
}
}
do {
memset(tmp, 0, width*height*sizeof(double));
did_something = 0;
for ( x=0; x<width; x++ ) {
for ( y=0; y<height; y++ ) {
double hdval;
hdval = hdr[x + width*y];
/* Pixel not saturated? */
if ( hdval <= SATURATION ) {
tmp[x + width*y] += hdval;
continue;
}
bloom_values(tmp, x, y, width, height, hdval);
tmp[x + width*y] += SATURATION;
did_something = 1;
}
}
/* Prepare new input if we're going round for another pass */
if ( did_something ) {
memcpy(hdr, tmp, width*height*sizeof(double));
}
} while ( did_something );
/* Turn into integer array of counts */
for ( x=0; x<width; x++ ) {
for ( y=0; y<height; y++ ) {
data[x + width*y] = (uint16_t)tmp[x + width*y];
}
}
free(tmp);
free(hdr);
return data;
}
void record_image(struct image *image)
{
int x, y;
double ph_per_e;
double sa_per_pixel;
const int do_bloom = 0;
/* How many photons are scattered per electron? */
ph_per_e = PULSE_ENERGY_DENSITY * pow(THOMSON_LENGTH, 2.0)
/ image->xray_energy;
printf("%e photons are scattered per electron\n", ph_per_e);
/* Solid angle subtended by a single pixel at the centre of the CCD */
sa_per_pixel = pow(1.0/image->resolution, 2.0) / image->camera_len;
printf("Solid angle of one pixel (at centre of CCD) = %e sr\n",
sa_per_pixel);
image->hdr = malloc(image->width * image->height * sizeof(double));
for ( x=0; x<image->width; x++ ) {
for ( y=0; y<image->height; y++ ) {
double counts, intensity, sa;
double complex val;
val = image->sfacs[x + image->width*y];
intensity = (double)(val * conj(val));
/* Add intensity contribution from water */
intensity += water_intensity(image->qvecs[x + image->width*y],
image->xray_energy);
/* What solid angle is subtended by this pixel? */
sa = sa_per_pixel * cos(image->twotheta[x + image->width*y]);
counts = intensity * ph_per_e * sa;
image->hdr[x + image->width*y] = counts;
}
progress_bar(x, image->width-1);
}
if ( do_bloom ) {
image->data = bloom(image->hdr, image->width, image->height);
} else {
image->data = malloc(image->width * image->height
* sizeof(uint16_t));
for ( x=0; x<image->width; x++ ) {
for ( y=0; y<image->height; y++ ) {
double val;
val = image->hdr[x + image->width*y];
if ( val > SATURATION ) val = SATURATION;
image->data[x + image->width*y] = (uint16_t)val;
}
}
}
}
|