List of usage examples for com.lowagie.text.pdf PdfReader LZWDecode
public static byte[] LZWDecode(byte in[])
From source file:org.opensha.commons.mapping.gmt.raster.RasterExtractor.java
License:Apache License
public BufferedImage getRasterImage() throws FileNotFoundException, IOException { ArrayList<String> lines = FileUtils.loadFile(psFileName); boolean reading = false; boolean colorimage = false; boolean ascii85 = false; boolean lzwDecode = false; int width = 0; int height = 0; int pixels = 0; int expected = 0; byte[] bytes = null; int byteCount = 0; int curLine = 0; StringBuilder asciiImage = null; for (String line : lines) { if (!reading) { if (line.contains("false 3 colorimage")) { reading = true;/*w ww. ja v a 2s . c o m*/ StringTokenizer tok = new StringTokenizer(line); width = Integer.parseInt(tok.nextToken()); height = Integer.parseInt(tok.nextToken()); int depth = Integer.parseInt(tok.nextToken()); if (depth != 8) { System.out.println("BAD DEPTH! EXITING!"); return null; } pixels = width * height; expected = pixels * 3; // pixels * 1 byte for R, G, and B bytes = new byte[expected]; System.out.println( "time to READ: false 3 colorimage " + width + "x" + height + " " + pixels + " px"); colorimage = true; continue; } else if (line.contains("ASCII85Decode")) { lzwDecode = line.contains("LZWDecode"); int depth = 0; for (int i = curLine - 2; i < curLine + 3; i++) { String parseLine = lines.get(i); StringTokenizer parseTok = new StringTokenizer(parseLine); while (parseTok.hasMoreTokens()) { String token = parseTok.nextToken(); if (token.contains("/Width")) width = Integer.parseInt(parseTok.nextToken()); else if (token.contains("/Height")) height = Integer.parseInt(parseTok.nextToken()); else if (token.contains("/BitsPerComponent")) depth = Integer.parseInt(parseTok.nextToken()); } if (width > 0 && height > 0 && depth > 0) break; } System.out.println(width + " " + height + " " + depth); if (width <= 0 || height <= 0 || depth != 8) return null; pixels = width * height; expected = pixels * 3; // pixels * 1 byte for R, G, and B bytes = new byte[expected]; System.out .println("time to READ! ASCII85Decode " + width + "x" + height + " " + pixels + " px"); reading = true; ascii85 = true; asciiImage = new StringBuilder(); continue; } } if (reading && bytes != null) { //we're in the middle of the string if (ascii85) { if (line.startsWith(">> image")) continue; // asciiImage += line + "\n"; asciiImage.append(line + "\n"); if (line.contains("~>")) { // System.out.println(line); break; } } else if (colorimage) { if (line.startsWith("U")) break; for (int i = 0; i < line.length(); i += 2) { bytes[byteCount] = (byte) Integer.parseInt(line.substring(i, i + 2), 16); byteCount++; } } } curLine++; } System.out.println("Done reading...converting."); if (ascii85) { InputStream is = new ByteArrayInputStream(asciiImage.toString().getBytes("UTF-8")); ASCII85InputStream ais = new ASCII85InputStream(is); // System.out.println(asciiImage); while (!ais.isEndReached()) { if (byteCount < expected) { int data = ais.read(); bytes[byteCount] = (byte) data; byteCount++; } else break; } if (lzwDecode) { bytes = PdfReader.LZWDecode(bytes); } ais.close(); } System.out.println("Read in " + byteCount + " bytes...expected: " + expected); return this.getBufferedImage(bytes, width, height); }