]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/jpeg/ExifGateway.java
9fefadf33c78003248e12e119481346c667166d6
[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                 // Not successful - warn if necessary
52                 if (!_exifFailWarned)
53                 {
54                         JOptionPane.showMessageDialog(null, I18nManager.getText("error.jpegload.exifreadfailed"),
55                                 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.WARNING_MESSAGE);
56                         _exifFailWarned = true;
57                 }
58                 return null;
59         }
60
61         /**
62          * @return key to use to describe library, matching key for about dialog
63          */
64         public static String getDescriptionKey()
65         {
66                 String key = ExifLibrarySwitch.USE_INTERNAL_LIBRARY?"internal":"external";
67                 if (_exifLibrary == null || !_exifLibrary.looksOK()) {key = key + ".failed";}
68                 return key;
69         }
70
71
72
73         /**
74          * @param inNumerator numerator from Rational
75          * @param inDenominator denominator from Rational
76          * @return the value of the specified number as a positive <code>double</code>.
77          * Prevents interpretation of 32 bit numbers as negative, and forces a positive answer
78          */
79         public static final double convertToPositiveValue(int inNumerator, int inDenominator)
80         {
81                 if (inDenominator == 0) return 0.0;
82                 double numeratorDbl = inNumerator;
83                 double denomDbl = inDenominator;
84                 if (inNumerator >= 0)
85                         return numeratorDbl / denomDbl;
86                 final double correction = Math.pow(2.0, 32);
87                 numeratorDbl += correction;
88                 if (inDenominator < 0) denomDbl += correction;
89                 return numeratorDbl / denomDbl;
90         }
91 }