Skip to content

Instantly share code, notes, and snippets.

@kubadlo
Created November 9, 2017 14:01
Show Gist options
  • Save kubadlo/1545d7b484059c0f1865f21ee24012cb to your computer and use it in GitHub Desktop.
Save kubadlo/1545d7b484059c0f1865f21ee24012cb to your computer and use it in GitHub Desktop.
package com.jakublesko;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Challenge337 {
private BufferedImage sourceImage;
private BufferedImage targetImage;
private int imageWidth;
private int imageHeight;
private Challenge337(String filename) {
loadImage(filename);
processImage();
renderImage();
}
private void processImage() {
if (sourceImage == null) {
System.err.println("No image for processing!");
return;
}
targetImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
for (int row = 0; row < imageHeight; row++) {
int[] orderedRow = new int[imageWidth];
int redPosition = 399;
for (int col = 0; col < imageWidth; col++) {
int aRGB = sourceImage.getRGB(col, row);
int red = (aRGB >> 16) & 0xFF;
int green = (aRGB >> 8) & 0xFF;
int blue = (aRGB) & 0xFF;
orderedRow[col] = aRGB;
if (red == 255 && green == 0 && blue == 0) {
redPosition = col + 1;
}
}
for (int i = 0; i < imageWidth - redPosition; i++) {
for (int j = orderedRow.length - 1; j > 0; j--) {
int temp = orderedRow[j];
orderedRow[j] = orderedRow[j - 1];
orderedRow[j - 1] = temp;
}
}
for (int col = 0; col < imageWidth; col++) {
targetImage.setRGB(col, row, orderedRow[col]);
}
}
}
private void loadImage(String fileName) {
try {
sourceImage = ImageIO.read(new File(fileName));
imageWidth = sourceImage.getWidth();
imageHeight = sourceImage.getHeight();
} catch (IOException exception) {
System.err.println("Error reading image file from disk!\n" + exception);
}
}
private void renderImage() {
if (targetImage == null) {
System.err.println("No image for rendering!");
return;
}
try {
ImageIO.write(targetImage, "png", new File("output.png"));
} catch (IOException exception) {
System.err.println("Error writing image file to disk!\n" + exception);
}
}
public static void main(String[] args) {
new Challenge337("input01.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment