Example #1
0
  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;
  }