danielp | b913fa7 | 2013-03-03 06:23:20 +0000 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.spartanrobotics; |
| 5 | |
| 6 | /** |
| 7 | * @author daniel |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | import java.awt.image.BufferedImage; |
| 12 | import java.io.File; |
| 13 | import java.io.IOException; |
| 14 | |
| 15 | import java.util.logging.Logger; |
| 16 | |
| 17 | import javax.imageio.ImageIO; |
| 18 | |
| 19 | import edu.wpi.first.wpijavacv.WPIColorImage; |
| 20 | |
| 21 | /** Get debug images for Java camera processor. */ |
| 22 | public class TestImageGetter implements ImageGetter{ |
| 23 | |
| 24 | private final static Logger LOG = Logger.getLogger( |
| 25 | TestImageGetter.class.getName()); |
| 26 | |
| 27 | private WPIColorImage[] loadedImages; |
| 28 | |
| 29 | private int imageIndex = -1; |
| 30 | private String currentName; |
| 31 | |
| 32 | /** Gets the name to display at the top of the image window. */ |
| 33 | public String getName() { |
| 34 | return currentName; |
| 35 | } |
| 36 | |
| 37 | /** Constructor |
| 38 | * |
| 39 | * @param path_to_images is the path to the directory where our images are. |
| 40 | * @throws IOException |
| 41 | */ |
| 42 | public TestImageGetter(String path_to_images) { |
| 43 | File directory = new File(path_to_images); |
| 44 | loadedImages = new WPIColorImage[directory.listFiles().length]; |
| 45 | |
| 46 | //pre-load all the images |
| 47 | int i = 0; |
| 48 | for (final File fileEntry : directory.listFiles()) { |
| 49 | try { |
| 50 | BufferedImage image = ImageIO.read(fileEntry); |
| 51 | if (image != null) { |
| 52 | loadedImages[i] = new WPIColorImage(image); |
| 53 | } else { |
| 54 | //we attempted to load what was not an image. Skip it |
| 55 | LOG.info("Preloading debug images; skipping incompatible: " + fileEntry.getName()); |
| 56 | continue; |
| 57 | } |
| 58 | } catch (IOException e) { |
| 59 | //we couldn't open a file. Skip it |
| 60 | LOG.info("Preloading debug images; skipping unopenable: " + fileEntry.getName()); |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | currentName = fileEntry.getName(); |
| 65 | ++i; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** Gets the next debugging image. |
| 70 | * |
| 71 | * @return Returns the next test image. |
| 72 | */ |
| 73 | public WPIColorImage getFrame() { |
| 74 | ++imageIndex; |
| 75 | if (imageIndex < loadedImages.length) { |
| 76 | return loadedImages[imageIndex]; |
| 77 | } else { |
| 78 | imageIndex = loadedImages.length - 1; |
| 79 | return loadedImages[imageIndex]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** Gets the previous debugging image. |
| 84 | * |
| 85 | * @return Returns the previous test image. |
| 86 | */ |
| 87 | public WPIColorImage getPrev() { |
| 88 | --imageIndex; |
| 89 | if (imageIndex > 0) { |
| 90 | return loadedImages[imageIndex]; |
| 91 | } else { |
| 92 | imageIndex = 0; |
| 93 | return loadedImages[imageIndex]; |
| 94 | } |
| 95 | } |
| 96 | } |