001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007package edu.wpi.first.wpilibj.image;
008
009import com.ni.vision.NIVision;
010import com.ni.vision.NIVision.Image;
011
012/**
013 * Class representing a generic image.
014 * @author dtjones
015 */
016public abstract class ImageBase {
017
018    /**
019     * Pointer to the image memory
020     */
021    public final Image image;
022    static final int DEFAULT_BORDER_SIZE = 3;
023
024    ImageBase(NIVision.ImageType type) throws NIVisionException {
025        image = NIVision.imaqCreateImage(type, DEFAULT_BORDER_SIZE);
026    }
027
028    /**
029     * Creates a new image pointing to the same data as the source image. The
030     * imgae data is not copied, it is just referenced by both objects. Freeing
031     * one will free both.
032     * @param sourceImage The image to reference
033     */
034    ImageBase(ImageBase sourceImage) {
035        image = sourceImage.image;
036    }
037
038    /**
039     * Write the image to a file.
040     *
041     * Supported extensions:
042     * .aipd or .apd AIPD
043     * .bmp BMP
044     * .jpg or .jpeg JPEG
045     * .jp2 JPEG2000
046     * .png PNG
047     * .tif or .tiff TIFF
048     *
049     * @param fileName The path to write the image to.
050     */
051    public void write(String fileName) throws NIVisionException {
052        NIVision.RGBValue value = new NIVision.RGBValue();
053        NIVision.imaqWriteFile(image, fileName, value);
054        value.free();
055    }
056
057    /**
058     * Release the memory associated with an image.
059     */
060    public void free() throws NIVisionException {
061        image.free();
062    }
063
064    /**
065     * Get the height of the image in pixels.
066     * @return The height of the image.
067     */
068    public int getHeight() throws NIVisionException {
069        return NIVision.imaqGetImageSize(image).height;
070    }
071
072    /**
073     * Get the width of the image in pixels.
074     * @return The width of the image.
075     */
076    public int getWidth() throws NIVisionException {
077        return NIVision.imaqGetImageSize(image).width;
078    }
079}