danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.frc971; |
| 5 | |
| 6 | /** |
| 7 | * @author daniel |
| 8 | * |
| 9 | */ |
| 10 | |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 11 | import java.io.File; |
| 12 | import java.io.IOException; |
| 13 | |
| 14 | import java.util.logging.Logger; |
| 15 | |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 16 | import javax.imageio.ImageIO; |
| 17 | |
| 18 | import edu.wpi.first.wpijavacv.WPIColorImage; |
| 19 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 20 | /** Get debug images for Java camera processor. */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 21 | public class TestImageGetter { |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 22 | |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 23 | private String path_to_images; |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 24 | |
| 25 | private final static Logger LOG = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); |
| 26 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 27 | /** The names of our debugging images, without paths. |
| 28 | * The GetNext method should be used to get the first |
| 29 | * image, and not GetCurrent. */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 30 | final static String[] images = {"45in_DoubleGreen.jpg", |
| 31 | "57inLargeTarget_DoubleGreenBK.jpg", |
| 32 | "FullField_DoubleGreenBK3.jpg", |
| 33 | "FullField_SmallGreen.jpg", |
| 34 | "HybridLine_DoubleGreenBK2.jpg", |
| 35 | "HybridLine_DoubleGreenBK3.jpg", |
| 36 | "HybridLine_DoubleGreenBK4.jpg", |
| 37 | "HybridLine_SmallGreen2.jpg", |
| 38 | "HybridLine_SmallGreen3.jpg", |
| 39 | "HybridLine_SmallGreen4.jpg", |
| 40 | "Midfield_DoubleGreenBK2.jpg", |
| 41 | "Midfield_SmallGreen2.jpg", |
| 42 | "Midfield_SmallGreen3.jpg", |
| 43 | "Midfield_SmallGreen4.jpg", |
| 44 | "OppLine_DoubleGreenBK2.jpg", |
| 45 | "OppLine_SmallGreen2.jpg", |
| 46 | "PyramidRight_DoubleGreenBK2.jpg", |
| 47 | "PyramidRight_SmallGreen2.jpg" |
| 48 | }; |
| 49 | |
| 50 | private int image_index = -1; |
| 51 | |
| 52 | private WPIColorImage current_image = null; |
| 53 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 54 | /** Helper method to concatenate paths, similar to Python's os.path.join(). */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 55 | private String cocatenate_paths(String path1, String path2) { |
| 56 | if (path1.charAt(path1.length() - 1) == '/') |
| 57 | return path1 + path2; |
| 58 | else |
| 59 | return path1 + "/" + path2; |
| 60 | } |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 61 | |
danielp | 3c598e5 | 2013-02-24 06:12:54 +0000 | [diff] [blame] | 62 | /** Gets the name to display at the top of the image window. */ |
| 63 | public String GetName() { |
| 64 | return images[image_index]; |
| 65 | } |
| 66 | |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 67 | /** Constructor |
| 68 | * |
| 69 | * @param path_to_images is the path to the directory where our images are. |
| 70 | */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 71 | public TestImageGetter(String path_to_images) { |
| 72 | this.path_to_images = path_to_images; |
| 73 | } |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 74 | |
| 75 | /** Gets the next debugging image. |
| 76 | * |
| 77 | * @return Returns a WPIColorImage. |
| 78 | */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 79 | public WPIColorImage GetNext() { |
| 80 | image_index++; |
| 81 | if (image_index < images.length) { |
| 82 | String image_to_get = images[image_index]; |
| 83 | try { |
| 84 | current_image = new WPIColorImage(ImageIO.read(new File(cocatenate_paths(path_to_images, image_to_get)))); |
| 85 | return current_image; |
| 86 | } |
| 87 | catch (IOException e) { |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 88 | LOG.warning("Could not open file."); |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | image_index--; |
| 94 | return null; |
| 95 | } |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 96 | |
| 97 | /** Gets the previous debugging image. |
| 98 | * |
| 99 | * @return Returns a WPIColorImage. |
| 100 | */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 101 | public WPIColorImage GetPrev() { |
| 102 | image_index--; |
| 103 | if (image_index >= 0) { |
| 104 | String image_to_get = images[image_index]; |
| 105 | try { |
| 106 | current_image = new WPIColorImage(ImageIO.read(new File(cocatenate_paths(path_to_images, image_to_get)))); |
| 107 | return current_image; |
| 108 | } |
| 109 | catch (IOException e) { |
danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame] | 110 | LOG.warning("Could not open file."); |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 111 | return null; |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | image_index++; |
| 116 | return null; |
| 117 | } |
danielp | 64c4e05 | 2013-02-23 07:21:41 +0000 | [diff] [blame] | 118 | |
| 119 | /** Gets the current debugging image. This is vestigial, it is not longer used. |
| 120 | * |
| 121 | * @return Returns a WPIColorImage |
| 122 | */ |
danielp | 4a35a7a | 2013-02-20 20:45:39 +0000 | [diff] [blame] | 123 | public WPIColorImage GetCurrent() { |
| 124 | return current_image; |
| 125 | } |
| 126 | } |