|
| 1 | +package com.offline.training.project.pj1; |
| 2 | + |
| 3 | +/* ImageUtils.java */ |
| 4 | + |
| 5 | +/* DO NOT CHANGE THIS FILE. */ |
| 6 | +/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */ |
| 7 | + |
| 8 | +/* You may wish to make temporary changes or insert println() statements */ |
| 9 | +/* while testing your code. When you're finished testing and debugging, */ |
| 10 | +/* though, make sure your code works with the original version of this file. */ |
| 11 | + |
| 12 | +/** |
| 13 | + * The ImageUtils class reads and writes TIFF file, converting to and from |
| 14 | + * pixel arrays in PixImage format or run-length encodings in |
| 15 | + * RunLengthEncoding format. Methods are also included for displaying images |
| 16 | + * in PixImage format. |
| 17 | + * |
| 18 | + * @author Joel Galenson |
| 19 | + **/ |
| 20 | + |
| 21 | +import java.awt.Color; |
| 22 | +import java.awt.event.WindowAdapter; |
| 23 | +import java.awt.event.WindowEvent; |
| 24 | +import java.awt.image.BufferedImage; |
| 25 | + |
| 26 | +import javax.media.jai.JAI; |
| 27 | +import javax.media.jai.RenderedImageAdapter; |
| 28 | +import javax.swing.Box; |
| 29 | +import javax.swing.ImageIcon; |
| 30 | +import javax.swing.JFrame; |
| 31 | +import javax.swing.JLabel; |
| 32 | + |
| 33 | +/** |
| 34 | + * ImageUtils contains utilities for reading, writing, and displaying images. |
| 35 | + * |
| 36 | + * It uses JAI to read and write TIFF files, as the standard libraries cannot |
| 37 | + * read them. |
| 38 | + * |
| 39 | + * All image data is in RGB format (see BufferedImage.getRGB). |
| 40 | + */ |
| 41 | +public class ImageUtils { |
| 42 | + |
| 43 | + /** |
| 44 | + * buffer2PixImage() converts a BufferedImage to a PixImage. |
| 45 | + * @param bImage the image to convert. |
| 46 | + * @return a PixImage with the same pixels as the BufferedImage. |
| 47 | + */ |
| 48 | + private static PixImage buffer2PixImage(BufferedImage bImage) { |
| 49 | + PixImage pImage = new PixImage(bImage.getWidth(), bImage.getHeight()); |
| 50 | + for (int x = 0; x < bImage.getWidth(); x++) { |
| 51 | + for (int y = 0; y < bImage.getHeight(); y++) { |
| 52 | + Color color = new Color(bImage.getRGB(x, y)); |
| 53 | + pImage.setPixel(x, y, (short) color.getRed(), (short) color.getGreen(), |
| 54 | + (short) color.getBlue()); |
| 55 | + } |
| 56 | + } |
| 57 | + return pImage; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * pixImage2buffer() converts a PixImage to a BufferedImage. |
| 62 | + * @param pImage the image to convert. |
| 63 | + * @return a BufferedImage with the same pixels as the PixImage. |
| 64 | + */ |
| 65 | + static BufferedImage pixImage2buffer(PixImage pImage) { |
| 66 | + BufferedImage bImage = new BufferedImage(pImage.getWidth(), |
| 67 | + pImage.getHeight(), |
| 68 | + BufferedImage.TYPE_INT_ARGB); |
| 69 | + for (int x = 0; x < bImage.getWidth(); x++) { |
| 70 | + for (int y = 0; y < bImage.getHeight(); y++) { |
| 71 | + bImage.setRGB(x, y, new Color(pImage.getRed(x, y), |
| 72 | + pImage.getGreen(x, y), |
| 73 | + pImage.getBlue(x, y)).getRGB()); |
| 74 | + } |
| 75 | + } |
| 76 | + return bImage; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * readTIFF() reads an image from a file and formats it as a BufferedImage. |
| 81 | + * @param filename the name of the file to read. |
| 82 | + * @return a BufferedImage of the file |
| 83 | + */ |
| 84 | + private static BufferedImage readTIFF(String filename) { |
| 85 | + return (new RenderedImageAdapter(JAI.create("fileload", filename))) |
| 86 | + .getAsBufferedImage(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * readTIFFPix() reads an image from a file and formats it as a PixImage. |
| 91 | + * @param filename the name of the file to read. |
| 92 | + * @return a PixImage of the file |
| 93 | + */ |
| 94 | + public static PixImage readTIFFPix(String filename) { |
| 95 | + return buffer2PixImage(readTIFF(filename)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * readTIFFRLE() reads an image from a file and formats it as a run-length |
| 100 | + * encoding. |
| 101 | + * @param filename the name of the file to read. |
| 102 | + * @return a RunLengthEncoding of the file. |
| 103 | + */ |
| 104 | + public static RunLengthEncoding readTIFFRLE(String filename) { |
| 105 | + return new RunLengthEncoding(readTIFFPix(filename)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * writeTIFF() writes a BufferedImage to a specified file in TIFF format. |
| 110 | + * @param rle the input BufferedImage. |
| 111 | + * @param filename the output filename. |
| 112 | + */ |
| 113 | + private static void writeTIFF(BufferedImage image, String filename) { |
| 114 | + JAI.create("filestore", image, filename, "tiff"); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * writeTIFF() writes a PixImage to a specified file in TIFF format. |
| 119 | + * @param image the input PixImage. |
| 120 | + * @param filename the output filename. |
| 121 | + */ |
| 122 | + public static void writeTIFF(PixImage image, String filename) { |
| 123 | + writeTIFF(pixImage2buffer(image), filename); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * writeTIFF() writes a run-length encoding to a specified file in TIFF |
| 128 | + * format. |
| 129 | + * @param rle the input run-length encoded image. |
| 130 | + * @param filename the output filename. |
| 131 | + */ |
| 132 | + public static void writeTIFF(RunLengthEncoding rle, String filename) { |
| 133 | + writeTIFF(rle.toPixImage(), filename); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * displayFrame displays a JFrame and pauses until the window is closed. |
| 138 | + * @param frame a JFrame to display. |
| 139 | + */ |
| 140 | + private static void displayFrame(final JFrame frame) { |
| 141 | + try { |
| 142 | + synchronized (ImageUtils.class) { |
| 143 | + frame.setResizable(false); |
| 144 | + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 145 | + frame.addWindowListener(new WindowAdapter() { |
| 146 | + @Override |
| 147 | + public void windowClosing(WindowEvent event) { |
| 148 | + synchronized (ImageUtils.class) { |
| 149 | + ImageUtils.class.notify(); |
| 150 | + frame.dispose(); |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + frame.pack(); |
| 155 | + frame.setVisible(true); |
| 156 | + ImageUtils.class.wait(); |
| 157 | + } |
| 158 | + } catch (InterruptedException e) { |
| 159 | + System.out.println("Interrupted Exception in displayFrame()."); |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * displayTIFFs displays a sequence of PixImages and pauses until the window |
| 166 | + * is closed. |
| 167 | + * @param images an array of PixImages to display. |
| 168 | + */ |
| 169 | + public static void displayTIFFs(PixImage[] images) { |
| 170 | + JFrame frame = new JFrame(); |
| 171 | + Box box = Box.createHorizontalBox(); |
| 172 | + for (int i = 0; i < images.length; i++) { |
| 173 | + box.add(new JLabel(new ImageIcon(pixImage2buffer(images[i])))); |
| 174 | + if (i < images.length - 1) { |
| 175 | + box.add(Box.createHorizontalStrut(10)); |
| 176 | + } |
| 177 | + } |
| 178 | + frame.add(box); |
| 179 | + displayFrame(frame); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * displayTIFF displays a PixIamge and pauses until the window is closed. |
| 184 | + * @param image the PixImage to display. |
| 185 | + */ |
| 186 | + public static void displayTIFF(PixImage image) { |
| 187 | + displayTIFFs(new PixImage[] { image }); |
| 188 | + } |
| 189 | +} |
0 commit comments