File: utils.c

package info (click to toggle)
libdc1394-22 2.2.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,924 kB
  • sloc: ansic: 16,415; sh: 10,205; makefile: 273
file content (398 lines) | stat: -rw-r--r-- 11,974 bytes parent folder | download | duplicates (9)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * 1394-Based Digital Camera Control Library
 *
 * Utilities
 *
 * Written by Damien Douxchamps <ddouxchamps@users.sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "internal.h"

const char *dc1394_feature_desc[DC1394_FEATURE_NUM] = {
    "Brightness",
    "Exposure",
    "Sharpness",
    "White Balance",
    "Hue",
    "Saturation",
    "Gamma",
    "Shutter",
    "Gain",
    "Iris",
    "Focus",
    "Temperature",
    "Trigger",
    "Trigger Delay",
    "White Shading",
    "Frame Rate",
    "Zoom",
    "Pan",
    "Tilt",
    "Optical Filter",
    "Capture Size",
    "Capture Quality"
};

const char *dc1394_error_strings[DC1394_ERROR_NUM] = {
    "Success",
    "Generic failure",
    "This node is not a camera",
    "Function not supported by this camera",
    "Camera not initialized",
    "Memory allocation failure",
    "Tagged register not found",
    "Could not allocate an ISO channel",
    "Could not allocate bandwidth",
    "IOCTL failure",
    "Capture is not set",
    "Capture is running",
    "RAW1394 failure",
    "Format_7 Error_flag_1 is set",
    "Format_7 Error_flag_2 is set",
    "Invalid argument value",
    "Requested value is out of range",
    "Invalid feature",
    "Invalid video format",
    "Invalid video mode",
    "Invalid framerate",
    "Invalid trigger mode",
    "Invalid trigger source",
    "Invalid ISO speed",
    "Invalid IIDC version",
    "Invalid Format_7 color coding",
    "Invalid Format_7 elementary Bayer tile",
    "Invalid capture mode",
    "Invalid error code",
    "Invalid Bayer method",
    "Invalid video1394 device",
    "Invalid operation mode",
    "Invalid trigger polarity",
    "Invalid feature mode",
    "Invalid log type",
    "Invalid byte order",
    "Invalid stereo method",
    "Basler error: no more SFF chunks",
    "Basler error: corrupted SFF chunk",
    "Basler error: unknown SFF chunk"
};

dc1394error_t
dc1394_get_image_size_from_video_mode(dc1394camera_t *camera, dc1394video_mode_t video_mode, uint32_t *w, uint32_t *h)
{
    dc1394error_t err;
    uint32_t sx, sy;

    switch(video_mode) {
    case DC1394_VIDEO_MODE_160x120_YUV444:
        *w = 160;*h=120;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_320x240_YUV422:
        *w = 320;*h=240;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_640x480_YUV411:
    case DC1394_VIDEO_MODE_640x480_YUV422:
    case DC1394_VIDEO_MODE_640x480_RGB8:
    case DC1394_VIDEO_MODE_640x480_MONO8:
    case DC1394_VIDEO_MODE_640x480_MONO16:
        *w =640;*h=480;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_800x600_YUV422:
    case DC1394_VIDEO_MODE_800x600_RGB8:
    case DC1394_VIDEO_MODE_800x600_MONO8:
    case DC1394_VIDEO_MODE_800x600_MONO16:
        *w=800;*h=600;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_1024x768_YUV422:
    case DC1394_VIDEO_MODE_1024x768_RGB8:
    case DC1394_VIDEO_MODE_1024x768_MONO8:
    case DC1394_VIDEO_MODE_1024x768_MONO16:
        *w=1024;*h=768;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_1280x960_YUV422:
    case DC1394_VIDEO_MODE_1280x960_RGB8:
    case DC1394_VIDEO_MODE_1280x960_MONO8:
    case DC1394_VIDEO_MODE_1280x960_MONO16:
        *w=1280;*h=960;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_1600x1200_YUV422:
    case DC1394_VIDEO_MODE_1600x1200_RGB8:
    case DC1394_VIDEO_MODE_1600x1200_MONO8:
    case DC1394_VIDEO_MODE_1600x1200_MONO16:
        *w=1600;*h=1200;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_FORMAT7_0:
    case DC1394_VIDEO_MODE_FORMAT7_1:
    case DC1394_VIDEO_MODE_FORMAT7_2:
    case DC1394_VIDEO_MODE_FORMAT7_3:
    case DC1394_VIDEO_MODE_FORMAT7_4:
    case DC1394_VIDEO_MODE_FORMAT7_5:
    case DC1394_VIDEO_MODE_FORMAT7_6:
    case DC1394_VIDEO_MODE_FORMAT7_7:
        // get the current color mode from the camera
        err=dc1394_format7_get_image_size(camera, video_mode, &sx, &sy);
        if (err!=DC1394_SUCCESS) {
            return err;
        }
        else {
            *w=sx;
            *h=sy;
        }
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_EXIF:
        return DC1394_FAILURE;
    }

    return DC1394_FAILURE;
}


dc1394error_t
dc1394_framerate_as_float(dc1394framerate_t framerate_enum, float *framerate)
{
    switch(framerate_enum)  {
    case DC1394_FRAMERATE_1_875:
        *framerate=1.875;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_3_75:
        *framerate=3.75;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_7_5:
        *framerate=7.5;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_15:
        *framerate=15.0;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_30:
        *framerate=30.0;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_60:
        *framerate=60.0;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_120:
        *framerate=120.0;
        return DC1394_SUCCESS;
    case DC1394_FRAMERATE_240:
        *framerate=240.0;
        return DC1394_SUCCESS;
    }
    return DC1394_INVALID_FRAMERATE;
}

dc1394error_t
dc1394_is_color(dc1394color_coding_t color_coding, dc1394bool_t *is_color)
{
    switch(color_coding)  {
    case DC1394_COLOR_CODING_MONO8:
    case DC1394_COLOR_CODING_MONO16:
    case DC1394_COLOR_CODING_MONO16S:
    case DC1394_COLOR_CODING_RAW8:
    case DC1394_COLOR_CODING_RAW16:
        *is_color=DC1394_FALSE;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_YUV411:
    case DC1394_COLOR_CODING_YUV422:
    case DC1394_COLOR_CODING_YUV444:
    case DC1394_COLOR_CODING_RGB8:
    case DC1394_COLOR_CODING_RGB16:
    case DC1394_COLOR_CODING_RGB16S:
        *is_color=DC1394_TRUE;
        return DC1394_SUCCESS;
    }
    return DC1394_INVALID_COLOR_CODING;
}

dc1394error_t
dc1394_get_color_coding_data_depth(dc1394color_coding_t color_coding, uint32_t * bits)
{
    switch(color_coding) {
    case DC1394_COLOR_CODING_MONO8:
    case DC1394_COLOR_CODING_YUV411:
    case DC1394_COLOR_CODING_YUV422:
    case DC1394_COLOR_CODING_YUV444:
    case DC1394_COLOR_CODING_RGB8:
    case DC1394_COLOR_CODING_RAW8:
        *bits = 8;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_MONO16:
    case DC1394_COLOR_CODING_RGB16:
    case DC1394_COLOR_CODING_MONO16S:
    case DC1394_COLOR_CODING_RGB16S:
    case DC1394_COLOR_CODING_RAW16:
        // shoudn't we return the real bit depth (e.g. 12) instead of systematically 16?
        *bits = 16;
        return DC1394_SUCCESS;
    }
    return DC1394_INVALID_COLOR_CODING;
}

dc1394error_t
dc1394_get_color_coding_bit_size(dc1394color_coding_t color_coding, uint32_t* bits)
{
    switch(color_coding) {
    case DC1394_COLOR_CODING_MONO8:
    case DC1394_COLOR_CODING_RAW8:
        *bits=8;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_YUV411:
        *bits=12;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_MONO16:
    case DC1394_COLOR_CODING_RAW16:
    case DC1394_COLOR_CODING_MONO16S:
    case DC1394_COLOR_CODING_YUV422:
        *bits=16;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_YUV444:
    case DC1394_COLOR_CODING_RGB8:
        *bits=24;
        return DC1394_SUCCESS;
    case DC1394_COLOR_CODING_RGB16:
    case DC1394_COLOR_CODING_RGB16S:
        *bits=48;
        return DC1394_SUCCESS;
    }
    return DC1394_INVALID_COLOR_CODING;
}

dc1394error_t
dc1394_get_color_coding_from_video_mode(dc1394camera_t *camera, dc1394video_mode_t video_mode, dc1394color_coding_t *color_coding)
{
    dc1394error_t err;
    dc1394color_coding_t id;

    switch(video_mode) {
    case DC1394_VIDEO_MODE_160x120_YUV444:
        *color_coding=DC1394_COLOR_CODING_YUV444;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_320x240_YUV422:
    case DC1394_VIDEO_MODE_640x480_YUV422:
    case DC1394_VIDEO_MODE_800x600_YUV422:
    case DC1394_VIDEO_MODE_1024x768_YUV422:
    case DC1394_VIDEO_MODE_1280x960_YUV422:
    case DC1394_VIDEO_MODE_1600x1200_YUV422:
        *color_coding=DC1394_COLOR_CODING_YUV422;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_640x480_YUV411:
        *color_coding=DC1394_COLOR_CODING_YUV411;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_640x480_RGB8:
    case DC1394_VIDEO_MODE_800x600_RGB8:
    case DC1394_VIDEO_MODE_1024x768_RGB8:
    case DC1394_VIDEO_MODE_1280x960_RGB8:
    case DC1394_VIDEO_MODE_1600x1200_RGB8:
        *color_coding=DC1394_COLOR_CODING_RGB8;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_640x480_MONO8:
    case DC1394_VIDEO_MODE_800x600_MONO8:
    case DC1394_VIDEO_MODE_1024x768_MONO8:
    case DC1394_VIDEO_MODE_1280x960_MONO8:
    case DC1394_VIDEO_MODE_1600x1200_MONO8:
        *color_coding=DC1394_COLOR_CODING_MONO8;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_800x600_MONO16:
    case DC1394_VIDEO_MODE_640x480_MONO16:
    case DC1394_VIDEO_MODE_1024x768_MONO16:
    case DC1394_VIDEO_MODE_1280x960_MONO16:
    case DC1394_VIDEO_MODE_1600x1200_MONO16:
        *color_coding=DC1394_COLOR_CODING_MONO16;
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_FORMAT7_0:
    case DC1394_VIDEO_MODE_FORMAT7_1:
    case DC1394_VIDEO_MODE_FORMAT7_2:
    case DC1394_VIDEO_MODE_FORMAT7_3:
    case DC1394_VIDEO_MODE_FORMAT7_4:
    case DC1394_VIDEO_MODE_FORMAT7_5:
    case DC1394_VIDEO_MODE_FORMAT7_6:
    case DC1394_VIDEO_MODE_FORMAT7_7:
        // get the current color mode from the camera
        err=dc1394_format7_get_color_coding(camera, video_mode, &id);
        if (err!=DC1394_SUCCESS) {
            return err;
        }
        else {
            *color_coding=id;
        }
        return DC1394_SUCCESS;
    case DC1394_VIDEO_MODE_EXIF:
        return DC1394_FAILURE;
    }

    return DC1394_FAILURE;
}

dc1394bool_t
dc1394_is_video_mode_scalable(dc1394video_mode_t video_mode)
{
    return ((video_mode>=DC1394_VIDEO_MODE_FORMAT7_MIN)&&(video_mode<=DC1394_VIDEO_MODE_FORMAT7_MAX));
}

dc1394bool_t
dc1394_is_video_mode_still_image(dc1394video_mode_t video_mode)
{
    return ((video_mode>=DC1394_VIDEO_MODE_FORMAT6_MIN)&&(video_mode<=DC1394_VIDEO_MODE_FORMAT6_MAX));
}

dc1394bool_t
dc1394_is_same_camera(dc1394camera_id_t id1, dc1394camera_id_t id2)
{
    return ((id1.guid==id2.guid)&&(id1.unit==id2.unit));
}

const char *
dc1394_feature_get_string(dc1394feature_t feature)
{
    if ((feature>DC1394_FEATURE_MAX)||(feature<DC1394_FEATURE_MIN))
        return NULL;

    return dc1394_feature_desc[feature-DC1394_FEATURE_MIN];
}

const char *
dc1394_error_get_string(dc1394error_t error)
{

    if ((error>DC1394_ERROR_MAX)||(error<DC1394_ERROR_MIN))
        return NULL;

    return dc1394_error_strings[-error-DC1394_ERROR_MAX];
}


/*
 * Checksum algorithms
 * Copyright (C) 2006 Mikael Olenfalk, Tobii Technology AB, Stockholm Sweden
 *
 * Written by Mikael Olenfalk <mikael _DOT_ olenfalk _AT_ tobii _DOT_ com>
 * Version : 16/02/2005
 */

uint16_t
dc1394_checksum_crc16 (const uint8_t* buffer, uint32_t buffer_size)
{
    uint32_t i, j, c, bit;
    uint32_t crc = 0;
    for (i = 0; i < buffer_size; i++) {
        c = (uint32_t)*buffer++;
        for (j = 0x80; j; j >>= 1) {
            bit = crc & 0x8000;
            crc <<= 1;
            if (c & j) bit ^= 0x8000;
            if (bit) crc ^= 0x1021;
        }
    }
    return (uint16_t)(crc & 0xffff);
}