protected void f_unfuck(int[] pixel, final int count, final int iter) throws Exception { HuffmanTree tree = f_ht1; int pi = 0; int[] diffbuf = new int[64]; int[] leaves = new int[64]; int token = -1; int zeros = 0; int bitcount = 0; int sign = 0; int diff = 0; for (int j = 0; j < count; ++j) { tree = f_ht1; for (int i = 0; i < 64; ++i) { diffbuf[i] = 0; leaves[i] = -1; } for (int i = 0; i < 64; ++i) { try { while ((token = tree.find(f_stream.bits(1))) < 0) ; /* do { final int dir = f_stream.bits(1); System.err.println(" dir: " + dir); token = tree.find(dir); } while ( token < 0 ); */ tree = f_ht2; // System.err.println("token: " + token); // leaves[i] = token; if (token == 0 && i > 0) break; if (token == 0xff) continue; zeros = token >> 4; bitcount = token & 15; i += zeros; if (bitcount == 0) continue; sign = f_stream.bits(1); diff = f_stream.bits(bitcount - 1); if (sign == 1) diff += 1 << (bitcount - 1); else diff += (-1 << bitcount) + 1; if (i < 64) diffbuf[i] = diff; } catch (Exception e) { System.err.println(); System.err.println("--"); System.err.println("iter: " + iter); System.err.println("i: " + i); // System.err.println("path: " + tree.findPath()); System.err.println("token: " + token); System.err.println("--"); System.err.println(); e.printStackTrace(); System.exit(1); } } diffbuf[0] += f_carry; f_carry = diffbuf[0]; /* System.err.print("diffbuf: " ); for ( int i = 0; i < 64; ++i ) System.err.print(diffbuf[i] + " "); System.err.println(); System.exit(1); */ for (int i = 0; i < 64; ++i) { if (f_pixelIndex++ % f_info.rawWidth == 0) f_base[0] = f_base[1] = 512; final int pii = pi + i; pixel[pii] = (f_base[i & 1] += diffbuf[i]); while (pixel[pii] < 0) pixel[pii] += 65536; while (pixel[pii] >= 65536) pixel[pii] -= 65536; // System.out.print(" " + pixel[pi + i]); } // System.out.println(); pi += 64; } }
public CaptureData decode() { f_cd = new CaptureData(f_info.width, f_info.height, FILTER_RGGB, 12); f_cd.setImageInfo(f_info); final int table = (f_info.table > 2) ? 2 : f_info.table; f_info.table = table; f_ht1 = new HuffmanTree(StaticTree.FIRST_TREE[table], 16); f_ht2 = new HuffmanTree(StaticTree.SECOND_TREE[table], 16); int black = 0; int black_count = 0; try { /* * Create fast reading stream from file. */ final byte[] array = new byte[f_size]; f_stream.seek(0); f_stream.readFully(array, 0, f_size); f_stream = SeekableInputStream.getInstance(f_info.order, array); /* * Begin the decompression by seeking to place of the good 10-bits. */ int top = 0; int left = 0; switch (f_info.rawWidth) { case 2144: top = 8; left = 4; break; // G1 case 2224: top = 6; left = 48; break; // EOS D30 case 2376: top = 6; left = 12; break; // G2 or G3 case 2672: top = 6; left = 12; break; // S50 case 3152: top = 12; left = 64; break; // EOS D60 } final int lowbits = f_canonHasLowbits(); if (lowbits == 0) System.err.println("Yikes--we have low bit data!"); if (lowbits == 1) System.err.println("Weird--we have uncompressed low-order bits first!"); System.err.println("lowbits: " + lowbits); final int hiLocation = 540 + lowbits * f_info.rawHeight * f_info.rawWidth / 4; f_stream.seek(hiLocation); int[] pixel = new int[f_info.rawWidth * 8]; int unfuck_iter = 0; System.err.println("Using decode table " + f_info.table + "..."); System.err.println( "Beginning total decompression (at file offset " + hiLocation + ") [" + f_stream.getPosition() + "]..."); for (int row = 0; row < f_info.rawHeight; row += 8) { final long _before_save = f_stream.getPosition(); f_unfuck(pixel, f_info.rawWidth >> 3, unfuck_iter); final long _after_save = f_stream.getPosition(); System.err.println( "Decompressed rows " + (row) + " through " + (row + 7) + " (file: " + _before_save + " - " + _after_save + ") iter " + unfuck_iter); ++unfuck_iter; if (lowbits != 0) { final long save = f_stream.getPosition(); f_stream.seek(26 + row * f_info.rawWidth / 4); int pi = 0; for (int i = 0; i < f_info.rawWidth << 1; ++i) { int c = f_stream.read(); for (int r = 0; r < 8; r += 2) { pixel[pi] = (pixel[pi] << 2) + ((c >> r) & 3); while (pixel[pi] >= 65536) pixel[pi] -= 65536; ++pi; } } f_stream.seek(save); } // DEBUG: StringBuffer buffer = new StringBuffer("pixel row " + row + ": "); for ( int pi = // 0; pi < f_info.rawWidth; ++pi ) buffer.append(pixel[pi] + " "); // System.out.println(buffer); for (int r = 0; r < 8; ++r) { for (int col = 0; col < f_info.rawWidth; ++col) { // DEBUG: System.out.println(" @@ pixel[" + (r * f_info.rawWidth + col) + "]: " + // pixel[r * f_info.rawWidth + col]); final int irow = row + r - top; if (irow >= 0 && irow < f_info.height) { if (col < left) { // System.out.println(" black (" + black_count + "): " + pixel[r * f_info.rawWidth // + col]); black += pixel[r * f_info.rawWidth + col]; ++black_count; } else { final int i = irow * f_info.width + col - left; f_cd.setElem(i, pixel[r * f_info.rawWidth + col]); } } } } } } catch (Exception e) { e.printStackTrace(); System.exit(1); } // DEBUG: for ( int y = 0; y < f_info.height; ++y ) { StringBuffer buffer = new // StringBuffer("irow " + y + ": "); for ( int x = 0; x < f_info.width; ++x ) // buffer.append(f_cd.get(x,y) + " "); System.out.println(buffer.toString()); } System.err.println("total black: " + black + " (" + black_count + ")"); // -- 3 March 2004 -- black = (black << BIT_REMAINDER) / ((f_info.rawWidth - f_info.width) * // f_info.height); black = black / ((f_info.rawWidth - f_info.width) * f_info.height); f_info.black = black; System.err.println("average black: " + black); f_blackScale(); // f_averageGreens(); return f_cd; }