summaryrefslogtreecommitdiff
path: root/WPIJavaCV/src/edu/wpi/first/wpijavacv/WPIImage.java
blob: 609aaaa5f0f7efb26bcdb92014f1ceafe27ccbda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpijavacv;

import static com.googlecode.javacv.cpp.opencv_core.*;
import java.awt.image.BufferedImage;

/**
 * This class is the superclass of all images.
 * 
 * @author Greg Granito
 */
public class WPIImage extends WPIDisposable {

    /** The underlying {@link IplImage} */
    protected IplImage image;

    /**
     * Instantiates a {@link WPIImage} from a {@link BufferedImage}.
     * Useful for interacting with swing.
     * This will not keep a reference to the given image, so any modification to this
     * {@link WPIImage} will not change the given image.
     * @param image the image to copy into a {@link WPIImage}
     */
    public WPIImage(BufferedImage image) {
        this(IplImage.createFrom(image));
    }

    /**
     * Instantiates a {@link WPIImage} from the given {@link IplImage}.
     * The resulting image will be directly wrapped around the given image, and so any modifications
     * to the {@link WPIImage} will reflect on the given image.
     * @param image the image to wrap
     */
    public WPIImage(IplImage image) {
        this.image = image;
    }

    /**
     * Returns the width of the image.
     * @return the width in pixels of the image
     */
    public int getWidth() {
        validateDisposed();

        return image.width();
    }

    /**
     * Returns the height of the image.
     * @return the height in pixels of the image
     */
    public int getHeight() {
        validateDisposed();

        return image.height();
    }

    /**
     * Copies this {@link WPIImage} into a {@link BufferedImage}.
     * This method will always generate a new image.
     * @return a copy of the image
     */
    public BufferedImage getBufferedImage() {
        validateDisposed();

        return image.getBufferedImage();
    }

    @Override
    protected void disposed() {
        image.release();
    }
}