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
|
/* Copyright 2016 Software Freedom Conservancy Inc.
*
* This software is licensed under the GNU LGPL (version 2.1 or later).
* See the COPYING file in this distribution.
*/
public errordomain GPhotoError {
LIBRARY
}
namespace GPhoto {
// ContextWrapper assigns signals to the various GPhoto.Context callbacks, as well as spins
// the event loop at opportune times.
public class ContextWrapper {
public Context context = new Context();
public ContextWrapper() {
context.set_idle_func(on_idle);
context.set_error_func(on_error);
context.set_status_func(on_status);
context.set_message_func(on_message);
context.set_progress_funcs(on_progress_start, on_progress_update, on_progress_stop);
}
public virtual void idle() {
}
public virtual void error(string text, void *data) {
}
public virtual void status(string text, void *data) {
}
public virtual void message(string text, void *data) {
}
public virtual void progress_start(float current, string text, void *data) {
}
public virtual void progress_update(float current, void *data) {
}
public virtual void progress_stop() {
}
private void on_idle(Context context) {
idle();
}
private void on_error(Context context, string text) {
error(text, null);
}
private void on_status(Context context, string text) {
status(text, null);
}
private void on_message(Context context, string text) {
message(text, null);
}
private uint on_progress_start(Context context, float target, string text) {
progress_start(target, text, null);
return 0;
}
private void on_progress_update(Context context, uint id, float current) {
progress_update(current, null);
}
private void on_progress_stop(Context context, uint id) {
progress_stop();
}
}
public class SpinIdleWrapper : ContextWrapper {
public SpinIdleWrapper() {
}
public override void idle() {
base.idle();
spin_event_loop();
}
public override void progress_update(float current, void *data) {
base.progress_update(current, data);
spin_event_loop();
}
}
// For CameraFileInfoFile, CameraFileInfoPreview, and CameraStorageInformation. See:
// http://redmine.yorba.org/issues/1851
// https://bugzilla.redhat.com/show_bug.cgi?id=585676
// https://sourceforge.net/tracker/?func=detail&aid=3000198&group_id=8874&atid=108874
public const int MAX_FILENAME_LENGTH = 63;
public const int MAX_BASEDIR_LENGTH = 255;
public bool get_info(Context context, Camera camera, string folder, string filename,
out CameraFileInfo info) throws Error {
if (folder.length > MAX_BASEDIR_LENGTH || filename.length > MAX_FILENAME_LENGTH) {
info = {};
return false;
}
Result res = camera.get_file_info(folder, filename, out info, context);
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error retrieving file information for %s/%s: %s",
(int) res, folder, filename, res.as_string());
return true;
}
// Libgphoto will in some instances refuse to get metadata from a camera, but the camera is accessible as a
// filesystem. In these cases shotwell can access the file directly. See:
// http://redmine.yorba.org/issues/2959
public PhotoMetadata? get_fallback_metadata(Camera camera, Context context, string folder, string filename) {
GPhoto.CameraStorageInformation *sifs = null;
int count = 0;
camera.get_storageinfo(&sifs, out count, context);
GPhoto.PortInfo port_info;
camera.get_port_info(out port_info);
string path;
port_info.get_path(out path);
string prefix = "disk:";
if(path.has_prefix(prefix))
path = path[prefix.length:path.length];
else
return null;
PhotoMetadata? metadata = new PhotoMetadata();
try {
metadata.read_from_file(File.new_for_path(path + folder + "/" + filename));
} catch {
metadata = null;
}
return metadata;
}
public Gdk.Pixbuf? load_preview(Context context, Camera camera, string folder, string filename,
out uint8[] raw, out size_t raw_length) throws Error {
raw = null;
raw_length = 0;
try {
raw = load_file_into_buffer(context, camera, folder, filename, GPhoto.CameraFileType.PREVIEW);
} catch {
PhotoMetadata metadata = get_fallback_metadata(camera, context, folder, filename);
if(null == metadata)
return null;
if(0 == metadata.get_preview_count())
return null;
PhotoPreview? preview = metadata.get_preview(metadata.get_preview_count() - 1);
raw = preview.flatten();
}
if (raw == null) {
raw_length = 0;
return null;
}
raw_length = raw.length;
// Try to make sure last two bytes are JPEG footer.
// This is necessary because GPhoto sometimes includes a few extra bytes. See
// Yorba bug #2905 and the following GPhoto bug:
// http://sourceforge.net/tracker/?func=detail&aid=3141521&group_id=8874&atid=108874
if (raw_length > 32) {
for (size_t i = raw_length - 2; i > raw_length - 32; i--) {
if (raw[i] == Jpeg.MARKER_PREFIX && raw[i + 1] == Jpeg.Marker.EOI) {
debug("Adjusted length of thumbnail for: %s", filename);
raw_length = i + 2;
break;
}
}
}
MemoryInputStream mins = new MemoryInputStream.from_data(raw, null);
return new Gdk.Pixbuf.from_stream_at_scale(mins, ImportPreview.MAX_SCALE, ImportPreview.MAX_SCALE, true, null);
}
public Gdk.Pixbuf? load_image(Context context, Camera camera, string folder, string filename)
throws Error {
InputStream ins = load_file_into_stream(context, camera, folder, filename, GPhoto.CameraFileType.NORMAL);
if (ins == null)
return null;
return new Gdk.Pixbuf.from_stream(ins, null);
}
public void save_image(Context context, Camera camera, string folder, string filename, File dest_file) throws Error {
var fd = Posix.creat(dest_file.get_path(), 0640);
if (fd < 0) {
throw new IOError.FAILED("[%d] Error creating file %s: %m", GLib.errno, dest_file.get_path());
}
GPhoto.CameraFile camera_file;
GPhoto.Result res = GPhoto.CameraFile.create_from_fd(out camera_file, fd);
if (res != Result.OK) {
throw new GPhotoError.LIBRARY("[%d] Error allocating camera file: %s", (int) res, res.as_string());
}
res = camera.get_file(folder, filename, GPhoto.CameraFileType.NORMAL, camera_file, context);
if (res != Result.OK) {
throw new GPhotoError.LIBRARY("[%d] Error retrieving file object for %s/%s: %s",
(int) res, folder, filename, res.as_string());
}
}
public PhotoMetadata? load_metadata(Context context, Camera camera, string folder, string filename)
throws Error {
uint8[] camera_raw = null;
try {
camera_raw = load_file_into_buffer(context, camera, folder, filename, GPhoto.CameraFileType.EXIF);
} catch {
return get_fallback_metadata(camera, context, folder, filename);
}
if (camera_raw == null || camera_raw.length == 0)
return null;
PhotoMetadata metadata = new PhotoMetadata();
metadata.read_from_app1_segment(camera_raw);
return metadata;
}
// Returns an InputStream for the requested camera file. The stream should be used
// immediately rather than stored, as the backing is temporary in nature.
public InputStream load_file_into_stream(Context context, Camera camera, string folder, string filename,
GPhoto.CameraFileType filetype) throws Error {
GPhoto.CameraFile camera_file;
GPhoto.Result res = GPhoto.CameraFile.create(out camera_file);
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error allocating camera file: %s", (int) res, res.as_string());
res = camera.get_file(folder, filename, filetype, camera_file, context);
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error retrieving file object for %s/%s: %s",
(int) res, folder, filename, res.as_string());
// if entire file fits in memory, return a stream from that ... can't merely wrap
// MemoryInputStream around the camera_file buffer, as that will be destroyed when the
// function returns
unowned uint8 *data;
ulong data_len;
res = camera_file.get_data_and_size(out data, out data_len);
if (res == Result.OK) {
uint8[] buffer = new uint8[data_len];
Memory.copy(buffer, data, buffer.length);
return new MemoryInputStream.from_data(buffer, on_mins_destroyed);
}
// if not stored in memory, try copying it to a temp file and then reading out of that
File temp = AppDirs.get_temp_dir().get_child("import.tmp");
res = camera_file.save(temp.get_path());
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error copying file %s/%s to %s: %s", (int) res,
folder, filename, temp.get_path(), res.as_string());
return temp.read(null);
}
private static void on_mins_destroyed(void *data) {
free(data);
}
// Returns a buffer with the requested file, if within reason. Use load_file for larger files.
public uint8[]? load_file_into_buffer(Context context, Camera camera, string folder,
string filename, CameraFileType filetype) throws Error {
GPhoto.CameraFile camera_file;
GPhoto.Result res = GPhoto.CameraFile.create(out camera_file);
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error allocating camera file: %s", (int) res, res.as_string());
res = camera.get_file(folder, filename, filetype, camera_file, context);
if (res != Result.OK)
throw new GPhotoError.LIBRARY("[%d] Error retrieving file object for %s/%s: %s",
(int) res, folder, filename, res.as_string());
// if buffer can be loaded into memory, return a copy of that (can't return buffer itself
// as it will be destroyed when the camera_file is unref'd)
unowned uint8 *data;
ulong data_len;
res = camera_file.get_data_and_size(out data, out data_len);
if (res != Result.OK)
return null;
uint8[] buffer = new uint8[data_len];
Memory.copy(buffer, data, buffer.length);
return buffer;
}
}
|