-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginJXR.cpp
More file actions
1475 lines (1271 loc) · 45.9 KB
/
PluginJXR.cpp
File metadata and controls
1475 lines (1271 loc) · 45.9 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==========================================================
// JPEG XR Loader & Writer
//
// Design and implementation by
// - Herve 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 "FreeImage.h"
#include "Utilities.h"
#include "../Metadata/FreeImageTag.h"
#include "../LibJXR/jxrgluelib/JXRGlue.h"
// ==========================================================
// Plugin Interface
// ==========================================================
static int s_format_id;
// ==========================================================
// FreeImageIO interface (I/O streaming functions)
// ==========================================================
/**
JXR wrapper for FreeImage I/O handle
*/
typedef struct tagFreeImageJXRIO {
FreeImageIO *io;
fi_handle handle;
} FreeImageJXRIO;
static ERR
_jxr_io_Read(WMPStream* pWS, void* pv, size_t cb) {
FreeImageJXRIO *fio = (FreeImageJXRIO*)pWS->state.pvObj;
return (fio->io->read_proc(pv, (unsigned)cb, 1, fio->handle) == 1) ? WMP_errSuccess : WMP_errFileIO;
}
static ERR
_jxr_io_Write(WMPStream* pWS, const void* pv, size_t cb) {
FreeImageJXRIO *fio = (FreeImageJXRIO*)pWS->state.pvObj;
if(0 != cb) {
return (fio->io->write_proc((void*)pv, (unsigned)cb, 1, fio->handle) == 1) ? WMP_errSuccess : WMP_errFileIO;
}
return WMP_errFileIO;
}
static ERR
_jxr_io_SetPos(WMPStream* pWS, size_t offPos) {
FreeImageJXRIO *fio = (FreeImageJXRIO*)pWS->state.pvObj;
return (fio->io->seek_proc(fio->handle, (long)offPos, SEEK_SET) == 0) ? WMP_errSuccess : WMP_errFileIO;
}
static ERR
_jxr_io_GetPos(WMPStream* pWS, size_t* poffPos) {
FreeImageJXRIO *fio = (FreeImageJXRIO*)pWS->state.pvObj;
long lOff = fio->io->tell_proc(fio->handle);
if(lOff == -1) {
return WMP_errFileIO;
}
*poffPos = (size_t)lOff;
return WMP_errSuccess;
}
static Bool
_jxr_io_EOS(WMPStream* pWS) {
FreeImageJXRIO *fio = (FreeImageJXRIO*)pWS->state.pvObj;
long currentPos = fio->io->tell_proc(fio->handle);
fio->io->seek_proc(fio->handle, 0, SEEK_END);
long fileRemaining = fio->io->tell_proc(fio->handle) - currentPos;
fio->io->seek_proc(fio->handle, currentPos, SEEK_SET);
return (fileRemaining > 0);
}
static ERR
_jxr_io_Close(WMPStream** ppWS) {
WMPStream *pWS = *ppWS;
// HACK : we use fMem to avoid a stream destruction by the library
// because FreeImage MUST HAVE the ownership of the stream
// see _jxr_io_Create
if(pWS && pWS->fMem) {
free(pWS);
*ppWS = NULL;
}
return WMP_errSuccess;
}
static ERR
_jxr_io_Create(WMPStream **ppWS, FreeImageJXRIO *jxr_io) {
*ppWS = (WMPStream*)calloc(1, sizeof(**ppWS));
if(*ppWS) {
WMPStream *pWS = *ppWS;
pWS->state.pvObj = jxr_io;
pWS->Close = _jxr_io_Close;
pWS->EOS = _jxr_io_EOS;
pWS->Read = _jxr_io_Read;
pWS->Write = _jxr_io_Write;
pWS->SetPos = _jxr_io_SetPos;
pWS->GetPos = _jxr_io_GetPos;
// HACK : we use fMem to avoid a stream destruction by the library
// because FreeImage MUST HAVE the ownership of the stream
// see _jxr_io_Close
pWS->fMem = FALSE;
return WMP_errSuccess;
}
return WMP_errOutOfMemory;
}
// ==========================================================
// JPEG XR Error handling
// ==========================================================
static const char*
JXR_ErrorMessage(const int error) {
switch(error) {
case WMP_errNotYetImplemented:
case WMP_errAbstractMethod:
return "Not yet implemented";
case WMP_errOutOfMemory:
return "Out of memory";
case WMP_errFileIO:
return "File I/O error";
case WMP_errBufferOverflow:
return "Buffer overflow";
case WMP_errInvalidParameter:
return "Invalid parameter";
case WMP_errInvalidArgument:
return "Invalid argument";
case WMP_errUnsupportedFormat:
return "Unsupported format";
case WMP_errIncorrectCodecVersion:
return "Incorrect codec version";
case WMP_errIndexNotFound:
return "Format converter: Index not found";
case WMP_errOutOfSequence:
return "Metadata: Out of sequence";
case WMP_errMustBeMultipleOf16LinesUntilLastCall:
return "Must be multiple of 16 lines until last call";
case WMP_errPlanarAlphaBandedEncRequiresTempFile:
return "Planar alpha banded encoder requires temp files";
case WMP_errAlphaModeCannotBeTranscoded:
return "Alpha mode cannot be transcoded";
case WMP_errIncorrectCodecSubVersion:
return "Incorrect codec subversion";
case WMP_errFail:
case WMP_errNotInitialized:
default:
return "Invalid instruction - please contact the FreeImage team";
}
}
// ==========================================================
// Helper functions & macro
// ==========================================================
#define JXR_CHECK(error_code) \
if(error_code < 0) { \
const char *error_message = JXR_ErrorMessage(error_code); \
throw error_message; \
}
// --------------------------------------------------------------------------
/**
Input conversions natively understood by FreeImage
@see GetNativePixelFormat
*/
typedef struct tagJXRInputConversion {
BITDEPTH_BITS bdBitDepth;
U32 cbitUnit;
FREE_IMAGE_TYPE image_type;
unsigned red_mask;
unsigned green_mask;
unsigned blue_mask;
} JXRInputConversion;
/**
Conversion table for native FreeImage formats
@see GetNativePixelFormat
*/
static JXRInputConversion s_FreeImagePixelInfo[] = {
// 1-bit bitmap
{ BD_1, 1, FIT_BITMAP, 0, 0, 0 },
// 8-, 24-, 32-bit bitmap
{ BD_8, 8, FIT_BITMAP, 0, 0, 0 },
{ BD_8, 24, FIT_BITMAP, 0, 0, 0 },
{ BD_8, 32, FIT_BITMAP, 0, 0, 0 },
// 16-bit RGB 565
{ BD_565, 16, FIT_BITMAP, FI16_565_RED_MASK, FI16_565_GREEN_MASK, FI16_565_BLUE_MASK },
// 16-bit RGB 555
{ BD_5, 16, FIT_BITMAP, FI16_555_RED_MASK, FI16_555_GREEN_MASK, FI16_555_BLUE_MASK },
// 16-bit greyscale, RGB16, RGBA16 bitmap
{ BD_16, 16, FIT_UINT16, 0, 0, 0 },
{ BD_16, 48, FIT_RGB16, 0, 0, 0 },
{ BD_16, 64, FIT_RGBA16, 0, 0, 0 },
// 32-bit float, RGBF, RGBAF bitmap
{ BD_32F, 32, FIT_FLOAT, 0, 0, 0 },
{ BD_32F, 96, FIT_RGBF, 0, 0, 0 },
{ BD_32F, 128, FIT_RGBAF, 0, 0, 0 }
};
/**
Scan input pixelInfo specifications and return the equivalent FreeImage info for loading
@param pixelInfo Image specifications
@param out_guid_format (returned value) output pixel format
@param image_type (returned value) Image type
@param bpp (returned value) Image bit depth
@param red_mask (returned value) RGB mask
@param green_mask (returned value) RGB mask
@param blue_mask (returned value) RGB mask
@return Returns WMP_errSuccess if successful, returns WMP_errFail otherwise
@see GetInputPixelFormat
*/
static ERR
GetNativePixelFormat(const PKPixelInfo *pixelInfo, PKPixelFormatGUID *out_guid_format, FREE_IMAGE_TYPE *image_type, unsigned *bpp, unsigned *red_mask, unsigned *green_mask, unsigned *blue_mask) {
const unsigned s_FreeImagePixelInfoSize = (unsigned)sizeof(s_FreeImagePixelInfo) / sizeof(*(s_FreeImagePixelInfo));
for(unsigned i = 0; i < s_FreeImagePixelInfoSize; i++) {
if(pixelInfo->bdBitDepth == s_FreeImagePixelInfo[i].bdBitDepth) {
if(pixelInfo->cbitUnit == s_FreeImagePixelInfo[i].cbitUnit) {
// found ! now get dst image format specifications
memcpy(out_guid_format, pixelInfo->pGUIDPixFmt, sizeof(PKPixelFormatGUID));
*image_type = s_FreeImagePixelInfo[i].image_type;
*bpp = s_FreeImagePixelInfo[i].cbitUnit;
*red_mask = s_FreeImagePixelInfo[i].red_mask;
*green_mask = s_FreeImagePixelInfo[i].green_mask;
*blue_mask = s_FreeImagePixelInfo[i].blue_mask;
return WMP_errSuccess;
}
}
}
// not found : need pixel format conversion
return WMP_errFail;
}
/**
Scan input file guid format and return the equivalent FreeImage info & target guid format for loading
@param pDecoder Decoder handle
@param guid_format (returned value) Output pixel format
@param image_type (returned value) Image type
@param bpp (returned value) Image bit depth
@param red_mask (returned value) RGB mask
@param green_mask (returned value) RGB mask
@param blue_mask (returned value) RGB mask
@return Returns TRUE if successful, returns FALSE otherwise
*/
static ERR
GetInputPixelFormat(PKImageDecode *pDecoder, PKPixelFormatGUID *guid_format, FREE_IMAGE_TYPE *image_type, unsigned *bpp, unsigned *red_mask, unsigned *green_mask, unsigned *blue_mask) {
ERR error_code = 0; // error code as returned by the interface
PKPixelInfo pixelInfo; // image specifications
try {
// get input file pixel format ...
PKPixelFormatGUID pguidSourcePF;
error_code = pDecoder->GetPixelFormat(pDecoder, &pguidSourcePF);
JXR_CHECK(error_code);
pixelInfo.pGUIDPixFmt = &pguidSourcePF;
// ... check for a supported format and get the format specifications
error_code = PixelFormatLookup(&pixelInfo, LOOKUP_FORWARD);
JXR_CHECK(error_code);
// search for an equivalent native FreeImage format
error_code = GetNativePixelFormat(&pixelInfo, guid_format, image_type, bpp, red_mask, green_mask, blue_mask);
if(error_code != WMP_errSuccess) {
// try to find a suitable conversion function ...
const PKPixelFormatGUID *ppguidTargetPF = NULL; // target pixel format
unsigned iIndex = 0; // first available conversion function
do {
error_code = PKFormatConverter_EnumConversions(&pguidSourcePF, iIndex, &ppguidTargetPF);
if(error_code == WMP_errSuccess) {
// found a conversion function, is the converted format a native FreeImage format ?
pixelInfo.pGUIDPixFmt = ppguidTargetPF;
error_code = PixelFormatLookup(&pixelInfo, LOOKUP_FORWARD);
JXR_CHECK(error_code);
error_code = GetNativePixelFormat(&pixelInfo, guid_format, image_type, bpp, red_mask, green_mask, blue_mask);
if(error_code == WMP_errSuccess) {
break;
}
}
// try next conversion function
iIndex++;
} while(error_code != WMP_errIndexNotFound);
}
return (error_code == WMP_errSuccess) ? WMP_errSuccess : WMP_errUnsupportedFormat;
} catch(...) {
return error_code;
}
}
// --------------------------------------------------------------------------
/**
Scan input dib format and return the equivalent PKPixelFormatGUID format for saving
@param dib Image to be saved
@param guid_format (returned value) GUID format
@param bHasAlpha (returned value) TRUE if an alpha layer is present
@return Returns TRUE if successful, returns FALSE otherwise
*/
static ERR
GetOutputPixelFormat(FIBITMAP *dib, PKPixelFormatGUID *guid_format, BOOL *bHasAlpha) {
const FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(dib);
const unsigned bpp = FreeImage_GetBPP(dib);
const FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);
*guid_format = GUID_PKPixelFormatDontCare;
*bHasAlpha = FALSE;
switch(image_type) {
case FIT_BITMAP: // standard image : 1-, 4-, 8-, 16-, 24-, 32-bit
switch(bpp) {
case 1:
// assume FIC_MINISBLACK
if(color_type == FIC_MINISBLACK) {
*guid_format = GUID_PKPixelFormatBlackWhite;
}
break;
case 8:
// assume FIC_MINISBLACK
if(color_type == FIC_MINISBLACK) {
*guid_format = GUID_PKPixelFormat8bppGray;
}
break;
case 16:
if ((FreeImage_GetRedMask(dib) == FI16_565_RED_MASK) && (FreeImage_GetGreenMask(dib) == FI16_565_GREEN_MASK) && (FreeImage_GetBlueMask(dib) == FI16_565_BLUE_MASK)) {
*guid_format = GUID_PKPixelFormat16bppRGB565;
} else {
// includes case where all the masks are 0
*guid_format = GUID_PKPixelFormat16bppRGB555;
}
break;
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
case 24:
*guid_format = GUID_PKPixelFormat24bppBGR;
break;
case 32:
*guid_format = GUID_PKPixelFormat32bppBGRA;
*bHasAlpha = TRUE;
break;
#elif FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_RGB
case 24:
*guid_format = GUID_PKPixelFormat24bppRGB;
break;
case 32:
*guid_format = GUID_PKPixelFormat32bppRGBA;
*bHasAlpha = TRUE;
break;
#endif
case 4:
default:
// not supported
break;
}
break;
case FIT_UINT16: // array of unsigned short : unsigned 16-bit
*guid_format = GUID_PKPixelFormat16bppGray;
break;
case FIT_FLOAT: // array of float : 32-bit IEEE floating point
*guid_format = GUID_PKPixelFormat32bppGrayFloat;
break;
case FIT_RGB16: // 48-bit RGB image : 3 x 16-bit
*guid_format = GUID_PKPixelFormat48bppRGB;
break;
case FIT_RGBA16: // 64-bit RGBA image : 4 x 16-bit
*guid_format = GUID_PKPixelFormat64bppRGBA;
*bHasAlpha = TRUE;
break;
case FIT_RGBF: // 96-bit RGB float image : 3 x 32-bit IEEE floating point
*guid_format = GUID_PKPixelFormat96bppRGBFloat;
break;
case FIT_RGBAF: // 128-bit RGBA float image : 4 x 32-bit IEEE floating point
*guid_format = GUID_PKPixelFormat128bppRGBAFloat;
*bHasAlpha = TRUE;
break;
case FIT_INT16: // array of short : signed 16-bit
case FIT_UINT32: // array of unsigned long : unsigned 32-bit
case FIT_INT32: // array of long : signed 32-bit
case FIT_DOUBLE: // array of double : 64-bit IEEE floating point
case FIT_COMPLEX: // array of FICOMPLEX : 2 x 64-bit IEEE floating point
default:
// unsupported format
break;
}
return (*guid_format != GUID_PKPixelFormatDontCare) ? WMP_errSuccess : WMP_errUnsupportedFormat;
}
// ==========================================================
// Metadata loading
// ==========================================================
/**
Read a JPEG-XR IFD as a buffer
@see ReadMetadata
*/
static ERR
ReadProfile(WMPStream* pStream, unsigned cbByteCount, unsigned uOffset, BYTE **ppbProfile) {
// (re-)allocate profile buffer
BYTE *pbProfile = *ppbProfile;
pbProfile = (BYTE*)realloc(pbProfile, cbByteCount);
if(!pbProfile) {
return WMP_errOutOfMemory;
}
// read the profile
if(WMP_errSuccess == pStream->SetPos(pStream, uOffset)) {
if(WMP_errSuccess == pStream->Read(pStream, pbProfile, cbByteCount)) {
*ppbProfile = pbProfile;
return WMP_errSuccess;
}
}
return WMP_errFileIO;
}
/**
Convert a DPKPROPVARIANT to a FITAG, then store the tag as FIMD_EXIF_MAIN
@see ReadDescriptiveMetadata
*/
static BOOL
ReadPropVariant(WORD tag_id, const DPKPROPVARIANT & varSrc, FIBITMAP *dib) {
DWORD dwSize;
if(varSrc.vt == DPKVT_EMPTY) {
return FALSE;
}
// get the tag key
TagLib& s = TagLib::instance();
const char *key = s.getTagFieldName(TagLib::EXIF_MAIN, tag_id, NULL);
if(!key) {
return FALSE;
}
// create a tag
FITAG *tag = FreeImage_CreateTag();
if(tag) {
// set tag ID
FreeImage_SetTagID(tag, tag_id);
// set tag type, count, length and value
switch (varSrc.vt) {
case DPKVT_LPSTR:
FreeImage_SetTagType(tag, FIDT_ASCII);
dwSize = (DWORD)strlen(varSrc.VT.pszVal) + 1;
FreeImage_SetTagCount(tag, dwSize);
FreeImage_SetTagLength(tag, dwSize);
FreeImage_SetTagValue(tag, varSrc.VT.pszVal);
break;
case DPKVT_LPWSTR:
FreeImage_SetTagType(tag, FIDT_UNDEFINED);
dwSize = (DWORD)(sizeof(U16) * (wcslen((wchar_t *) varSrc.VT.pwszVal) + 1)); // +1 for NULL term
FreeImage_SetTagCount(tag, dwSize);
FreeImage_SetTagLength(tag, dwSize);
FreeImage_SetTagValue(tag, varSrc.VT.pwszVal);
break;
case DPKVT_UI2:
FreeImage_SetTagType(tag, FIDT_SHORT);
FreeImage_SetTagCount(tag, 1);
FreeImage_SetTagLength(tag, 2);
FreeImage_SetTagValue(tag, &varSrc.VT.uiVal);
break;
case DPKVT_UI4:
FreeImage_SetTagType(tag, FIDT_LONG);
FreeImage_SetTagCount(tag, 1);
FreeImage_SetTagLength(tag, 4);
FreeImage_SetTagValue(tag, &varSrc.VT.ulVal);
break;
default:
assert(FALSE); // This case is not handled
break;
}
// get the tag desctiption
const char *description = s.getTagDescription(TagLib::EXIF_MAIN, tag_id);
FreeImage_SetTagDescription(tag, description);
// store the tag
FreeImage_SetMetadata(FIMD_EXIF_MAIN, dib, key, tag);
FreeImage_DeleteTag(tag);
}
return TRUE;
}
/**
Read JPEG-XR descriptive metadata and store as EXIF_MAIN metadata
@see ReadPropVariant
*/
static ERR
ReadDescriptiveMetadata(PKImageDecode *pID, FIBITMAP *dib) {
// get Exif TIFF metadata
const DESCRIPTIVEMETADATA *pDescMetadata = &pID->WMP.sDescMetadata;
// convert metadata to FITAG and store into the EXIF_MAIN metadata model
ReadPropVariant(WMP_tagImageDescription, pDescMetadata->pvarImageDescription, dib);
ReadPropVariant(WMP_tagCameraMake, pDescMetadata->pvarCameraMake, dib);
ReadPropVariant(WMP_tagCameraModel, pDescMetadata->pvarCameraModel, dib);
ReadPropVariant(WMP_tagSoftware, pDescMetadata->pvarSoftware, dib);
ReadPropVariant(WMP_tagDateTime, pDescMetadata->pvarDateTime, dib);
ReadPropVariant(WMP_tagArtist, pDescMetadata->pvarArtist, dib);
ReadPropVariant(WMP_tagCopyright, pDescMetadata->pvarCopyright, dib);
ReadPropVariant(WMP_tagRatingStars, pDescMetadata->pvarRatingStars, dib);
ReadPropVariant(WMP_tagRatingValue, pDescMetadata->pvarRatingValue, dib);
ReadPropVariant(WMP_tagCaption, pDescMetadata->pvarCaption, dib);
ReadPropVariant(WMP_tagDocumentName, pDescMetadata->pvarDocumentName, dib);
ReadPropVariant(WMP_tagPageName, pDescMetadata->pvarPageName, dib);
ReadPropVariant(WMP_tagPageNumber, pDescMetadata->pvarPageNumber, dib);
ReadPropVariant(WMP_tagHostComputer, pDescMetadata->pvarHostComputer, dib);
return WMP_errSuccess;
}
/**
Read ICC, XMP, Exif, Exif-GPS, IPTC, descriptive (i.e. Exif-TIFF) metadata
@see ReadProfile, ReadDescriptiveMetadata
*/
static ERR
ReadMetadata(PKImageDecode *pID, FIBITMAP *dib) {
ERR error_code = 0; // error code as returned by the interface
size_t currentPos = 0; // current stream position
WMPStream *pStream = pID->pStream;
WmpDEMisc *wmiDEMisc = &pID->WMP.wmiDEMisc;
BYTE *pbProfile = NULL;
try {
// save current position
error_code = pStream->GetPos(pStream, ¤tPos);
JXR_CHECK(error_code);
// ICC profile
if(0 != wmiDEMisc->uColorProfileByteCount) {
unsigned cbByteCount = wmiDEMisc->uColorProfileByteCount;
unsigned uOffset = wmiDEMisc->uColorProfileOffset;
error_code = ReadProfile(pStream, cbByteCount, uOffset, &pbProfile);
JXR_CHECK(error_code);
FreeImage_CreateICCProfile(dib, pbProfile, cbByteCount);
}
// XMP metadata
if(0 != wmiDEMisc->uXMPMetadataByteCount) {
unsigned cbByteCount = wmiDEMisc->uXMPMetadataByteCount;
unsigned uOffset = wmiDEMisc->uXMPMetadataOffset;
error_code = ReadProfile(pStream, cbByteCount, uOffset, &pbProfile);
JXR_CHECK(error_code);
// store the tag as XMP
FITAG *tag = FreeImage_CreateTag();
if(tag) {
FreeImage_SetTagLength(tag, cbByteCount);
FreeImage_SetTagCount(tag, cbByteCount);
FreeImage_SetTagType(tag, FIDT_ASCII);
FreeImage_SetTagValue(tag, pbProfile);
FreeImage_SetTagKey(tag, g_TagLib_XMPFieldName);
FreeImage_SetMetadata(FIMD_XMP, dib, FreeImage_GetTagKey(tag), tag);
FreeImage_DeleteTag(tag);
}
}
// IPTC metadata
if(0 != wmiDEMisc->uIPTCNAAMetadataByteCount) {
unsigned cbByteCount = wmiDEMisc->uIPTCNAAMetadataByteCount;
unsigned uOffset = wmiDEMisc->uIPTCNAAMetadataOffset;
error_code = ReadProfile(pStream, cbByteCount, uOffset, &pbProfile);
JXR_CHECK(error_code);
// decode the IPTC profile
read_iptc_profile(dib, pbProfile, cbByteCount);
}
// Exif metadata
if(0 != wmiDEMisc->uEXIFMetadataByteCount) {
unsigned cbByteCount = wmiDEMisc->uEXIFMetadataByteCount;
unsigned uOffset = wmiDEMisc->uEXIFMetadataOffset;
error_code = ReadProfile(pStream, cbByteCount, uOffset, &pbProfile);
JXR_CHECK(error_code);
// decode the Exif profile
jpegxr_read_exif_profile(dib, pbProfile, cbByteCount, uOffset);
}
// Exif-GPS metadata
if(0 != wmiDEMisc->uGPSInfoMetadataByteCount) {
unsigned cbByteCount = wmiDEMisc->uGPSInfoMetadataByteCount;
unsigned uOffset = wmiDEMisc->uGPSInfoMetadataOffset;
error_code = ReadProfile(pStream, cbByteCount, uOffset, &pbProfile);
JXR_CHECK(error_code);
// decode the Exif-GPS profile
jpegxr_read_exif_gps_profile(dib, pbProfile, cbByteCount, uOffset);
}
// free profile buffer
free(pbProfile);
// restore initial position
error_code = pID->pStream->SetPos(pID->pStream, currentPos);
JXR_CHECK(error_code);
// as a LAST STEP, read descriptive metadata
// these metadata overwrite possible identical Exif-TIFF metadata
// that could have been read inside the Exif IFD
return ReadDescriptiveMetadata(pID, dib);
} catch(...) {
// free profile buffer
free(pbProfile);
if(currentPos) {
// restore initial position
pStream->SetPos(pStream, currentPos);
}
return error_code;
}
}
// ==========================================================
// Metadata saving
// ==========================================================
/**
Convert a FITAG (coming from FIMD_EXIF_MAIN) to a DPKPROPVARIANT.
No allocation is needed here, the function just copy pointers when needed.
@see WriteDescriptiveMetadata
*/
static BOOL
WritePropVariant(FIBITMAP *dib, WORD tag_id, DPKPROPVARIANT & varDst) {
FITAG *tag = NULL;
TagLib& s = TagLib::instance();
// clear output DPKPROPVARIANT
varDst.vt = DPKVT_EMPTY;
// given the tag id, get the tag key
const char *key = s.getTagFieldName(TagLib::EXIF_MAIN, tag_id, NULL);
// then, get the tag info
if(!FreeImage_GetMetadata(FIMD_EXIF_MAIN, dib, key, &tag)) {
return FALSE;
}
// set the tag value
switch(FreeImage_GetTagType(tag)) {
case FIDT_ASCII:
varDst.vt = DPKVT_LPSTR;
varDst.VT.pszVal = (char*)FreeImage_GetTagValue(tag);
break;
case FIDT_BYTE:
case FIDT_UNDEFINED:
varDst.vt = DPKVT_LPWSTR;
varDst.VT.pwszVal = (U16*)FreeImage_GetTagValue(tag);
break;
case FIDT_SHORT:
varDst.vt = DPKVT_UI2;
varDst.VT.uiVal = *((U16*)FreeImage_GetTagValue(tag));
break;
case FIDT_LONG:
varDst.vt = DPKVT_UI4;
varDst.VT.ulVal = *((U32*)FreeImage_GetTagValue(tag));
break;
default:
break;
}
return TRUE;
}
/**
Write EXIF_MAIN metadata to JPEG-XR descriptive metadata
@see WritePropVariant
*/
static ERR
WriteDescriptiveMetadata(PKImageEncode *pIE, FIBITMAP *dib) {
ERR error_code = 0; // error code as returned by the interface
DESCRIPTIVEMETADATA DescMetadata;
// fill the DESCRIPTIVEMETADATA structure (use pointers to arrays when needed)
WritePropVariant(dib, WMP_tagImageDescription, DescMetadata.pvarImageDescription);
WritePropVariant(dib, WMP_tagCameraMake, DescMetadata.pvarCameraMake);
WritePropVariant(dib, WMP_tagCameraModel, DescMetadata.pvarCameraModel);
WritePropVariant(dib, WMP_tagSoftware, DescMetadata.pvarSoftware);
WritePropVariant(dib, WMP_tagDateTime, DescMetadata.pvarDateTime);
WritePropVariant(dib, WMP_tagArtist, DescMetadata.pvarArtist);
WritePropVariant(dib, WMP_tagCopyright, DescMetadata.pvarCopyright);
WritePropVariant(dib, WMP_tagRatingStars, DescMetadata.pvarRatingStars);
WritePropVariant(dib, WMP_tagRatingValue, DescMetadata.pvarRatingValue);
WritePropVariant(dib, WMP_tagCaption, DescMetadata.pvarCaption);
WritePropVariant(dib, WMP_tagDocumentName, DescMetadata.pvarDocumentName);
WritePropVariant(dib, WMP_tagPageName, DescMetadata.pvarPageName);
WritePropVariant(dib, WMP_tagPageNumber, DescMetadata.pvarPageNumber);
WritePropVariant(dib, WMP_tagHostComputer, DescMetadata.pvarHostComputer);
// copy the structure to the encoder
error_code = pIE->SetDescriptiveMetadata(pIE, &DescMetadata);
// no need to free anything here
return error_code;
}
/**
Write ICC, XMP, Exif, Exif-GPS, IPTC, descriptive (i.e. Exif-TIFF) metadata
*/
static ERR
WriteMetadata(PKImageEncode *pIE, FIBITMAP *dib) {
ERR error_code = 0; // error code as returned by the interface
BYTE *profile = NULL;
unsigned profile_size = 0;
try {
// write ICC profile
{
FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
if(iccProfile->data) {
error_code = pIE->SetColorContext(pIE, (U8*)iccProfile->data, iccProfile->size);
JXR_CHECK(error_code);
}
}
// write descriptive metadata
if(FreeImage_GetMetadataCount(FIMD_EXIF_MAIN, dib)) {
error_code = WriteDescriptiveMetadata(pIE, dib);
JXR_CHECK(error_code);
}
// write IPTC metadata
if(FreeImage_GetMetadataCount(FIMD_IPTC, dib)) {
// create a binary profile
if(write_iptc_profile(dib, &profile, &profile_size)) {
// write the profile
error_code = PKImageEncode_SetIPTCNAAMetadata_WMP(pIE, profile, profile_size);
JXR_CHECK(error_code);
// release profile
free(profile);
profile = NULL;
}
}
// write XMP metadata
{
FITAG *tag_xmp = NULL;
if(FreeImage_GetMetadata(FIMD_XMP, dib, g_TagLib_XMPFieldName, &tag_xmp)) {
error_code = PKImageEncode_SetXMPMetadata_WMP(pIE, (BYTE*)FreeImage_GetTagValue(tag_xmp), FreeImage_GetTagLength(tag_xmp));
JXR_CHECK(error_code);
}
}
// write Exif metadata
{
if(tiff_get_ifd_profile(dib, FIMD_EXIF_EXIF, &profile, &profile_size)) {
error_code = PKImageEncode_SetEXIFMetadata_WMP(pIE, profile, profile_size);
JXR_CHECK(error_code);
// release profile
free(profile);
profile = NULL;
}
}
// write Exif GPS metadata
{
if(tiff_get_ifd_profile(dib, FIMD_EXIF_GPS, &profile, &profile_size)) {
error_code = PKImageEncode_SetGPSInfoMetadata_WMP(pIE, profile, profile_size);
JXR_CHECK(error_code);
// release profile
free(profile);
profile = NULL;
}
}
return WMP_errSuccess;
} catch(...) {
free(profile);
return error_code;
}
}
// ==========================================================
// Quantization tables (Y, U, V, YHP, UHP, VHP),
// optimized for PSNR
// ==========================================================
static const int DPK_QPS_420[11][6] = { // for 8 bit only
{ 66, 65, 70, 72, 72, 77 },
{ 59, 58, 63, 64, 63, 68 },
{ 52, 51, 57, 56, 56, 61 },
{ 48, 48, 54, 51, 50, 55 },
{ 43, 44, 48, 46, 46, 49 },
{ 37, 37, 42, 38, 38, 43 },
{ 26, 28, 31, 27, 28, 31 },
{ 16, 17, 22, 16, 17, 21 },
{ 10, 11, 13, 10, 10, 13 },
{ 5, 5, 6, 5, 5, 6 },
{ 2, 2, 3, 2, 2, 2 }
};
static const int DPK_QPS_8[12][6] = {
{ 67, 79, 86, 72, 90, 98 },
{ 59, 74, 80, 64, 83, 89 },
{ 53, 68, 75, 57, 76, 83 },
{ 49, 64, 71, 53, 70, 77 },
{ 45, 60, 67, 48, 67, 74 },
{ 40, 56, 62, 42, 59, 66 },
{ 33, 49, 55, 35, 51, 58 },
{ 27, 44, 49, 28, 45, 50 },
{ 20, 36, 42, 20, 38, 44 },
{ 13, 27, 34, 13, 28, 34 },
{ 7, 17, 21, 8, 17, 21 }, // Photoshop 100%
{ 2, 5, 6, 2, 5, 6 }
};
static const int DPK_QPS_16[11][6] = {
{ 197, 203, 210, 202, 207, 213 },
{ 174, 188, 193, 180, 189, 196 },
{ 152, 167, 173, 156, 169, 174 },
{ 135, 152, 157, 137, 153, 158 },
{ 119, 137, 141, 119, 138, 142 },
{ 102, 120, 125, 100, 120, 124 },
{ 82, 98, 104, 79, 98, 103 },
{ 60, 76, 81, 58, 76, 81 },
{ 39, 52, 58, 36, 52, 58 },
{ 16, 27, 33, 14, 27, 33 },
{ 5, 8, 9, 4, 7, 8 }
};
static const int DPK_QPS_16f[11][6] = {
{ 148, 177, 171, 165, 187, 191 },
{ 133, 155, 153, 147, 172, 181 },
{ 114, 133, 138, 130, 157, 167 },
{ 97, 118, 120, 109, 137, 144 },
{ 76, 98, 103, 85, 115, 121 },
{ 63, 86, 91, 62, 96, 99 },
{ 46, 68, 71, 43, 73, 75 },
{ 29, 48, 52, 27, 48, 51 },
{ 16, 30, 35, 14, 29, 34 },
{ 8, 14, 17, 7, 13, 17 },
{ 3, 5, 7, 3, 5, 6 }
};
static const int DPK_QPS_32f[11][6] = {
{ 194, 206, 209, 204, 211, 217 },
{ 175, 187, 196, 186, 193, 205 },
{ 157, 170, 177, 167, 180, 190 },
{ 133, 152, 156, 144, 163, 168 },
{ 116, 138, 142, 117, 143, 148 },
{ 98, 120, 123, 96, 123, 126 },
{ 80, 99, 102, 78, 99, 102 },
{ 65, 79, 84, 63, 79, 84 },
{ 48, 61, 67, 45, 60, 66 },
{ 27, 41, 46, 24, 40, 45 },
{ 3, 22, 24, 2, 21, 22 }
};
// ==========================================================
// Plugin Implementation
// ==========================================================
static const char * DLL_CALLCONV
Format() {
return "JPEG-XR";
}
static const char * DLL_CALLCONV
Description() {
return "JPEG XR image format";
}
static const char * DLL_CALLCONV
Extension() {
return "jxr,wdp,hdp";
}
static const char * DLL_CALLCONV
RegExpr() {
return NULL;
}
static const char * DLL_CALLCONV
MimeType() {
return "image/vnd.ms-photo";
}
static BOOL DLL_CALLCONV
Validate(FreeImageIO *io, fi_handle handle) {
BYTE jxr_signature[3] = { 0x49, 0x49, 0xBC };
BYTE signature[3] = { 0, 0, 0 };
io->read_proc(&signature, 1, 3, handle);
return (memcmp(jxr_signature, signature, 3) == 0);
}
static BOOL DLL_CALLCONV
SupportsExportDepth(int depth) {
return (
(depth == 1) ||
(depth == 8) ||
(depth == 16) ||
(depth == 24) ||
(depth == 32)
);
}
static BOOL DLL_CALLCONV
SupportsExportType(FREE_IMAGE_TYPE type) {
return (
(type == FIT_BITMAP) ||
(type == FIT_UINT16) ||
(type == FIT_RGB16) ||
(type == FIT_RGBA16) ||
(type == FIT_FLOAT) ||
(type == FIT_RGBF) ||
(type == FIT_RGBAF)
);
}
static BOOL DLL_CALLCONV
SupportsICCProfiles() {
return TRUE;
}
static BOOL DLL_CALLCONV
SupportsNoPixels() {
return TRUE;
}
// ==========================================================
// Open & Close
// ==========================================================
static void * DLL_CALLCONV
Open(FreeImageIO *io, fi_handle handle, BOOL read) {
WMPStream *pStream = NULL; // stream interface
if(io && handle) {
// allocate the FreeImageIO stream wrapper
FreeImageJXRIO *jxr_io = (FreeImageJXRIO*)malloc(sizeof(FreeImageJXRIO));
if(jxr_io) {
jxr_io->io = io;
jxr_io->handle = handle;
// create a JXR stream wrapper
if(_jxr_io_Create(&pStream, jxr_io) != WMP_errSuccess) {
free(jxr_io);
return NULL;
}
}
}
return pStream;
}
static void DLL_CALLCONV
Close(FreeImageIO *io, fi_handle handle, void *data) {
WMPStream *pStream = (WMPStream*)data;
if(pStream) {
// free the FreeImageIO stream wrapper
FreeImageJXRIO *jxr_io = (FreeImageJXRIO*)pStream->state.pvObj;
free(jxr_io);
// free the JXR stream wrapper
pStream->fMem = TRUE;
_jxr_io_Close(&pStream);
}
}
// ==========================================================
// Load
// ==========================================================
/**
Set decoder parameters
@param pDecoder Decoder handle
@param flags FreeImage load flags
*/
static void
SetDecoderParameters(PKImageDecode *pDecoder, int flags) {
// load image & alpha for formats with alpha
pDecoder->WMP.wmiSCP.uAlphaMode = 2;
// more options to come ...
}
/**
Copy or convert & copy decoded pixels into the dib
@param pDecoder Decoder handle
@param out_guid_format Target guid format
@param dib Output dib