forked from milk-org/ImageStreamIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImCreate_img.c
139 lines (110 loc) · 3.67 KB
/
ImCreate_img.c
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
/*
* Example code to write image in shared memory
*
* compile with:
* gcc ImCreate_img.c ImageStreamIO.c -o ImCreate_img -lm -lpthread
* gcc ImCreate_img.c ImageStreamIO.c -DHAVE_CUDA -o ImCreate_test -lm -lpthread -I/opt/cuda/include -L/opt/cuda/lib64 -lcudart
*
* Required files in compilation directory :
* ImCreate_img.c : source code (this file)
* ImageStreamIO.c : ImageStreamIO source code
* ImageStreamIO.h : ImageCreate function prototypes
* ImageStruct.h : Image structure definition
*
* EXECUTION:
* ./ImCreate_img
* (no argument)
*
* Creates a circular buffer imtest00 in shared memory
* Updates the image every ~ 10ms, forever...
* A square is rotating around the center of the image
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ImageStruct.h"
#include "ImageStreamIO.h"
int main()
{
IMAGE imarray; // pointer to array of images
long naxis; // number of axis
uint8_t atype; // data type
uint32_t *imsize; // image size
int shared; // 1 if image in shared memory
int NBkw; // number of keywords supported
// image will be 2D
naxis = 2;
// image size will be 512 x 512
imsize = (uint32_t *) malloc(sizeof(uint32_t)*naxis);
imsize[0] = 512;
imsize[1] = 512;
// image will be float type
// see file ImageStruct.h for list of supported types
atype = _DATATYPE_FLOAT;
// image will be in shared memory
shared = 1;
// allocate space for 10 keywords
NBkw = 10;
// create an image in shared memory
ImageStreamIO_createIm_gpu(&imarray, "imtest00", naxis, imsize, atype, -1, shared, IMAGE_NB_SEMAPHORE, NBkw, MATH_DATA);
free(imsize);
strcpy(imarray.kw[0].name, "keyword_long");
imarray.kw[0].type = 'L';
imarray.kw[0].value.numl = 42;
strcpy(imarray.kw[1].name, "keyword_float");
imarray.kw[1].type = 'D';
imarray.kw[1].value.numf = 3.141592;
strcpy(imarray.kw[2].name, "keyword_string");
imarray.kw[2].type = 'S';
strcpy(imarray.kw[2].value.valstr, "Hello!");
float angle;
float r;
float r1;
long ii, jj;
float x, y, x0, y0, xc, yc;
// float squarerad=20;
long dtus = 100000; // update every 1ms
float dangle = 0.02;
int s;
int semval;
float *current_image;
// writes a square in image
// square location rotates around center
angle = 0.0;
r = 100.0;
x0 = 0.5*imarray.md->size[0];
y0 = 0.5*imarray.md->size[1];
while (1)
{
// disk location
xc = x0 + r*cos(angle);
yc = y0 + r*sin(angle);
imarray.md->write = 1; // set this flag to 1 when writing data
for(ii=0; ii<imarray.md->size[0]; ii++)
for(jj=0; jj<imarray.md->size[1]; jj++)
{
x = 1.0*ii;
y = 1.0*jj;
float dx = x-xc;
float dy = y-yc;
imarray.array.F[ii*imarray.md->size[1]+jj] = cos(0.03*dx)*cos(0.03*dy)*exp(-1.0e-4*(dx*dx+dy*dy));
//if( (x-xc<squarerad) && (x-xc>-squarerad) && (y-yc<squarerad) && (y-yc>-squarerad))
// imarray.array.F[jj*imarray.md->size[0]+ii] = 1.0;
//else
// imarray.array.F[jj*imarray.md->size[0]+ii] = 0.0;
}
imarray.md->cnt1 = 0;
imarray.md->cnt0++;
// POST ALL SEMAPHORES
ImageStreamIO_sempost(&imarray, -1);
imarray.md->write = 0; // Done writing data
usleep(dtus);
angle += dangle;
if(angle > 2.0*3.141592)
angle -= 2.0*3.141592;
//printf("Wrote square at position xc = %16f yc = %16f\n", xc, yc);
//fflush(stdout);
}
return 0;
}