<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.imageio.*;
import java.net.*;
import java.io.*;


/** An instance contains 
 (1) an original image map (class ImageMap) and
 (2) a possibly altered (by methods in this instance) version of the original image map.
 */
public class ImageServer {
    
    /** DM provides methods for extracting components of an rgb pixel. */
    public static final DirectColorModel DM= (DirectColorModel) ColorModel.getRGBdefault();
    
    /** The following four constants of this class indicate a color.*/
    /** Color gray */
    public static final int GRAY= 0;
    
    /** Color red */
    public static final int RED= 1;
    
    /** Color green */
    public static final int GREEN= 2;
    
    /** Color blue */
    public static final int BLUE= 3;
    
    private ImageMap originalMap; // The original image map, for restoration purposes
    private ImageMap currentMap;  // The altered map
    
    /** Constructor: an instance for im.
        Precondition: im != null. */
    public ImageServer(ImageMap im) {
        originalMap= im;
        currentMap= originalMap.copy();
    }
    
    /** = the current image map. */
    public ImageMap getCurrentMap() {
        return currentMap;
    }
    
    /** = the original image map */
    public ImageMap getOriginalMap() {
        return originalMap;
    }
    
    /** Invert the image, replacing each element with its color complement. */
    public void invert() {
        
        int len= currentMap.getRows() * currentMap.getCols();
        // invert all pixels (leave alpha/transparency value alone)
        for (int p= 0; p &lt; len; p= p+1) {
                int rgb= currentMap.getPixel(p);
                int red= 255 - DM.getRed(rgb);
                int blue= 255 - DM.getBlue(rgb);
                int green= 255 - DM.getGreen(rgb);
                int alpha= DM.getAlpha(rgb);
                currentMap.setPixel(p,
                              (alpha &lt;&lt; 24) | (red &lt;&lt; 16) | (green &lt;&lt; 8) | blue);
        }
    }
    
    /** Transpose the image that is in variable currentMap.  */
    public void transpose() {
        
        // Implement this procedure AND THEN DELETE THIS COMMENT
        
        // Follow this plan: 
        // (1) Create a new ImageMap b (say), storing in it a COPY of currentMap;
        // (2) In b, swap the numbers of rows and columns (i.e. the two fields);
        // (2) Store the transpose of currentMap.map in b.map; and
        // (3) assign b to currentMap.
        
    }
    
    /** Reflect this image (which in variable imageMap) around the
        horizontal middle. */
    public void hreflect() {
        int rows= currentMap.getRows();
        int cols= currentMap.getCols();
        int h= 0;
        int k= rows-1;
        //invariant: rows 0..h-1 and k+1.. have been inverted
        while (h &lt; k) {
            // Swap row h with row k
            for (int c= 0; c != cols; c= c+1) {
                currentMap.swapPixels(h,c,k,c);
            }
            
            h= h+1; k= k-1;
        }
    }
    
    /** Reflect this image (whch in variable imageMap) around
        the vertical middle. */
    public void vreflect() {
        // Implement this procedure AND THEN DELETE THIS COMMENT
    }
    
    /* Filter the image to be only a single color, which is given by
     parameter color.&lt;br&gt;&lt;br&gt;
     
     Precondition: color is one of the four color constants GRAY,
     RED, GREEN, BLUE of this class.&lt;br&gt;&lt;br&gt;
     
     Gray is achieved by making all three color components equal to the
     average of the red, green, and blue components. For the other three
     colors, make the non-used components 0. The alpha component should
     not be changed.
     */
    public void filter(int color) {
        // Implement this procedure AND THEN DELETE THIS COMMENT
    }
    
  
    
    /** Hide message m in this image, using the ascii representation of each character in m.
        Return true if it succeeds and false if it doesn't.
        [under what condition might it not succeed?]
     */
    public boolean hide(String m) {
        // Implement this procedure AND THEN DELETE THIS COMMENT
        
        return false;
    }
    
    
    /** Extract and return the message hidden in this image.
        Return null if no message detected. */
    public String reveal() {
        // Implement this procedure AND THEN DELETE THIS COMMENT
        
        return null;
    }
    
    
    /** Restore the original image in the current one */
    public void restore() {
        currentMap= originalMap.copy();
    }
    
    /** Store this image in file fname in format form */
    public void write(String fname, String form) throws java.io.IOException {
      Image image= currentMap.getImage();
      BufferedImage bimage= 
          new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
      Graphics g= bimage.createGraphics();

      g.drawImage(image, 0, 0, null);
      g.dispose();
      File f= new File(fname);
      if (f.exists()) {
          System.out.println("File " + f.getAbsolutePath() + " exists. It was not overwritten.");
          return;
      }
      ImageIO.write(bimage, form, f);
      System.out.println("Image written to " + f.getAbsolutePath());
    }  
    
}
</pre></body></html>