summaryrefslogtreecommitdiff
path: root/src/photos/RawSupport.vala
blob: 441b899fe1918c6fdfcdb9a25279d96490afe7b1 (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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public class RawFileFormatDriver : PhotoFileFormatDriver {
    private static RawFileFormatDriver instance = null;
    
    public static void init() {
        instance = new RawFileFormatDriver();
        RawFileFormatProperties.init();
    }
    
    public static RawFileFormatDriver get_instance() {
        return instance;
    }
    
    public override PhotoFileFormatProperties get_properties() {
        return RawFileFormatProperties.get_instance();
    }
    
    public override PhotoFileReader create_reader(string filepath) {
        return new RawReader(filepath);
    }
    
    public override PhotoMetadata create_metadata() {
        return new PhotoMetadata();
    }
    
    public override bool can_write_image() {
        return false;
    }
    
    public override bool can_write_metadata() {
        return false;
    }
    
    public override PhotoFileWriter? create_writer(string filepath) {
        return null;
    }
    
    public override PhotoFileMetadataWriter? create_metadata_writer(string filepath) {
        return null;
    }
    
    public override PhotoFileSniffer create_sniffer(File file, PhotoFileSniffer.Options options) {
        return new RawSniffer(file, options);
    }
}

public class RawFileFormatProperties : PhotoFileFormatProperties {
    private static string[] KNOWN_EXTENSIONS = {
        "3fr", "arw", "srf", "sr2", "bay", "crw", "cr2", "cap", "iiq", "eip", "dcs", "dcr", "drf",
        "k25", "kdc", "dng", "erf", "fff", "mef", "mos", "mrw", "nef", "nrw", "orf", "ptx", "pef",
        "pxn", "r3d", "raf", "raw", "rw2", "raw", "rwl", "rwz", "x3f", "srw"
    };

    private static string[] KNOWN_MIME_TYPES = {
        /* a catch-all MIME type for all formats supported by the dcraw command-line
           tool (and hence libraw) */
        "image/x-dcraw",
    
        /* manufacturer blessed MIME types */
        "image/x-canon-cr2",
        "image/x-canon-crw",
        "image/x-fuji-raf",
        "image/x-adobe-dng",
        "image/x-panasonic-raw",
        "image/x-raw",
        "image/x-minolta-mrw",
        "image/x-nikon-nef",
        "image/x-olympus-orf",
        "image/x-pentax-pef",
        "image/x-sony-arw",
        "image/x-sony-srf",
        "image/x-sony-sr2",
        "image/x-samsung-raw",

        /* generic MIME types for file extensions*/
        "image/x-3fr",
        "image/x-arw",
        "image/x-srf",
        "image/x-sr2",
        "image/x-bay",
        "image/x-crw",
        "image/x-cr2",
        "image/x-cap",
        "image/x-iiq",
        "image/x-eip",
        "image/x-dcs",
        "image/x-dcr",
        "image/x-drf",
        "image/x-k25",
        "image/x-kdc",
        "image/x-dng",
        "image/x-erf",
        "image/x-fff",
        "image/x-mef",
        "image/x-mos",
        "image/x-mrw",
        "image/x-nef",
        "image/x-nrw",
        "image/x-orf",
        "image/x-ptx",
        "image/x-pef",
        "image/x-pxn",
        "image/x-r3d",
        "image/x-raf",
        "image/x-raw",
        "image/x-rw2",
        "image/x-raw",
        "image/x-rwl",
        "image/x-rwz",
        "image/x-x3f",
        "image/x-srw"
    };
    
    private static RawFileFormatProperties instance = null;
    
    public static void init() {
        instance = new RawFileFormatProperties();
    }
    
    public static RawFileFormatProperties get_instance() {
        return instance;
    }
    
    public override PhotoFileFormat get_file_format() {
        return PhotoFileFormat.RAW;
    }

    public override string get_user_visible_name() {
        return _("RAW");
    }

    public override PhotoFileFormatFlags get_flags() {
        return PhotoFileFormatFlags.NONE;
    }
    
    public override string get_default_extension() {
        // Because RAW is a smorgasbord of file formats and exporting to a RAW file is
        // not expected, this function should probably never be called.  However, need to pick
        // one, so here it is.
        return "raw";
    }
    
    public override string[] get_known_extensions() {
        return KNOWN_EXTENSIONS;
    }
    
    public override string get_default_mime_type() {
        return KNOWN_MIME_TYPES[0];
    }
    
    public override string[] get_mime_types() {
        return KNOWN_MIME_TYPES;
    }
}

public class RawSniffer : PhotoFileSniffer {
    public RawSniffer(File file, PhotoFileSniffer.Options options) {
        base (file, options);
    }
    
    public override DetectedPhotoInformation? sniff(out bool is_corrupted) throws Error {
        // this sniffer doesn't detect corrupted files
        is_corrupted = false;
        
        DetectedPhotoInformation detected = new DetectedPhotoInformation();
        
        GRaw.Processor processor = new GRaw.Processor();
        processor.output_params->user_flip = GRaw.Flip.NONE;
        
        try {
            processor.open_file(file.get_path());
            processor.unpack();
            processor.adjust_sizes_info_only();
        } catch (GRaw.Exception exception) {
            if (exception is GRaw.Exception.UNSUPPORTED_FILE)
                return null;
            
            throw exception;
        }
        
        detected.image_dim = Dimensions(processor.get_sizes().iwidth, processor.get_sizes().iheight);
        detected.colorspace = Gdk.Colorspace.RGB;
        detected.channels = 3;
        detected.bits_per_channel = 8;
        
        RawReader reader = new RawReader(file.get_path());
        try {
            detected.metadata = reader.read_metadata();
        } catch (Error err) {
            // ignored
        }
        
        if (detected.metadata != null) {
            uint8[]? flattened_sans_thumbnail = detected.metadata.flatten_exif(false);
            if (flattened_sans_thumbnail != null && flattened_sans_thumbnail.length > 0)
                detected.exif_md5 = md5_binary(flattened_sans_thumbnail, flattened_sans_thumbnail.length);
            
            uint8[]? flattened_thumbnail = detected.metadata.flatten_exif_preview();
            if (flattened_thumbnail != null && flattened_thumbnail.length > 0)
                detected.thumbnail_md5 = md5_binary(flattened_thumbnail, flattened_thumbnail.length);
        }
        
        if (calc_md5)
            detected.md5 = md5_file(file);
        
        detected.format_name = "raw";
        detected.file_format = PhotoFileFormat.RAW;
        
        return detected;
    }
}

public class RawReader : PhotoFileReader {
    public RawReader(string filepath) {
        base (filepath, PhotoFileFormat.RAW);
    }
    
    public override PhotoMetadata read_metadata() throws Error {
        PhotoMetadata metadata = new PhotoMetadata();
        metadata.read_from_file(get_file());
        
        return metadata;
    }
    
    public override Gdk.Pixbuf unscaled_read() throws Error {
        GRaw.Processor processor = new GRaw.Processor();
        processor.configure_for_rgb_display(false);
        processor.output_params->user_flip = GRaw.Flip.NONE;
        
        processor.open_file(get_filepath());
        processor.unpack();
        processor.process();
        
        return processor.make_mem_image().get_pixbuf_copy();
    }
    
    public override Gdk.Pixbuf scaled_read(Dimensions full, Dimensions scaled) throws Error {
        double width_proportion = (double) scaled.width / (double) full.width;
        double height_proportion = (double) scaled.height / (double) full.height;
        bool half_size = width_proportion < 0.5 && height_proportion < 0.5;
        
        GRaw.Processor processor = new GRaw.Processor();
        processor.configure_for_rgb_display(half_size);
        processor.output_params->user_flip = GRaw.Flip.NONE;
        
        processor.open_file(get_filepath());
        processor.unpack();
        processor.process();
        
        GRaw.ProcessedImage image = processor.make_mem_image();
        
        return resize_pixbuf(image.get_pixbuf_copy(), scaled, Gdk.InterpType.BILINEAR);
    }
}

// Development mode of a RAW photo.
public enum RawDeveloper {
    SHOTWELL = 0,  // Developed internally by Shotwell
    CAMERA,        // JPEG from RAW+JPEG pair (if available)
    EMBEDDED;      // Largest-size
    
    public static RawDeveloper[] as_array() {
        return { SHOTWELL, CAMERA, EMBEDDED };
    }
    
    public string to_string() {
        switch (this) {
            case SHOTWELL:
                return "SHOTWELL";
            case CAMERA:
                return "CAMERA";
            case EMBEDDED:
                return "EMBEDDED";
            default:
                assert_not_reached();
        }
    }
    
    public static RawDeveloper from_string(string value) {
        switch (value) {
            case "SHOTWELL":
                return SHOTWELL;
            case "CAMERA":
                return CAMERA;
            case "EMBEDDED":
                return EMBEDDED;
            default:
                assert_not_reached();
        }
    }
    
    public string get_label() {
        switch (this) {
            case SHOTWELL:
                return _("Shotwell");
            case CAMERA:
            case EMBEDDED:
                return _("Camera");
            default:
                assert_not_reached();
        }
    }
    
    // Determines if two RAW developers are equivalent, treating camera and embedded
    // as the same.
    public bool is_equivalent(RawDeveloper d) {
        if (this == d)
            return true;
        
        if ((this == RawDeveloper.CAMERA && d == RawDeveloper.EMBEDDED) ||
            (this == RawDeveloper.EMBEDDED && d == RawDeveloper.CAMERA))
            return true;
        
        return false;
    }
    
    // Creates a backing JPEG.
    // raw_filepath is the full path of the imported RAW file.
    public BackingPhotoRow create_backing_row_for_development(string raw_filepath,
        string? camera_development_filename = null) throws Error {
        BackingPhotoRow ns = new BackingPhotoRow();
        File master = File.new_for_path(raw_filepath);
        string name, ext;
        disassemble_filename(master.get_basename(), out name, out ext);
        
        string basename;
        
        // If this image is coming in with an existing development, use its existing
        // filename instead.
        if (camera_development_filename == null) {
            basename = name + "_" + ext +
                (this != CAMERA ? ("_" + this.to_string().down()) : "") + ".jpg";
        } else {
            basename = camera_development_filename;
        }
        
        bool c;
        File? new_back = generate_unique_file(master.get_parent(), basename, out c);
        claim_file(new_back);
        ns.file_format = PhotoFileFormat.JFIF;
        ns.filepath = new_back.get_path();
        
        return ns;
    }
}