/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.imaging;

/**
 * Used to specify pixel density and physical dimensions when reading or
 * storing image information.
 */
public final class PixelDensity {
    private final double horizontalDensity;
    private final double verticalDensity;
    // / One-tenth of a millimetre units.
    private final int unitLength;

    private PixelDensity(final double horizontalDensity, final double verticalDensity,
            final int unitLength) {
        this.horizontalDensity = horizontalDensity;
        this.verticalDensity = verticalDensity;
        this.unitLength = unitLength;
    }

    public static PixelDensity createUnitless(final double x, final double y) {
        return new PixelDensity(x, y, 0);
    }

    public static PixelDensity createFromPixelsPerInch(final double x, final double y) {
        return new PixelDensity(x, y, 254);
    }

    public static PixelDensity createFromPixelsPerMetre(final double x, final double y) {
        return new PixelDensity(x, y, 10000);
    }

    public static PixelDensity createFromPixelsPerCentimetre(final double x, final double y) {
        return new PixelDensity(x, y, 100);
    }

    public boolean isUnitless() {
        return unitLength == 0;
    }

    public boolean isInInches() {
        return unitLength == 254;
    }

    public boolean isInCentimetres() {
        return unitLength == 100;
    }

    public boolean isInMetres() {
        return unitLength == 10000;
    }

    public double getRawHorizontalDensity() {
        return horizontalDensity;
    }

    public double getRawVerticalDensity() {
        return verticalDensity;
    }

    public double horizontalDensityInches() {
        if (isInInches()) {
            return horizontalDensity;
        } else {
            return horizontalDensity * 254 / unitLength;
        }
    }

    public double verticalDensityInches() {
        if (isInInches()) {
            return verticalDensity;
        } else {
            return verticalDensity * 254 / unitLength;
        }
    }

    public double horizontalDensityMetres() {
        if (isInMetres()) {
            return horizontalDensity;
        } else {
            return horizontalDensity * 10000 / unitLength;
        }
    }

    public double verticalDensityMetres() {
        if (isInMetres()) {
            return verticalDensity;
        } else {
            return verticalDensity * 10000 / unitLength;
        }
    }

    public double horizontalDensityCentimetres() {
        if (isInCentimetres()) {
            return horizontalDensity;
        } else {
            return horizontalDensity * 100 / unitLength;
        }
    }

    public double verticalDensityCentimetres() {
        if (isInCentimetres()) {
            return verticalDensity;
        } else {
            return verticalDensity * 100 / unitLength;
        }
    }
}

@@ -978,16 +978,227 @@
 nsity %7B%0A
+    private static final int PIXEL_NO_UNIT = 0;%0A    private static final int PIXEL_PER_INCH = 254;%0A    private static final int PIXEL_PER_METRE = 10000;%0A    private static final int PIXEL_PER_CENTIMETRE = 100;%0A%0A
     priv
@@ -1738,17 +1738,29 @@
 y(x, y, 
-0
+PIXEL_NO_UNIT
 );%0A    %7D
@@ -1888,19 +1888,30 @@
 y(x, y, 
-254
+PIXEL_PER_INCH
 );%0A    %7D
@@ -2040,21 +2040,31 @@
 y(x, y, 
-10000
+PIXEL_PER_METRE
 );%0A    %7D
@@ -2198,19 +2198,36 @@
 y(x, y, 
-100
+PIXEL_PER_CENTIMETRE
 );%0A    %7D
@@ -2291,17 +2291,29 @@
 ngth == 
-0
+PIXEL_NO_UNIT
 ;%0A    %7D%0A
@@ -2376,19 +2376,30 @@
 ngth == 
-254
+PIXEL_PER_INCH
 ;%0A    %7D%0A
@@ -2467,19 +2467,36 @@
 ngth == 
-100
+PIXEL_PER_CENTIMETRE
 ;%0A    %7D%0A
@@ -2559,21 +2559,31 @@
 ngth == 
-10000
+PIXEL_PER_METRE
 ;%0A    %7D%0A
@@ -2913,35 +2913,46 @@
 zontalDensity * 
-254
+PIXEL_PER_INCH
  / unitLength;%0A 
@@ -3129,19 +3129,30 @@
 nsity * 
-254
+PIXEL_PER_INCH
  / unitL
@@ -3335,37 +3335,47 @@
 zontalDensity * 
-10000
+PIXEL_PER_METRE
  / unitLength;%0A 
@@ -3552,21 +3552,31 @@
 nsity * 
-10000
+PIXEL_PER_METRE
  / unitL
@@ -3769,35 +3769,52 @@
 zontalDensity * 
-100
+PIXEL_PER_CENTIMETRE
  / unitLength;%0A 
@@ -4001,19 +4001,36 @@
 nsity * 
-100
+PIXEL_PER_CENTIMETRE
  / unitL

Extract magic numbers into constants