summaryrefslogtreecommitdiff
path: root/camlibs/jl2005c/library.c
blob: 257663c7fb21d4ee9f47e51fb78e02da855566e5 (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
/* library.c
 *
 * Copyright (C) 2006 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

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

#include <bayer.h>
#include <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 "jl2005c.h"
#include <gphoto2/gphoto2-port.h>

#define GP_MODULE "jl2005c"

struct {
   	char *name;
	CameraDriverStatus status;
   	unsigned short idVendor;
   	unsigned short idProduct;
} models[] = {
        {"Argus DC1512e", GP_DRIVER_STATUS_EXPERIMENTAL, 0x0979, 0x0227},
        {"Amazing Spiderman",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x0979, 0x0227},
        {"Sakar no. 75379",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x0979, 0x0227},
        {"Sakar Kidz-Cam no. 88379",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x0979, 0x0227},
        {"Sakar clipshot no. 1169x",     GP_DRIVER_STATUS_EXPERIMENTAL, 0x0979, 0x0227},
	{NULL,0,0,0}
};

int
camera_id (CameraText *id)
{
    	strcpy (id->text, "Argus DC1512e");
    	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_IMAGE;
       		a.folder_operations = GP_FOLDER_OPERATION_NONE;
       		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)
{
	int num_pics;
	num_pics = camera->pl->nb_entries;
	GP_DEBUG("camera->pl->nb_entries = %i\n",camera->pl->nb_entries);
    	sprintf (summary->text,_("This camera contains a Jeilin JL2005%c chipset.\n" 
			"The number of photos in it is %i. \n"), 
			camera->pl->model, num_pics);  
    	return GP_OK;
}


static int camera_manual (Camera *camera, CameraText *manual, GPContext *context) 
{
	strcpy(manual->text, 
	_(
        "This driver supports cameras with Jeilin jl3005c chip \n"
	"These cameras do not support deletion of photos, nor uploading\n"
	"of data. \n"
	"Decoding of compressed photos may or may not work well\n" 
	"and does not work equally well for all supported cameras.\n"
	"Photo data processing for Argus QuickClix is NOT SUPPORTED.\n"
	"If present on the camera, video clip frames are downloaded \n"
	"as consecutive still photos.\n"
	"For further details please consult libgphoto2/camlibs/README.jl2005c\n"
	)); 

	return (GP_OK);
}


static int
camera_about (Camera *camera, CameraText *about, GPContext *context)
{
    	strcpy (about->text, _("jl2005c camera library\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 n;
	n = camera->pl->nb_entries;
    	gp_list_populate (list, "jl_%03i.ppm", n);
    	return GP_OK;
}

static int
get_info_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileInfo *info, void *data, GPContext *context)
{
	info->file.fields = GP_FILE_INFO_TYPE;
	strcpy (info->file.type, GP_MIME_PPM);

	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 w, h = 0, b = 0, k;
	unsigned char *pic_data, *pic_buffer; 
	int HEADERSIZE=16;
	unsigned char compressed;
	unsigned long start_of_photo;
	unsigned int blocksize = 0;
	int filled = 0;

	GP_DEBUG ("Downloading pictures!\n");
	/* These are cheap cameras. There ain't no EXIF data. */
	if (GP_FILE_TYPE_EXIF == type) return GP_ERROR_FILE_EXISTS;

	/* Get the number of the photo on the camera */
	k = gp_filesystem_number (camera->fs, "/", filename, context); 
	/* Determine the "compression" setting from the PAT table */
	compressed = (camera->pl->info[48+16*k+2]>>4) & 0x0f;
	h = camera->pl->info[48+16*k+4];
	w = camera->pl->info[48+16*k+5];

	GP_DEBUG ("height is %i\n", h);
    
	b = jl2005c_get_pic_data_size(camera->pl, camera->pl->info, k);
	GP_DEBUG("b = %i = 0x%x bytes\n", b,b);
	start_of_photo = jl2005c_get_start_of_photo(camera->pl, 
						    camera->pl->info, k);
	GP_DEBUG("start_of_photo number %i = 0x%lx \n", k,start_of_photo);
	pic_buffer = malloc (b+16);
	if (!pic_buffer) return GP_ERROR_NO_MEMORY;
	memset (pic_buffer, 0, b+16);
	GP_DEBUG ("buffersize b+16 = %i = 0x%x bytes\n", b+16,b+16); 
	/* copy info line for photo from allocation table, as header */
	memcpy(pic_buffer, camera->pl->info+48+16*k, 16);
	/* Camera can download in blocks of 0xfa00, with last block possibly short. 
	 * So first we set up a cache (if not done already) to hold raw data.
	 */
	pic_data = pic_buffer+HEADERSIZE;
	if (! camera->pl->data_cache ) {
		camera->pl->data_cache = malloc (0xfa00);
	}
	if (! camera->pl->data_cache ) {
		GP_DEBUG ("no cache memory allocated!\n");
		return GP_ERROR_NO_MEMORY;
	}
	
	/* Is there data in the cache, or not? If yes, read from it into the 
	 * current photo, immediately. Update settings. But first a sanity check. 
	 */

	if (start_of_photo < camera->pl->bytes_put_away) {
		GP_DEBUG("photo number %i starts in a funny place!\n",k);
//		camera->pl->bytes_put_away=start_of_photo;
//		memset(camera->pl->data_cache, 0, 0xfa00);
		jl2005c_reset(camera, camera->port);
		jl2005c_rewind (camera, camera->port);
		camera->pl->bytes_read_from_camera=0;
//		return (GP_ERROR);
	}
	if (start_of_photo+b > camera->pl->total_data_in_camera) {
		GP_DEBUG("photo number %i ends in a funny place!\n",k);
		GP_DEBUG("Allocation unit size may be wrong for this camera\n");
		return (GP_ERROR);
	}

	/* This while loop is entered if the photo number k-1 was not requested 
	 * and thus has not been downloaded. The camera's rudimentary hardware 
	 * obliges us to download the corresponding data anyway and toss it. 
	 */
	while (camera->pl->bytes_read_from_camera <= start_of_photo) {

		camera->pl->data_to_read = camera->pl->total_data_in_camera
			    - camera->pl->bytes_read_from_camera;
		blocksize = 0xfa00;
		if (camera->pl->data_to_read < blocksize)
			blocksize = camera->pl->data_to_read; 
		GP_DEBUG("blocksize = 0x%x\n", blocksize);
		if(blocksize)
			jl2005c_get_picture_data (
					    camera->port, 
					    (char *) camera->pl->data_cache, 
					    blocksize);
		camera->pl->bytes_read_from_camera += blocksize;
	}

	camera->pl->bytes_put_away=start_of_photo;

	if (camera->pl->bytes_read_from_camera > start_of_photo) {
		if(start_of_photo + b <= camera->pl->bytes_read_from_camera) {
			memcpy(pic_data, camera->pl->data_cache
			    + (start_of_photo%0xfa00)
			    , b);
			camera->pl->bytes_put_away += b;
			/* Photo data is contained in what is already downloaded. 
			 * Jump immediately to process the photo. 
		         */
		} else {
			/* photo starts in one block and ends in another */
			filled = camera->pl->bytes_read_from_camera 
				    - start_of_photo;

			memcpy(pic_data, camera->pl->data_cache
				    + (start_of_photo%0xfa00), 
				    filled);

			camera->pl->bytes_put_away += filled;
		}
	}
	while (camera->pl->bytes_put_away < start_of_photo + b ) {

		camera->pl->data_to_read = camera->pl->total_data_in_camera
			    - camera->pl->bytes_read_from_camera;
		blocksize = 0xfa00;
		if (camera->pl->data_to_read < blocksize)
			blocksize = camera->pl->data_to_read; 
		GP_DEBUG("blocksize = 0x%x\n", blocksize);
		if(blocksize)
			jl2005c_get_picture_data (
					    camera->port, 
					    (char *) camera->pl->data_cache, 
					    blocksize);
		camera->pl->bytes_read_from_camera += blocksize;
		
		if (camera->pl->bytes_read_from_camera >= start_of_photo + b ) {
			GP_DEBUG("THIS ONE?\n");
			memcpy(pic_data+filled, camera->pl->data_cache, 
						b - filled);
			camera->pl->bytes_put_away += b - filled;
			break;
		} else {	
			GP_DEBUG("THIS ONE??\n");
			if (!blocksize)
				break;
			memcpy(pic_data+filled, camera->pl->data_cache, blocksize);
			camera->pl->bytes_put_away += blocksize;
			filled += blocksize;
		}
	}

	if (GP_FILE_TYPE_RAW == type) {
		gp_file_set_mime_type(file, GP_MIME_RAW);
		gp_file_set_name(file, filename);
		gp_file_set_data_and_size(file, (char *)pic_buffer , b+16 );
		return GP_OK;
	} else return GP_ERROR_NOT_SUPPORTED;
	
	

        return GP_OK;
}






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

static int
camera_exit (Camera *camera, GPContext *context)
{
//	int blocksize = 0xfa00;
	GP_DEBUG ("jl2005c camera_exit");
	/* These cameras want all data to be dumped. If that is not yet done,
	 * then do it now, before exiting! */
/*	while (camera->pl->bytes_read_from_camera < 
				    camera->pl->total_data_in_camera ) {
		if (! camera->pl->data_cache )
			camera->pl->data_cache = malloc (0xfa00);
		blocksize=0xfa00;
		if (camera->pl->bytes_read_from_camera +0xfa00 >=
				    camera->pl->total_data_in_camera ) 
			blocksize = camera->pl->total_data_in_camera -
					camera->pl->bytes_read_from_camera;
		if(blocksize) 
			jl2005c_get_picture_data (
					    camera->port, 
					    (char *) camera->pl->data_cache, 
					    blocksize);
		camera->pl->bytes_read_from_camera 
						+= blocksize;
		
	}
*/
	jl2005c_reset(camera, camera->port);
	gp_port_close(camera->port);
	if (camera->pl) {
		free (camera->pl);
		camera->pl = NULL;
	}
	return GP_OK;
}

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

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

	/* First, set up all the function pointers */
        camera->functions->manual	= camera_manual;
	camera->functions->summary      = camera_summary;
	camera->functions->about        = camera_about;
	camera->functions->exit	    = camera_exit;
   
	GP_DEBUG ("Initializing the camera\n");
	ret = gp_port_get_settings(camera->port,&settings);
	if (ret < 0) return ret; 

	switch (camera->port->type) {
		case GP_PORT_SERIAL:
			return ( GP_ERROR );
		case GP_PORT_USB:
			settings.usb.config = 1;
			settings.usb.altsetting = 0;
			settings.usb.interface = 0;
		/* inep 0x84 used to initialize. Then switch to 0x82! */
			settings.usb.inep = 0x84;
			settings.usb.outep = 0x03;
			break;
		default:
			return ( GP_ERROR );
	}

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

	GP_DEBUG("interface = %i\n", settings.usb.interface);
	GP_DEBUG("inep = %x\n", settings.usb.inep);	
	GP_DEBUG("outep = %x\n", settings.usb.outep);

        /* 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;
	memset (camera->pl, 0, sizeof (CameraPrivateLibrary));
	/* Connect to the camera */
	camera->pl->bytes_read_from_camera = 0;
	camera->pl->total_data_in_camera=0;
	camera->pl->data_to_read = 0;
	camera->pl->data_used_from_block = 0;
	camera->pl->bytes_put_away = 0;
	camera->pl->data_cache = NULL;
	jl2005c_init (camera,camera->port, camera->pl);

	return GP_OK;
}