1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging;
18
19
20
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
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 }