/camera/imageviewer

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/imageviewer
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define fg_apc_data TCameraLink

#include <fgrab_struct.h>
#include <fgrab_prototyp.h>
#include <fgrab_define.h>

#include "ds_log.h"
#include "cl.h"

#define DLLNAME 		"libFullAreaGray8.so"
#define CL_BOARD 		0
#define CL_PORT			PORT_A			// Full speed, PORT_AB ?
#define CL_SAMPLES_PER_PIXEL 	1
#define CL_BYTES_PER_SAMPLE 	1
#define CL_BUFFERS		16
#define CL_SECURITY_BUFFERS	2			// We will skip the buffer if less than that amount of free buffers remained before overwrite
#define CL_TIMEOUT		2			// In seconds


static int cl_new_frame_callback(frameindex_t frame, CameraLink *ctx) {
    void *data = Fg_getImagePtr(ctx->fg, frame, CL_PORT);
    ctx->cb(ctx->cbctx, frame,  data);
    return 0;
}



CameraLink *cl_create() {
    CameraLink *ctx = malloc(sizeof(CameraLink));
    if (ctx) {
	memset(ctx, 0, sizeof(CameraLink));
    }
}

int cl_init(CameraLink *ctx) {
    return 0;
}

void cl_free(CameraLink *ctx) {
    g_assert(ctx);
    
    cl_close(ctx);
}

void cl_destroy(CameraLink *ctx) {
    cl_free(ctx);    
    free(ctx);
}

int cl_set_max_resolution(CameraLink *ctx, int width, int height) {
    g_assert(ctx);
    g_assert(!ctx->running);

    ctx->max_width = width;
    ctx->max_height = height;
}

int cl_set_resolution(CameraLink *ctx, int width, int height) {
    int err;
    
    g_assert(ctx);
    g_assert(ctx->running);
    g_assert(width <= ctx->max_width);
    g_assert(height <= ctx->max_height);    

    err = Fg_setParameter(ctx->fg, FG_WIDTH, &width, CL_PORT);
    ds_exception_reterr(err == 0, -1, "Fg_setParameter(width = %i) failed: %s", width, Fg_getLastErrorDescription(ctx->fg));

    err = Fg_setParameter(ctx->fg, FG_HEIGHT, &height, CL_PORT);
    ds_exception_reterr(err == 0, -1, "Fg_setParameter(height = %i) failed: %s", height, Fg_getLastErrorDescription(ctx->fg));

    ctx->width = width;
    ctx->height = height;

    return 0;
}

int cl_open(CameraLink *ctx, int width, int height, int flags) {
    int err;
    size_t buf_size;
    void *buf;
    
    double frame_rate;
    
    g_assert(ctx);
    
    if (!ctx->max_width) ctx->max_width = width;
    if (!ctx->max_height) ctx->max_height = height;
    
    if (ctx->fg) cl_close(ctx);
    
    ctx->fg = Fg_Init(DLLNAME, CL_BOARD);
    ds_exception_reterr(ctx->fg != NULL, -1, "Error in Fg_Init: %s", Fg_getLastErrorDescription(NULL));
    
    buf_size = ctx->max_width * ctx->max_height * CL_SAMPLES_PER_PIXEL * CL_BYTES_PER_SAMPLE * CL_BUFFERS;
    
    buf = Fg_AllocMem(ctx->fg, buf_size, CL_BUFFERS, CL_PORT);
    ds_exception_reterr(buf != NULL, -1, "Error in Fg_AllocMem (%i bytes): %s", buf_size, Fg_getLastErrorDescription(NULL));

    err = Fg_setParameter(ctx->fg, FG_WIDTH, &width, CL_PORT);
    ds_exception_reterr(err == 0, -1, "Fg_setParameter(width = %i) failed: %s", width, Fg_getLastErrorDescription(ctx->fg));

    err = Fg_setParameter(ctx->fg, FG_HEIGHT, &height, CL_PORT);
    ds_exception_reterr(err == 0, -1, "Fg_setParameter(height = %i) failed: %s", height, Fg_getLastErrorDescription(ctx->fg));
    
    if (ctx->cb) {
        struct FgApcControl ctrl;

	ctrl.version = 0;
	ctrl.data = (struct fg_apc_data*)ctx;
	ctrl.func = &cl_new_frame_callback;
	ctrl.flags = FG_APC_DEFAULTS;
	ctrl.timeout = 1;
    
	err = Fg_registerApcHandler(ctx->fg, CL_PORT, &ctrl, FG_APC_CONTROL_BASIC); 
	ds_exception_reterr(err == 0, -1, "Fg_registerApcHandler() failed: %s", Fg_getLastErrorDescription(ctx->fg));
    }

    if (flags&CL_FLAGS_SECURE) {
	err = Fg_AcquireEx(ctx->fg, CL_PORT, GRAB_INFINITE, ACQ_BLOCK, buf);
    } else {
	err = Fg_Acquire(ctx->fg, CL_PORT, GRAB_INFINITE);
    }
    
    ds_exception_reterr(err == 0, -1, "Fg_Acquire() failed: %s", Fg_getLastErrorDescription(ctx->fg));

    ctx->running = 1;
    ctx->width = width;
    ctx->height = height;
    
    return 0;
}

int cl_close(CameraLink *ctx) {
    ctx->running = 0;
    
    if (ctx->fg) {
	Fg_stopAcquire(ctx->fg, CL_PORT);
	if (ctx->cb) Fg_registerApcHandler(ctx->fg, CL_PORT, NULL, FG_APC_CONTROL_BASIC);
	Fg_FreeMem(ctx->fg, CL_PORT);
	Fg_FreeGrabber(ctx->fg);
	ctx->fg = NULL;
    }
    
    return 0;
}


int cl_register_frame_callback(CameraLink *ctx, TCameraFrameCallback callback, void *data) {
    g_assert(ctx);
    gl_assert(!ctx->running);
    
    ctx->cb = callback; 
    ctx->cbctx = data;
    
    return 0;
}


int cl_run_frames_continuous(CameraLink *ctx, int *run_flag, TCameraFrameCallback callback, void *cbctx) {
    frameindex_t cur_frame = 0, last_frame = 1, new_frame;
    void *data;

    g_assert(ctx);
    g_assert(run_flag);
    g_assert(callback);
    g_assert(ctx->running);

    while (*run_flag) {
        new_frame = Fg_getLastPicNumberBlocking(ctx->fg, cur_frame + 1, CL_PORT, CL_TIMEOUT);
	if (new_frame < 0) {
	    continue;
/*	    ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
	    return -1;*/
	} else {
	    last_frame = new_frame;
	}
	
	if ((cur_frame + CL_BUFFERS - CL_SECURITY_BUFFERS) < last_frame) {
	    cur_frame = last_frame - CL_BUFFERS + CL_SECURITY_BUFFERS;
	}
	
	data = Fg_getImagePtr(ctx->fg, ++cur_frame, CL_PORT);
	if (data) callback(cbctx, cur_frame, data);
    }

    return 0;
}




//int cl_get_next_frame






/*
int cl_advance_next_frame(CameraLink *ctx) {
    frameindex_t res;
    
    ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");

    if (ctx->current_frame == ctx->last_frame) {
	res = Fg_getLastPicNumberBlocking(ctx->fg, ctx->last_frame + 1, CL_PORT, CL_TIMEOUT);
	if (!res) {
	    ds_error("Fg_getLastPicNumberBlocking() failed: %s", Fg_getLastErrorDescription(ctx->fg));
	    return -1;
	}
	
	ctx->last_frame = res;
    }
    
    return 0;
}

int cl_get_frame(CameraLink *ctx) {
    ds_exception_reterr(ctx->running, -1, "CameraLink context is not initialized");

    Fg_getImagePtr(ctx->fg, ctx->current_frame++, CL_PORT);
}
*/