summaryrefslogtreecommitdiff
path: root/src/imaging/CMUCameraUtils.cpp
blob: 3e10570ad3758c39c6d1bdf388e88ef8cb3d9082 (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
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  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 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
//
//  Current versions can be found at www.libavg.de
//
//  Original author of this file is Nick Hebner (hebnern@gmail.com).
//

#include "CMUCameraUtils.h"

#include "../base/Logger.h"
#include "../base/Exception.h"
#include "../base/StringHelper.h"

namespace avg {

using namespace std;

#define FORMAT_0 0
// Format 0 Modes
#define MODE_160_120_YUV444  0
#define MODE_320x240_YUV422  1
#define MODE_640x480_YUV411  2
#define MODE_640x480_YUV422  3
#define MODE_640x480_RGB     4
#define MODE_640x480_MONO    5
#define MODE_640x480_MONO16  6

#define FORMAT_1 1
// Format 1 Modes
#define MODE_800x600_YUV422  0
#define MODE_800x600_RGB     1
#define MODE_800x600_MONO    2
#define MODE_1024x768_YUV422 3
#define MODE_1024x768_RGB    4
#define MODE_1024x768_MONO   5
#define MODE_800x600_MONO16  6
#define MODE_1024x768_MONO16 7

#define FORMAT_2 2
#define MODE_1280x960_YUV422  0
#define MODE_1280x960_RGB     1
#define MODE_1280x960_MONO    2
#define MODE_1600x1200_YUV422 3
#define MODE_1600x1200_RGB    4
#define MODE_1600x1200_MONO   5
#define MODE_1280x960_MONO16  6
#define MODE_1600x1200_MONO16 7

// Framerates
#define FRAMERATE_1_875 0
#define FRAMERATE_3_75  1
#define FRAMERATE_7_5   2
#define FRAMERATE_15    3
#define FRAMERATE_30    4
#define FRAMERATE_60    5
#define FRAMERATE_120   6
#define FRAMERATE_240   7

void getVideoFormatAndMode(IntPoint& Size, PixelFormat pf, 
        unsigned long* pVideoFormat, unsigned long* pVideoMode) 
{
    *pVideoMode = -1;
    *pVideoFormat = -1;
    if (Size.x == 320 && Size.y == 240) {
        *pVideoFormat = FORMAT_0;
        if (pf == YCbCr422) {
            *pVideoMode = MODE_320x240_YUV422;
        }
    } else if (Size.x == 640 && Size.y == 480) {
        *pVideoFormat = FORMAT_0;
        if (pf == I8 || pf == BAYER8) {
            *pVideoMode = MODE_640x480_MONO;
        } else if (pf == I16) {
            *pVideoMode = MODE_640x480_MONO16;
        } else if (pf == YCbCr411) {
            *pVideoMode = MODE_640x480_YUV411;
        } else if (pf == YCbCr422) {
            *pVideoMode = MODE_640x480_YUV422;
        } else if (pf == R8G8B8 || pf == B8G8R8) {
            *pVideoMode = MODE_640x480_RGB;
        }
    } else if (Size.x == 800 && Size.y == 600) {
        *pVideoFormat = FORMAT_1;
        if (pf == I8 || pf == BAYER8) {
            *pVideoMode = MODE_800x600_MONO;
        } else if (pf == I16) {
            *pVideoMode = MODE_800x600_MONO16;
        } else if (pf == YCbCr422) {
            *pVideoMode = MODE_800x600_YUV422;
        } else if (pf == R8G8B8 || pf == B8G8R8) {
            *pVideoMode = MODE_800x600_RGB;
        }
    } else if (Size.x == 1024 && Size.y == 768) {
        *pVideoFormat = FORMAT_1;
        if (pf == I8 || pf == BAYER8) {
            *pVideoMode = MODE_1024x768_MONO;
        } else if (pf == I16) {
            *pVideoMode = MODE_1024x768_MONO16;
        } else if (pf == YCbCr422) {
            *pVideoMode = MODE_1024x768_YUV422;
        } else if (pf == R8G8B8 || pf == B8G8R8) {
            *pVideoMode = MODE_1024x768_RGB;
        }
    } else if (Size.x == 1280 && Size.y == 960) {
        *pVideoFormat = FORMAT_2;
        if (pf == I8 || pf == BAYER8) {
            *pVideoMode = MODE_1280x960_MONO;
        } else if (pf == I16) {
            *pVideoMode = MODE_1280x960_MONO16;
        } else if (pf == YCbCr422) {
            *pVideoMode = MODE_1280x960_YUV422;
        } else if (pf == R8G8B8 || pf == B8G8R8) {
            *pVideoMode = MODE_1280x960_RGB;
        }
    } else if (Size.x == 1600 && Size.y == 1200) {
        *pVideoFormat = FORMAT_2;
        if (pf == I8 || pf == BAYER8) {
            *pVideoMode = MODE_1600x1200_MONO;
        } else if (pf == I16) {
            *pVideoMode = MODE_1600x1200_MONO16;
        } else if (pf == YCbCr422) {
            *pVideoMode = MODE_1600x1200_YUV422;
        } else if (pf == R8G8B8 || pf == B8G8R8) {
            *pVideoMode = MODE_1600x1200_RGB;
        }
    }
    if (*pVideoMode == -1 || *pVideoFormat == -1) { 
        throw Exception(AVG_ERR_INVALID_ARGS,
                "Unsupported or illegal camera mode ("+toString(Size.x)+", "+toString(Size.y)+
                "), "+getPixelFormatString(pf)+".");
    }
}

unsigned long getFrameRateConst(float frameRate)
{
    if (frameRate == 1.875f) {
        return FRAMERATE_1_875;
    } else if (frameRate == 3.75f) {
        return FRAMERATE_3_75;
    } else if (frameRate == 7.5f) {
        return FRAMERATE_7_5;
    } else if (frameRate == 15) {
        return FRAMERATE_15;
    } else if (frameRate == 30) {
        return FRAMERATE_30;
    } else if (frameRate == 60) {
        return FRAMERATE_60;
    } else if (frameRate == 120) {
        return FRAMERATE_120;
    } else if (frameRate == 240) {
        return FRAMERATE_240;
    } else {
        throw Exception(AVG_ERR_INVALID_ARGS,
                "Unsupported or illegal value ("+toString(frameRate)+
                ") for camera framerate.");
    }
}

float getFrameRateFloat(unsigned long frameRate)
{
    if(frameRate == FRAMERATE_1_875){
        return 1.875;
    } else if (frameRate == FRAMERATE_3_75){
        return 3.75;
    } else if (frameRate == FRAMERATE_7_5){
        return 7.5;
    } else if (frameRate == FRAMERATE_15){
        return 15;
    } else if (frameRate == FRAMERATE_30){
        return 30;
    } else if (frameRate == FRAMERATE_60){
        return 60;
    } else if (frameRate == FRAMERATE_120){
        return 120;
    } else if (frameRate == FRAMERATE_240){
        return 240;
    } else {
        throw Exception(AVG_ERR_INVALID_ARGS,
                "Unsupported or illegal value ("+toString(frameRate)+
                ") as camera framerate.");
    }
}

CAMERA_FEATURE getFeatureID(CameraFeature Feature)
{
    switch(Feature) {
        case CAM_FEATURE_BRIGHTNESS:
            return FEATURE_BRIGHTNESS;
        case CAM_FEATURE_EXPOSURE:
            return FEATURE_AUTO_EXPOSURE;
        case CAM_FEATURE_SHARPNESS:
            return FEATURE_SHARPNESS;
        case CAM_FEATURE_WHITE_BALANCE:
            return FEATURE_WHITE_BALANCE;
        case CAM_FEATURE_HUE:
            return FEATURE_HUE;
        case CAM_FEATURE_SATURATION:
            return FEATURE_SATURATION;
        case CAM_FEATURE_GAMMA:
            return FEATURE_GAMMA;
        case CAM_FEATURE_SHUTTER:
            return FEATURE_SHUTTER;
        case CAM_FEATURE_GAIN:
            return FEATURE_GAIN;
        case CAM_FEATURE_IRIS:
            return FEATURE_IRIS;
        case CAM_FEATURE_FOCUS:
            return FEATURE_FOCUS;
        case CAM_FEATURE_TEMPERATURE:
            return FEATURE_TEMPERATURE;
        case CAM_FEATURE_TRIGGER:
            return FEATURE_TRIGGER_MODE;
        case CAM_FEATURE_ZOOM:
            return FEATURE_ZOOM;
        case CAM_FEATURE_PAN:
            return FEATURE_PAN;
        case CAM_FEATURE_TILT:
            return FEATURE_TILT;
        case CAM_FEATURE_OPTICAL_FILTER:
            return FEATURE_OPTICAL_FILTER;
        case CAM_FEATURE_CAPTURE_SIZE:
            return FEATURE_CAPTURE_SIZE;
        case CAM_FEATURE_CAPTURE_QUALITY:
            return FEATURE_CAPTURE_QUALITY;
        default:
            return FEATURE_INVALID_FEATURE;
    }
}

void getImageSizeAndPF(unsigned long videoFormat, unsigned long videoMode,
    IntPoint &pSize, PixelFormat &pPixelFormat)
{
    int format = (int) videoFormat;
    int mode = (int) videoMode;
    switch(format) {
        case FORMAT_0: {
            if (mode == MODE_160_120_YUV444) {
                pSize = IntPoint(160,120);
                pPixelFormat = PixelFormat(NO_PIXELFORMAT); //Not supported by libavg
                return;
            } else if (mode == MODE_320x240_YUV422) {
                pSize = IntPoint(320,240);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_640x480_YUV411) {
                pSize = IntPoint(640,480);
                pPixelFormat = PixelFormat(YCbCr411);
                return;
            } else if (mode == MODE_640x480_YUV422) {
                pSize = IntPoint(640,480);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_640x480_RGB) {
                pSize = IntPoint(640,480);
                pPixelFormat = PixelFormat(R8G8B8);
                return;
            } else if (mode == MODE_640x480_MONO) {
                pSize = IntPoint(640,480);
                pPixelFormat = PixelFormat(I8);
                return;
            } else if (mode == MODE_640x480_MONO16) {
                pSize = IntPoint(640,480);
                pPixelFormat = PixelFormat(I16);
                return;
            } else {
                AVG_ASSERT(false);
                return;
            }
            break;
        } case FORMAT_1: {
            if (mode == MODE_800x600_YUV422) {
                pSize = IntPoint(800,600);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_800x600_RGB) {
                pSize = IntPoint(800,600);
                pPixelFormat = PixelFormat(R8G8B8);
                return;
            } else if (mode == MODE_800x600_MONO) {
                pSize = IntPoint(800,600);
                pPixelFormat = PixelFormat(I8);
                return;
            } else if (mode == MODE_1024x768_YUV422) {
                pSize = IntPoint(1024,768);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_1024x768_RGB) {
                pSize = IntPoint(1024,768);
                pPixelFormat = PixelFormat(R8G8B8);
                return;
            } else if (mode == MODE_1024x768_MONO) {
                pSize = IntPoint(1024,768);
                pPixelFormat = PixelFormat(I8);
                return;
            } else if (mode == MODE_800x600_MONO16) {
                pSize = IntPoint(800,600);
                pPixelFormat = PixelFormat(I16);
                return;
            } else if (mode == MODE_1024x768_MONO16) {
                pSize = IntPoint(1024,768);
                pPixelFormat = PixelFormat(I16);
                return;
            } else {
                AVG_ASSERT(false);
                return;
            }
            break;
        } case FORMAT_2: {
            if (mode == MODE_1280x960_YUV422) {
                pSize = IntPoint(1280,960);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_1280x960_RGB) {
                pSize = IntPoint(1280,960);
                pPixelFormat = PixelFormat(R8G8B8);
                return;
            } else if (mode == MODE_1280x960_MONO) {
                pSize = IntPoint(1280,960);
                pPixelFormat = PixelFormat(I8);
                return;
            } else if (mode == MODE_1600x1200_YUV422) {
                pSize = IntPoint(1600,1200);
                pPixelFormat = PixelFormat(YCbCr422);
                return;
            } else if (mode == MODE_1600x1200_RGB) {
                pSize = IntPoint(1600,1200);
                pPixelFormat = PixelFormat(R8G8B8);
                return;
            } else if (mode == MODE_1600x1200_MONO) {
                pSize = IntPoint(1600,1200);
                pPixelFormat = PixelFormat(I8);
                return;
            } else if (mode == MODE_1280x960_MONO16) {
                pSize = IntPoint(1280,960);
                pPixelFormat = PixelFormat(I16);
                return;
            } else if (mode == MODE_1600x1200_MONO16) {
                pSize = IntPoint(1600,1200);
                pPixelFormat = PixelFormat(I16);
                return;
            } else {
                AVG_ASSERT(false);
                return;
            }
            break;
        } default: {
            AVG_ASSERT(false);
            return;
        }
    }
}

}