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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
// ==========================================================
// RAW camera image loader
//
// Design and implementation by
// - Hervé Drolon (drolon@infonie.fr)
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at your own risk!
// ==========================================================
#include "../LibRawLite/libraw/libraw.h"
#include "FreeImage.h"
#include "Utilities.h"
#include "../Metadata/FreeImageTag.h"
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ==========================================================
// Internal functions
// ==========================================================
// ----------------------------------------------------------
// FreeImage datastream wrapper
// ----------------------------------------------------------
class LibRaw_freeimage_datastream : public LibRaw_abstract_datastream {
private:
FreeImageIO *_io;
fi_handle _handle;
long _eof;
public:
LibRaw_freeimage_datastream(FreeImageIO *io, fi_handle handle) : _io(io), _handle(handle) {
long start_pos = io->tell_proc(handle);
io->seek_proc(handle, 0, SEEK_END);
_eof = io->tell_proc(handle);
io->seek_proc(handle, start_pos, SEEK_SET);
}
~LibRaw_freeimage_datastream() {
}
virtual void * make_jas_stream() {
return NULL;
}
virtual int valid() {
return (_io && _handle);
}
virtual int read(void *buffer, size_t size, size_t count) {
if(substream) return substream->read(buffer, size, count);
return _io->read_proc(buffer, (unsigned)size, (unsigned)count, _handle);
}
virtual int eof() {
if(substream) return substream->eof();
return (_io->tell_proc(_handle) >= _eof);
}
virtual int seek(INT64 offset, int origin) {
if(substream) return substream->seek(offset, origin);
return _io->seek_proc(_handle, (long)offset, origin);
}
virtual INT64 tell() {
if(substream) return substream->tell();
return _io->tell_proc(_handle);
}
virtual int get_char() {
int c = 0;
if(substream) return substream->get_char();
if(!_io->read_proc(&c, 1, 1, _handle)) return -1;
return c;
}
virtual char* gets(char *buffer, int length) {
if (substream) return substream->gets(buffer, length);
memset(buffer, 0, length);
for(int i = 0; i < length; i++) {
if(!_io->read_proc(&buffer[i], 1, 1, _handle))
return NULL;
if(buffer[i] == 0x0A)
break;
}
return buffer;
}
virtual int scanf_one(const char *fmt, void* val) {
std::string buffer;
char element = 0;
bool bDone = false;
if(substream) return substream->scanf_one(fmt,val);
do {
if(_io->read_proc(&element, 1, 1, _handle) == 1) {
switch(element) {
case '0':
case '\n':
case ' ':
case '\t':
bDone = true;
break;
default:
break;
}
buffer.append(&element, 1);
} else {
return 0;
}
} while(!bDone);
return sscanf(buffer.c_str(), fmt, val);
}
};
// ----------------------------------------------------------
/**
Convert a processed raw data array to a FIBITMAP
@param image Processed raw image
@return Returns the converted dib if successfull, returns NULL otherwise
*/
static FIBITMAP *
libraw_ConvertToDib(libraw_processed_image_t *image) {
FIBITMAP *dib = NULL;
try {
unsigned width = image->width;
unsigned height = image->height;
unsigned bpp = image->bits;
if(bpp == 16) {
// allocate output dib
dib = FreeImage_AllocateT(FIT_RGB16, width, height);
if(!dib) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// write data
WORD *raw_data = (WORD*)image->data;
for(unsigned y = 0; y < height; y++) {
FIRGB16 *output = (FIRGB16*)FreeImage_GetScanLine(dib, height - 1 - y);
for(unsigned x = 0; x < width; x++) {
output[x].red = raw_data[0];
output[x].green = raw_data[1];
output[x].blue = raw_data[2];
raw_data += 3;
}
}
} else if(bpp == 8) {
// allocate output dib
dib = FreeImage_AllocateT(FIT_BITMAP, width, height, 24);
if(!dib) {
throw FI_MSG_ERROR_DIB_MEMORY;
}
// write data
BYTE *raw_data = (BYTE*)image->data;
for(unsigned y = 0; y < height; y++) {
RGBTRIPLE *output = (RGBTRIPLE*)FreeImage_GetScanLine(dib, height - 1 - y);
for(unsigned x = 0; x < width; x++) {
output[x].rgbtRed = raw_data[0];
output[x].rgbtGreen = raw_data[1];
output[x].rgbtBlue = raw_data[2];
raw_data += 3;
}
}
}
} catch(const char *text) {
FreeImage_OutputMessageProc(s_format_id, text);
}
return dib;
}
/**
Get the embedded JPEG preview image from RAW picture with included Exif Data.
@param RawProcessor Libraw handle
@param flags JPEG load flags
@return Returns the loaded dib if successfull, returns NULL otherwise
*/
static FIBITMAP *
libraw_LoadEmbeddedPreview(LibRaw& RawProcessor, int flags) {
FIBITMAP *dib = NULL;
libraw_processed_image_t *thumb_image = NULL;
try {
// unpack data
if(RawProcessor.unpack_thumb() != LIBRAW_SUCCESS) {
// run silently "LibRaw : failed to run unpack_thumb"
return NULL;
}
// retrieve thumb image
int error_code = 0;
thumb_image = RawProcessor.dcraw_make_mem_thumb(&error_code);
if(thumb_image) {
if(thumb_image->type != LIBRAW_IMAGE_BITMAP) {
// attach the binary data to a memory stream
FIMEMORY *hmem = FreeImage_OpenMemory((BYTE*)thumb_image->data, (DWORD)thumb_image->data_size);
// get the file type
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
if(fif == FIF_JPEG) {
// rotate according to Exif orientation
flags |= JPEG_EXIFROTATE;
}
// load an image from the memory stream
dib = FreeImage_LoadFromMemory(fif, hmem, flags);
// close the stream
FreeImage_CloseMemory(hmem);
} else {
// convert processed data to output dib
dib = libraw_ConvertToDib(thumb_image);
}
} else {
throw "LibRaw : failed to run dcraw_make_mem_thumb";
}
// clean-up and return
RawProcessor.dcraw_clear_mem(thumb_image);
return dib;
} catch(const char *text) {
// clean-up and return
if(thumb_image) {
RawProcessor.dcraw_clear_mem(thumb_image);
}
if(text != NULL) {
FreeImage_OutputMessageProc(s_format_id, text);
}
}
return NULL;
}
/**
Load raw data and convert to FIBITMAP
@param RawProcessor Libraw handle
@param bitspersample Output bitdepth (8- or 16-bit)
@return Returns the loaded dib if successfull, returns NULL otherwise
*/
static FIBITMAP *
libraw_LoadRawData(LibRaw& RawProcessor, int bitspersample) {
FIBITMAP *dib = NULL;
libraw_processed_image_t *processed_image = NULL;
try {
// set decoding parameters
// -----------------------
// (-6) 16-bit or 8-bit
RawProcessor.imgdata.params.output_bps = bitspersample;
// (-g power toe_slope)
if(bitspersample == 16) {
// set -g 1 1 for linear curve
RawProcessor.imgdata.params.gamm[0] = 1;
RawProcessor.imgdata.params.gamm[1] = 1;
} else if(bitspersample == 8) {
// by default settings for rec. BT.709 are used: power 2.222 (i.e. gamm[0]=1/2.222) and slope 4.5
RawProcessor.imgdata.params.gamm[0] = 1/2.222;
RawProcessor.imgdata.params.gamm[1] = 4.5;
}
// (-W) Don't use automatic increase of brightness by histogram
RawProcessor.imgdata.params.no_auto_bright = 1;
// (-a) Use automatic white balance obtained after averaging over the entire image
RawProcessor.imgdata.params.use_auto_wb = 1;
// (-q 3) Adaptive homogeneity-directed demosaicing algorithm (AHD)
RawProcessor.imgdata.params.user_qual = 3;
// -----------------------
// unpack data
if(RawProcessor.unpack() != LIBRAW_SUCCESS) {
throw "LibRaw : failed to unpack data";
}
// process data (... most consuming task ...)
if(RawProcessor.dcraw_process() != LIBRAW_SUCCESS) {
throw "LibRaw : failed to process data";
}
// retrieve processed image
int error_code = 0;
processed_image = RawProcessor.dcraw_make_mem_image(&error_code);
if(processed_image) {
// type SHOULD be LIBRAW_IMAGE_BITMAP, but we'll check
if(processed_image->type != LIBRAW_IMAGE_BITMAP) {
throw "invalid image type";
}
// only 3-color images supported...
if(processed_image->colors != 3) {
throw "only 3-color images supported";
}
} else {
throw "LibRaw : failed to run dcraw_make_mem_image";
}
// convert processed data to output dib
dib = libraw_ConvertToDib(processed_image);
// clean-up and return
RawProcessor.dcraw_clear_mem(processed_image);
return dib;
} catch(const char *text) {
// clean-up and return
if(processed_image) {
RawProcessor.dcraw_clear_mem(processed_image);
}
FreeImage_OutputMessageProc(s_format_id, text);
}
return NULL;
}
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "RAW";
}
static const char * DLL_CALLCONV
Description() {
return "RAW camera image";
}
static const char * DLL_CALLCONV
Extension() {
/**
Below are known RAW file extensions that you can check using FreeImage_GetFIFFromFormat.
If a file extension is not listed, that doesn't mean that you cannot load it.
Using FreeImage_GetFileType is the best way to know if a RAW file format is supported.
*/
static const char *raw_extensions =
"3fr," // Hasselblad Digital Camera Raw Image Format.
"arw," // Sony Digital Camera Raw Image Format for Alpha devices.
"bay," // Casio Digital Camera Raw File Format.
"bmq," // NuCore Raw Image File.
"cap," // Phase One Digital Camera Raw Image Format.
"cine," // Phantom Software Raw Image File.
"cr2," // Canon Digital Camera RAW Image Format version 2.0. These images are based on the TIFF image standard.
"crw," // Canon Digital Camera RAW Image Format version 1.0.
"cs1," // Sinar Capture Shop Raw Image File.
"dc2," // Kodak DC25 Digital Camera File.
"dcr," // Kodak Digital Camera Raw Image Format for these models: Kodak DSC Pro SLR/c, Kodak DSC Pro SLR/n, Kodak DSC Pro 14N, Kodak DSC PRO 14nx.
"drf," // Kodak Digital Camera Raw Image Format.
"dsc," // Kodak Digital Camera Raw Image Format.
"dng," // Adobe Digital Negative: DNG is publicly available archival format for the raw files generated by digital cameras. By addressing the lack of an open standard for the raw files created by individual camera models, DNG helps ensure that photographers will be able to access their files in the future.
"erf," // Epson Digital Camera Raw Image Format.
"fff," // Imacon Digital Camera Raw Image Format.
"ia," // Sinar Raw Image File.
"iiq," // Phase One Digital Camera Raw Image Format.
"k25," // Kodak DC25 Digital Camera Raw Image Format.
"kc2," // Kodak DCS200 Digital Camera Raw Image Format.
"kdc," // Kodak Digital Camera Raw Image Format.
"mdc," // Minolta RD175 Digital Camera Raw Image Format.
"mef," // Mamiya Digital Camera Raw Image Format.
"mos," // Leaf Raw Image File.
"mrw," // Minolta Dimage Digital Camera Raw Image Format.
"nef," // Nikon Digital Camera Raw Image Format.
"nrw," // Nikon Digital Camera Raw Image Format.
"orf," // Olympus Digital Camera Raw Image Format.
"pef," // Pentax Digital Camera Raw Image Format.
"ptx," // Pentax Digital Camera Raw Image Format.
"pxn," // Logitech Digital Camera Raw Image Format.
"qtk," // Apple Quicktake 100/150 Digital Camera Raw Image Format.
"raf," // Fuji Digital Camera Raw Image Format.
"raw," // Panasonic Digital Camera Image Format.
"rdc," // Digital Foto Maker Raw Image File.
"rw2," // Panasonic LX3 Digital Camera Raw Image Format.
"rwl," // Leica Camera Raw Image Format.
"rwz," // Rawzor Digital Camera Raw Image Format.
"sr2," // Sony Digital Camera Raw Image Format.
"srf," // Sony Digital Camera Raw Image Format for DSC-F828 8 megapixel digital camera or Sony DSC-R1.
"srw," // Samsung Raw Image Format.
"sti"; // Sinar Capture Shop Raw Image File.
// "x3f" // Sigma Digital Camera Raw Image Format for devices based on Foveon X3 direct image sensor.
return raw_extensions;
}
static const char * DLL_CALLCONV
RegExpr() {
return NULL;
}
static const char * DLL_CALLCONV
MimeType() {
return "image/x-dcraw";
}
static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
LibRaw RawProcessor;
BOOL bSuccess = TRUE;
// wrap the input datastream
LibRaw_freeimage_datastream datastream(io, handle);
// open the datastream
if(RawProcessor.open_datastream(&datastream) != LIBRAW_SUCCESS) {
bSuccess = FALSE; // LibRaw : failed to open input stream (unknown format)
}
// clean-up internal memory allocations
RawProcessor.recycle();
return bSuccess;
}
static BOOL DLL_CALLCONV
SupportsExportDepth(int depth) {
return FALSE;
}
static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type) {
return FALSE;
}
static BOOL DLL_CALLCONV
SupportsICCProfiles() {
return TRUE;
}
static BOOL DLL_CALLCONV
SupportsNoPixels() {
return TRUE;
}
// ----------------------------------------------------------
static FIBITMAP * DLL_CALLCONV
Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
FIBITMAP *dib = NULL;
LibRaw RawProcessor;
BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
try {
// wrap the input datastream
LibRaw_freeimage_datastream datastream(io, handle);
// set decoding parameters
// the following parameters affect data reading
// --------------------------------------------
// (-s [0..N-1]) Select one raw image from input file
RawProcessor.imgdata.params.shot_select = 0;
// (-w) Use camera white balance, if possible (otherwise, fallback to auto_wb)
RawProcessor.imgdata.params.use_camera_wb = 1;
// (-h) outputs the image in 50% size
RawProcessor.imgdata.params.half_size = ((flags & RAW_HALFSIZE) == RAW_HALFSIZE) ? 1 : 0;
// open the datastream
if(RawProcessor.open_datastream(&datastream) != LIBRAW_SUCCESS) {
throw "LibRaw : failed to open input stream (unknown format)";
}
if(header_only) {
// header only mode
dib = FreeImage_AllocateHeaderT(header_only, FIT_RGB16, RawProcessor.imgdata.sizes.width, RawProcessor.imgdata.sizes.height);
}
else if((flags & RAW_PREVIEW) == RAW_PREVIEW) {
// try to get the embedded JPEG
dib = libraw_LoadEmbeddedPreview(RawProcessor, 0);
if(!dib) {
// no JPEG preview: try to load as 8-bit/sample (i.e. RGB 24-bit)
dib = libraw_LoadRawData(RawProcessor, 8);
}
}
else if((flags & RAW_DISPLAY) == RAW_DISPLAY) {
// load raw data as 8-bit/sample (i.e. RGB 24-bit)
dib = libraw_LoadRawData(RawProcessor, 8);
}
else {
// default: load raw data as linear 16-bit/sample (i.e. RGB 48-bit)
dib = libraw_LoadRawData(RawProcessor, 16);
}
// save ICC profile if present
if(dib && (NULL != RawProcessor.imgdata.color.profile)) {
FreeImage_CreateICCProfile(dib, RawProcessor.imgdata.color.profile, RawProcessor.imgdata.color.profile_length);
}
// try to get JPEG embedded Exif metadata
if(dib && !((flags & RAW_PREVIEW) == RAW_PREVIEW)) {
FIBITMAP *metadata_dib = libraw_LoadEmbeddedPreview(RawProcessor, FIF_LOAD_NOPIXELS);
if(metadata_dib) {
FreeImage_CloneMetadata(dib, metadata_dib);
FreeImage_Unload(metadata_dib);
}
}
// clean-up internal memory allocations
RawProcessor.recycle();
return dib;
} catch(const char *text) {
if(dib) {
FreeImage_Unload(dib);
}
RawProcessor.recycle();
FreeImage_OutputMessageProc(s_format_id, text);
}
return NULL;
}
// ==========================================================
// Init
// ==========================================================
void DLL_CALLCONV
InitRAW(Plugin *plugin, int format_id) {
s_format_id = format_id;
plugin->format_proc = Format;
plugin->description_proc = Description;
plugin->extension_proc = Extension;
plugin->regexpr_proc = RegExpr;
plugin->open_proc = NULL;
plugin->close_proc = NULL;
plugin->pagecount_proc = NULL;
plugin->pagecapability_proc = NULL;
plugin->load_proc = Load;
plugin->save_proc = NULL;
plugin->validate_proc = Validate;
plugin->mime_proc = MimeType;
plugin->supports_export_bpp_proc = SupportsExportDepth;
plugin->supports_export_type_proc = SupportsExportType;
plugin->supports_icc_profiles_proc = SupportsICCProfiles;
plugin->supports_no_pixels_proc = SupportsNoPixels;
}
|