summaryrefslogtreecommitdiff
path: root/camlibs/digigr8/library.c
blob: 1bbcbca3944cf612eefbce7ad7e4ac0212b7a76b (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
/* library.c for libgphoto2/camlibs/digigr8
 *
 * Copyright (C) 2005 - 2010 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 "digigr8.h"

#include <gphoto2/gphoto2-port.h>

#define GP_MODULE "digigr8"

static const struct {
	char *name;
	CameraDriverStatus status;
	unsigned short idVendor;
	unsigned short idProduct;
} models[] = {
	{"Digigr8",		GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"Cobra Digital Camera DC150", GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770,
									0x905c},
	{"Che-Ez Snap SNAP-U",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"DC-N130t",		GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905C},
	{"Soundstar TDC-35",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"Nexxtech Mini Digital Camera", GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770,
									0x905c},
	{"Vivitar Vivicam35",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"Praktica Slimpix",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"ZINA Mini Digital Keychain Camera",	GP_DRIVER_STATUS_EXPERIMENTAL,
							    0x2770, 0x905c},
	{"Pixie Princess Jelly-Soft",		GP_DRIVER_STATUS_EXPERIMENTAL,
							    0x2770, 0x905c},
	{"Sakar Micro Digital 2428x",		GP_DRIVER_STATUS_EXPERIMENTAL,
							    0x2770, 0x905c},
	{"Stop & Shop 87096",		GP_DRIVER_STATUS_EXPERIMENTAL,
							    0x2770, 0x905c},
	{"Jazz JDC9",		GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"Aries Digital Keychain Camera, ITEM 128986",
				GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x905c},
	{"Disney pix micro",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9050},
	{"Lego Bionicle",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9051},
	{"Barbie Camera (Digital Blue)",	GP_DRIVER_STATUS_EXPERIMENTAL,
							    0x2770, 0x9051},
	/* from IRC reporter, adam@piggz.co.uk */
	{"Disney pix micro 2",	GP_DRIVER_STATUS_EXPERIMENTAL, 0x2770, 0x9052},
	{"Suprema Digital Keychain Camera",	GP_DRIVER_STATUS_EXPERIMENTAL,
								0x2770, 0x913d},
	{"Sakar 28290 and 28292  Digital Concepts Styleshot",
						GP_DRIVER_STATUS_EXPERIMENTAL,
							0x2770, 0x913d},
	{"Sakar 23070  Crayola Digital Camera", GP_DRIVER_STATUS_EXPERIMENTAL,
							0x2770, 0x913d},
	{"Sakar 92045  Spiderman",    GP_DRIVER_STATUS_EXPERIMENTAL,
							0x2770, 0x913d},
	{NULL,0,0,0}
};

int
camera_id(CameraText *id)
{
	strncpy (id->text, "SQ905C chipset camera",32);
	return GP_OK;
}


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

	for (i = 0; models[i].name; i++) {
		memset (&a, 0, sizeof(a));
		strncpy (a.model, models[i].name,32);
		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)
{
	if (!camera->pl->init_done)
		digi_init (camera->port, camera->pl);
	snprintf (summary->text, 100,
			("Your USB camera seems to have an SQ905C chipset.\n"
			"The total number of pictures in it is %i\n"),
				camera->pl->nb_entries);
	return GP_OK;
}

static int camera_manual(Camera *camera, CameraText *manual,
							GPContext *context)
{
	strncpy(manual->text,
	_(
	"For cameras with insides from S&Q Technologies, which have the \n"
	"USB Vendor ID 0x2770 and Product ID 0x905C, 0x9050, 0x9051,\n"
	"0x9052, or 0x913D.  Photos are saved in PPM format.\n\n"
	"Some of these cameras allow software deletion of all photos.\n"
	"Others do not. No supported camera can do capture-image. All\n"
	"can do capture-preview (image captured and sent to computer).\n"
	"If delete-all does work for your camera, then capture-preview will\n"
	"have the side-effect that it also deletes what is on the camera.\n\n"
	"File uploading is not supported for these cameras. Also, none of the\n"
	"supported cameras allow deletion of individual photos by use of a\n"
	"software command.\n"
	), 700);
	return (GP_OK);
}


static int
camera_about(Camera *camera, CameraText *about, GPContext *context)
{
	strncpy (about->text, _("sq905C generic driver\n"
			    "Theodore Kilgore <kilgota@auburn.edu>\n"),64);
	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 n;
	if(!camera->pl->init_done)
		digi_init (camera->port, camera->pl);
	GP_DEBUG ("List files in %s\n", folder);
	n = camera->pl->nb_entries;
	gp_list_populate(list, "pict%03i.ppm", n);
	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)
{
	int status = GP_OK;
	Camera *camera = user_data;
	unsigned int b;
	unsigned int w, h;
	int k, next;
	unsigned char comp_ratio;
	unsigned char lighting;
	unsigned char *data = NULL;
	unsigned char *p_data = NULL;
	unsigned char *ppm;
	unsigned char *ptr;
	unsigned char gtable[256];
	int size;

	if (!camera->pl->init_done)
		digi_init (camera->port, camera->pl);

	/* Get the entry number of the photo on the camera */
	k = gp_filesystem_number (camera->fs, "/", filename, context);

	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;
	}

	next = camera->pl->last_fetched_entry +1;
	while (next < k) {
		b = digi_get_data_size (camera->pl, next);
		data = malloc(b);
		if(!data) return GP_ERROR_NO_MEMORY;
		digi_read_picture_data (camera->port, data, b, next);
		free(data);
		next ++;
	}

	comp_ratio = digi_get_comp_ratio (camera->pl, k);
	w = digi_get_picture_width (camera->pl, k);
	switch (w) {
	case 176: h = 144; break;
	case 640: h = 480; break;
	case 320: h = 240; break;
	default:  h = 288; break;
	}
	lighting = camera->pl->catalog[k*0x10+0x0b];
	b = digi_get_data_size (camera->pl, k);
	if (!b) {
		GP_DEBUG("Photo number %i deleted?\n",k+1);
		camera->pl->last_fetched_entry = k;
		return GP_OK;
	}
	if (b < w*h) {
		GP_DEBUG("need %d bytes, supposed to read only %d", w*h, b);
		return GP_ERROR;
	}
	data = malloc (b);
	if(!data) return GP_ERROR_NO_MEMORY;

	GP_DEBUG("Fetch entry %i\n", k);
	digi_read_picture_data (camera->port, data, b, k);
	camera->pl->last_fetched_entry = k;

	if (GP_FILE_TYPE_RAW == type) {	/* type is GP_FILE_TYPE_RAW */
		size = b;
		gp_file_set_mime_type (file, GP_MIME_RAW);
		gp_file_append(file, (char *)data, size);
		/* Save photo's catalog entry as a footer for the raw file */
		gp_file_append(file, (char *)camera->pl->catalog
						+ k * 0x10, 0x10);
		/* Reset camera when done, for more graceful exit. */
		if (k +1 == camera->pl->nb_entries) {
			digi_rewind (camera->port, camera->pl);
		}
		free(data);
		return(GP_OK);
	}

	/*
	 * Now put the data into a PPM image file.
	 */

	ppm = malloc (w * h * 3 + 256); /* room for data + header */
	if (!ppm) {
		status = GP_ERROR_NO_MEMORY;
		goto end;
	}
	snprintf ((char *)ppm, 64,
			"P6\n"
			"# CREATOR: gphoto2, SQ905C library\n"
			"%d %d\n"
			"255\n", w, h);
	size = strlen ((char *)ppm);
	ptr = ppm + size;
	size = size + (w * h * 3);
	GP_DEBUG ("size = %i\n", size);
	p_data = calloc(w , h);
	if (!p_data) {
		status =  GP_ERROR_NO_MEMORY;
		free (ppm);
		goto end;
	}
	if(comp_ratio) {
		digi_decompress (p_data, data, w, h);
	} else
		memcpy(p_data, data, w * h);
	GP_DEBUG("w %d, h %d, size %d", w, h, size);
	gp_ahd_decode(p_data, w , h , ptr, BAYER_TILE_BGGR);
	free(p_data);
	digi_postprocess(w, h, ptr);
	if (lighting < 0x40) {
	GP_DEBUG(
		"Low light condition. Using default gamma. \
						No white balance.\n");
		gp_gamma_fill_table (gtable, .65);
		gp_gamma_correct_single(gtable,ptr,w*h);
	} else
		white_balance (ptr, w*h, 1.1);
	gp_file_set_mime_type (file, GP_MIME_PPM);
	gp_file_set_data_and_size (file, (char *)ppm, size);
	/* Reset camera when done, for more graceful exit. */
	if (k + 1 == camera->pl->nb_entries) {
		digi_rewind (camera->port, camera->pl);
	}
 end:
	free(data);
	return status;
}

static int
delete_all_func(CameraFilesystem *fs, const char *folder, void *data,
						GPContext *context)
{
	Camera *camera = data;
	if (!camera->pl->delete_all)
		return GP_ERROR_NOT_SUPPORTED;
	if (!camera->pl->init_done)
		digi_init(camera->port, camera->pl);
	digi_delete_all(camera->port, camera->pl);
	return GP_OK;
}

static int
camera_capture_preview(Camera *camera, CameraFile *file, GPContext *context)

{
	unsigned char get_size[0x50];
	unsigned char *raw_data;
	unsigned char *frame_data;
	unsigned char *ppm, *ptr;
	char lighting;
	unsigned char gtable[256];
	int size;
	int w = 320;
	int h = 240;
	int b;

	digi_reset (camera->port);
	gp_port_usb_msg_write (camera->port, 0x0c, 0x1440, 0x110f, NULL, 0);
	gp_port_read(camera->port, (char *)get_size, 0x50);
	GP_DEBUG("get_size[0x40] = 0x%x\n", get_size[0x40]);
	lighting = get_size[0x48];
        b = get_size[0x40] | get_size[0x41] << 8 | get_size[0x42] << 16
						| get_size[0x43]<<24;
	GP_DEBUG("b = 0x%x\n", b);
	raw_data = malloc(b);

	if(!raw_data)
		return GP_ERROR_NO_MEMORY;

	if (!((gp_port_read(camera->port, (char *)raw_data, b)==b))) {
		free(raw_data);
		GP_DEBUG("Error in reading data\n");
		return GP_ERROR;
	}
	frame_data = calloc(w , h);
	if (!frame_data) {
		free(raw_data);
		return GP_ERROR_NO_MEMORY;
	}
	digi_decompress (frame_data, raw_data, w, h);
	free(raw_data);
	/* Now put the data into a PPM image file. */
	ppm = malloc (w * h * 3 + 256);
	if (!ppm) {
		free(frame_data);
		return GP_ERROR_NO_MEMORY;
	}
	snprintf ((char *)ppm, 64,
		"P6\n"
		"# CREATOR: gphoto2, SQ905C 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);
	gp_ahd_decode (frame_data, w , h , ptr, BAYER_TILE_BGGR);
	free(frame_data);
        if (lighting < 0x40) {
		GP_DEBUG(
		"Low light condition. Default gamma. No white balance.\n");
		gp_gamma_fill_table (gtable, .65);
		gp_gamma_correct_single(gtable,ptr,w*h);
	} else
		white_balance(ptr, w * h, 1.1);
	gp_file_set_mime_type(file, GP_MIME_PPM);
	gp_file_set_data_and_size(file, (char *)ppm, size);
	digi_reset(camera->port);
	return(GP_OK);
}

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

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

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

static CameraFilesystemFuncs fsfuncs = {
	.file_list_func = file_list_func,
	.get_file_func = get_file_func,
	.delete_all_func = delete_all_func
};

int
camera_init(Camera *camera, GPContext *context)
{
	GPPortSettings settings;
	CameraAbilities abilities;
	int ret = 0;

	ret = gp_camera_get_abilities(camera,&abilities);
	if (ret < 0) return ret;
	GP_DEBUG("product number is 0x%x\n", abilities.usb_product);

	/* Now, 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->catalog = NULL;
	camera->pl->nb_entries = 0;
	switch (abilities.usb_product) {
	case 0x9050:
	case 0x9051:
	case 0x9052:
		camera->pl->delete_all = 1;
		break;
	default:
		camera->pl->delete_all = 0;
	}
	camera->pl->init_done=0;

	/* Do digi_init() only if needed for the requested operation. */

	return GP_OK;
}