summaryrefslogtreecommitdiff
path: root/camlibs/sq905/sq905.c
blob: 16ece4a6f93de98fdc661320d0b7eeea289d9758 (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
/*
 * sq905.c
 *
 * Here, the functions which are needed to get data from the camera.  
 *
 * Copyright (c) 2003 Theodore Kilgore <kilgota@auburn.edu>
 * Camera library support under libgphoto2.1.1 for camera(s) 
 * with chipset from Service & Quality Technologies, Taiwan. 
 * The chip supported by this driver is presumed to be the SQ905,  
 *
 * Licensed under GNU Lesser General Public License, as part of Gphoto
 * camera support project. For a copy of the license, see the file 
 * COPYING in the main source tree of libgphoto2.
 */    

#include <config.h>


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#ifdef OS2
#include <db.h>
#endif

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

#define GP_MODULE "sq905" 

#define SQWRITE gp_port_usb_msg_write
#define SQREAD  gp_port_usb_msg_read

#define zero 	"\x0"

static int
sq_read_data (GPPort *port, unsigned char *data, int size)
{
	SQWRITE (port, 0x0c, 0x03, size, zero, 1); 
	gp_port_read (port, (char *)data, size); 
	return GP_OK;
}


int sq_access_reg (GPPort *port, int reg) 
{
	char c;
	SQWRITE (port, 0x0c, 0x06, reg, zero, 1);	/* Access a register */
	SQREAD (port, 0x0c, 0x07, 0x00, &c, 1);
	return GP_OK;
}

int 
sq_init (GPPort *port, CameraPrivateLibrary *priv)
{
	unsigned char c[0x4];
	int i;
	unsigned char *catalog = malloc(0x4000);
	unsigned char *catalog_tmp;
	if (!catalog) return GP_ERROR_NO_MEMORY;

	sq_reset (port);
    	sq_access_reg(port, ID);	/* Access model or chip id */

	sq_read_data (port, c, 4);
	sq_reset (port);
	if (!memcmp (c, "\x09\x05\x01\x19", 4)) {
		priv->model = SQ_MODEL_POCK_CAM;
	} else if (!memcmp (c, "\x09\x05\x01\x32", 4)) {
		priv->model = SQ_MODEL_MAGPIX;
/*
	} else if (!memcmp (c, "\x09\x13\x06\x67", 4)) {
		priv->model = SQ_MODEL_913c;
*/
/*
	} else if (!memcmp (c, "\x50\x05\x00\x26", 4)) {
		priv->model = SQ_MODEL_PRECISION;
*/
	} else {
		priv->model = SQ_MODEL_DEFAULT;
	}

	sq_access_reg(port, CONFIG);	     /* Access config */
	sq_read_data(port, catalog, 0x4000); /* We need 16 bytes for each photo. */
	sq_reset (port);

	/* The first occurence of a zero denotes end of files entries (here clips count as 1 entry) */
	for (i=0; i<0x4000 && catalog[i]; i+=16) ;
	priv->nb_entries = i>>4;
	if (i) {
		catalog_tmp = realloc(catalog, i);
		if (catalog_tmp)
			priv->catalog = catalog_tmp;
		else
			priv->catalog = catalog;
	} else {
		free (catalog);
		priv->catalog = NULL;	/* We just have freed catalog_tmp */
	}

	sq_reset (port);

	priv->last_fetched_entry = -1;
	free(priv->last_fetched_data);
	priv->last_fetched_data = NULL;

	return GP_OK;
}

int
sq_get_num_frames (CameraPrivateLibrary *priv, int entry)
{
	if (sq_is_clip(priv, entry)) {
		GP_DEBUG(" Number of frames in clip %i is %i\n", entry, priv->catalog[16*entry+7]);	
		return priv->catalog[16*entry+7];
	} else {
		return 1;
	}
}


int
sq_get_comp_ratio (CameraPrivateLibrary *priv, int entry)
{
	switch (priv->catalog[16*entry]) {
	case 0x61:
	case 0x62:
	case 0x63:
	case 0x76: return 2;
	case 0x41:
	case 0x42:
	case 0x43:
	case 0x52:
	case 0x53:
	case 0x56: 
	case 0x72: return 1;
	default:
		GP_DEBUG ("Your camera has unknown resolution settings.\n");
			return 0;
	}
}

int
sq_get_picture_width (CameraPrivateLibrary *priv, int entry)
{
	switch (priv->catalog[16*entry]) {  
	case 0x41:
	case 0x52:
	case 0x61: return 352;
	case 0x42:
	case 0x62:
	case 0x72: return 176;
	case 0x43:
	case 0x53:
	case 0x63: return 320;
	case 0x56:
	case 0x76: return 640;
	default:
		GP_DEBUG ("Your pictures have unknown width.\n");
			return 0;
	}
}

int
sq_is_clip (CameraPrivateLibrary *priv, int entry)
{
	switch (priv->catalog[16*entry]) {  
	case 0x52:
	case 0x53:
	case 0x72: return 1;
	default:   return 0;
	}
}

int
sq_rewind (GPPort *port, CameraPrivateLibrary *priv)
{
	static unsigned char dummy_buf[0x4000];
	
	
	GP_DEBUG("REWIND cam's data pointer");

	/* One has to read the catalog to rewind the data stream...
	 * I don't know if it's by design. There ought to be something better to do... :-/
	 */


	sq_access_reg(port, CONFIG);		/* Access config */

	sq_read_data(port, dummy_buf, 0x4000);
	sq_reset (port);
	sq_access_reg(port, DATA);	/* Access photo data */

	priv->last_fetched_entry = -1;
	free(priv->last_fetched_data);
	priv->last_fetched_data = NULL;
	return GP_OK;
}

int
sq_reset (GPPort *port)
{
	sq_access_reg(port, CLEAR);	/* Release current register */

    	return GP_OK;
}

int
sq_read_picture_data (GPPort *port, unsigned char *data, int size )
{
	int remainder = size % 0x8000;
	int offset = 0;
	char c;

	while ((offset + 0x8000 < size)) {
		sq_read_data (port, data + offset, 0x8000);
		offset = offset + 0x8000;
	}
 	sq_read_data (port, data + offset, remainder);

    	SQWRITE (port, 0x0c, 0xc0, 0x00, &c, 1);  
    	return GP_OK;
} 

int
sq_delete_all (GPPort *port, CameraPrivateLibrary *priv)
{
	/* This will work on the Argus DC-1510, perhaps some others. 
	 * Will not successfully delete on all SQ905 cameras!
	 */  
	 
	switch (priv->catalog[2]) {
	case 0xd0: 				/* Searches for DC-1510 */
		sq_access_reg(port, CAPTURE);	/* used here to delete */

		sq_reset(port);
		break;
	default:
		return (GP_ERROR_NOT_SUPPORTED);	
	}
    	return GP_OK;
}

int 
sq_preprocess (SQModel model, int comp_ratio, unsigned char is_in_clip,  
				    unsigned char *data, int w, int h)
{
	int i, m, b;
	unsigned char temp;
	b=w*h/comp_ratio;

	GP_DEBUG("Running sq_preprocess\n");

	if (!is_in_clip) {
		/* Turning the picture right-side up. */
    		for (i = 0; i < b/2; ++i) {
        		temp = data[i];
        		data[i] = data[b -1 -i];
        		data[b - 1 - i] = temp;
    		}    	
		/* But clip frames are already right-side-up */
    	}
	/*
	 * POCK_CAM needs de-mirror-imaging, too. But if a photo is 
	 * compressed we de-mirror after decompression, so not here. 
	 */
	if ((model == SQ_MODEL_POCK_CAM) && (comp_ratio == 1)) {
    		for (i = 0; i < h; i++) {
			for (m = 0 ; m < w/2; m++) { 
        			temp = data[w*i +m];
				data[w*i + m] = data[w*i + w -1 -m];
				data[w*i + w - 1 - m] = temp;
			}
    		} 
	}
	return GP_OK;
}