diff --git a/ykoiso/src/B110_Resolution.java b/ykoiso/src/B110_Resolution.java new file mode 100644 index 0000000000000000000000000000000000000000..4823f1cc0f73c6b8e2c4744ae8d05b84b34722cf --- /dev/null +++ b/ykoiso/src/B110_Resolution.java @@ -0,0 +1,137 @@ +import java.util.Scanner; + +public class B110_Resolution { + // 重ねる際ずらす量 + final int SHIFT_HEIGHT = 1; + final int SHIFT_WIDTH = 1; + // 分割倍率 + final int SPLIT_MAG = 2; + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + // 定数読み込み + final int height = scan.nextInt(); + final int width = scan.nextInt(); + // 画像インスタンス生成 + int[][] img1 = new int[height][width]; + int[][] img2 = new int[height][width]; + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + img1[H][W] = scan.nextInt(); + } + } + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + img2[H][W] = scan.nextInt(); + } + } + B110_Resolution re = new B110_Resolution(); + // 分割画像の生成 + SplitImage splitImg1 = re.new SplitImage(height, width); + SplitImage splitImg2 = re.new SplitImage(height, width); + splitImg1.makeImage(img1, height, width); + splitImg2.makeImage(img2, height, width); + // 高解像度画像の生成 + HighResoImage newImage = re.new HighResoImage(height, width); + newImage.makeImage(splitImg1, splitImg2); + // 画像の出力 + newImage.showImage(); + scan.close(); + } + + // 分割画像 + class SplitImage { + final int height; + final int width; + int[][] img; + + // コンストラクタ + SplitImage(final int height, final int width) { + this.height = height * 2; + this.width = width * 2; + img = new int[this.height][this.width]; + } + + // テスト用 + public void setImg(final int[][] img) { + this.img = img; + } + + // 元画像から分割画像の生成 + public void makeImage(final int[][] img, final int height, final int width) { + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + splitPixel(img, H, W); + } + } + } + + // 元画像の画素に対して分割した画素を生成する + public void splitPixel(final int[][] img, final int originalH, final int originalW) { + int splitH = originalH * SPLIT_MAG; + int splitW = originalW * SPLIT_MAG; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + this.img[splitH + i][splitW + j] = img[originalH][originalW]; + } + } + } + + public int getHeight() { + return height; + } + + public int getWidth() { + return width; + } + + public int getImgPixel(final int H, final int W) { + return img[H][W]; + } + } + // 高解像度画像 + class HighResoImage { + final int height; + final int width; + int[][] img; + + HighResoImage(final int height, final int width) { + this.height = height * 2 - SHIFT_HEIGHT; + this.width = width * 2 - SHIFT_WIDTH; + img = new int[this.height][this.width]; + } + + // 2つの画像から高解像度の画像の生成 + // 画像をずらして重ねて各画素の値の平均を取る + public void makeImage(final SplitImage img1, final SplitImage img2) { + int splitHeight = img1.getHeight(); + int splitWidth = img1.getWidth(); + for (int img1H = 1; img1H < splitHeight; img1H++) { + for (int img1W = 1; img1W < splitWidth; img1W++) { + int img2H = img1H - SHIFT_HEIGHT; + int img2W = img1W - SHIFT_WIDTH; + img[img2H][img2W] = + (img1.getImgPixel(img1H, img1W) + img2.getImgPixel(img2H, img2W)) / 2; + } + } + } + + // 画像の出力 + public void showImage() { + for (int H = 0; H < height; H++) { + for (int W = 0; W < width; W++) { + System.out.print(img[H][W]); + if (W != width - 1) + System.out.print(" "); + } + System.out.println(); + } + } + + // テスト用 + public int[][] getImg() { + return img; + } + } + +} diff --git a/ykoiso/test/B110_ResolutionTest.java b/ykoiso/test/B110_ResolutionTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8d1d0d8dcb8170714635c782604ad3d59f2ef556 --- /dev/null +++ b/ykoiso/test/B110_ResolutionTest.java @@ -0,0 +1,44 @@ +import static org.junit.Assert.assertThat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; + +public class B110_ResolutionTest { + B110_Resolution re = new B110_Resolution(); + B110_Resolution.HighResoImage sut; + B110_Resolution.SplitImage img1 = re.new SplitImage(2, 2); + B110_Resolution.SplitImage img2 = re.new SplitImage(2, 2); + + @Test + public void 同じ画素の値がかぶっていないとき() { + int[][] originalImg1 = + {{0, 0, 255, 255}, {0, 0, 255, 255}, {0, 0, 255, 255}, {0, 0, 255, 255}}; + img1.setImg(originalImg1); + int[][] originalImg2 = + {{255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}}; + img2.setImg(originalImg2); + sut = re.new HighResoImage(2, 2); + sut.makeImage(img1, img2); + int[][] actual = sut.getImg(); + int[][] expected = {{127, 127, 127}, {127, 127, 127}, {127, 127, 127}}; + assertThat(actual, is(expected)); + } + + @Test + public void 同じ画素の値がかぶっているとき() { + int[][] originalImg1 = + {{0, 255, 255, 255}, {0, 255, 255, 255}, {0, 255, 255, 255}, {0, 255, 255, 255}}; + img1.setImg(originalImg1); + int[][] originalImg2 = {{55, 255, 255, 255}, {155, 255, 255, 255}, {55, 255, 255, 255}, + {155, 255, 255, 255}}; + img2.setImg(originalImg2); + sut = re.new HighResoImage(2, 2); + sut.makeImage(img1, img2); + int[][] actual = sut.getImg(); + int[][] expected = {{155, 255, 255}, {205, 255, 255}, {155, 255, 255}}; + assertThat(actual, is(expected)); + } +}