summaryrefslogtreecommitdiff
path: root/camlibs/polaroid/pdc320.c
blob: 61b7421471fac1a269eb232a61280c90c9594e5d (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
/* pdc320.c
 *
 * Copyright 2001 Lutz Mueller
 *
 * 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
 */

/* Originally written by Peter Desnoyers <pjd@fred.cambridge.ma.us>,
 * and adapted for gphoto2 by
 * Nathan Stenzel <nathanstenzel@users.sourceforge.net> and
 * Lutz Mueller <lutz@users.sourceforge.net>
 *
 * Maintained by Nathan Stenzel <nathanstenzel@users.sourceforge.net>
 *
 * Original windows drivers available at:
 *	http://www.polaroid.com/service/software/index.html
 */

#define _DEFAULT_SOURCE

#include "config.h"

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

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

#include "libgphoto2/i18n.h"

#include "pdc320.h"


#define GP_MODULE "pdc320"

/*******************************************************************************/
/* NOTICE: There is a 16x8 block of pixels after the image data.               */
/* All of them are about the same and I believe they are for image correction. */
/*******************************************************************************/

/* Notes on what needs work:
 * 	Make a better header that could correct color problems....
 *		or manage to decode, color correct, and save the image.
 * The quantization tables are incorrect for the PDC320.
 */

/*
 * For the PDC640SE, the init sequence is INIT,ID,ENDINIT,STATE,NUM,SIZE,PIC.
 *
 * During handshake, if the first read byte is not 6 then it is not good. Read
 * the number of bytes mentioned +2 ("FE FF"?)and then INIT and try SIZE
 * again (this is a loop).
 *
 * It is possible that the PDC320 might do a similar handshaking sequence at
 * that same point.
 */

typedef enum {
	PDC320,
	PDC640SE
} PDCModel;

struct _CameraPrivateLibrary {
	PDCModel model;
};

static int
pdc320_escape(
	const unsigned char *inbuf, int inbuflen,
	unsigned char *outbuf
) {
	int i,j;

	j = 0;
	for (i=0;i<inbuflen;i++) {
		switch (inbuf[i]) {
		case 0xe3:
			outbuf[j++] = 0xe5;outbuf[j++] = 3;
			break;
		case 0xe4:
			outbuf[j++] = 0xe5;outbuf[j++] = 2;
			break;
		case 0xe5:
			outbuf[j++] = 0xe5;outbuf[j++] = 1;
			break;
		case 0xe6:
			outbuf[j++] = 0xe5;outbuf[j++] = 0;
			break;
		default:outbuf[j++] = inbuf[i];
			break;
		}
	}
	return j;
}

static int
pdc320_calc_checksum(const unsigned char *buf, int buflen) {
	int checksum, j;

	checksum = 0;
	j = 0;
	for (j=0;j<buflen/2;j++) {
		checksum += buf[j*2];
		checksum += buf[j*2+1]<<8;
	}
	if (buflen & 1)
		checksum += buf[buflen-1];

	while (checksum > 0xffff) {
		checksum =
			(checksum & 0xffff) +
			((checksum >> 16) & 0xffff);
	}
	return 0xffff-checksum; /* neg checksum actually */
}

static int
pdc320_command(
	GPPort *port,
	const unsigned char *cmd, int cmdlen
) {
	unsigned char csum[2];
	int checksum, off, ret;
	unsigned char *newcmd;

	checksum = pdc320_calc_checksum (cmd, cmdlen);
	csum[0] = checksum & 0xff;
	csum[1] = (checksum >> 8) & 0xff;
	/* 4 times 0xe6, checksum and command, both might be escaped */
	newcmd = malloc (2*(sizeof(csum)+cmdlen)+4);
	if (!newcmd)
		return GP_ERROR_NO_MEMORY;
	memset (newcmd, 0xe6, 4); off = 4;
	off += pdc320_escape (cmd, cmdlen, newcmd + off);
	off += pdc320_escape (csum, 2, newcmd + off);
	ret = gp_port_write (port, (char *)newcmd, off);
	free(newcmd);
	return ret;
}

static int
pdc320_simple_command (GPPort *port, const unsigned char cmd) {
	return pdc320_command(port, &cmd, 1);
}

static int
pdc320_simple_reply (GPPort *port, const unsigned char expcode,
	unsigned int replysize, unsigned char *reply
) {
	unsigned char csum[2];
	int calccsum;

	CR (gp_port_read (port, (char *)reply, 1));
	if (reply[0] != expcode) {
		GP_DEBUG("*** reply got 0x%02x, expected 0x%02x\n",
			reply[0], expcode
		);
		return GP_ERROR;
	}
	CR (gp_port_read (port, (char *)reply+1, replysize-1));
	CR (gp_port_read (port, (char *)csum, 2));
	calccsum = pdc320_calc_checksum (reply, replysize);
	if (calccsum != ((csum[1] << 8) | csum[0])) {
		GP_DEBUG("csum %x vs %x\n",calccsum,((csum[0]<<8)|csum[1]));
		return GP_ERROR;
	}
	return GP_OK;
}

static int
pdc320_simple_command_reply (GPPort *port,
	const unsigned char cmd, const unsigned char expcode,
	unsigned int replysize, unsigned char *reply
) {
	CR (pdc320_simple_command (port, cmd));
	CR (pdc320_simple_reply (port, expcode, replysize, reply));
	return GP_OK;
}

static int
pdc320_init (GPPort *port)
{
	unsigned char buf[32];
	unsigned char e6[4];
	int i;

	GP_DEBUG ("*** PDC320_INIT ***");

	/* The initial command is prefixed by 4 raw E6. */
	memset(e6,0xe6,sizeof(e6));
	CR (gp_port_write (port, (char *)e6, sizeof (e6) ));

	GP_DEBUG ("*** PDC320_INIT ***");
	CR (pdc320_simple_command_reply (port, PDC320_INIT, 5, 1, buf));
	GP_DEBUG ("*** PDC320_ID ***");
	CR (pdc320_simple_command_reply (port, PDC320_ID, 0, 12, buf));
	GP_DEBUG ("*** PDC320_STATE ***");
	CR (pdc320_simple_command_reply (port, PDC320_STATE, 2, 22, buf));

	for (i=0;i<9;i++) {
		GP_DEBUG ("%d: %d (0x%x)",i,((buf[2+i*2]<<8)|buf[2+2*i+1]), ((buf[2+i*2]<<8)|buf[2+2*i+1]));
	}
	GP_DEBUG ("*** PDC320_ENDINIT ***");
	return pdc320_simple_command_reply (port, PDC320_ENDINIT, 9, 1, buf);
}

static int
pdc320_num (GPPort *port)
{
	unsigned char buf[2];

	GP_DEBUG ("*** PDC320_NUM ***");
	CR (pdc320_simple_command_reply (port, PDC320_NUM, 3, 2, buf));
	GP_DEBUG ("The camera contains %i files.", buf[1]);
	return buf[1];
}

static int
pdc320_delete (GPPort *port)
{
	unsigned char buf[1];

	GP_DEBUG ("*** PDC320_DELETE ***");
	return pdc320_simple_command_reply(port, PDC320_DEL, 8, 1, buf);
}

static int
pdc320_size (Camera *camera, int n)
{
	int size;
	unsigned char buf[5];
	unsigned char cmd[2];

	GP_DEBUG ("*** PDC320_SIZE ***");
	cmd[0] = PDC320_SIZE;
	cmd[1] = n;
	CR (pdc320_command (camera->port, cmd, sizeof (cmd)));
	CR (pdc320_simple_reply (camera->port, 6, 5, buf));
	size = (buf[1] << 24) + (buf[2] << 16) + (buf[3] << 8) + buf[4];
	GP_DEBUG ("Image %i has size %i.", n, size);
	return (size);
}

/* Unclear. */
static int
pdc320_0c (Camera *camera, int n)
{
	int size, i;
	unsigned char buf[3], *xbuf;
	unsigned char cmd[2];

	cmd[0] = 0x0c;
	cmd[1] = n;		/* n is from 1 on up */

	/* Write the command */
	GP_DEBUG ("*** PDC320_0c ***");
	CR (pdc320_command (camera->port, cmd, sizeof (cmd)));
	CR (gp_port_read (camera->port, (char *)buf, 3));
	if (buf[0] != 7)
		return GP_ERROR;
	size = (buf[1] << 8) | buf[2];
	xbuf = malloc (size);
	CR (gp_port_read (camera->port, (char *)xbuf, size));
	for (i=0; i<size; i++) {
		GP_DEBUG ("buf[%d]=0x%02x", i, xbuf[i]);
	}
	CR (gp_port_read (camera->port, (char *)buf, 2));
	/* checksum is calculated from both, but i am not clear how. */
	free (xbuf);
	return GP_OK;
}

static int
pdc320_pic (Camera *camera, int n, unsigned char **data, int *size)
{
	unsigned char cmd[2];
	unsigned char buf[2048];
	int remaining, f1, f2, i, len;
	int chunksize=2000;

	/* Get the size of the picture and allocate the memory */
	GP_DEBUG ("Checking size of image %i...", n);
	CR (*size = pdc320_size (camera, n));
	*data = malloc (sizeof (char) * (*size));
	if (!*data)
		return (GP_ERROR_NO_MEMORY);

	cmd[0] = PDC320_PIC;
	cmd[1] = n;

	CR_FREE (pdc320_command (camera->port, cmd, sizeof (cmd)), *data);
	switch (camera->pl->model) {
	case PDC640SE:
		chunksize = 528;
		break;
	case PDC320:
		chunksize = 2000;
		break;
	}

	len = *size;
	for (i = 0; i < *size; i += chunksize) {

		/* How many bytes do we read in this round? */
		remaining = *size - i;
		len = (remaining > chunksize) ? chunksize : remaining;

		/* Read the frame number */
		usleep (10000);
		CR_FREE (gp_port_read (camera->port, (char *)buf, 5), *data);
		f1 = (buf[1] << 8) + buf[2];
		f2 = (buf[3] << 8) + buf[4];
		GP_DEBUG ("Reading frame %d "
				 "(%d)...", f1, f2);

		/* Read the actual data */
		usleep(1000);
		CR_FREE (gp_port_read (camera->port, (char *)*data + i, len), *data);

		/* Read the checksum */
		CR_FREE (gp_port_read (camera->port, (char *)buf, 2), *data);
	}

	return (GP_OK);
}

int
camera_id (CameraText *id)
{
	strcpy (id->text, "Polaroid DC320");

	return (GP_OK);
}

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

	for (i = 0; models[i].model; i++) {
		memset(&a, 0, sizeof(a));
		strcpy (a.model, models[i].model);
		a.status		= GP_DRIVER_STATUS_EXPERIMENTAL;
		a.port			= GP_PORT_SERIAL;
		a.speed[0]		= 115200;
		a.speed[1]		= 0;
		a.operations		= GP_OPERATION_NONE;
		a.file_operations	= GP_FILE_OPERATION_NONE;
		a.folder_operations	= GP_FOLDER_OPERATION_DELETE_ALL;
		CR (gp_abilities_list_append (list, a));
	}
	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;
	jpeg *myjpeg;
	chunk *tempchunk;
	int r, n, size, width, height;
	unsigned char *data;

	if ((type != GP_FILE_TYPE_RAW) && (type != GP_FILE_TYPE_NORMAL))
		return (GP_ERROR_NOT_SUPPORTED);

	/*
	 * Get the number of the picture from the filesystem and increment
	 * since we need a range starting with 1.
	 */
	GP_DEBUG ("Getting number from fs...");
	CR (n = gp_filesystem_number (camera->fs, folder, filename, context));
	n++;

	/* Get the file */
	GP_DEBUG ("Getting file %i...", n);
	CR (pdc320_pic (camera, n, &data, &size));
	r = pdc320_0c (camera, n);
	if (r < GP_OK) {
		free (data);
		return r;
	}

	/* Post-processing */
	switch (type) {
	case GP_FILE_TYPE_RAW:
		CR (gp_file_set_data_and_size (file, (char *)data, size));
		CR (gp_file_set_mime_type (file, GP_MIME_RAW));
		break;
	case GP_FILE_TYPE_NORMAL:
	default:
		GP_DEBUG ("Using Nathan Stenzel's experimental jpeg.c\n");
		GP_DEBUG ("About to make jpeg header\n");
		width = data[4]*256 + data[5];
		height = data[2]*256 + data[3];
		GP_DEBUG ("Width=%i\tHeight=%i\n", width, height);
		myjpeg = gpi_jpeg_header(width,height/2, 0x11,0x11,0x21, 1,0,0, &chrominance, &luminance,
		    0,0,0, gpi_jpeg_chunk_new_filled(HUFF_00),
		    gpi_jpeg_chunk_new_filled(HUFF_10), NULL, NULL);
		GP_DEBUG ("Turning the picture data into a chunk data type\n");
		tempchunk = gpi_jpeg_chunk_new(size);
		tempchunk->data = data;
		GP_DEBUG ("Adding the picture data to the jpeg structure\n");
		gpi_jpeg_add_marker(myjpeg, tempchunk, 6, size-1);
		GP_DEBUG ("Writing the jpeg file\n");
		gpi_jpeg_write(file, filename, myjpeg);
		GP_DEBUG ("Cleaning up the mess\n");
		gpi_jpeg_destroy(myjpeg);
		free (tempchunk);
		break;
    	}
	return (GP_OK);
}

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

	CR (pdc320_delete (camera->port));
	return (GP_OK);
}

static int
camera_about (Camera *camera, CameraText *about, GPContext *context)
{
	strcpy (about->text, _("Download program for several Polaroid cameras. "
		"Originally written by Peter Desnoyers "
		"<pjd@fred.cambridge.ma.us>, and adapted for gphoto2 by "
		"Nathan Stenzel <nathanstenzel@users.sourceforge.net> and "
		"Lutz Mueller <lutz@users.sf.net>.\n"
		"Polaroid 640SE testing was done by Michael Golden "
		"<naugrim@juno.com>."));
	return (GP_OK);
}

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

	/* Fill the list */
	CR (n = pdc320_num (camera->port));
	gp_list_populate (list, "PDC320%04i.jpg", n);
	return (GP_OK);
}

static int
camera_summary (Camera *camera, CameraText *summary, GPContext *context)
{
	char buf[12];

	GP_DEBUG ("*** PDC320_ID ***");
	CR (pdc320_simple_command_reply (camera->port, PDC320_ID, 0, 12, (unsigned char *)buf));
	sprintf (summary->text, _("Model: %x, %x, %x, %x"), buf[8],buf[9],buf[10],buf[11]);
	return (GP_OK);
}

static int
camera_exit (Camera *camera, GPContext *context)
{
	if (!camera)
		return (GP_ERROR_BAD_PARAMETERS);

	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,
	.delete_all_func = delete_all_func
};

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

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

	/* Now, tell the filesystem where to get lists and info */
	gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);

	camera->pl = malloc (sizeof (CameraPrivateLibrary));
	if (!camera->pl)
		return (GP_ERROR_NO_MEMORY);

	/* What model are we talking to? */
	gp_camera_get_abilities (camera, &abilities);
	if (	!strcmp (abilities.model, "Polaroid:Fun! 320") ||
		!strcmp (abilities.model, "Polaroid Fun! 320")
	)
		camera->pl->model = PDC320;
	else if (!strcmp (abilities.model, "Polaroid:640SE") ||
		 !strcmp (abilities.model, "Polaroid 640SE")
	)
		camera->pl->model = PDC640SE;
	else {
		free (camera->pl);
		camera->pl = NULL;
		return (GP_ERROR_MODEL_NOT_FOUND);
	}

	/* Open the port and check if the camera is there */
	gp_port_get_settings (camera->port, &settings);
	if (!settings.serial.speed)
		settings.serial.speed = 115200;
	gp_port_set_settings (camera->port, settings);
	gp_port_set_timeout (camera->port, 30000);

	/* Check if the camera is really there */
	result = pdc320_init (camera->port);
	if (result < 0) {
		free (camera->pl);
		camera->pl = NULL;
		return (result);
	}
	return (GP_OK);
}