Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author anton
- */
- public class ImagenPGM {
- private int alto;
- private int ancho;
- private double pw;
- private int[][] matriz;
- private String nombreFichero;
- private int blancoAbsoluto;
- public void leeFichero(String nombreFicheroEntrada, String nombreFicheroSalida) {
- File fEntrada = new File(nombreFichero);
- Scanner scFile = null;
- FileWriter fw = null;
- ImagenPGM imagen;
- try {
- scFile = new Scanner(fEntrada);
- fw = new FileWriter(nombreFicheroSalida);
- scFile.nextLine();
- scFile.nextLine();
- String[] tamaño = scFile.nextLine().split(" ");
- this.ancho = Integer.parseInt(tamaño[0]);
- this.alto = Integer.parseInt(tamaño[1]);
- this.pw = Double.parseDouble(scFile.nextLine());
- this.matriz = new int[alto][ancho];
- for (int i = 0; i < alto; i++) {
- for (int j = 0; j < ancho; j++) {
- int normalizado = (int) ((double) (scFile.nextInt()) / pw * 255);
- matriz[i][j] = normalizado;
- }
- }
- imagen = new ImagenPGM(alto, ancho, pw, matriz);
- filtroCaja(matriz);
- fw.write("P2" + "\n");
- fw.write("# " + nombreFicheroSalida + "\n");
- fw.write(imagen.getAncho() + " " + imagen.getAlto() + "\n");
- fw.write("255" + "\n");
- for (int i = 0; i < alto; i++) {
- for (int j = 0; j < ancho; j++) {
- fw.write(this.matriz[i][j] + " ");
- }
- fw.write("\n");
- }
- } catch (Exception e) {
- System.out.println("Se ha producido un error al leer" + e.toString()
- );
- e.printStackTrace();
- } finally {
- if (scFile != null) {
- scFile.close();
- }
- if (fw != null) {
- try {
- fw.close();
- } catch (Exception e) {
- System.out.println("Error al cerrar el fichero: " + e.toString());
- }
- }
- }
- }
- public void filtroCaja(int[][] matriz) {
- int[][] fotoNueva = new int[this.alto][this.ancho];
- for (int i = 0; i < this.alto; i++) {
- for (int j = 0; j < this.ancho; j++) {
- if (i == 0 && j == 0) { // arriba izquierda
- fotoNueva[i][j] = ((matriz[0][0] + matriz[1][0] + matriz[0][1] + matriz[1][1]) / 4);
- } else if (i == 0 && j == this.ancho - 1) { // arriba derecha
- fotoNueva[i][j] = ((matriz[0][j] + matriz[1][j] + matriz[0][j - 1] + matriz[1][j - 1]) / 4);
- } else if (i == this.alto - 1 && j == 0) { //abajo izquierda
- fotoNueva[i][j] = ((matriz[i][0] + matriz[i][1] + matriz[i - 1][0] + matriz[i - 1][1]) / 4);
- } else if (i == this.alto - 1 && j == this.ancho - 1) { //abajo derecha
- fotoNueva[i][j] = ((matriz[i][j] + matriz[i - 1][j] + matriz[i][j - 1] + matriz[i - 1][j - 1]) / 4);
- } else if (j == 0) { //columna izquierda
- 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);
- } else if (j == this.ancho - 1) { //columna derecha
- 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);
- } else if (i == 0) { //fila arruba
- 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);
- } else if (i == this.alto - 1) { //fila abajo
- 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);
- } else { // las del medio
- 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]
- + matriz[i + 1][j - 1] + matriz[i + 1][j] + matriz[i + 1][j + 1]) / 9);
- }
- }
- }
- this.matriz = fotoNueva;
- }
- public ImagenPGM(String nombreFichero) {
- this.nombreFichero = nombreFichero;
- }
- public ImagenPGM() {
- }
- public ImagenPGM(int alto, int ancho, double pw, int[][] matriz) {
- this.alto = alto;
- this.ancho = ancho;
- this.pw = pw;
- this.matriz = matriz;
- }
- public int getAlto() {
- return alto;
- }
- public void setAlto(int alto) {
- this.alto = alto;
- }
- public int getAncho() {
- return ancho;
- }
- public void setAncho(int ancho) {
- this.ancho = ancho;
- }
- public double getPw() {
- return pw;
- }
- public void setPw(double pw) {
- this.pw = pw;
- }
- public int[][] getMatriz() {
- return matriz;
- }
- public void setMatriz(int[][] matriz) {
- this.matriz = matriz;
- }
- public String getNombreFichero() {
- return nombreFichero;
- }
- public void setNombreFichero(String nombreFichero) {
- this.nombreFichero = nombreFichero;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement