From a1697c32c2310edbb2c9649bbef3ebfd52c04f56 Mon Sep 17 00:00:00 2001 From: Exr0n <howdy@exr0n.com> Date: Thu, 18 Jan 2024 21:05:17 -0500 Subject: [PATCH] truncate if too long --- src/edu/caltech/cs2/lab02/Image.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/edu/caltech/cs2/lab02/Image.java b/src/edu/caltech/cs2/lab02/Image.java index 8301f43..ca40a5e 100644 --- a/src/edu/caltech/cs2/lab02/Image.java +++ b/src/edu/caltech/cs2/lab02/Image.java @@ -69,9 +69,12 @@ public class Image { int ctr = 0; for (char c : text.toCharArray()) { // https://stackoverflow.com/a/2451660 for (int j=0; j<8; j++) { +// bits_to_write[ctr] = (c >> (7-j)) & 1; + if (ctr >= height*width) break; bits_to_write[ctr] = (c >> j) & 1; ctr ++; } + if (ctr >= height*width) break; } Pixel[][] new_pix = new Pixel[height][width]; @@ -79,8 +82,9 @@ public class Image { ctr = 0; for (int i=0; i<height; i++) { for (int j=0; j<width; j++) { - new_pix[i][j] = this.pixels[i][j]; - new_pix[i][j].fixLowestBitOfR(bits_to_write[ctr]); +// new_pix[i][j] = this.pixels[i][j]; +// new_pix[i][j].fixLowestBitOfR(bits_to_write[ctr]); + new_pix[i][j] = this.pixels[i][j].fixLowestBitOfR(bits_to_write[ctr]); ctr ++; } } -- GitLab