| 1 | /* |
| 2 | * Copyright (C) 2008 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 3 or |
| 6 | * version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | package uk.me.parabola.imgfmt.app.net; |
| 14 | |
| 15 | import uk.me.parabola.imgfmt.app.BitWriter; |
| 16 | |
| 17 | /** |
| 18 | * A bit writer that can be configured with different bit width and sign properties. |
| 19 | * |
| 20 | * The sign choices are: |
| 21 | * negative: all numbers are negative and so can be represented without a sign bit. (or all positive |
| 22 | * if this is false). |
| 23 | * signed: numbers are positive and negative, and so have sign bit. |
| 24 | * |
| 25 | * The bit width is composed of two parts since it is represented as a difference between |
| 26 | * a well known minimum value and the actual value. |
| 27 | */ |
| 28 | class VarBitWriter { |
| 29 | private final BitWriter bw; |
| 30 | private final int minWidth; |
| 31 | int bitWidth; |
| 32 | boolean negative; |
| 33 | boolean signed; |
| 34 | |
| 35 | VarBitWriter(BitWriter bw, int minWidth) { |
| 36 | this.bw = bw; |
| 37 | this.minWidth = minWidth; |
| 38 | } |
| 39 | |
| 40 | public VarBitWriter(BitWriter bw, int minWidth, boolean negative, boolean signed, int width) { |
| 41 | this(bw, minWidth); |
| 42 | this.negative = negative; |
| 43 | this.signed = signed; |
| 44 | if (width > minWidth) |
| 45 | this.bitWidth = width - minWidth; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Write the number to the bit stream. If the number cannot be written |
| 50 | * correctly with this bit writer then an exception is thrown. This shouldn't |
| 51 | * happen since we check before hand and create a new writer if the numbers are not |
| 52 | * going to fit. |
| 53 | * |
| 54 | * @param n The number to be written. |
| 55 | */ |
| 56 | public void write(int n) { |
| 57 | if (!checkFit(n)) |
| 58 | throw new Abandon("number does not fit bit space available"); |
| 59 | |
| 60 | if (n < 0 && negative) |
| 61 | n = -n; |
| 62 | |
| 63 | if (signed) { |
| 64 | int mask = (1 << (minWidth + bitWidth+2)) - 1; |
| 65 | n &= mask; |
| 66 | } |
| 67 | |
| 68 | bw.putn(n, minWidth+bitWidth + ((signed)?1:0)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Checks to see if the number that we want to write can be written by this writer. |
| 73 | * @param n The number we would like to write. |
| 74 | * @return True if all is OK for writing it. |
| 75 | */ |
| 76 | boolean checkFit(int n) { |
| 77 | if (negative) { |
| 78 | if (n > 0) { |
| 79 | return false; |
| 80 | } else { |
| 81 | n = -n; |
| 82 | } |
| 83 | } else if (signed && n < 0) { |
| 84 | n = -1 - n; |
| 85 | } |
| 86 | |
| 87 | int mask = (1 << minWidth + bitWidth) - 1; |
| 88 | |
| 89 | return n == (n & mask); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Write the format of this bit writer to the output stream. Used at the beginning and |
| 94 | * when changing the bit widths. |
| 95 | */ |
| 96 | public void writeFormat() { |
| 97 | bw.put1(negative); |
| 98 | bw.put1(signed); |
| 99 | bw.putn(bitWidth, 4); |
| 100 | } |
| 101 | } |