/* * Based on i/o subroutines in C provided by G. Gimel'farb * Ported to Java by Andrew Probert: 10 March 2011 * * Updated by jnie014 to read files as bytes, rather than text: 16 March 2011 * * Functions for reading in and saving grayscale PGM and colour PPM format files. * */
public class PGM_PPM_Handler {
/* ** i/o images in pgmm format */
/* * name -- an image file specification * id -- the type of image (an output external variable) * row -- the number of image rows (an output external variable) * col -- the number of image columns (an output external variable) *-------------------------------------------------------------------------- * Terminates the program if the file can't be open or is not in pgm format */ static char[] readFilePGM_PPM( String name, int [] id, int[] row, int[] col ) { String str; java.io.BufferedReader bReader; int i, j, k, k2; char[] crowstr = new char[10]; for (i = 0; i < 10; i++) crowstr[i] = 0; char [] ccolstr = new char[10]; for (i = 0; i < 10; i++) ccolstr[i] = 0; String rowstr; String colstr; int base, maxpts, column, line; char[] image;
byte[] asBytes = new byte[10000]; // Store all data read from file as bytes java.io.FileInputStream fis = null; java.io.BufferedInputStream bis = null; int byteCount = 0; // Counts the number of bytes in the header information int fileSize = 0; // Stores the size of the whole file try { fis = new java.io.FileInputStream(name); bis = new java.io.BufferedInputStream(fis); fileSize = fis.available(); //System.out.println("Total number of bytes in file: "+fileSize); asBytes = new byte[fileSize]; fis.read(asBytes);
} catch (Exception e) { System.out.println("Error: "+e.getMessage()); } finally { try { fis.close(); bis.close(); } catch (Exception e) {} } java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(asBytes); // A stream of bytes, created from the byte[] above try { bReader = new java.io.BufferedReader(new java.io.InputStreamReader(bais));
str = bReader.readLine(); byteCount += str.getBytes().length; byteCount++; str = str.trim(); if ( !str.equals("P5") && !str.equals("P6")) { System.out.println( "\n readFilePGM_PPM(): file " + name + " is neither a PGM nor a PPM file; its magic key is " + str); return null; }
if (str.equals("P5")) id[0] = 1; if (str.equals("P6")) id[0] = 3;
do { str = bReader.readLine(); byteCount += str.getBytes().length; byteCount++;} while( str.charAt(0)=='#' ); //System.out.println(str);
k = 0; char temp = str.charAt(k); do { temp = str.charAt(k); ccolstr[k] = temp; k++; } while( temp != ' ' ); ccolstr[k]='\0'; colstr = String.valueOf(ccolstr).trim(); //System.out.println(colstr);
//k++; k2=0; temp = str.charAt(k2 + k); do{ temp = str.charAt(k2 + k); crowstr[k2] = temp; k2++; } while( (k2 + k) < str.length() ); crowstr[k2] = '\0'; rowstr = String.valueOf(crowstr).trim(); //System.out.println(rowstr);
public class PGM_PPM_Handler {
/* ** i/o images in pgmm format */
/* * name -- an image file specification * id -- the type of image (an output external variable) * row -- the number of image rows (an output external variable) * col -- the number of image columns (an output external variable) *-------------------------------------------------------------------------- * Terminates the program if the file can't be open or is not in pgm format */ static char[] readFilePGM_PPM( String name, int [] id, int[] row, int[] col ) { String str; java.io.BufferedReader bReader; int i, j, k, k2; char[] crowstr = new char[10]; for (i = 0; i < 10; i++) crowstr[i] = 0; char [] ccolstr = new char[10]; for (i = 0; i < 10; i++) ccolstr[i] = 0; String rowstr; String colstr; int base, maxpts, column, line; char[] image;
byte[] asBytes = new byte[10000]; // Store all data read from file as bytes java.io.FileInputStream fis = null; java.io.BufferedInputStream bis = null; int byteCount = 0; // Counts the number of bytes in the header information int fileSize = 0; // Stores the size of the whole file try { fis = new java.io.FileInputStream(name); bis = new java.io.BufferedInputStream(fis); fileSize = fis.available(); //System.out.println("Total number of bytes in file: "+fileSize); asBytes = new byte[fileSize]; fis.read(asBytes);
} catch (Exception e) { System.out.println("Error: "+e.getMessage()); } finally { try { fis.close(); bis.close(); } catch (Exception e) {} } java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(asBytes); // A stream of bytes, created from the byte[] above try { bReader = new java.io.BufferedReader(new java.io.InputStreamReader(bais));
str = bReader.readLine(); byteCount += str.getBytes().length; byteCount++; str = str.trim(); if ( !str.equals("P5") && !str.equals("P6")) { System.out.println( "\n readFilePGM_PPM(): file " + name + " is neither a PGM nor a PPM file; its magic key is " + str); return null; }
if (str.equals("P5")) id[0] = 1; if (str.equals("P6")) id[0] = 3;
do { str = bReader.readLine(); byteCount += str.getBytes().length; byteCount++;} while( str.charAt(0)=='#' ); //System.out.println(str);
k = 0; char temp = str.charAt(k); do { temp = str.charAt(k); ccolstr[k] = temp; k++; } while( temp != ' ' ); ccolstr[k]='\0'; colstr = String.valueOf(ccolstr).trim(); //System.out.println(colstr);
//k++; k2=0; temp = str.charAt(k2 + k); do{ temp = str.charAt(k2 + k); crowstr[k2] = temp; k2++; } while( (k2 + k) < str.length() ); crowstr[k2] = '\0'; rowstr = String.valueOf(crowstr).trim(); //System.out.println(rowstr);