View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging;
18  
19  /**
20   * Used to specify pixel density and physical dimensions when reading or storing image information.
21   */
22  public final class PixelDensity {
23      private static final int PIXEL_NO_UNIT = 0;
24      private static final int PIXEL_PER_INCH = 254;
25      private static final int PIXEL_PER_METRE = 10000;
26      private static final int PIXEL_PER_CENTIMETRE = 100;
27  
28      public static PixelDensity createFromPixelsPerCentimetre(final double x, final double y) {
29          return new PixelDensity(x, y, PIXEL_PER_CENTIMETRE);
30      }
31  
32      public static PixelDensity createFromPixelsPerInch(final double x, final double y) {
33          return new PixelDensity(x, y, PIXEL_PER_INCH);
34      }
35  
36      public static PixelDensity createFromPixelsPerMetre(final double x, final double y) {
37          return new PixelDensity(x, y, PIXEL_PER_METRE);
38      }
39  
40      public static PixelDensity createUnitless(final double x, final double y) {
41          return new PixelDensity(x, y, PIXEL_NO_UNIT);
42      }
43  
44      private final double horizontalDensity;
45  
46      private final double verticalDensity;
47  
48      // / One-tenth of a millimetre units.
49      private final int unitLength;
50  
51      private PixelDensity(final double horizontalDensity, final double verticalDensity, final int unitLength) {
52          this.horizontalDensity = horizontalDensity;
53          this.verticalDensity = verticalDensity;
54          this.unitLength = unitLength;
55      }
56  
57      public double getRawHorizontalDensity() {
58          return horizontalDensity;
59      }
60  
61      public double getRawVerticalDensity() {
62          return verticalDensity;
63      }
64  
65      public double horizontalDensityCentimetres() {
66          if (isInCentimetres()) {
67              return horizontalDensity;
68          }
69          return horizontalDensity * PIXEL_PER_CENTIMETRE / unitLength;
70      }
71  
72      public double horizontalDensityInches() {
73          if (isInInches()) {
74              return horizontalDensity;
75          }
76          return horizontalDensity * PIXEL_PER_INCH / unitLength;
77      }
78  
79      public double horizontalDensityMetres() {
80          if (isInMetres()) {
81              return horizontalDensity;
82          }
83          return horizontalDensity * PIXEL_PER_METRE / unitLength;
84      }
85  
86      public boolean isInCentimetres() {
87          return unitLength == PIXEL_PER_CENTIMETRE;
88      }
89  
90      public boolean isInInches() {
91          return unitLength == PIXEL_PER_INCH;
92      }
93  
94      public boolean isInMetres() {
95          return unitLength == PIXEL_PER_METRE;
96      }
97  
98      public boolean isUnitless() {
99          return unitLength == PIXEL_NO_UNIT;
100     }
101 
102     public double verticalDensityCentimetres() {
103         if (isInCentimetres()) {
104             return verticalDensity;
105         }
106         return verticalDensity * PIXEL_PER_CENTIMETRE / unitLength;
107     }
108 
109     public double verticalDensityInches() {
110         if (isInInches()) {
111             return verticalDensity;
112         }
113         return verticalDensity * PIXEL_PER_INCH / unitLength;
114     }
115 
116     public double verticalDensityMetres() {
117         if (isInMetres()) {
118             return verticalDensity;
119         }
120         return verticalDensity * PIXEL_PER_METRE / unitLength;
121     }
122 }