001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging;
018
019/**
020 * Used to specify pixel density and physical dimensions when reading or storing image information.
021 */
022public final class PixelDensity {
023    private static final int PIXEL_NO_UNIT = 0;
024    private static final int PIXEL_PER_INCH = 254;
025    private static final int PIXEL_PER_METRE = 10000;
026    private static final int PIXEL_PER_CENTIMETRE = 100;
027
028    public static PixelDensity createFromPixelsPerCentimetre(final double x, final double y) {
029        return new PixelDensity(x, y, PIXEL_PER_CENTIMETRE);
030    }
031
032    public static PixelDensity createFromPixelsPerInch(final double x, final double y) {
033        return new PixelDensity(x, y, PIXEL_PER_INCH);
034    }
035
036    public static PixelDensity createFromPixelsPerMetre(final double x, final double y) {
037        return new PixelDensity(x, y, PIXEL_PER_METRE);
038    }
039
040    public static PixelDensity createUnitless(final double x, final double y) {
041        return new PixelDensity(x, y, PIXEL_NO_UNIT);
042    }
043
044    private final double horizontalDensity;
045
046    private final double verticalDensity;
047
048    // / One-tenth of a millimetre units.
049    private final int unitLength;
050
051    private PixelDensity(final double horizontalDensity, final double verticalDensity, final int unitLength) {
052        this.horizontalDensity = horizontalDensity;
053        this.verticalDensity = verticalDensity;
054        this.unitLength = unitLength;
055    }
056
057    public double getRawHorizontalDensity() {
058        return horizontalDensity;
059    }
060
061    public double getRawVerticalDensity() {
062        return verticalDensity;
063    }
064
065    public double horizontalDensityCentimetres() {
066        if (isInCentimetres()) {
067            return horizontalDensity;
068        }
069        return horizontalDensity * PIXEL_PER_CENTIMETRE / unitLength;
070    }
071
072    public double horizontalDensityInches() {
073        if (isInInches()) {
074            return horizontalDensity;
075        }
076        return horizontalDensity * PIXEL_PER_INCH / unitLength;
077    }
078
079    public double horizontalDensityMetres() {
080        if (isInMetres()) {
081            return horizontalDensity;
082        }
083        return horizontalDensity * PIXEL_PER_METRE / unitLength;
084    }
085
086    public boolean isInCentimetres() {
087        return unitLength == PIXEL_PER_CENTIMETRE;
088    }
089
090    public boolean isInInches() {
091        return unitLength == PIXEL_PER_INCH;
092    }
093
094    public boolean isInMetres() {
095        return unitLength == PIXEL_PER_METRE;
096    }
097
098    public boolean isUnitless() {
099        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}