Advertisement
Guest User

Untitled

a guest
May 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.88 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.font.*;
  3. import java.awt.geom.*;
  4. import java.awt.image.BufferedImage;
  5. import java.lang.reflect.Array;
  6. import java.text.*;
  7. import java.util.*;
  8. import java.util.List; // resolves problem with java.awt.List and java.util.List
  9.  
  10. /**
  11. * A class that represents a picture. This class inherits from SimplePicture and
  12. * allows the student to add functionality to the Picture class.
  13. *
  14. * @author Barbara Ericson ericson@cc.gatech.edu
  15. */
  16. public class Picture extends SimplePicture {
  17. ///////////////////// constructors //////////////////////////////////
  18.  
  19. /**
  20. * Constructor that takes no arguments
  21. */
  22. public Picture() {
  23. /*
  24. * not needed but use it to show students the implicit call to super() child
  25. * constructors always call a parent constructor
  26. */
  27. super();
  28. }
  29.  
  30. /**
  31. * Constructor that takes a file name and creates the picture
  32. *
  33. * @param fileName the name of the file to create the picture from
  34. */
  35. public Picture(String fileName) {
  36. // let the parent class handle this fileName
  37. super(fileName);
  38. }
  39.  
  40. /**
  41. * Constructor that takes the width and height
  42. *
  43. * @param height the height of the desired picture
  44. * @param width the width of the desired picture
  45. */
  46. public Picture(int height, int width) {
  47. // let the parent class handle this width and height
  48. super(width, height);
  49. }
  50.  
  51. /**
  52. * Constructor that takes a picture and creates a copy of that picture
  53. *
  54. * @param copyPicture the picture to copy
  55. */
  56. public Picture(Picture copyPicture) {
  57. // let the parent class do the copy
  58. super(copyPicture);
  59. }
  60.  
  61. /**
  62. * Constructor that takes a buffered image
  63. *
  64. * @param image the buffered image to use
  65. */
  66. public Picture(BufferedImage image) {
  67. super(image);
  68. }
  69.  
  70. ////////////////////// methods ///////////////////////////////////////
  71.  
  72. /**
  73. * Method to return a string with information about this picture.
  74. *
  75. * @return a string with information about the picture such as fileName, height
  76. * and width.
  77. */
  78. public String toString() {
  79. String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth();
  80. return output;
  81.  
  82. }
  83.  
  84. public void encode(Picture messagePict) {
  85. Random seedGenerator = new Random();
  86. int seed = seedGenerator.nextInt(16);
  87. System.out.println("Encoding Seed = " + seed);
  88. encodeSeed(seed);
  89. Random rand = new Random(seed);
  90. Pixel[][] messagePixels = messagePict.getPixels2D();
  91. Pixel[][] currentPixels = this.getPixels2D();
  92. int openSpacesLeft = messagePixels.length * messagePixels[0].length;
  93. boolean spacesMap[] = new boolean[openSpacesLeft];
  94. java.util.Arrays.fill(spacesMap, false);
  95. for (int x = 1; x < messagePixels.length - 1; x ++) {
  96. for (int y = 1; y < messagePixels[x].length - 1; y ++) {
  97. int tempIndex = rand.nextInt(openSpacesLeft);
  98. int index = tempIndex;
  99. for (int i = 0; i < spacesMap.length; i++) {
  100. if (!spacesMap[i]) {
  101. tempIndex--;
  102. if (tempIndex == 0) {
  103. index = i;
  104. i = spacesMap.length;
  105. }
  106. }
  107. }
  108.  
  109. spacesMap[index] = true;
  110. int encodingPosY = index / messagePixels.length;
  111. int encodingPosX = index % messagePixels.length;
  112. openSpacesLeft--;
  113. // changing the blue values now
  114. Pixel currPixel = null;
  115. Pixel messagePixel = null;
  116. if (encodingPosX != 0 && encodingPosX != messagePixels.length && encodingPosY != 0 && encodingPosY != messagePixels.length) {
  117. currPixel = currentPixels[encodingPosX][encodingPosY];
  118. if (currPixel.getBlue() % 2 == 1) {
  119. currPixel.setBlue(currPixel.getBlue() - 1);
  120. }
  121. messagePixel = messagePixels[x][y];
  122. if (messagePixel.colorDistance(Color.BLACK) < 50) {
  123. currPixel.setBlue(currPixel.getBlue() + 1);
  124. }
  125. }
  126. }
  127. }
  128. }
  129.  
  130.  
  131. public Picture decode() {
  132. int seed = getSeed();
  133. System.out.println("Decoded Seed = " + seed);
  134. Random rand = new Random(seed);
  135. Picture messagePicture = new Picture(this.getHeight(), this.getWidth());
  136. Pixel[][] messagePixels = messagePicture.getPixels2D();
  137. int openSpacesLeft = messagePixels.length * messagePixels[0].length;
  138. boolean spacesMap[] = new boolean[openSpacesLeft];
  139. java.util.Arrays.fill(spacesMap, false);
  140. Pixel[][] currentPixels = this.getPixels2D();
  141. for (int x = 1; x < messagePixels.length - 1; x++) {
  142. for (int y = 1; y < messagePixels[x].length - 1; y++) {
  143. int tempIndex = rand.nextInt(openSpacesLeft);
  144. int index = tempIndex;
  145. for (int i = 0; i < spacesMap.length; i++) {
  146. if (!spacesMap[i]) {
  147. tempIndex--;
  148. if (tempIndex == 0) {
  149. index = i;
  150. i = spacesMap.length;
  151. }
  152. }
  153. }
  154. spacesMap[index] = true;
  155. int encodingPosY = index / messagePixels.length;
  156. int encodingPosX = index % messagePixels.length;
  157. openSpacesLeft--;
  158. // extracting the blue values now
  159. //Pixel[][] currentPixels = this.getPixels2D();
  160. // messagePixels[x][y] = currentPixels[encodingPosX][encodingPosY];
  161. if (encodingPosX != 0 && encodingPosX != messagePixels.length && encodingPosY != 0 && encodingPosY != messagePixels.length) {
  162. if (currentPixels[encodingPosX][encodingPosY].getBlue() % 2 == 0) {
  163. messagePixels[x][y].setColor(Color.WHITE);
  164. } else {
  165. messagePixels[x][y].setColor(Color.BLACK);
  166. }
  167. }
  168. }
  169. }
  170. return messagePicture;
  171. }
  172.  
  173. public int getSeed() {
  174. Pixel[][] pixels = this.getPixels2D();
  175. int seedTotal = 0;
  176. if (pixels[0][0].getBlue() % 2 == 1) {
  177. seedTotal += 8;
  178. }
  179. if (pixels[pixels.length - 1][0].getBlue() % 2 == 1) {
  180. seedTotal += 4;
  181. }
  182. if (pixels[pixels.length - 1][pixels[0].length - 1].getBlue() % 2 == 1) {
  183. seedTotal += 2;
  184. }
  185. if (pixels[0][pixels[0].length - 1].getBlue() % 2 == 1) {
  186. seedTotal += 1;
  187. }
  188. return seedTotal;
  189. }
  190.  
  191. public void encodeSeed(int seed) {
  192. String binary = Integer.toBinaryString(seed);
  193. String paddingZeros = "";
  194. for (int i = 0; i < 4 - binary.length(); i++) {
  195. paddingZeros += "0";
  196. }
  197. binary = paddingZeros + binary;
  198. boolean topLeft = binary.substring(0, 1).equals("1");
  199. boolean topRight = binary.substring(1, 2).equals("1");
  200. boolean BottomRight = binary.substring(2, 3).equals("1");
  201. boolean BottomLeft = binary.substring(3, 4).equals("1");
  202. Pixel[][] currentPixels = this.getPixels2D();
  203. if (currentPixels[0][0].getBlue() % 2 == 1) {
  204. currentPixels[0][0].setBlue(currentPixels[0][0].getBlue() + 1);
  205. }
  206. if (currentPixels[currentPixels.length - 1][0].getBlue() % 2 == 1) {
  207. currentPixels[currentPixels.length - 1][0].setBlue(currentPixels[currentPixels.length - 1][0].getBlue() + 1);
  208. }
  209. if (currentPixels[currentPixels.length - 1][currentPixels[0].length - 1].getBlue() % 2 == 1) {
  210. currentPixels[currentPixels.length - 1][currentPixels[0].length - 1].setBlue(currentPixels[currentPixels.length - 1][currentPixels[0].length - 1].getBlue() + 1);
  211. }
  212. if (currentPixels[0][currentPixels[0].length - 1].getBlue() % 2 == 1) {
  213. currentPixels[0][currentPixels[0].length - 1].setBlue(currentPixels[0][currentPixels[0].length - 1].getBlue() + 1);
  214. }
  215. if (topLeft) {
  216. currentPixels[0][0].setBlue(currentPixels[0][0].getBlue() + 1);
  217. }
  218. if (topRight) {
  219. currentPixels[currentPixels.length - 1][0].setBlue(currentPixels[currentPixels.length - 1][0].getBlue() + 1);
  220. }
  221. if (BottomRight) {
  222. currentPixels[currentPixels.length - 1][currentPixels[0].length - 1].setBlue(currentPixels[currentPixels.length - 1][currentPixels[0].length - 1].getBlue() + 1);
  223. }
  224. if (BottomLeft) {
  225. currentPixels[0][currentPixels[0].length - 1].setBlue(currentPixels[0][currentPixels[0].length - 1].getBlue() + 1);
  226. }
  227. }
  228.  
  229. public void encodeOG(Picture messagePict) {
  230. Pixel[][] messagePixels = messagePict.getPixels2D();
  231. Pixel[][] currPixels = this.getPixels2D();
  232. Pixel currPixel = null;
  233. Pixel messagePixel = null;
  234. int count = 0;
  235. for (int row = 0; row < this.getHeight(); row++) {
  236. for (int col = 0; col < this.getWidth(); col++) {
  237. // if the current pixel red is odd make it even
  238. currPixel = currPixels[row][col];
  239. if (currPixel.getRed() % 2 == 1)
  240. currPixel.setRed(currPixel.getRed() - 1);
  241. messagePixel = messagePixels[row][col];
  242. if (messagePixel.colorDistance(Color.BLACK) < 50) {
  243. currPixel.setRed(currPixel.getRed() + 1);
  244. count++;
  245. }
  246. }
  247. }
  248. System.out.println(count);
  249. }
  250.  
  251. public Picture decodeOG() {
  252. Pixel[][] pixels = this.getPixels2D();
  253. int height = this.getHeight();
  254. int width = this.getWidth();
  255. Pixel currPixel = null;
  256.  
  257. Pixel messagePixel = null;
  258. Picture messagePicture = new Picture(height, width);
  259. Pixel[][] messagePixels = messagePicture.getPixels2D();
  260. int count = 0;
  261. for (int row = 0; row < this.getHeight(); row++) {
  262. for (int col = 0; col < this.getWidth(); col++) {
  263. currPixel = pixels[row][col];
  264. messagePixel = messagePixels[row][col];
  265. if (currPixel.getRed() % 2 == 1) {
  266. messagePixel.setColor(Color.BLACK);
  267. count++;
  268. }
  269. }
  270. }
  271. System.out.println(count);
  272. return messagePicture;
  273. }
  274.  
  275. /** Method to set the blue to 0 */
  276. public void zeroBlue() {
  277. Pixel[][] pixels = this.getPixels2D();
  278. for (Pixel[] rowArray : pixels) {
  279. for (Pixel pixelObj : rowArray) {
  280. pixelObj.setBlue(0);
  281. }
  282. }
  283. }
  284.  
  285. /**
  286. * Method that mirrors the picture around a vertical mirror in the center of the
  287. * picture from left to right
  288. */
  289. public void mirrorVertical() {
  290. Pixel[][] pixels = this.getPixels2D();
  291. Pixel leftPixel = null;
  292. Pixel rightPixel = null;
  293. int width = pixels[0].length;
  294. for (int row = 0; row < pixels.length; row++) {
  295. for (int col = 0; col < width / 2; col++) {
  296. leftPixel = pixels[row][col];
  297. rightPixel = pixels[row][width - 1 - col];
  298. rightPixel.setColor(leftPixel.getColor());
  299. }
  300. }
  301. }
  302.  
  303. public void RightToLeft() {
  304. Pixel[][] pixels = this.getPixels2D();
  305. Pixel leftPixel = null;
  306. Pixel rightPixel = null;
  307. int width = pixels[0].length;
  308. for (int row = 0; row < pixels.length; row++) {
  309. for (int col = width - 1; col > width / 2; col--) {
  310. leftPixel = pixels[row][col];
  311. rightPixel = pixels[row][width - 1 - col];
  312. rightPixel.setColor(leftPixel.getColor());
  313. }
  314. }
  315. }
  316.  
  317. public void mirrorHorizontal() {
  318. Pixel[][] pixels = this.getPixels2D();
  319. Pixel topPixel = null;
  320. Pixel bottomPixel = null;
  321. int height = pixels.length;
  322. for (int row = 0; row < height / 2; row++) {
  323. for (int col = 0; col < pixels[0].length; col++) {
  324. topPixel = pixels[row][col];
  325. bottomPixel = pixels[height - 1 - row][col];
  326. bottomPixel.setColor(topPixel.getColor());
  327. }
  328. }
  329. }
  330.  
  331. public void botToTop() {
  332. Pixel[][] pixels = this.getPixels2D();
  333. Pixel topPixel = null;
  334. Pixel bottomPixel = null;
  335. int height = pixels.length;
  336. for (int row = height - 1; row > height / 2; row--) {
  337. for (int col = 0; col < pixels[0].length; col++) {
  338. topPixel = pixels[row][col];
  339. bottomPixel = pixels[height - 1 - row][col];
  340. bottomPixel.setColor(topPixel.getColor());
  341. }
  342. }
  343. }
  344.  
  345. public void mirrorDiagonal() {
  346. Pixel[][] pixels = this.getPixels2D();
  347. Pixel leftPixel = null;
  348. Pixel rightPixel = null;
  349. int height = pixels.length;
  350. for (int row = 0; row < height; row++) {
  351. for (int col = 0; col < row; col++) {
  352. leftPixel = pixels[row][col];
  353. rightPixel = pixels[col][row];
  354. rightPixel.setColor(leftPixel.getColor());
  355. }
  356. }
  357. }
  358.  
  359. /** Mirror just part of a picture of a temple */
  360. public void mirrorTemple() {
  361. int mirrorPoint = 276;
  362. Pixel leftPixel = null;
  363. Pixel rightPixel = null;
  364. int count = 0;
  365. Pixel[][] pixels = this.getPixels2D();
  366.  
  367. // loop through the rows
  368. for (int row = 27; row < 97; row++) {
  369. // loop from 13 to just before the mirror point
  370. for (int col = 13; col < mirrorPoint; col++) {
  371.  
  372. leftPixel = pixels[row][col];
  373. rightPixel = pixels[row][mirrorPoint - col + mirrorPoint];
  374. rightPixel.setColor(leftPixel.getColor());
  375. }
  376. }
  377. }
  378.  
  379. public void mirrorArms() {
  380. int mirrorPoint = 194;
  381. Pixel leftPixel = null;
  382. Pixel rightPixel = null;
  383. Pixel[][] pixels = this.getPixels2D();
  384. for (int row = 163; row < mirrorPoint; row++) {
  385. for (int col = 103; col < 291; col++) {
  386. leftPixel = pixels[row][col];
  387. rightPixel = pixels[mirrorPoint - row + mirrorPoint][col];
  388. rightPixel.setColor(leftPixel.getColor());
  389. }
  390. }
  391. }
  392.  
  393. public void mirrorGull() {
  394. int mirrorPoint = 342;
  395. Pixel leftPixel = null;
  396. Pixel rightPixel = null;
  397. Pixel[][] pixels = this.getPixels2D();
  398. for (int row = 234; row < 321; row++) {
  399. for (int col = 239; col < mirrorPoint; col++) {
  400. leftPixel = pixels[row][col];
  401. rightPixel = pixels[row][mirrorPoint - col + mirrorPoint];
  402. rightPixel.setColor(leftPixel.getColor());
  403. }
  404. }
  405. }
  406.  
  407. /**
  408. * copy from the passed fromPic to the specified startRow and startCol in the
  409. * current picture
  410. *
  411. * @param fromPic the picture to copy from
  412. * @param startRow the start row to copy to
  413. * @param startCol the start col to copy to
  414. */
  415. public void copy(Picture fromPic, int startRow, int startCol) {
  416. Pixel fromPixel = null;
  417. Pixel toPixel = null;
  418. Pixel[][] toPixels = this.getPixels2D();
  419. Pixel[][] fromPixels = fromPic.getPixels2D();
  420. for (int fromRow = 0, toRow = startRow; fromRow < fromPixels.length && toRow < toPixels.length; fromRow++, toRow++) {
  421. for (int fromCol = 0, toCol = startCol; fromCol < fromPixels[0].length && toCol < toPixels[0].length; fromCol++, toCol++) {
  422. fromPixel = fromPixels[fromRow][fromCol];
  423. toPixel = toPixels[toRow][toCol];
  424. toPixel.setColor(fromPixel.getColor());
  425. }
  426. }
  427. }
  428.  
  429. public void copy2(Picture fromPic, int startRow, int startCol, int endRow, int endCol) {
  430. Pixel fromPixel = null;
  431. Pixel toPixel = null;
  432. Pixel[][] toPixels = this.getPixels2D();
  433. Pixel[][] fromPixels = fromPic.getPixels2D();
  434. for (int fromRow = 0, toRow = startRow; fromRow < fromPixels.length && toRow < endRow; fromRow++, toRow++) {
  435. for (int fromCol = 0, toCol = startCol; fromCol < fromPixels[0].length && toCol < endCol; fromCol++, toCol++) {
  436. fromPixel = fromPixels[fromRow][fromCol];
  437. toPixel = toPixels[toRow][toCol];
  438. toPixel.setColor(fromPixel.getColor());
  439. }
  440. }
  441. }
  442.  
  443. /** Method to create a collage of several pictures */
  444. public void createCollageOrignal() {
  445. Picture flower1 = new Picture("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\flower1.jpg");
  446. Picture flower2 = new Picture("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\flower2.jpg");
  447. this.copy(flower1, 0, 0);
  448. this.copy(flower2, 100, 0);
  449. this.copy(flower1, 200, 0);
  450. Picture flowerNoBlue = new Picture(flower2);
  451. flowerNoBlue.zeroBlue();
  452. this.copy(flowerNoBlue, 300, 0);
  453. this.copy(flower1, 400, 0);
  454. this.copy(flower2, 500, 0);
  455. this.mirrorVertical();
  456. this.write("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\640x480.jpg");
  457. }
  458.  
  459. public void createMyCollage() {
  460. Picture flower3 = new Picture("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\flower1.jpg");
  461. Picture flower4 = new Picture("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\flower2.jpg");
  462. this.copy(flower3, 0, 0);
  463. this.copy(flower4, 100, 0);
  464. this.copy(flower3, 200, 0);
  465. Picture flowerNoBlue = new Picture(flower4);
  466. Picture flowerGray = new Picture(flower3);
  467. flowerNoBlue.fixUnderWater();
  468. flowerGray.grayScale();
  469. Picture flowerKeepOnlyRed = new Picture(flower4);
  470. flowerKeepOnlyRed.keepOnlyRed();
  471. this.copy(flowerNoBlue, 300, 200);
  472. this.copy(flowerGray, 400, 300);
  473. this.copy(flowerKeepOnlyRed, 500, 400);
  474. this.mirrorVertical();
  475. this.write("C:\\Users\\glicka0192\\Documents\\GitHub\\Glick_Aaron_apcsa-p33\\Unit16\\src\\images\\640x480.jpg");
  476. }
  477.  
  478. /**
  479. * Method to show large changes in color
  480. *
  481. * @param edgeDist the distance for finding edges
  482. */
  483. public void edgeDetection(int edgeDist) {
  484. Pixel mainPixel = null;
  485. Pixel rightPixel = null;
  486. Pixel bottomPixel = null;
  487. Pixel[][] pixels = this.getPixels2D();
  488. Color mainColor = null;
  489. for (int row = 0; row < pixels.length - 1; row++) {
  490. for (int col = 0; col < pixels[0].length - 1; col++) {
  491. mainPixel = pixels[row][col];
  492. rightPixel = pixels[row][col + 1];
  493. bottomPixel = pixels[row + 1][col];
  494. mainColor = mainPixel.getColor();
  495. if (rightPixel.colorDistance(mainColor) > edgeDist || bottomPixel.colorDistance(mainColor) > edgeDist)
  496. mainPixel.setColor(Color.BLACK);
  497. else
  498. mainPixel.setColor(Color.WHITE);
  499. }
  500. }
  501. }
  502.  
  503. public void keepOnlyBlue() {
  504. Pixel[][] pixels = this.getPixels2D();
  505. for (Pixel[] row : pixels) {
  506. for (Pixel p : row) {
  507. p.setRed(0);
  508. p.setGreen(0);
  509. }
  510. }
  511. }
  512.  
  513. public void keepOnlyRed() {
  514. Pixel[][] pixels = this.getPixels2D();
  515. for (Pixel[] row : pixels) {
  516. for (Pixel p : row) {
  517. p.setBlue(0);
  518. p.setGreen(0);
  519. }
  520. }
  521. }
  522.  
  523. public void keepOnlyGreen() {
  524. Pixel[][] pixels = this.getPixels2D();
  525. for (Pixel[] row : pixels) {
  526. for (Pixel p : row) {
  527. p.setRed(0);
  528. p.setBlue(0);
  529. }
  530. }
  531. }
  532.  
  533. public void fixUnderWater() {
  534. Pixel[][] pixels = this.getPixels2D();
  535. for (Pixel[] row : pixels) {
  536. for (Pixel p : row) {
  537. p.setBlue(p.getBlue() - 60);
  538. p.setGreen(p.getGreen() - 60);
  539. p.setRed(p.getRed() + 60);
  540. }
  541. }
  542. }
  543.  
  544. public void grayScale() {
  545. Pixel[][] pixels = this.getPixels2D();
  546. for (Pixel[] row : pixels) {
  547. for (Pixel p : row) {
  548. int greenval = p.getGreen();
  549. p.setRed(greenval);
  550. p.setBlue(greenval);
  551. }
  552. }
  553. }
  554.  
  555. public void negative() {
  556. Pixel[][] pixels = this.getPixels2D();
  557. for (Pixel[] row : pixels) {
  558. for (Pixel p : row) {
  559. p.setRed(255 - p.getRed());
  560. p.setGreen(255 - p.getGreen());
  561. p.setBlue(255 - p.getBlue());
  562. }
  563. }
  564. }
  565.  
  566. } // this } is the end of class Picture, put all new methods before this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement