From d2789c1696f36efc13d0df53b98d2370e7476738 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 25 Mar 2015 20:52:19 +0100 Subject: Week 8 done --- Week8/src/qtrees/QTree.java | 74 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 3 deletions(-) (limited to 'Week8/src/qtrees/QTree.java') diff --git a/Week8/src/qtrees/QTree.java b/Week8/src/qtrees/QTree.java index ab11898..24fa5e0 100644 --- a/Week8/src/qtrees/QTree.java +++ b/Week8/src/qtrees/QTree.java @@ -3,32 +3,100 @@ package qtrees; import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.util.logging.Level; +import java.util.logging.Logger; +/** + * Representation of a Quad-tree + * @author Camil Staps, s4498062 + */ public class QTree { + /** the root of the tree */ QTNode root; + /** + * Construct the tree based on an ASCII representation of a bitstream on a reader + * @param input the reader + */ public QTree( Reader input ) { root = readQTree( input ); } + /** + * Construct the tree based on a bitmap + * @param bitmap the bitmap + */ public QTree( Bitmap bitmap ) { root = bitmap2QTree( 0, 0, bitmap.getWidth(), bitmap ); } + /** + * Fill a bitmap based on this tree + * @param bitmap the bitmap + */ public void fillBitmap ( Bitmap bitmap ) { root.fillBitmap(0, 0, bitmap.getWidth(), bitmap); } - public void writeQTree( Writer sb ) { + /** + * Write the quad-tree as compressed bitstream + * @param sb the Writer to write to + * @throws IOException is passed on from Writer + */ + public void writeQTree( Writer sb ) throws IOException { root.writeNode( sb ); } + /** + * Read a Quad-tree node based on an ASCII representation of a bitstream on a Reader + * @param input the Reader + * @return the node + */ private static QTNode readQTree( Reader input ) { - return null; + try { + int read = input.read(); + if (read == '1') { + GreyNode node = new GreyNode(); + for (int i = 0; i < 4; i++) + node.setChild(i, readQTree(input)); + return node; + } else { + read = input.read(); + if (read == '1') { + return new WhiteLeaf(); + } else { + return new BlackLeaf(); + } + } + } catch (IOException ex) { + Logger.getLogger(QTree.class.getName()).log(Level.SEVERE, null, ex); + return null; + } } + /** + * Get a (compressed) node from a (part of a) bitmap + * @param x the x coordinate of the top left corner + * @param y the y coordinate of the top left corner + * @param width the width of the part to read + * @param bitmap the bitmap + * @return the (compressed) node + */ public static QTNode bitmap2QTree( int x, int y, int width, Bitmap bitmap ) { - return null; + if (width == 1) { + if (bitmap.getBit(x,y)) { + return new WhiteLeaf(); + } else { + return new BlackLeaf(); + } + } else { + GreyNode node = new GreyNode(); + node.setChild(0, bitmap2QTree(x, y, width / 2, bitmap)); + node.setChild(1, bitmap2QTree(x + width / 2, y, width / 2, bitmap)); + node.setChild(2, bitmap2QTree(x + width / 2, y + width / 2, width / 2, bitmap)); + node.setChild(3, bitmap2QTree(x, y + width / 2, width / 2, bitmap)); + return node.compress(); + } } } -- cgit v1.2.3