    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
    021 * storing image information.
    022 */
    023public final class PixelDensity {
    024    private static final int PIXEL_NO_UNIT = 0;
    025    private static final int PIXEL_PER_INCH = 254;
    026    private static final int PIXEL_PER_METRE = 10000;
    027    private static final int PIXEL_PER_CENTIMETRE = 100;
    028
    029    private final double horizontalDensity;
    030    private final double verticalDensity;
    031    // / One-tenth of a millimetre units.
    032    private final int unitLength;
    033
    034    private PixelDensity(final double horizontalDensity, final double verticalDensity,
    035            final int unitLength) {
    036        this.horizontalDensity = horizontalDensity;
    037        this.verticalDensity = verticalDensity;
    038        this.unitLength = unitLength;
    039    }
    040
    041    public static PixelDensity createUnitless(final double x, final double y) {
    042        return new PixelDensity(x, y, PIXEL_NO_UNIT);
    043    }
    044
    045    public static PixelDensity createFromPixelsPerInch(final double x, final double y) {
    046        return new PixelDensity(x, y, PIXEL_PER_INCH);
    047    }
    048
    049    public static PixelDensity createFromPixelsPerMetre(final double x, final double y) {
    050        return new PixelDensity(x, y, PIXEL_PER_METRE);
    051    }
    052
    053    public static PixelDensity createFromPixelsPerCentimetre(final double x, final double y) {
    054        return new PixelDensity(x, y, PIXEL_PER_CENTIMETRE);
    055    }
    056
    057    public boolean isUnitless() {
    058        return unitLength == PIXEL_NO_UNIT;
    059    }
    060
    061    public boolean isInInches() {
    062        return unitLength == PIXEL_PER_INCH;
    063    }
    064
    065    public boolean isInCentimetres() {
    066        return unitLength == PIXEL_PER_CENTIMETRE;
    067    }
    068
    069    public boolean isInMetres() {
    070        return unitLength == PIXEL_PER_METRE;
    071    }
    072
    073    public double getRawHorizontalDensity() {
    074        return horizontalDensity;
    075    }
    076
    077    public double getRawVerticalDensity() {
    078        return verticalDensity;
    079    }
    080
    081    public double horizontalDensityInches() {
    082        if (isInInches()) {
    083            return horizontalDensity;
    084        }
    085        return horizontalDensity * PIXEL_PER_INCH / unitLength;
    086    }
    087
    088    public double verticalDensityInches() {
    089        if (isInInches()) {
    090            return verticalDensity;
    091        }
    092        return verticalDensity * PIXEL_PER_INCH / unitLength;
    093    }
    094
    095    public double horizontalDensityMetres() {
    096        if (isInMetres()) {
    097            return horizontalDensity;
    098        }
    099        return horizontalDensity * PIXEL_PER_METRE / unitLength;
    100    }
    101
    102    public double verticalDensityMetres() {
    103        if (isInMetres()) {
    104            return verticalDensity;
    105        }
    106        return verticalDensity * PIXEL_PER_METRE / unitLength;
    107    }
    108
    109    public double horizontalDensityCentimetres() {
    110        if (isInCentimetres()) {
    111            return horizontalDensity;
    112        }
    113        return horizontalDensity * PIXEL_PER_CENTIMETRE / unitLength;
    114    }
    115
    116    public double verticalDensityCentimetres() {
    117        if (isInCentimetres()) {
    118            return verticalDensity;
    119        }
    120        return verticalDensity * PIXEL_PER_CENTIMETRE / unitLength;
    121    }
    122}