teaching machines

CS1: Lecture 27 – BufferedImage

Dear students,

In homeworks 4 and 5, we work with images. Loops are a natural companion to images, which are large collections of indexed data that we are not even tempted to process without loops. The general pattern of visiting all pixels of an image is this:

for (int r = 0; r < image.getHeight(); ++r) {
  for (int c = 0; c < image.getWidth(); ++c) {
    // Retrieve the color at the current pixel, or set it.
    int packedColor = image.getRGB(c, r);
    image.setRGB(c, r, ...);
  }
}

We’ll use nested loops and get some practice with the BufferedImage class using these exercises:

  • Rearrange the color channels of an image.
  • Combine the left half of one image with the right half of another.
  • Generate an XOR texture.
  • Make a particular color transparent.
  • Invert an image.
  • Rotate an image 90 degrees.
  • Generate an animated GIF.

TODO

Here’s your TODO list to complete before we meet again:

  • CS 145, your lab is posted.
  • I think I’ve fixed the execution order bug in the SpecCheckers, which I think only affected people on Windows. Pull from the template remote to grab the updated SpecCheckers for homeworks 3, 4, and 5.

See you next class!

Sincerely,

P.S. It’s time for a haiku!

Then: Who does your hair?
Now: Who does your Photoshop?
Later: Are you real?

P.P.S. Here’s the code we wrote together in class…

ChannelSwap.java

package lecture1104.cs145;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ChannelSwap {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/skittles.jpg"));
    for (int r = 0; r < image.getHeight(); ++r) {
      for (int c = 0; c < image.getWidth(); ++c) {
        int packedColor = image.getRGB(c, r);
        Color color = new Color(packedColor);
        Color reorderedColor = new Color(color.getGreen(), color.getGreen(), color.getGreen());
        int packedReorderedColor = reorderedColor.getRGB();
        image.setRGB(c, r, packedReorderedColor);
      }
    }

    ImageIO.write(image, "jpg", new File("/Users/johnch/Desktop/out2.jpg"));
  }
}

Hulk.java

package lecture1104.cs145;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Hulk {
  public static void main(String[] args) throws IOException {
    BufferedImage original = ImageIO.read(new File("/Users/johnch/Desktop/hulk.jpg"));
    BufferedImage invisible = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int r = 0; r < original.getHeight(); ++r) {
      for (int c = 0; c < original.getWidth(); ++c) {
        int packedColor = original.getRGB(c, r);
        Color color = new Color(packedColor);
        if (color.getGreen() > 50 && color.getRed() < 100 && color.getBlue() < 100) {
          color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 10);
        }
        invisible.setRGB(c, r, color.getRGB());
      }
    }

    System.out.println("HELP!!!!");

    ImageIO.write(invisible, "png", new File("/Users/johnch/Desktop/hulkplease.png"));
  }
}

TwoFace.java

package lecture1104.cs145;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class TwoFace {
  public static void main(String[] args) throws IOException {
    BufferedImage left = ImageIO.read(new File("/Users/johnch/Desktop/harvey_dent.jpg"));
    BufferedImage right = ImageIO.read(new File("/Users/johnch/Desktop/full_villain.jpg"));
    BufferedImage split = new BufferedImage(left.getWidth(), left.getHeight(), BufferedImage.TYPE_INT_RGB);

    for (int r = 0; r < left.getHeight(); ++r) {
      for (int c = 0; c < left.getWidth(); ++c) {
        if (c < left.getWidth() / 2) {
          split.setRGB(c, r, left.getRGB(c, r));
        } else {
          split.setRGB(c, r, right.getRGB(c, r));
        }
      }
    }

    ImageIO.write(split, "jpg", new File("/Users/johnch/Desktop/split.jpg"));
  }
}

Xor.java

package lecture1104.cs145;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Xor {
  public static void main(String[] args) throws IOException {
    BufferedImage texture = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);

    for (int r = 0; r < texture.getHeight(); ++r) {
      for (int c = 0; c < texture.getWidth(); ++c) {
        texture.setRGB(c, r, new Color(c ^ r, c, r).getRGB());
      }
    }

    ImageIO.write(texture, "png", new File("/Users/johnch/Desktop/xor.png"));
  }
}

GreenScreen.java

package lecture1104.cs148;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class GreenScreen {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/raccoon.jpg"));
    BufferedImage cleared = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);

    for (int r = 0; r < image.getHeight(); r++) {
      for (int c = 0; c < image.getWidth(); c++) {
        Color color = new Color(image.getRGB(c, r));
        if (color.getGreen() > 20 && color.getRed() < 100 && color.getBlue() < 100) {
          color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
        }
        cleared.setRGB(c, r, color.getRGB());
      }
    }

    ImageIO.write(cleared, "png", new File("/Users/johnch/Desktop/cleared.png"));
  }
}

Swap.java

package lecture1104.cs148;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Swap {
  public static void main(String[] args) throws IOException {
    BufferedImage image = ImageIO.read(new File("/Users/johnch/Desktop/peacock.jpg"));

    for (int r = 0; r < image.getHeight(); r++) {
      for (int c = 0; c < image.getWidth(); c++) {
        Color color = new Color(image.getRGB(c, r));
        Color swappedColor = new Color(color.getGreen(), color.getBlue(), color.getRed());
        image.setRGB(c, r, swappedColor.getRGB());
      }
    }

    ImageIO.write(image, "jpg", new File("/Users/johnch/Desktop/out2.jpg"));
  }
}

TwoFace.java

package lecture1104.cs148;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class TwoFace {
  public static void main(String[] args) throws IOException {
    BufferedImage left = ImageIO.read(new File("/Users/johnch/Desktop/harvey_dent.jpg"));
    BufferedImage right = ImageIO.read(new File("/Users/johnch/Desktop/full_villain.jpg"));
    BufferedImage split = new BufferedImage(left.getWidth(), left.getHeight(), BufferedImage.TYPE_INT_RGB);

    for (int r = 0; r < left.getHeight(); r++) {
      for (int c = 0; c < left.getWidth(); c++) {
        if (c < left.getWidth() / 2) {
          split.setRGB(c, r, left.getRGB(c, r));
        } else {
          split.setRGB(c, r, right.getRGB(c, r));
        }
      }
    }

    ImageIO.write(split, "jpg", new File("/Users/johnch/Desktop/twoface.jpg"));
  }
}

Xor.java

package lecture1104.cs148;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Xor {
  public static void main(String[] args) throws IOException {
    BufferedImage texture = new BufferedImage(500, 710, BufferedImage.TYPE_INT_RGB);

    for (int r = 0; r < texture.getHeight(); r++) {
      for (int c = 0; c < texture.getWidth(); c++) {
        texture.setRGB(c, r, new Color((c ^ r) % 256, c % 256, r % 256).getRGB());
      }
    }

    ImageIO.write(texture, "png", new File("/Users/johnch/Desktop/xor.png"));
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *