Advertisement
Marin126

ImagenPGM

Apr 16th, 2020
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. /*
  6. * To change this license header, choose License Headers in Project Properties.
  7. * To change this template file, choose Tools | Templates
  8. * and open the template in the editor.
  9. */
  10. /**
  11. *
  12. * @author anton
  13. */
  14. public class ImagenPGM {
  15.  
  16. private int alto;
  17. private int ancho;
  18. private double pw;
  19. private int[][] matriz;
  20. private String nombreFichero;
  21. private int blancoAbsoluto;
  22.  
  23. public void leeFichero(String nombreFicheroEntrada, String nombreFicheroSalida) {
  24. File fEntrada = new File(nombreFichero);
  25. Scanner scFile = null;
  26. FileWriter fw = null;
  27. ImagenPGM imagen;
  28. try {
  29. scFile = new Scanner(fEntrada);
  30. fw = new FileWriter(nombreFicheroSalida);
  31. scFile.nextLine();
  32. scFile.nextLine();
  33. String[] tamaño = scFile.nextLine().split(" ");
  34. this.ancho = Integer.parseInt(tamaño[0]);
  35. this.alto = Integer.parseInt(tamaño[1]);
  36. this.pw = Double.parseDouble(scFile.nextLine());
  37. this.matriz = new int[alto][ancho];
  38.  
  39. for (int i = 0; i < alto; i++) {
  40. for (int j = 0; j < ancho; j++) {
  41. int normalizado = (int) ((double) (scFile.nextInt()) / pw * 255);
  42. matriz[i][j] = normalizado;
  43.  
  44. }
  45.  
  46. }
  47. imagen = new ImagenPGM(alto, ancho, pw, matriz);
  48. filtroCaja(matriz);
  49.  
  50. fw.write("P2" + "\n");
  51. fw.write("# " + nombreFicheroSalida + "\n");
  52. fw.write(imagen.getAncho() + " " + imagen.getAlto() + "\n");
  53. fw.write("255" + "\n");
  54.  
  55. for (int i = 0; i < alto; i++) {
  56. for (int j = 0; j < ancho; j++) {
  57.  
  58. fw.write(this.matriz[i][j] + " ");
  59. }
  60. fw.write("\n");
  61. }
  62. } catch (Exception e) {
  63. System.out.println("Se ha producido un error al leer" + e.toString()
  64. );
  65. e.printStackTrace();
  66. } finally {
  67. if (scFile != null) {
  68. scFile.close();
  69. }
  70. if (fw != null) {
  71. try {
  72.  
  73. fw.close();
  74. } catch (Exception e) {
  75. System.out.println("Error al cerrar el fichero: " + e.toString());
  76. }
  77. }
  78. }
  79.  
  80. }
  81.  
  82. public void filtroCaja(int[][] matriz) {
  83. int[][] fotoNueva = new int[this.alto][this.ancho];
  84. for (int i = 0; i < this.alto; i++) {
  85. for (int j = 0; j < this.ancho; j++) {
  86. if (i == 0 && j == 0) { // arriba izquierda
  87. fotoNueva[i][j] = ((matriz[0][0] + matriz[1][0] + matriz[0][1] + matriz[1][1]) / 4);
  88. } else if (i == 0 && j == this.ancho - 1) { // arriba derecha
  89. fotoNueva[i][j] = ((matriz[0][j] + matriz[1][j] + matriz[0][j - 1] + matriz[1][j - 1]) / 4);
  90. } else if (i == this.alto - 1 && j == 0) { //abajo izquierda
  91. fotoNueva[i][j] = ((matriz[i][0] + matriz[i][1] + matriz[i - 1][0] + matriz[i - 1][1]) / 4);
  92. } else if (i == this.alto - 1 && j == this.ancho - 1) { //abajo derecha
  93. fotoNueva[i][j] = ((matriz[i][j] + matriz[i - 1][j] + matriz[i][j - 1] + matriz[i - 1][j - 1]) / 4);
  94. } else if (j == 0) { //columna izquierda
  95. fotoNueva[i][j] = ((matriz[i - 1][j] + matriz[i - 1][j + 1] + matriz[i][j] + matriz[i][j + 1] + matriz[i + 1][j] + matriz[i + 1][j + 1]) / 6);
  96. } else if (j == this.ancho - 1) { //columna derecha
  97. fotoNueva[i][j] = ((matriz[i - 1][j - 1] + matriz[i - 1][j] + matriz[i][j - 1] + matriz[i][j] + matriz[i + 1][j - 1] + matriz[i + 1][j]) / 6);
  98. } else if (i == 0) { //fila arruba
  99. fotoNueva[i][j] = ((matriz[i][j - 1] + matriz[i][j] + matriz[i][j + 1] + matriz[i + 1][j - 1] + matriz[i + 1][j] + matriz[i + 1][j + 1]) / 6);
  100. } else if (i == this.alto - 1) { //fila abajo
  101. fotoNueva[i][j] = ((matriz[i - 1][j - 1] + matriz[i - 1][j] + matriz[i - 1][j + 1] + matriz[i][j - 1] + matriz[i][j] + matriz[i][j + 1]) / 6);
  102. } else { // las del medio
  103. fotoNueva[i][j] = ((matriz[i - 1][j - 1] + matriz[i - 1][j] + matriz[i - 1][j + 1]
  104. + matriz[i][j - 1] + matriz[i][j] + matriz[i][j + 1]
  105. + matriz[i + 1][j - 1] + matriz[i + 1][j] + matriz[i + 1][j + 1]) / 9);
  106.  
  107. }
  108. }
  109. }
  110.  
  111. this.matriz = fotoNueva;
  112.  
  113. }
  114.  
  115. public ImagenPGM(String nombreFichero) {
  116. this.nombreFichero = nombreFichero;
  117. }
  118.  
  119. public ImagenPGM() {
  120. }
  121.  
  122. public ImagenPGM(int alto, int ancho, double pw, int[][] matriz) {
  123. this.alto = alto;
  124. this.ancho = ancho;
  125. this.pw = pw;
  126. this.matriz = matriz;
  127.  
  128. }
  129.  
  130. public int getAlto() {
  131. return alto;
  132. }
  133.  
  134. public void setAlto(int alto) {
  135. this.alto = alto;
  136. }
  137.  
  138. public int getAncho() {
  139. return ancho;
  140. }
  141.  
  142. public void setAncho(int ancho) {
  143. this.ancho = ancho;
  144. }
  145.  
  146. public double getPw() {
  147. return pw;
  148. }
  149.  
  150. public void setPw(double pw) {
  151. this.pw = pw;
  152. }
  153.  
  154. public int[][] getMatriz() {
  155. return matriz;
  156. }
  157.  
  158. public void setMatriz(int[][] matriz) {
  159. this.matriz = matriz;
  160. }
  161.  
  162. public String getNombreFichero() {
  163. return nombreFichero;
  164. }
  165.  
  166. public void setNombreFichero(String nombreFichero) {
  167. this.nombreFichero = nombreFichero;
  168. }
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement