Remove generated files
[framework/connectivity/libgphoto2.git] / camlibs / jamcam / jamcam.c
1 /****************************************************************/
2 /* jamcam.c - Gphoto2 library for the KBGear JamCam v2 and v3   */
3 /*                                                              */
4 /* Copyright © 2001 Chris Pinkham                             */
5 /*                                                              */
6 /* Author: Chris Pinkham <cpinkham@infi.net>                    */
7 /*                                                              */
8 /* This library is free software; you can redistribute it       */
9 /* and/or modify it under the terms of the GNU Library General  */
10 /* Public License as published by the Free Software Foundation; */
11 /* either version 2 of the License, or (at your option) any     */
12 /* later version.                                               */
13 /*                                                              */
14 /* This library is distributed in the hope that it will be      */
15 /* useful, but WITHOUT ANY WARRANTY; without even the implied   */
16 /* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      */
17 /* PURPOSE.  See the GNU Library General Public License for     */
18 /* more details.                                                */
19 /*                                                              */
20 /* You should have received a copy of the GNU Library General   */
21 /* Public License along with this library; if not, write to the */
22 /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
23 /* Boston, MA 02111-1307, USA.                                  */
24 /****************************************************************/
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <gphoto2/gphoto2.h>
33 #include <bayer.h>
34 #include <gamma.h>
35
36 #ifdef ENABLE_NLS
37 #  include <libintl.h>
38 #  undef _
39 #  define _(String) dgettext (GETTEXT_PACKAGE, String)
40 #  ifdef gettext_noop
41 #    define N_(String) gettext_noop (String)
42 #  else
43 #    define N_(String) (String)
44 #  endif
45 #else
46 #  define _(String) (String)
47 #  define N_(String) (String)
48 #endif
49
50 #include "library.h"
51
52 #define GP_MODULE "jamcam"
53 #define TIMEOUT       2000
54
55 #define JAMCAM_VERSION "0.6"
56 #define JAMCAM_LAST_MOD "11/28/2001 14:51 EST"
57
58 /* define what cameras we support */
59 static const struct {
60         char *model;
61         int usb_vendor;
62         int usb_product;
63 } models[] = {
64         { "KBGear:JamCam", 0x084E, 0x0001 },
65         { NULL, 0, 0 }
66 };
67
68
69 int camera_id (CameraText *id)
70 {
71         /* GP_DEBUG ("* camera_id"); */
72
73         strcpy (id->text, "jamcam");
74
75         return (GP_OK);
76 }
77
78 int camera_abilities (CameraAbilitiesList *list)
79 {
80         int x = 0;
81         char *ptr;
82         CameraAbilities a;
83
84         /* GP_DEBUG ("* camera_abilities"); */
85
86         ptr = models[x].model;
87         while (ptr) {
88                 memset(&a, 0, sizeof(a));
89                 strcpy (a.model, ptr );
90                 a.status = GP_DRIVER_STATUS_PRODUCTION;
91                 a.port     = GP_PORT_SERIAL | GP_PORT_USB;
92                 a.speed[0] = 57600;
93                 a.speed[1] = 0;
94
95                 /* fixme, need to set these operations lists to correct values */
96                 a.operations        =   GP_OPERATION_NONE;
97                 a.file_operations   =   GP_FILE_OPERATION_PREVIEW;
98
99                 a.folder_operations =   GP_FOLDER_OPERATION_NONE;
100
101                 a.usb_vendor = models[x].usb_vendor;
102                 a.usb_product = models[x].usb_product;
103
104                 gp_abilities_list_append (list, a);
105
106                 ptr = models[++x].model;
107         }
108
109         return (GP_OK);
110 }
111
112 static int file_list_func (CameraFilesystem *fs, const char *folder,
113                            CameraList *list, void *data, GPContext *context)
114 {
115         Camera *camera = data;
116         int count;
117
118         GP_DEBUG ("* file_list_func");
119         GP_DEBUG ("*** folder: %s", folder);
120
121         CHECK (count = jamcam_file_count (camera));
122         CHECK (gp_list_populate (list, "pic_%04i.ppm", count));
123
124         return (GP_OK);
125 }
126
127 static int get_info_func (CameraFilesystem *fs, const char *folder,
128                 const char *filename, CameraFileInfo *info, void *data,
129                 GPContext *context)
130 {
131         Camera *camera = data;
132         int n;
133         struct jamcam_file *jc_file;
134
135         GP_DEBUG ("* get_info_func");
136         GP_DEBUG ("*** folder: %s", folder);
137         GP_DEBUG ("*** filename: %s",filename);
138
139         /* Get the file number from the CameraFileSystem */
140         CHECK (n = gp_filesystem_number (camera->fs, folder,
141                                          filename, context));
142
143         jc_file = jamcam_file_info( camera, n );
144
145         /* fixme, get file size also */
146         info->file.fields = GP_FILE_INFO_TYPE;
147         strcpy (info->file.type, GP_MIME_PPM);
148         info->file.width = jc_file->width;
149         info->file.height = jc_file->height;
150
151         info->preview.fields = GP_FILE_INFO_TYPE;
152         strcpy (info->preview.type, GP_MIME_PPM);
153         info->preview.width = 80;
154         info->preview.height = 60;
155
156         return (GP_OK);
157 }
158
159 static int camera_exit (Camera *camera, GPContext *context)
160 {
161         GP_DEBUG ("* camera_exit");
162
163         jamcam_file_count (camera);
164         return (GP_OK);
165 }
166
167 #define CHECK_free(result) {            \
168         int res;                        \
169         res = result;                   \
170         if (res < 0)  {                 \
171                 free(raw); free(ppm);   \
172                 return (res);           \
173         }                               \
174 }
175
176 static int get_file_func (CameraFilesystem *fs, const char *folder,
177                           const char *filename, CameraFileType type,
178                           CameraFile *file, void *user_data, GPContext *context)
179 {
180         Camera *camera = user_data;
181         char *raw, *ppm;
182         char tmp_filename[128];
183         unsigned char gtable[256];
184         char *ptr;
185         int size = 0, n = 0;
186         int width, height;
187         struct jamcam_file *jc_file;
188
189         GP_DEBUG ("* camera_file_get");
190         GP_DEBUG ("*** folder: %s", folder);
191         GP_DEBUG ("*** filename: %s",filename);
192         GP_DEBUG ("*** type: %d", type);
193
194         CHECK (n = gp_filesystem_number (camera->fs, folder,
195                                          filename, context));
196
197         if (gp_context_cancel (context) == GP_CONTEXT_FEEDBACK_CANCEL)
198                 return (GP_ERROR_CANCEL);
199
200         raw = malloc(640*480 * 3);
201         ppm = malloc(640*480 * 3 + 200);
202         
203         switch (type) {
204         case GP_FILE_TYPE_PREVIEW:
205
206                 CHECK_free (jamcam_request_thumbnail (camera, file, raw, &size, n, context));
207
208
209                 width = 80;
210                 height = 60;
211
212                 sprintf( ppm,
213                         "P6\n"
214                         "# CREATOR: gphoto2, jamcam library\n"
215                         "%d %d\n"
216                         "255\n", width, height );
217
218                 ptr = ppm + strlen( ppm );
219
220                 size = strlen( ppm ) + ( height * width * 3 );
221
222                 gp_bayer_decode(raw, width, height, ptr, BAYER_TILE_GBRG );
223                 gp_gamma_fill_table( gtable, 0.5 );
224                 gp_gamma_correct_single( gtable, ptr, height * width );
225
226                 CHECK_free (gp_file_set_mime_type (file, GP_MIME_PPM));
227                 CHECK_free (gp_file_set_name (file, filename));
228                 CHECK_free (gp_file_append (file, ppm, size));
229                 break;
230
231         case GP_FILE_TYPE_NORMAL:
232                 CHECK_free (jamcam_request_image (camera, file, raw, &size, n, context));
233
234                 jc_file = jamcam_file_info (camera, n);
235
236                 sprintf( ppm,
237                         "P6\n"
238                         "# CREATOR: gphoto2, jamcam library\n"
239                         "%d %d\n"
240                         "255\n", jc_file->width, jc_file->height );
241
242                 ptr = ppm + strlen( ppm );
243
244                 size = strlen( ppm ) + ( jc_file->width * jc_file->height * 3 );
245
246                 gp_bayer_decode( raw, jc_file->width, jc_file->height, ptr,
247                         BAYER_TILE_GBRG );
248                 gp_gamma_fill_table( gtable, 0.5 );
249                 gp_gamma_correct_single( gtable, ptr, jc_file->width * jc_file->height );
250
251                 CHECK_free (gp_file_set_mime_type (file, GP_MIME_PPM));
252                 CHECK_free (gp_file_set_name (file, filename));
253                 CHECK_free (gp_file_append (file, ppm, size));
254                 break;
255
256         case GP_FILE_TYPE_RAW:
257                 CHECK_free (jamcam_request_image (camera, file, raw, &size, n, context));
258                 CHECK_free (gp_file_set_mime_type (file, GP_MIME_RAW));
259                 strcpy( tmp_filename, filename );
260                 tmp_filename[strlen(tmp_filename)-3] = 'r';
261                 tmp_filename[strlen(tmp_filename)-2] = 'a';
262                 tmp_filename[strlen(tmp_filename)-1] = 'w';
263                 CHECK_free (gp_file_set_name (file, tmp_filename));
264                 CHECK_free (gp_file_append (file, raw, size));
265                 break;
266         default:
267                 free(raw); free(ppm);
268                 return (GP_ERROR_NOT_SUPPORTED);
269         }
270         free(raw); free(ppm);
271         return (GP_OK);
272 }
273
274 static int camera_summary (Camera *camera, CameraText *summary, GPContext *context)
275 {
276         int count;
277         char tmp[1024];
278
279         GP_DEBUG ("* camera_summary");
280
281         /* possibly get # pics, mem free, etc. */
282         count = jamcam_file_count (camera);
283
284         sprintf (tmp, _("Frames Taken     : %4d\n"), count);
285         strcat (summary->text, tmp );
286
287         return (GP_OK);
288 }
289
290 static int camera_about (Camera *camera, CameraText *about, GPContext *context)
291 {
292         GP_DEBUG ("* camera_about");
293
294         strcpy (about->text,
295                 _("jamcam library v" JAMCAM_VERSION
296                 " " JAMCAM_LAST_MOD "\n"
297                 "Chris Pinkham <cpinkham@infi.net>\n"
298                 "Support for KBGear JamCam v2.0 & v3.0 digital cameras\n"
299                 "based on reverse engineering serial protocol.\n"
300                 "\n"));
301
302         return (GP_OK);
303 }
304
305 static CameraFilesystemFuncs fsfuncs = {
306         .file_list_func = file_list_func,
307         .get_file_func = get_file_func,
308         .get_info_func = get_info_func,
309 };
310
311 int camera_init (Camera *camera, GPContext *context)
312 {
313         int count;
314         GPPortSettings settings;
315
316         GP_DEBUG ("* camera_init");
317         GP_DEBUG ("   * jamcam library for Gphoto2 by Chris Pinkham <cpinkham@infi.net>");
318         GP_DEBUG ("   * jamcam library v%s, %s", JAMCAM_VERSION, JAMCAM_LAST_MOD );
319
320         /* First, set up all the function pointers */
321         camera->functions->exit         = camera_exit;
322         camera->functions->summary      = camera_summary;
323         camera->functions->about        = camera_about;
324
325         CHECK (gp_port_get_settings (camera->port, &settings));
326         switch( camera->port->type ) {
327         case GP_PORT_SERIAL:
328                 settings.serial.speed    = 57600;
329                 settings.serial.bits     = 8;
330                 settings.serial.parity   = 0;
331                 settings.serial.stopbits = 1;
332                 break;
333         case GP_PORT_USB:
334                 /* Use the defaults the core parsed */
335                 break;
336         default:
337                 fprintf( stderr, "Unknown port type: %d\n", camera->port->type );
338                 return ( GP_ERROR );
339                 break;
340         }
341         CHECK (gp_port_set_settings (camera->port, settings));
342         CHECK (gp_port_set_timeout (camera->port, TIMEOUT));
343
344         /* check to see if camera is really there */
345         CHECK (jamcam_enq (camera));
346
347         /* get number of images */
348         CHECK (count = jamcam_file_count (camera));
349
350         /* Set up the CameraFilesystem */
351         return gp_filesystem_set_funcs (camera->fs, &fsfuncs, camera);
352 }
353