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 } else { 085 return horizontalDensity * PIXEL_PER_INCH / unitLength; 086 } 087 } 088 089 public double verticalDensityInches() { 090 if (isInInches()) { 091 return verticalDensity; 092 } else { 093 return verticalDensity * PIXEL_PER_INCH / unitLength; 094 } 095 } 096 097 public double horizontalDensityMetres() { 098 if (isInMetres()) { 099 return horizontalDensity; 100 } else { 101 return horizontalDensity * PIXEL_PER_METRE / unitLength; 102 } 103 } 104 105 public double verticalDensityMetres() { 106 if (isInMetres()) { 107 return verticalDensity; 108 } else { 109 return verticalDensity * PIXEL_PER_METRE / unitLength; 110 } 111 } 112 113 public double horizontalDensityCentimetres() { 114 if (isInCentimetres()) { 115 return horizontalDensity; 116 } else { 117 return horizontalDensity * PIXEL_PER_CENTIMETRE / unitLength; 118 } 119 } 120 121 public double verticalDensityCentimetres() { 122 if (isInCentimetres()) { 123 return verticalDensity; 124 } else { 125 return verticalDensity * PIXEL_PER_CENTIMETRE / unitLength; 126 } 127 } 128}