From 4778cc2609f5add5c19bc54b46a385461c1f4226 Mon Sep 17 00:00:00 2001 From: Lutz Mueller Date: Mon, 26 Nov 2001 12:37:33 +0000 Subject: [PATCH] Speed improvements in the konica driver (avoid redundant traffic). git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@3221 67ed7778-7388-44ab-90cf-0a291f65f57c --- camlibs/konica/library.c | 123 +++++++++++++++--------- libgphoto2/gphoto2-file.c | 12 ++- libgphoto2/gphoto2-filesys.c | 178 +++++++++++++++++++++++++++-------- libgphoto2/gphoto2-filesys.h | 10 +- 4 files changed, 230 insertions(+), 93 deletions(-) diff --git a/camlibs/konica/library.c b/camlibs/konica/library.c index a926ad778..adda3f067 100644 --- a/camlibs/konica/library.c +++ b/camlibs/konica/library.c @@ -55,6 +55,7 @@ #define CHECK_NULL(r) {if (!(r)) return (GP_ERROR_BAD_PARAMETERS);} #define LOG_ERR(c,r,e) {if ((r)==result) gp_camera_set_error((c),(e));} +#define GP_MODULE "konica" static void log_error (Camera *c, int result) @@ -113,19 +114,46 @@ struct _CameraPrivateLibrary { static int localization_file_read (Camera* camera, const char* file_name, unsigned char** data, long int* data_size); +static int +get_info (Camera *camera, unsigned int n, CameraFileInfo *info) +{ + unsigned long image_id; + unsigned int buffer_size, exif_size; + unsigned char *buffer = NULL; + int protected; + + GP_DEBUG ("*** Getting info for picture %i...", n); + CHECK (camera, k_get_image_information (camera->port, + camera->pl->image_id_long, n, &image_id, &exif_size, &protected, + &buffer, &buffer_size)); + free (buffer); + + info->audio.fields = GP_FILE_INFO_NONE; + + info->preview.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_TYPE; + info->preview.size = buffer_size; + strcpy (info->preview.type, GP_MIME_JPEG); + + info->file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_PERMISSIONS | + GP_FILE_INFO_TYPE | GP_FILE_INFO_NAME; + info->file.size = exif_size; + info->file.permissions = GP_FILE_PERM_READ; + if (!protected) + info->file.permissions |= GP_FILE_PERM_DELETE; + strcpy (info->file.type, GP_MIME_JPEG); + snprintf (info->file.name, sizeof (info->file.name), + "%06i.jpeg", (int) image_id); + + return (GP_OK); +} static int file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list, void *data) { + CameraFileInfo info; KStatus status; unsigned int i; - unsigned char *information_buffer = NULL; - unsigned int exif_size, information_buffer_size; - int protected; - unsigned long image_id; - char filename[1024]; - int result; Camera *camera = data; /* @@ -137,17 +165,14 @@ file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list, for (i = 0; i < status.pictures; i++) { /* Get information */ - result = k_get_image_information (camera->port, - camera->pl->image_id_long, i + 1, &image_id, - &exif_size, &protected, &information_buffer, - &information_buffer_size); - free (information_buffer); - information_buffer = NULL; - CHECK (camera, result); + CHECK (camera, get_info (camera, i + 1, &info)); - /* Append to the list */ - sprintf (filename, "%06i.jpeg", (int) image_id); - CHECK (camera, gp_list_append (list, filename, NULL)); + /* + * Append directly to the filesystem instead of to the list, + * because we have additional information. + */ + gp_filesystem_append (camera->fs, folder, info.file.name); + gp_filesystem_set_info_noop (camera->fs, folder, &info); } return (GP_OK); @@ -332,35 +357,17 @@ set_info_func (CameraFilesystem *fs, const char *folder, const char *file, } static int -get_info_func (CameraFilesystem *fs, const char *folder, const char *file, +get_info_func (CameraFilesystem *fs, const char *folder, const char *filename, CameraFileInfo *info, void *data) { Camera *camera = data; - unsigned long image_id; - unsigned int information_buffer_size, exif_size; - unsigned char *information_buffer = NULL; - int protected, n; + int n; /* We need image numbers starting with 1 */ - CHECK (camera, n = gp_filesystem_number (camera->fs, folder, file)); + CHECK (camera, n = gp_filesystem_number (camera->fs, folder, filename)); n++; - CHECK (camera, k_get_image_information (camera->port, - camera->pl->image_id_long, n, &image_id, &exif_size, &protected, - &information_buffer, &information_buffer_size)); - free (information_buffer); - - info->preview.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_TYPE; - info->preview.size = information_buffer_size; - strcpy (info->preview.type, GP_MIME_JPEG); - - info->file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_PERMISSIONS | - GP_FILE_INFO_TYPE | GP_FILE_INFO_NAME; - info->file.size = exif_size; - info->file.permissions = GP_FILE_PERM_READ; - if (!protected) info->file.permissions |= GP_FILE_PERM_DELETE; - strcpy (info->file.type, GP_MIME_JPEG); - strcpy (info->file.name, file); + CHECK (camera, get_info (camera, n, info)); return (GP_OK); } @@ -521,9 +528,11 @@ camera_capture (Camera* camera, CameraCaptureType type, CameraFilePath* path) { unsigned long image_id; int exif_size; - unsigned char *information_buffer = NULL; - unsigned int information_buffer_size; + unsigned char *buffer = NULL; + unsigned int buffer_size; int protected; + CameraFile *file = NULL; + CameraFileInfo info; CHECK_NULL (camera && path); @@ -533,16 +542,36 @@ camera_capture (Camera* camera, CameraCaptureType type, CameraFilePath* path) /* Take the picture. */ CHECK (camera, k_take_picture (camera->port, camera->pl->image_id_long, - &image_id, &exif_size, &information_buffer, - &information_buffer_size, &protected)); - - free (information_buffer); - + &image_id, &exif_size, &buffer, &buffer_size, &protected)); sprintf (path->name, "%06i.jpeg", (int) image_id); - strcpy (path->folder, "/"); - CHECK (camera, gp_filesystem_append (camera->fs, path->folder, + strcpy (path->folder, "/"); + CHECK (camera, gp_filesystem_append (camera->fs, path->folder, path->name)); + info.preview.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_TYPE; + info.preview.size = buffer_size; + strcpy (info.preview.type, GP_MIME_JPEG); + + info.file.fields = GP_FILE_INFO_SIZE | GP_FILE_INFO_PERMISSIONS | + GP_FILE_INFO_TYPE | GP_FILE_INFO_NAME; + info.file.size = exif_size; + info.file.permissions = GP_FILE_PERM_READ; + if (!protected) + info.file.permissions |= GP_FILE_PERM_DELETE; + strcpy (info.file.type, GP_MIME_JPEG); + snprintf (info.file.name, sizeof (info.file.name), + "%06i.jpeg", (int) image_id); + gp_filesystem_set_info_noop (camera->fs, path->folder, &info); + + gp_file_new (&file); + gp_file_set_name (file, info.file.name); + gp_file_set_mime_type (file, GP_MIME_JPEG); + gp_file_set_type (file, GP_FILE_TYPE_PREVIEW); + gp_file_set_data_and_size (file, buffer, buffer_size); + free (buffer); + gp_filesystem_set_file_noop (camera->fs, path->folder, file); + gp_file_unref (file); + return (GP_OK); } diff --git a/libgphoto2/gphoto2-file.c b/libgphoto2/gphoto2-file.c index 655910e8d..9f77bc162 100644 --- a/libgphoto2/gphoto2-file.c +++ b/libgphoto2/gphoto2-file.c @@ -335,17 +335,25 @@ gp_file_clean (CameraFile *file) int gp_file_copy (CameraFile *destination, CameraFile *source) { + int ref_count; + CHECK_NULL (destination && source); - if (destination->data) + gp_log (GP_LOG_DEBUG, "gphoto2-file", "Copying '%s' onto '%s'...", + source->name, destination->name); + + ref_count = destination->ref_count; + if (destination->data) { free (destination->data); + destination->data = NULL; + } memcpy (destination, source, sizeof (CameraFile)); + destination->ref_count = ref_count; destination->data = malloc (sizeof (char) * source->size); if (!destination->data) return (GP_ERROR_NO_MEMORY); - memcpy (destination->data, source->data, source->size); return (GP_OK); diff --git a/libgphoto2/gphoto2-filesys.c b/libgphoto2/gphoto2-filesys.c index bf02f83a1..02610664f 100644 --- a/libgphoto2/gphoto2-filesys.c +++ b/libgphoto2/gphoto2-filesys.c @@ -113,10 +113,23 @@ delete_all_files (CameraFilesystem *fs, int x) /* Get rid of cached files */ for (y = 0; y < fs->folder[x].count; y++) { - gp_file_unref (fs->folder[x].file[y].preview); - gp_file_unref (fs->folder[x].file[y].normal); - gp_file_unref (fs->folder[x].file[y].raw); - } + if (fs->folder[x].file[y].preview) { + gp_file_unref (fs->folder[x].file[y].preview); + fs->folder[x].file[y].preview = NULL; + } + if (fs->folder[x].file[y].normal) { + gp_file_unref (fs->folder[x].file[y].normal); + fs->folder[x].file[y].normal = NULL; + } + if (fs->folder[x].file[y].raw) { + gp_file_unref (fs->folder[x].file[y].raw); + fs->folder[x].file[y].raw = NULL; + } + if (fs->folder[x].file[y].audio) { + gp_file_unref (fs->folder[x].file[y].audio); + fs->folder[x].file[y].audio = NULL; + } + } free (fs->folder[x].file); fs->folder[x].file = NULL; @@ -504,9 +517,22 @@ delete_file (CameraFilesystem *fs, int x, int y) CameraFilesystemFile *new_fip; /* Get rid of cached files */ - gp_file_unref (fs->folder[x].file[y].preview); - gp_file_unref (fs->folder[x].file[y].normal); - gp_file_unref (fs->folder[x].file[y].raw); + if (fs->folder[x].file[y].preview) { + gp_file_unref (fs->folder[x].file[y].preview); + fs->folder[x].file[y].preview = NULL; + } + if (fs->folder[x].file[y].normal) { + gp_file_unref (fs->folder[x].file[y].normal); + fs->folder[x].file[y].normal = NULL; + } + if (fs->folder[x].file[y].raw) { + gp_file_unref (fs->folder[x].file[y].raw); + fs->folder[x].file[y].raw = NULL; + } + if (fs->folder[x].file[y].audio) { + gp_file_unref (fs->folder[x].file[y].audio); + fs->folder[x].file[y].audio = NULL; + } /* Move all files behind one position up */ if (y < fs->folder[x].count - 1) @@ -618,9 +644,9 @@ gp_filesystem_list_files (CameraFilesystem *fs, const char *folder, gp_log (GP_LOG_DEBUG, "gphoto2-filesystem", "Querying folder %s...", folder); + CHECK_RESULT (delete_all_files (fs, x)); CHECK_RESULT (fs->file_list_func (fs, folder, list, fs->list_data)); - CHECK_RESULT (delete_all_files (fs, x)); CHECK_RESULT (count = gp_list_count (list)); for (y = 0; y < count; y++) { @@ -1165,7 +1191,6 @@ gp_filesystem_get_file (CameraFilesystem *fs, const char *folder, CameraFile *file) { int x, y, result; - CameraFile *tmp; CHECK_NULL (fs && folder && file && filename); CHECK_ABS (folder); @@ -1207,56 +1232,32 @@ gp_filesystem_get_file (CameraFilesystem *fs, const char *folder, return (GP_ERROR); } - CHECK_RESULT (gp_file_new (&tmp)); - gp_file_set_name (tmp, filename); - gp_file_set_type (tmp, type); - result = fs->get_file_func (fs, folder, filename, type, tmp, + result = fs->get_file_func (fs, folder, filename, type, file, fs->file_data); if (result != GP_OK) { gp_log (GP_LOG_DEBUG, "gphoto2-fs", "Failed to get file."); - gp_file_unref (tmp); return (result); } - switch (type) { - case GP_FILE_TYPE_PREVIEW: - fs->folder[x].file[y].preview = tmp; - break; - case GP_FILE_TYPE_NORMAL: - fs->folder[x].file[y].normal = tmp; - break; - case GP_FILE_TYPE_RAW: - fs->folder[x].file[y].raw = tmp; - break; - case GP_FILE_TYPE_AUDIO: - fs->folder[x].file[y].audio = tmp; - break; - default: - gp_file_unref (tmp); - gp_log (GP_LOG_ERROR, "gphoto2-filesystem", - _("Unknown file type %i"), type); - return (GP_ERROR); - } - /* We don't trust the camera drivers */ - CHECK_RESULT (gp_file_set_type (tmp, type)); - CHECK_RESULT (gp_file_set_name (tmp, filename)); + CHECK_RESULT (gp_file_set_type (file, type)); + CHECK_RESULT (gp_file_set_name (file, filename)); + + /* Cache this file */ + CHECK_RESULT (gp_filesystem_set_file_noop (fs, folder, file)); /* * Often, thumbnails are of a different mime type than the normal * picture. In this case, we should rename the file. */ if (type != GP_FILE_TYPE_NORMAL) - CHECK_RESULT (gp_file_adjust_name_for_mime_type (tmp)); - - /* Remember the file (cache) */ - CHECK_RESULT (gp_file_copy (file, tmp)); + CHECK_RESULT (gp_file_adjust_name_for_mime_type (file)); return (GP_OK); } /** - * gp_filesystem_set_info-funcs: + * gp_filesystem_set_info_funcs: * @fs: a #CameraFilesystem * @get_info_func: the function to retrieve file information * @set_info_func: the function to set file information @@ -1323,6 +1324,101 @@ gp_filesystem_get_info (CameraFilesystem *fs, const char *folder, return (GP_OK); } +/** + * gp_filesystem_set_file_noop: + * @fs: a #CameraFilesystem + * @folder: + * @file: a #CameraFile + * + * Tells the @fs about a file. Typically, camera drivers will call this + * function in case they get information about a file (i.e. preview) "for free" + * on #gp_camera_capture or #gp_camera_folder_list_files. + * + * Return value: a gphoto2 error code. + **/ +int +gp_filesystem_set_file_noop (CameraFilesystem *fs, const char *folder, + CameraFile *file) +{ + CameraFileType type; + const char *filename; + int x, y; + + CHECK_NULL (fs && folder && file); + CHECK_ABS (folder); + + /* Search folder and file */ + CHECK_RESULT (x = gp_filesystem_folder_number (fs, folder)); + CHECK_RESULT (gp_file_get_name (file, &filename)); + CHECK_RESULT (y = gp_filesystem_number (fs, folder, filename)); + + CHECK_RESULT (gp_file_get_type (file, &type)); + switch (type) { + case GP_FILE_TYPE_PREVIEW: + if (fs->folder[x].file[y].preview) + gp_file_unref (fs->folder[x].file[y].preview); + fs->folder[x].file[y].preview = file; + gp_file_ref (file); + break; + case GP_FILE_TYPE_NORMAL: + if (fs->folder[x].file[y].normal) + gp_file_unref (fs->folder[x].file[y].normal); + fs->folder[x].file[y].normal = file; + gp_file_ref (file); + break; + case GP_FILE_TYPE_RAW: + if (fs->folder[x].file[y].raw) + gp_file_unref (fs->folder[x].file[y].raw); + fs->folder[x].file[y].raw = file; + gp_file_ref (file); + break; + case GP_FILE_TYPE_AUDIO: + if (fs->folder[x].file[y].audio) + gp_file_unref (fs->folder[x].file[y].audio); + fs->folder[x].file[y].audio = file; + gp_file_ref (file); + break; + default: + gp_log (GP_LOG_ERROR, "gphoto2-filesystem", + _("Unknown file type %i"), type); + return (GP_ERROR); + } + + return (GP_OK); +} + +/** + * gp_filesystem_set_info_noop: + * @fs: a #CameraFilesystem + * @folder: + * @info: + * + * In contrast to #gp_filesystem_set_info, #gp_filesystem_set_info_noop + * will only change the file information in the @fs. Typically, camera + * drivers will use this function in case they get file information "for free" + * on #gp_camera_capture or #gp_camera_folder_list_files. + * + * Return value: a gphoto2 error code + **/ +int +gp_filesystem_set_info_noop (CameraFilesystem *fs, const char *folder, + CameraFileInfo *info) +{ + int x, y; + + CHECK_NULL (fs && folder && info); + CHECK_ABS (folder); + + /* Search folder and file */ + CHECK_RESULT (x = gp_filesystem_folder_number (fs, folder)); + CHECK_RESULT (y = gp_filesystem_number (fs, folder, info->file.name)); + + memcpy (&fs->folder[x].file[y].info, info, sizeof (CameraFileInfo)); + fs->folder[x].file[y].info_dirty = 0; + + return (GP_OK); +} + /** * gp_filesystem_set_info: * @fs: a #CameraFilesystem diff --git a/libgphoto2/gphoto2-filesys.h b/libgphoto2/gphoto2-filesys.h index c023b14e8..b1f337cb2 100644 --- a/libgphoto2/gphoto2-filesys.h +++ b/libgphoto2/gphoto2-filesys.h @@ -90,9 +90,13 @@ int gp_filesystem_new (CameraFilesystem **fs); int gp_filesystem_free (CameraFilesystem *fs); /* Manual editing */ -int gp_filesystem_append (CameraFilesystem *fs, const char *folder, - const char *filename); -int gp_filesystem_reset (CameraFilesystem *fs); +int gp_filesystem_append (CameraFilesystem *fs, const char *folder, + const char *filename); +int gp_filesystem_set_info_noop (CameraFilesystem *fs, const char *folder, + CameraFileInfo *info); +int gp_filesystem_set_file_noop (CameraFilesystem *fs, const char *folder, + CameraFile *file); +int gp_filesystem_reset (CameraFilesystem *fs); /* Information retrieval */ int gp_filesystem_count (CameraFilesystem *fs, const char *folder);