]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/jpeg/ExifGateway.java
Version 15.2, November 2013
[GpsPrune.git] / tim / prune / jpeg / ExifGateway.java
1 package tim.prune.jpeg;
2
3 import java.io.File;
4 import javax.swing.JOptionPane;
5 import tim.prune.I18nManager;
6
7 /**
8  * Skeleton gateway to the Exif functions.
9  * This is required by Debian to divert Exif handling
10  * to the external libmetadata-extractor-java library
11  * instead of the included modified routines.
12  *
13  * Switching between internal and external libraries is
14  * handled by the ExifLibrarySwitch
15  */
16 public abstract class ExifGateway
17 {
18         /** Library object to call */
19         private static ExifLibrary _exifLibrary = null;
20         /** Flag to set whether failure warning has already been shown */
21         private static boolean _exifFailWarned = false;
22
23         /** Static block to initialise library */
24         static
25         {
26                 String libraryClass = ExifLibrarySwitch.USE_INTERNAL_LIBRARY?"InternalExifLibrary":"ExternalExifLibrary";
27                 try
28                 {
29                         _exifLibrary = (ExifLibrary) Class.forName("tim.prune.jpeg." + libraryClass).newInstance();
30                 }
31                 catch (Throwable nolib) {_exifLibrary = null;}
32         }
33
34
35         /**
36          * Get the Jpeg data from the given file
37          * @param inFile file to read
38          * @return jpeg data, or null if none found
39          */
40         public static JpegData getJpegData(File inFile)
41         {
42                 try
43                 {
44                         // Call library (if found)
45                         if (_exifLibrary != null) {
46                                 JpegData data = _exifLibrary.getJpegData(inFile);
47                                 return data;
48                         }
49                 }
50                 catch (LinkageError nolib) {
51                         System.err.println("Link: " + nolib.getMessage());
52                         nolib.printStackTrace();
53                 }
54                 // Not successful - warn if necessary
55                 if (!_exifFailWarned)
56                 {
57                         JOptionPane.showMessageDialog(null, I18nManager.getText("error.jpegload.exifreadfailed"),
58                                 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.WARNING_MESSAGE);
59                         _exifFailWarned = true;
60                 }
61                 return null;
62         }
63
64         /**
65          * @return key to use to describe library, matching key for about dialog
66          */
67         public static String getDescriptionKey()
68         {
69                 String key = ExifLibrarySwitch.USE_INTERNAL_LIBRARY?"internal":"external";
70                 if (_exifLibrary == null || !_exifLibrary.looksOK()) {key = key + ".failed";}
71                 return key;
72         }
73
74
75         /**
76          * @param inNumerator numerator from Rational
77          * @param inDenominator denominator from Rational
78          * @return the value of the specified number as a positive <code>double</code>.
79          * Prevents interpretation of 32 bit numbers as negative, and forces a positive answer
80          */
81         public static final double convertToPositiveValue(int inNumerator, int inDenominator)
82         {
83                 if (inDenominator == 0) return 0.0;
84                 double numeratorDbl = inNumerator;
85                 double denomDbl = inDenominator;
86                 if (inNumerator >= 0)
87                         return numeratorDbl / denomDbl;
88                 final double correction = Math.pow(2.0, 32);
89                 numeratorDbl += correction;
90                 if (inDenominator < 0) denomDbl += correction;
91                 return numeratorDbl / denomDbl;
92         }
93
94
95         /**
96          * @param inNumerator numerator from Rational
97          * @param inDenominator denominator from Rational
98          * @return the value of the specified number as a positive <code>double</code>.
99          * Forces a positive answer
100          */
101         public static final double convertToPositiveValue(long inNumerator, long inDenominator)
102         {
103                 if (inDenominator == 0L) return 0.0;
104                 final double numeratorDbl = inNumerator;
105                 final double denomDbl = inDenominator;
106                 return numeratorDbl / denomDbl;
107         }
108 }