summaryrefslogtreecommitdiff
path: root/camlibs/sq905/library.c
blob: addbd99754d809c2fc22bc6f40df14d67056d175 (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
/* library.c
 *
 * Copyright (C) 2003 Theodore Kilgore <kilgota@auburn.edu>
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#define _DEFAULT_SOURCE

#include <config.h>

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

#include <libgphoto2/bayer.h>
#include <libgphoto2/gamma.h>


#include <gphoto2/gphoto2.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define _(String) (String)
#  define N_(String) (String)
#endif

#include "sq905.h"

#include <gphoto2/gphoto2-port.h>

#define GP_MODULE "sq905"

static const struct {
	char *name;
	CameraDriverStatus status;
	unsigned short idVendor;
	unsigned short idProduct;
} models[] = {
	{"SQ chip camera",    GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Argus DC-1510",     GP_DRIVER_STATUS_PRODUCTION,   0x2770, 0x9120},
	{"Gear to go",        GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Mitek CD10" ,       GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Mitek CD30P",       GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"GTW Electronics",   GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Concord Eye-Q Easy",GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Concord Eye-Q Duo", GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Che-ez Snap",       GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"PockCam",           GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Magpix B350",       GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Jenoptik JDC 350",  GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Precision Mini Digital Camera",
	                      GP_DRIVER_STATUS_PRODUCTION , 0x2770 , 0x9120},
	{"iConcepts digital camera" ,
                          GP_DRIVER_STATUS_PRODUCTION , 0x2770 , 0x9120},
	{"Sakar Kidz Cam 86379",    GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770,
								    0x9120},
	{"ViviCam3350",       GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"ViviCam5B",         GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"DC-N130ta",         GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"SY-2107C",          GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Shark SDC-513",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Shark SDC-519",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Shark 2-in-1 Mini", GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Argus DC-1730",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x913c},
	{"Request Ultra Slim",
	                      GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{"Global Point Splash Mini (underwater camera)",
	                      GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9120},
	{NULL,0,0,0}
};

int
camera_id (CameraText *id)
{
	strcpy (id->text, "SQ chipset camera");

	return GP_OK;
}


int
camera_abilities (CameraAbilitiesList *list)
{
	int i;
	CameraAbilities a;

	for (i = 0; models[i].name; i++) {
		memset (&a, 0, sizeof(a));
		strcpy (a.model, models[i].name);
		a.status = models[i].status;
		a.port   = GP_PORT_USB;
		a.speed[0] = 0;
		a.usb_vendor = models[i].idVendor;
		a.usb_product= models[i].idProduct;
		if (a.status == GP_DRIVER_STATUS_EXPERIMENTAL)
			a.operations = GP_OPERATION_NONE;
		else
			a.operations = GP_OPERATION_CAPTURE_PREVIEW;
		a.folder_operations = GP_FOLDER_OPERATION_DELETE_ALL;
		a.file_operations   = GP_FILE_OPERATION_PREVIEW
					    + GP_FILE_OPERATION_RAW;
		gp_abilities_list_append (list, a);
	}

	return GP_OK;
}

static int
camera_summary (Camera *camera, CameraText *summary, GPContext *context)
{
	sprintf (summary->text,_("Your USB camera has a S&Q chipset.\n"
				"The total number of pictures taken is %i\n"
				"Some of these could be clips containing\n"
				"several frames\n"),

				camera->pl->nb_entries);

	return GP_OK;
}

static int camera_manual (Camera *camera, CameraText *manual,
						    GPContext *context)
{
	strcpy(manual->text,
	_(
	"For cameras with S&Q Technologies chip.\n"
	"Should work with gtkam. Photos will be saved in PPM format.\n\n"

	"All known S&Q cameras have two resolution settings. What\n"
	"those are, will depend on your particular camera.\n"
	"A few of these cameras allow deletion of all photos. Most do not.\n"
	"Uploading of data to the camera is not supported.\n"
	"The photo compression mode found on many of the S&Q\n"
	"cameras is supported, to some extent.\n"
	"If present on the camera, video clips are seen as subfolders.\n"
	"Gtkam will download these separately. When clips are present\n"
	"on the camera, there is a little triangle before the name of\n"
	"the camera. If no folders are listed, click on the little \n"
	"triangle to make them appear. Click on a folder to enter it\n"
	"and see the frames in it, or to download them. The frames will\n"
	"be downloaded as separate photos, with special names which\n"
	"specify from which clip they came. Thus, you may freely \n"
	"choose to save clip frames in separate directories. or not.\n"
	));

	return (GP_OK);
}



static int
camera_about (Camera *camera, CameraText *about, GPContext *context)
{
	strcpy (about->text, _("sq905 generic driver\n"
			    "Theodore Kilgore <kilgota@auburn.edu>\n"));

	return GP_OK;
}

/*************** File and Downloading Functions *******************/


static int
file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
                void *data, GPContext *context)
{
	Camera *camera = data;
	int i,n;
	GP_DEBUG ("List files in %s\n", folder);
	if (0==strcmp(folder, "/")) {
		for (i=0, n=0; i<camera->pl->nb_entries; i++) {
			if (!sq_is_clip(camera->pl, i)) n++;
		}
		gp_list_populate(list, "pict%03i.ppm", n);
	} else {
		char format[16];
		n = atoi(folder+1+4);	/* '/' + 'clip' */
		snprintf(format, sizeof(format), "%03i_%%03i.ppm", n);
		for (i=0; i<camera->pl->nb_entries && n>0; i++) {
			if (sq_is_clip(camera->pl, i)) n--;
		}
		i--;
		if (!sq_is_clip(camera->pl, i))
			return GP_ERROR_DIRECTORY_NOT_FOUND;
		gp_list_populate(list, format,
				    sq_get_num_frames(camera->pl, i));
	}
	return GP_OK;
}

static int
folder_list_func (CameraFilesystem *fs, const char *folder, CameraList *list,
		void *data, GPContext *context)
{
	Camera *camera = data;
	int i,n;
	GP_DEBUG ("List folders in %s\n", folder);
	if (0==strcmp(folder, "/")) {
		for (i=0, n=0; i<camera->pl->nb_entries; i++) {
			if (sq_is_clip(camera->pl, i)) n++;
		}
		gp_list_populate (list, "clip%03i", n);

	}
	return GP_OK;
}

static int
get_info_func (CameraFilesystem *fs, const char *folder, const char *file,
		      CameraFileInfo *info, void *data, GPContext *context)
{
	memset (info, 0, sizeof(CameraFileInfo));
	/* FIXME: fill in some stuff? */
	return (GP_OK);
}


static int
get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileType type, CameraFile *file, void *user_data,
	       GPContext *context)
{
	Camera *camera = user_data;
	int i, w, h, b, entry, frame, is_in_clip;
	int nb_frames, to_fetch;
	int do_preprocess;
	unsigned char comp_ratio;
	unsigned char *frame_data, *rawdata;
	unsigned char *ppm, *ptr;
	unsigned char gtable[256];
	int size;
	int this_cam_tile;

	if (GP_FILE_TYPE_EXIF ==type) return GP_ERROR_FILE_EXISTS;

	if (GP_FILE_TYPE_RAW!=type && GP_FILE_TYPE_NORMAL
				    !=type && GP_FILE_TYPE_PREVIEW!=type) {

		return GP_ERROR_NOT_SUPPORTED;
	}


	/* Get the entry number of the photo on the camera */
	entry = -1;
	if (0==strcmp(folder, "/")) {
		i = atoi(filename+4);
		do {
			do entry++;
			while (sq_is_clip(camera->pl, entry)
					&& entry<camera->pl->nb_entries);
			i--;
		}
		while (i>0);
		if (entry == camera->pl->nb_entries)
			return GP_ERROR_FILE_NOT_FOUND;
		frame = 0;
		is_in_clip = 0;
	} else {
		i = atoi(folder+1+4);
		do {
			do entry++;
			while (!sq_is_clip(camera->pl, entry) &&
						entry<camera->pl->nb_entries);
			i--;
		} while (i>0);
		if (entry == camera->pl->nb_entries)
			return GP_ERROR_DIRECTORY_NOT_FOUND;
		frame = atoi(filename+4)-1;
		if (frame >= sq_get_num_frames(camera->pl, entry))
			return GP_ERROR_FILE_NOT_FOUND;
		is_in_clip = 1;
	}

	GP_DEBUG ("Download file %s from %s, entry = %d, frame = %d\n",
					    filename, folder, entry, frame);

	/* Fetch entries until the one we need, and toss all before
	 * the one we need.
	 * TODO: Either find out how to use the location info in the catalog
	 * to download just the entry needed, or show it is as impossible as
	 * it seems to be.
	 */

	GP_DEBUG ("last entry was %d\n", camera->pl->last_fetched_entry);

	/* Change register to DATA, but only if necessary */
	if ((camera->pl->last_fetched_entry == -1)
	|| ((is_in_clip) && (frame == 0)) )

	sq_access_reg(camera->port, DATA);

	if (camera->pl->last_fetched_entry > entry) {
		sq_rewind(camera->port, camera->pl);
	}
	do_preprocess = 0;
	do {
		to_fetch = camera->pl->last_fetched_entry;
		if (to_fetch < entry) {
			to_fetch++;
			free(camera->pl->last_fetched_data);
			camera->pl->last_fetched_data = NULL;
		}
		nb_frames = sq_get_num_frames(camera->pl, to_fetch);
		comp_ratio = sq_get_comp_ratio (camera->pl, to_fetch);
		w = sq_get_picture_width (camera->pl, to_fetch);
		switch (w) {
		case 176: h = 144; break;
		case 640: h = 480; break;
		case 320: h = 240; break;
		default:  h = 288; break;
		}
		if (!comp_ratio) {
			sq_rewind(camera->port, camera->pl);
			return GP_ERROR;
		}
		b = nb_frames * w * h / comp_ratio;
		do_preprocess = 1;
		if (camera->pl->last_fetched_data) break;

		camera->pl->last_fetched_data = malloc (nb_frames*w*h);
		if (!camera->pl->last_fetched_data) {
			sq_rewind(camera->port, camera->pl);
			return GP_ERROR_NO_MEMORY;
		}
		GP_DEBUG("Fetch entry %i\n", to_fetch);
		sq_read_picture_data
			    (camera->port, camera->pl->last_fetched_data, b);
		camera->pl->last_fetched_entry = to_fetch;
	} while (camera->pl->last_fetched_entry<entry);

	frame_data = camera->pl->last_fetched_data+(w*h)*frame/comp_ratio;
	/* sq_preprocess ( ) turns the photo right-side-up and for some
	 * models must also de-mirror the photo
	 */

	if (GP_FILE_TYPE_RAW!=type) {

		if (do_preprocess) {
			sq_preprocess(camera->pl->model, comp_ratio,
					is_in_clip, frame_data, w, h);
		}

		/*
		 * Now put the data into a PPM image file.
		 */
		ppm = malloc (w * h * 3 + 256); /* room for data + header */
		if (!ppm) { return GP_ERROR_NO_MEMORY; }
		sprintf ((char *)ppm,
			"P6\n"
			"# CREATOR: gphoto2, SQ905 library\n"
			"%d %d\n"
			"255\n", w, h);
		size = strlen ((char *)ppm);
		ptr = ppm + size;

			switch (camera->pl->model) {
			case SQ_MODEL_POCK_CAM:
			case SQ_MODEL_MAGPIX:
				this_cam_tile = BAYER_TILE_GBRG;
				break;
			default:
				this_cam_tile = BAYER_TILE_BGGR;
				break;
			}
		size = size + (w * h * 3);
		GP_DEBUG ("size = %i\n", size);
		if (comp_ratio>1) {
			rawdata = malloc (w*h);
			if (!rawdata) {
				free (ppm);
				return GP_ERROR_NO_MEMORY;
			}
			sq_decompress (camera->pl->model, rawdata,
						frame_data, w, h);
			gp_gamma_fill_table (gtable, .65);
		} else {
			rawdata = frame_data;
			gp_gamma_fill_table (gtable, .55);
		}
		gp_ahd_decode (rawdata, w , h , ptr, this_cam_tile);
		gp_gamma_correct_single (gtable, ptr, w * h);

		gp_file_set_mime_type (file, GP_MIME_PPM);
		gp_file_set_data_and_size (file, (char *)ppm, size);
		if (rawdata != frame_data) free (rawdata);

	} else {	/* type is GP_FILE_TYPE_RAW */
		size = w*h/comp_ratio;
		rawdata = malloc (size+16);
		if (!rawdata) return GP_ERROR_NO_MEMORY;
		memcpy (rawdata, frame_data, size);
		memcpy (rawdata+size,camera->pl->catalog+16*entry,16);
		gp_file_set_mime_type (file, GP_MIME_RAW);
	        gp_file_set_data_and_size (file, (char *)rawdata, size+16);
	}
	/* Reset camera when done, for more graceful exit. */
	if ((!(is_in_clip)&&(entry +1 == camera->pl->nb_entries))
	|| ((is_in_clip)&& (frame +1 == nb_frames )))
		sq_reset (camera->port);

	return GP_OK;
}

static int
delete_all_func (CameraFilesystem *fs, const char *folder, void *data,
		 GPContext *context)
{
	Camera *camera = data;

	GP_DEBUG(" * delete_all_func()");
	sq_delete_all(camera->port, camera->pl);

	return (GP_OK);
}

static int
camera_capture_preview (Camera *camera, CameraFile *file, GPContext *context)
{
	unsigned char *frame_data;
	unsigned char *ppm, *ptr;
	unsigned char gtable[256];
	int size;
	int w = 320;
	int h = 240;
	int b=0x12c40;

	camera->pl->last_fetched_data = malloc (b);
	if (!camera->pl->last_fetched_data) {
		sq_rewind(camera->port, camera->pl);
		return GP_ERROR_NO_MEMORY;
	}

        sq_access_reg(camera->port, CAPTURE);
	sq_read_picture_data (camera->port, camera->pl->last_fetched_data, b);
	frame_data = camera->pl->last_fetched_data + 0x40;
	sq_preprocess(camera->pl->model, 1, 0, frame_data, w, h);

	/* Now put the data into a PPM image file. */
	ppm = malloc (w * h * 3 + 256);
	if (!ppm)
		return GP_ERROR_NO_MEMORY;
	sprintf ((char *)ppm,
		"P6\n"
		"# CREATOR: gphoto2, SQ905 library\n"
		"%d %d\n"
		"255\n", w, h);
	ptr = ppm + strlen ((char*)ppm);
	size = strlen ((char*)ppm) + (w * h * 3);
	GP_DEBUG ("size = %i\n", size);
	switch (camera->pl->model) {
	case SQ_MODEL_POCK_CAM:
		gp_bayer_decode (frame_data, w , h , ptr, BAYER_TILE_GBRG);
		break;
	default:
		gp_bayer_decode (frame_data, w , h , ptr, BAYER_TILE_BGGR);
		break;
	}

	/* TO DO:
	 * Adapt some postprocessing routine to work here, because results
	 * can vary greatly, depending both on lighting conditions and on
	 * camera model.
	 */

	gp_gamma_fill_table (gtable, .5);
	gp_gamma_correct_single (gtable, ptr, w * h);
	gp_file_set_mime_type (file, GP_MIME_PPM);
	gp_file_set_data_and_size (file, (char *)ppm, size);

	sq_reset(camera->port);
        sq_access_reg(camera->port, CAPTURE);
	sq_reset(camera->port);

	return (GP_OK);
}

/*************** Exit and Initialization Functions ******************/

static int
camera_exit (Camera *camera, GPContext *context)
{
	GP_DEBUG ("SQ camera_exit");
	sq_reset (camera->port);

	if (camera->pl) {
		free (camera->pl->catalog);
		free (camera->pl->last_fetched_data);
		free (camera->pl);
		camera->pl = NULL;
	}

	return GP_OK;
}

static CameraFilesystemFuncs fsfuncs = {
	.file_list_func = file_list_func,
	.folder_list_func = folder_list_func,
	.get_file_func = get_file_func,
	.get_info_func = get_info_func,
	.delete_all_func = delete_all_func,
};
int
camera_init(Camera *camera, GPContext *context)
{
	GPPortSettings settings;
	int ret = 0;

	/* First, set up all the function pointers */
	camera->functions->summary	= camera_summary;
	camera->functions->manual	= camera_manual;
	camera->functions->about	= camera_about;
	camera->functions->capture_preview
					= camera_capture_preview;
	camera->functions->exit		= camera_exit;

	GP_DEBUG ("Initializing the camera\n");

	ret = gp_port_get_settings(camera->port,&settings);
	if (ret < 0) return ret;

	ret = gp_port_set_settings(camera->port,settings);
	if (ret < 0) return ret;

	/* Tell the CameraFilesystem where to get lists from */
	gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
	camera->pl = malloc (sizeof (CameraPrivateLibrary));
	if (!camera->pl) return GP_ERROR_NO_MEMORY;
	camera->pl->model = 0;
	camera->pl->catalog = NULL;
	camera->pl->nb_entries = 0;
	camera->pl->last_fetched_entry = -1;
	camera->pl->last_fetched_data = NULL;

	/* Connect to the camera */
	ret = sq_init (camera->port, camera->pl);
	if (ret != GP_OK) {
		free(camera->pl);
		return ret;
	};

	return GP_OK;
}