Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Unit for "Pixel per Inch" added. #19

Merged
merged 1 commit into from
Nov 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions common-java8/src/main/java/systems/uom/common/NonSI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
package systems.uom.common;

import static tec.uom.se.unit.Units.ONE;
import static tec.uom.se.unit.MetricPrefix.CENTI;
import static tec.uom.se.unit.Units.AMPERE;
import static tec.uom.se.unit.Units.BECQUEREL;
Expand All @@ -41,6 +40,7 @@
import static tec.uom.se.unit.Units.METRES_PER_SQUARE_SECOND;
import static tec.uom.se.unit.Units.MOLE;
import static tec.uom.se.unit.Units.NEWTON;
import static tec.uom.se.unit.Units.ONE;
import static tec.uom.se.unit.Units.PASCAL;
import static tec.uom.se.unit.Units.RADIAN;
import static tec.uom.se.unit.Units.SECOND;
Expand Down Expand Up @@ -84,6 +84,7 @@
import si.uom.quantity.KinematicViscosity;
import systems.uom.quantity.Information;
import systems.uom.quantity.InformationRate;
import systems.uom.quantity.Resolution;
import tec.uom.se.AbstractSystemOfUnits;
import tec.uom.se.AbstractUnit;
import tec.uom.se.function.LogConverter;
Expand Down Expand Up @@ -247,20 +248,6 @@ static NonSI getInstance() {
static final Unit<Length> POINT = addUnit(INCH.multiply(13837)
.divide(1000000));

/**
* A unit of length equal to <code>1/72 {@link #INCH}</code> (standard name
* <code>pixel</code>). It is the American point rounded to an even 1/72
* inch.
*
* @see #POINT
*/
static final Unit<Length> PIXEL = addUnit(INCH.divide(72));

/**
* Equivalent {@link #PIXEL}
*/
static final Unit<Length> COMPUTER_POINT = PIXEL;

// ////////////
// Duration //
// ////////////
Expand Down Expand Up @@ -446,6 +433,29 @@ static NonSI getInstance() {
* Equivalent {@link #BYTE}
*/
static final Unit<Information> OCTET = BYTE;

/**
* A pixel has 4 channels which define transparency (alpha), red, green and blue color values.
* Each channel is one byte wide.
*
* @see #BYTE
*/
static final Unit<Information> PIXEL = addUnit(BYTE.multiply(4.0));

/**
* Pixel per inch describe the resolution for any output device (monitor, printer) that deals with
* outputting digital raster images.
*
* @see #INCH
* @see #PIXEL
*/
static final Unit<Resolution> PIXEL_PER_INCH = addUnit(PIXEL
.divide(INCH).asType(Resolution.class));

/**
* Equivalent {@link #PIXEL}
*/
static final Unit<Information> COMPUTER_POINT = PIXEL;

// ////////////////////
// Electric current //
Expand Down Expand Up @@ -700,4 +710,4 @@ private static <U extends AbstractUnit<?>> U addUnit(U unit, Class<? extends Qu
// protected static <U extends Unit<?>> U addUnit(U unit, String name) {
//
// }
}
}
34 changes: 34 additions & 0 deletions common-java8/src/test/java/systems/uom/common/PixelTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package systems.uom.common;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import javax.measure.IncommensurableException;
import javax.measure.UnconvertibleException;
import javax.measure.quantity.Length;

import org.junit.Test;

import systems.uom.quantity.Information;
import systems.uom.quantity.Resolution;
import tec.uom.se.ComparableQuantity;
import tec.uom.se.quantity.Quantities;
import tec.uom.se.unit.MetricPrefix;
import tec.uom.se.unit.Units;

public class PixelTest {

@Test
public void testPPItoMetreConversion() throws UnconvertibleException, IncommensurableException {
/*
* 960 Pixel are .254 m long if the PPI is 96. 1 inch (0.0254 m) / 96 ppi x (96 x 10) px = 0.254 m
*/
final ComparableQuantity<Information> screenWidth = Quantities.getQuantity(960, NonSI.PIXEL);
final ComparableQuantity<Resolution> screenResolution = Quantities.getQuantity(96, NonSI.PIXEL_PER_INCH);

ComparableQuantity<Length> ppi = screenWidth.divide(screenResolution).asType(Length.class);
ComparableQuantity<Length> pixelToMetre = ppi.to(Units.METRE);
assertThat(pixelToMetre.getValue().doubleValue(),is(.254));
assertThat(pixelToMetre.to(MetricPrefix.CENTI(Units.METRE)).getValue().doubleValue(), is(25.4));
}
}
42 changes: 42 additions & 0 deletions quantity/src/main/java/systems/uom/quantity/Resolution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Unit-API - Units of Measurement API for Java
* Copyright (c) 2005-2015, Jean-Marie Dautelle, Werner Keil, V2COM.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package systems.uom.quantity;

import javax.measure.Quantity;

/**
* Measure of information per length.
* The metric system unit for this quantity is "bit".
*
* @author <a href="mailto:oliver+uom.bugabinga.net">Oliver Krylow</a>
* @author <a href="mailto:[email protected]">Jean-Marie Dautelle</a>
* @author <a href="mailto:[email protected]">Werner Keil</a>
* @version 1.2
*
* @see <a href="https://en.wikipedia.org/wiki/Display_resolution">Wikipedia: Display Resolution</a>
*/
public interface Resolution extends Quantity<Resolution> {
}