summaryrefslogtreecommitdiff
path: root/camlibs/stv0680/library.c
blob: 3b032c475eaa1eb1d12f0f9b61faff63e8b78ec7 (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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
 * STV0680 Vision Camera Chipset Driver
 * Copyright 2000 Adam Harrison <adam@antispin.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <gphoto2/gphoto2.h>
#include <gphoto2/gphoto2-port.h>

#include "libgphoto2/i18n.h"

#include "stv0680.h"
#include "library.h"
#include "sharpen.h"
#include "stv0680-bayer.h"
#include "saturate.h"
#include "libgphoto2/bayer.h"
#include "demosaic_sharpen.h"


#define CMD_RETRIES		0x03

#define STV0680_QCIF_WIDTH	178
#define STV0680_QCIF_HEIGHT	146
#define STV0680_CIF_WIDTH	356
#define STV0680_CIF_HEIGHT	292
#define STV0680_QVGA_WIDTH	324
#define STV0680_QVGA_HEIGHT	244
#define STV0680_VGA_WIDTH	644
#define STV0680_VGA_HEIGHT	484

static unsigned char
stv0680_checksum(const unsigned char *data, int start, int end)
{
	unsigned char sum = 0;
	int i;

	for(i = start; i <= end; ++i) {
		sum += data[i];
		sum &= 0xff;
	}
	return sum;
}

static int stv0680_cmd(GPPort *port, unsigned char cmd,
		unsigned char data1, unsigned char data2, unsigned char data3,
		unsigned char *response, unsigned char response_len)
{
	unsigned char packet[] = { 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x03 };
	unsigned char rhdr[6];
	int ret;

	/* build command packet */
	packet[1] = cmd;
	packet[2] = response_len;
	packet[3] = data1;
	packet[4] = data2;
	packet[5] = data3;
	packet[6] = stv0680_checksum(packet, 1, 5);

	/* write to port */
	printf("Writing packet to port\n");
	if((ret = gp_port_write(port, (char *)packet, 8)) < GP_OK)
		return ret;

	printf("Reading response header\n");
	/* read response header */
	if((ret = gp_port_read(port, (char *)rhdr, 6)) != 6)
		return ret;

	printf("Read response\n");
	/* read response */
	if((ret = gp_port_read(port, (char *)response, response_len)) != response_len)
	    return ret;

	printf("Validating packet [0x%X,0x%X,0x%X,0x%X,0x%X,0x%X]\n",
	       rhdr[0], rhdr[1], rhdr[2], rhdr[3], rhdr[4], rhdr[5]);
	/* validate response */
	if(rhdr[0] != 0x02 || rhdr[1] != cmd ||
	   rhdr[2] != response_len ||
	   rhdr[3] != stv0680_checksum(response, 0, response_len - 1) ||
	   rhdr[4] != stv0680_checksum(rhdr, 1, 3) ||
	   rhdr[5] != 0x03)
		return GP_ERROR_BAD_PARAMETERS;

	printf("Packet OK\n");
	return GP_OK;
}

int stv0680_try_cmd(GPPort *port, unsigned char cmd,
		unsigned short data,
		unsigned char *response, unsigned char response_len
) {
	int ret;
	switch (port->type) {
	case GP_PORT_USB:
	    /* Most significant bit set, data flows from camera->host */
	    if (cmd & 0x80)
		ret=gp_port_usb_msg_read(port,cmd,data,0,(char *)response,response_len);
	    else
		ret=gp_port_usb_msg_write(port,cmd,data,0,(char *)response,response_len);

	    if (ret == response_len)
		return GP_OK;
	    return ret;
	    break;
	case GP_PORT_SERIAL: {
	    int retries = CMD_RETRIES;
	    while(retries--) {
		/* data3 was always 0 */
		switch(ret=stv0680_cmd(port,cmd,(data>>8)&0xff,data&0xff,0,response,response_len)) {
		case GP_ERROR_TIMEOUT:
		case GP_ERROR_BAD_PARAMETERS:
			break;
		default:
			return ret;
		}
	    }
	    break;
	}
	default:
	    return GP_ERROR_NOT_SUPPORTED;
	}
	return GP_ERROR_IO;
}

static int
stv0680_last_error(GPPort *port, struct stv680_error_info *errinf) {
	int ret;

	ret = stv0680_try_cmd(port, CMDID_CLEAR_COMMS_ERROR, 0,
		(void*)errinf, sizeof(*errinf));
	if (ret != GP_OK)
	    return ret;
	return GP_OK;
}

int stv0680_ping(GPPort *port)
{
	unsigned char pong[2];
	int ret;

	ret=stv0680_try_cmd(port, CMDID_PING, 0x55AA, pong, sizeof(pong));
	if (ret != GP_OK)
	    return ret;
	if ((pong[0]!=0x55) || (pong[1]!=0xAA)) {
	    printf("CMDID_PING successful, but returned bad values?\n");
	    return GP_ERROR_IO;
	}
	return GP_OK;
}

int stv0680_file_count(GPPort *port, int *count)
{
	struct stv680_image_info imginfo;
	int ret;

	ret = stv0680_try_cmd(port,CMDID_GET_IMAGE_INFO,0,
		(void*)&imginfo,sizeof(imginfo));
	if (ret != GP_OK)
	    return ret;
	*count = (imginfo.index[0]<<8)|imginfo.index[1];
	return GP_OK;
}

/**
 * Get image, with just bayer applied.
 */
int stv0680_get_image_raw(GPPort *port, int image_no, CameraFile *file)
{
	struct stv680_image_header imghdr;
	char header[80];
	unsigned char *raw, *data;
	int h,w,ret,size;

	ret = stv0680_try_cmd(port, CMDID_UPLOAD_IMAGE, image_no,
		(void*)&imghdr, sizeof(imghdr));
	if(ret != GP_OK)
	    return ret;

	w = (imghdr.width[0]  << 8) | imghdr.width[1];
	h = (imghdr.height[0] << 8) | imghdr.height[1];
	size = (imghdr.size[0] << 24) | (imghdr.size[1] << 16) |
	    (imghdr.size[2]<<8) | imghdr.size[3];
	raw = malloc(size);
	if (!raw)
	    return GP_ERROR_NO_MEMORY;
	if ((ret=gp_port_read(port, (char *)raw, size))<0) {
	    free (raw);
	    return ret;
	}

	sprintf(header, "P6\n# gPhoto2 stv0680 image\n%d %d\n255\n", w, h);
	gp_file_append(file, header, strlen(header));
	data = malloc(size * 3);
	if (!data) {
	    free (raw);
	    return GP_ERROR_NO_MEMORY;
	}
	gp_bayer_decode(raw,w,h,data,BAYER_TILE_GBRG_INTERLACED);
	free(raw);
	gp_file_append(file, (char *)data, size*3);
	free(data);
	return GP_OK;
}

int stv0680_get_image(GPPort *port, int image_no, CameraFile *file)
{
	struct stv680_image_header imghdr;
	char header[200];
	unsigned char buf[16];
	unsigned char *raw, *tmpdata1, *tmpdata2, *data;
	int h,w,ret,coarse,fine,size;

	/* Despite the documentation saying so, CMDID_UPLOAD_IMAGE does not
	 * return an image_header. The first 8 byte are correct, but the
	 * next 8 appear strange. So we call CMDID_GET_IMAGE_HEADER before.
	 */
	ret = stv0680_try_cmd(port, CMDID_GET_IMAGE_HEADER, image_no,
		(void*)&imghdr, sizeof(imghdr));
	if(ret != GP_OK)
		return ret;
	ret = stv0680_try_cmd(port, CMDID_UPLOAD_IMAGE, image_no,
		(void*)buf, sizeof(buf));
	if(ret != GP_OK)
		return ret;
	w = (imghdr.width[0]  << 8) | imghdr.width[1];
	h = (imghdr.height[0] << 8) | imghdr.height[1];
	size = (imghdr.size[0] << 24) | (imghdr.size[1] << 16) |
	    (imghdr.size[2]<<8) | imghdr.size[3];
	fine = (imghdr.fine_exposure[0]<<8)|imghdr.fine_exposure[1];
	coarse = (imghdr.coarse_exposure[0]<<8)|imghdr.coarse_exposure[1];
	raw = malloc(size);
	if (!raw)
	    return GP_ERROR_NO_MEMORY;
	sprintf(header, "P6\n# gPhoto2 stv0680 image\n#flags %x sgain %d sclkdiv %d avgpix %d fine %d coarse %d\n%d %d\n255\n", imghdr.flags, imghdr.sensor_gain, imghdr.sensor_clkdiv, imghdr.avg_pixel_value, fine, coarse , w, h);

	gp_file_append(file, header, strlen(header));
	if ((ret=gp_port_read(port, (char *)raw, size))<0) {
		free (raw);
		return ret;
	}

	data = malloc(size * 3);
	if (!data) {
		free (raw);
		return GP_ERROR_NO_MEMORY;
	}
	tmpdata1 = malloc(size * 3);
	if (!tmpdata1) {
		free (raw);
		free (data);
		return GP_ERROR_NO_MEMORY;
	}
	tmpdata2 = malloc(size * 3);
	if (!tmpdata2) {
		free (raw);
		free (data);
		free (tmpdata1);
		return GP_ERROR_NO_MEMORY;
	}
	gp_bayer_expand (raw, w, h, tmpdata1, BAYER_TILE_GBRG_INTERLACED);
	light_enhance(w,h,coarse,imghdr.avg_pixel_value,fine,tmpdata1);
	/* gp_bayer_interpolate (tmpdata1, w, h, BAYER_TILE_GBRG_INTERLACED); */
	stv680_hue_saturation (w, h, tmpdata1, tmpdata2 );
	demosaic_sharpen (w, h, tmpdata2, tmpdata1, 2, BAYER_TILE_GBRG_INTERLACED);
	sharpen (w, h, tmpdata1, data, 16);
	free(tmpdata2);
	free(tmpdata1);
	free(raw);
	gp_file_append(file, (char *)data, 3*size);
	free(data);
	return GP_OK;
}

int stv0680_get_image_preview(GPPort *port, int image_no, CameraFile *file)
{
	struct stv680_image_header imghdr;
	char header[64];
	unsigned char *raw, *data;
	unsigned int h,w,rh,rw,scale,rsize,size;
	int ret;

	switch (port->type) {
	case GP_PORT_USB:
		if ((ret = stv0680_try_cmd(port, CMDID_UPLOAD_IMAGE,
			image_no, (void*)&imghdr, sizeof(imghdr)) < 0)) {
			return ret;
		}
		rw = (imghdr.width[0]  << 8) | imghdr.width[1];
		rh = (imghdr.height[0] << 8) | imghdr.height[1];
		rsize = (imghdr.size[0] << 24) | (imghdr.size[1] << 16) |
		    (imghdr.size[2]<<8) | imghdr.size[3];
		scale = (rw>>8)+1;
		break;
	default:
	case GP_PORT_SERIAL:
		ret = stv0680_try_cmd(port, CMDID_UPLOAD_THUMBNAIL, image_no,
			(void*)&imghdr, sizeof(imghdr));
		if(ret != GP_OK)
			return ret;
		rw = (imghdr.width[0]  << 8) | imghdr.width[1];
		rh = (imghdr.height[0] << 8) | imghdr.height[1];
		rsize = (imghdr.size[0] << 24) | (imghdr.size[1] << 16) |
		    (imghdr.size[2]<<8) | imghdr.size[3];
		scale = 0;
		break;
	}
	raw = calloc(1, rsize);
	if (!raw) return GP_ERROR_NO_MEMORY;
	if ((ret=gp_port_read(port, (char *)raw, rsize))<0) {
		free(raw);
		return ret;
	}
	w = rw >> scale;
	h = rh >> scale;
	size = w * h;

	sprintf(header, "P6\n# gPhoto2 stv0680 image\n%d %d\n255\n", w, h);
	gp_file_append(file, header, strlen(header));
	data = calloc(1,size * 3);

	if (!scale)
	    gp_bayer_decode(raw, rw, rh, data, BAYER_TILE_GBRG_INTERLACED);
	else
	    bayer_unshuffle_preview(rw, rh, scale, raw, data);
	free(raw);
	gp_file_append(file, (char *)data, size*3);
	free(data);
	return GP_OK;
}

int stv0680_capture(GPPort *port)
{
	int ret;
	struct stv680_error_info errinf;

	ret = stv0680_try_cmd(port, CMDID_GRAB_IMAGE, GRAB_UPDATE_INDEX|GRAB_USE_CAMERA_INDEX, NULL, 0);
	if (ret!=GP_OK)
		return ret;
	/* wait until it is done */
	do {
	    ret = stv0680_last_error(port, &errinf);
	    if (ret != GP_OK)
		return ret;
	    if (errinf.error == CAMERR_BAD_EXPOSURE) {
		gp_port_set_error(port,_("Bad exposure (not enough light probably)"));
		return GP_ERROR;
	    }
	    if (errinf.error != CAMERR_BUSY)
		fprintf(stderr,"stv680_capture: error was %d.%d\n",errinf.error,errinf.info);
	} while (errinf.error == CAMERR_BUSY);
	return GP_OK;
}

#if 0
/* this somehow terminates after some images. timeouts due to low exposure?
 * I haven't found out yet. But this would be the right way to do it.
 */
int stv0680_capture_preview(GPPort *port, char **data, int *size)
{
	struct stv680_image_header imghdr;
	struct stv680_image_info imginfo;
	struct stv680_error_info errinf;
	char header[64];
	unsigned char *raw, *bayerpre;
	int fine,coarse,h,w,ret;

	ret = stv0680_last_error(port, &errinf);
	if (ret != GP_OK)
	    return ret;

	ret = stv0680_try_cmd(port, CMDID_GRAB_IMAGE, GRAB_USE_CAMERA_INDEX, NULL,0);
	if(ret != GP_OK)
		return ret;
	do {
		ret = stv0680_try_cmd(port, CMDID_CLEAR_COMMS_ERROR, 0, (void*)&errinf, sizeof(errinf));
		if (ret != GP_OK)
		    return ret;
		if (errinf.error == CAMERR_BAD_EXPOSURE) {
		    gp_port_set_error(port,_("Bad exposure (not enough light probably)"));
		    return GP_ERROR;
		}
		if (errinf.error != CAMERR_BUSY)
		    fprintf(stderr,"stv680_capture: error was %d.%d\n",errinf.error,errinf.info);
	} while (errinf.error == CAMERR_BUSY);

#if 0
	fprintf(stderr,"image flag %x\n",imghdr.flags);
	fprintf(stderr,"sensor gain %d\n",imghdr.sensor_gain);
	fprintf(stderr,"sensor clkdiv %d\n",imghdr.sensor_clkdiv);
	fprintf(stderr,"avg pixel value %d\n",imghdr.avg_pixel_value);
#endif
	ret = stv0680_try_cmd(port, CMDID_GET_IMAGE_INFO, 0, (void*)&imginfo, sizeof(imginfo));
	if (ret!=GP_OK)
	    return ret;
	ret = stv0680_try_cmd(port, CMDID_UPLOAD_IMAGE, (imginfo.index[0]<<8)|imginfo.index[1], (void*)&imghdr, sizeof(imghdr));
	if(ret != GP_OK)
		return ret;
	fine = (imghdr.fine_exposure[0]<<8)|imghdr.fine_exposure[1];
	coarse = (imghdr.coarse_exposure[0]<<8)|imghdr.coarse_exposure[1];
	w = (imghdr.width[0]  << 8) | imghdr.width[1];
	h = (imghdr.height[0] << 8) | imghdr.height[1];
	*size = (imghdr.size[0] << 24) | (imghdr.size[1] << 16) |
	    (imghdr.size[2]<<8) | imghdr.size[3];

	raw = malloc(*size);
	if ((ret=gp_port_read(port, raw, *size))<0)
	    return ret;

	sprintf(header, "P6\n# gPhoto2 stv0680 image\n%d %d\n255\n", w, h);
	*data = malloc(((*size) * 3) + strlen(header));
	strcpy(*data, header);
	bayerpre = malloc(((*size)*3));
	gp_bayer_expand (raw, w, h, bayerpre, BAYER_TILE_GBRG_INTERLACED);
	light_enhance(w,h,coarse,fine,imghdr.avg_pixel_value,bayerpre);
	/* gp_bayer_interpolate (bayerpre, w, h, BAYER_TILE_GBRG_INTERLACED); */
	demosaic_sharpen (w, h, bayerpre, *data + strlen(header), 2, BAYER_TILE_GBRG_INTERLACED);
	/* sharpen (w, h, bayerpre,*data + strlen(header), 20); */
	free(bayerpre);
	free(raw);
	*size *= 3;
	*size += strlen(header);
	return GP_OK;
}
#endif

int stv0680_capture_preview(GPPort *port, char **data, int *size)
{
	char header[64];
	struct stv680_camera_info caminfo;
	unsigned char *raw,*bayerpre;
	int xsize,h,w,i;
	int ret;
	struct capmode {
	    int mask,w,h,mode;
	} capmodes[4] = {
	    { 1, STV0680_CIF_WIDTH,  STV0680_CIF_HEIGHT,  0x000 },
	    { 2, STV0680_VGA_WIDTH,  STV0680_VGA_HEIGHT,  0x100 },
	    { 4, STV0680_QCIF_WIDTH, STV0680_QCIF_HEIGHT, 0x200 },
	    { 8, STV0680_QVGA_WIDTH, STV0680_QVGA_HEIGHT, 0x300 },
	};

	/* Get Camera Information */
	if ((ret = stv0680_try_cmd(port, CMDID_GET_CAMERA_INFO, 0,
				(void*)&caminfo, sizeof(caminfo)) < 0))
		return ret;

	/* serial cameras don't have video... Too slow. */
	if (!(caminfo.hardware_config & HWCONFIG_HAS_VIDEO))
	    return GP_ERROR_NOT_SUPPORTED;

	/* Look for the first supported mode, with decreasing size */
	for (i=0;i<4;i++)
	    if (caminfo.capabilities & capmodes[i].mask)
		break;
	if (i==4) {
	    fprintf(stderr,"Neither CIF, QCIF, QVGA nor VGA supported?\n");
	    return GP_ERROR;
	}
	w = capmodes[i].w;
	h = capmodes[i].h;

	xsize = (w+2)*(h+2);

	/* Send parameter according to mode */
	ret = stv0680_try_cmd(port,CMDID_START_VIDEO,capmodes[i].mode,NULL,0x0);

	if (ret!=GP_OK)
		return ret;

	*size= xsize;
	raw = malloc(*size);
	switch(gp_port_read(port, (char *)raw, *size)) {
	case GP_ERROR_TIMEOUT:
		printf("read timeout\n");
		break;
	case GP_ERROR:
		printf("IO error\n");
		break;
	default:break;
	}
	if ((ret = stv0680_try_cmd(port, CMDID_STOP_VIDEO, 0, NULL, 0)!=GP_OK)) {
		free (raw);
		return ret;
	}
	sprintf(header, "P6\n# gPhoto2 stv0680 image\n%d %d\n255\n", w, h);
	*data = malloc((*size * 3) + strlen(header));
	strcpy(*data, header);
	bayerpre = malloc(((*size)*3));
	/* no light enhancement here, we do not get the exposure values? */
	gp_bayer_decode (raw, w, h, bayerpre, BAYER_TILE_GBRG_INTERLACED);
	demosaic_sharpen (w, h, bayerpre, (unsigned char *)*data + strlen(header), 2, BAYER_TILE_GBRG_INTERLACED);
	/* sharpen (w, h, bayerpre,*data + strlen(header), 20); */
	free(raw);
	free(bayerpre);
	*size *= 3;
	*size += strlen(header);
	return GP_OK;
}


int stv0680_delete_all(GPPort *port) {
    return stv0680_try_cmd(port,CMDID_SET_IMAGE_INDEX,0,NULL,0);
}

int stv0680_summary(GPPort *port, char *txt)
{
    struct stv680_camera_info caminfo;
    struct stv680_image_info imginfo;
    int ret;

    strcpy(txt,_("Information on STV0680-based camera:\n"));
    /* Get Camera Information */
    if ((ret = stv0680_try_cmd(port, CMDID_GET_CAMERA_INFO,
				0, (void*)&caminfo, sizeof(caminfo)) < 0))
	return ret;
    sprintf(txt+strlen(txt),_("Firmware Revision: %d.%d\n"),
	    caminfo.firmware_revision[0],
	    caminfo.firmware_revision[1]
    );
    sprintf(txt+strlen(txt),_("ASIC Revision: %d.%d\n"),
	    caminfo.asic_revision[0],
	    caminfo.asic_revision[1]
    );
    sprintf(txt+strlen(txt),_("Sensor ID: %d.%d\n"),
	    caminfo.sensor_id[0],
	    caminfo.sensor_id[1]
    );
    /* HWCONFIG_COMMSLINK_MASK ... not really needed, the user knows, he
     * plugged it in. */
    sprintf(txt+strlen(txt),_("Camera is configured for lights flickering by %dHz.\n"),
    	(caminfo.hardware_config & HWCONFIG_FLICKERFREQ_60HZ)?60:50
    );
    sprintf(txt+strlen(txt),_("Memory in camera: %d Mbit.\n"),
    	(caminfo.hardware_config & HWCONFIG_MEMSIZE_16MBIT)?16:64
    );
    if (caminfo.hardware_config & HWCONFIG_HAS_THUMBNAILS)
	strcat(txt,_("Camera supports Thumbnails.\n"));
    if (caminfo.hardware_config & HWCONFIG_HAS_VIDEO)
	strcat(txt,_("Camera supports Video.\n"));
    /* HWCONFIG_STARTUP_COMPLETED ... Would the camera even answer if not ? */
    if (caminfo.hardware_config & HWCONFIG_IS_MONOCHROME)
	strcat(txt,_("Camera pictures are monochrome.\n"));
    if (caminfo.hardware_config & HWCONFIG_MEM_FITTED) /* Is this useful? */
	strcat(txt,_("Camera has memory.\n"));

    strcat(txt,_("Camera supports videoformats: "));
    if (caminfo.capabilities & CAP_CIF) strcat(txt,"CIF ");
    if (caminfo.capabilities & CAP_VGA) strcat(txt,"VGA ");
    if (caminfo.capabilities & CAP_QCIF) strcat(txt,"QCIF ");
    if (caminfo.capabilities & CAP_QVGA) strcat(txt,"QVGA ");
    strcat(txt,"\n");
    sprintf(txt+strlen(txt),_("Vendor ID: %02x%02x\n"),
	    caminfo.vendor_id[0],
	    caminfo.vendor_id[1]
    );
    sprintf(txt+strlen(txt),_("Product ID: %02x%02x\n"),
	    caminfo.product_id[0],
	    caminfo.product_id[1]
    );
    if ((ret = stv0680_try_cmd(port, CMDID_GET_IMAGE_INFO, 0,
		    (void*)&imginfo, sizeof(imginfo))!=GP_OK))
	return ret;
    sprintf(txt+strlen(txt),_("Number of Images: %d\n"),
	    (imginfo.index[0]<<8)|imginfo.index[1]
    );
    sprintf(txt+strlen(txt),_("Maximum number of Images: %d\n"),
	    (imginfo.maximages[0]<<8)|imginfo.maximages[1]
    );
    sprintf(txt+strlen(txt),_("Image width: %d\n"),
	    (imginfo.width[0]<<8)|imginfo.width[1]
    );
    sprintf(txt+strlen(txt),_("Image height: %d\n"),
	    (imginfo.height[0]<<8)|imginfo.height[1]
    );
    sprintf(txt+strlen(txt),_("Image size: %d\n"),
	    (imginfo.size[0]<<24)|(imginfo.size[1]<<16)|(imginfo.size[2]<<8)|
	     imginfo.size[3]
    );
    sprintf(txt+strlen(txt),_("Thumbnail width: %d\n"),imginfo.thumb_width);
    sprintf(txt+strlen(txt),_("Thumbnail height: %d\n"),imginfo.thumb_height);
    sprintf(txt+strlen(txt),_("Thumbnail size: %d\n"),
	    (imginfo.thumb_size[0]<<8)|imginfo.thumb_size[1]
    );
    return GP_OK;
}