Example #1
0
    public ComponentMap(final PixelFormat.Composition src, final PixelFormat.Composition dst) {
      final int sCompCount = src.componentCount();
      final int dCompCount = dst.componentCount();
      final PixelFormat.CType[] sCompOrder = src.componentOrder();
      final PixelFormat.CType[] dCompOrder = dst.componentOrder();

      dst2src = new int[dCompCount];
      for (int dIdx = 0; dIdx < dCompCount; dIdx++) {
        dst2src[dIdx] = PixelFormatUtil.find(dCompOrder[dIdx], sCompOrder, true);
      }
      src2dst = new int[sCompCount];
      for (int sIdx = 0; sIdx < sCompCount; sIdx++) {
        src2dst[sIdx] = PixelFormatUtil.find(sCompOrder[sIdx], dCompOrder, true);
      }
      srcRGBA = new int[4];
      srcRGBA[0] = PixelFormatUtil.find(PixelFormat.CType.R, sCompOrder, false);
      srcRGBA[1] = PixelFormatUtil.find(PixelFormat.CType.G, sCompOrder, false);
      srcRGBA[2] = PixelFormatUtil.find(PixelFormat.CType.B, sCompOrder, false);
      srcRGBA[3] = PixelFormatUtil.find(PixelFormat.CType.A, sCompOrder, false);
      hasSrcRGB = 0 <= srcRGBA[0] && 0 <= srcRGBA[1] && 0 <= srcRGBA[2];
    }
Example #2
0
  public static void convert(
      final ComponentMap cmap,
      final PixelFormat.Composition dstComp,
      final Bitstream<ByteBuffer> dstBitStream,
      final PixelFormat.Composition srcComp,
      final Bitstream<ByteBuffer> srcBitStream)
      throws IllegalStateException, IOException {
    final int sCompCount = srcComp.componentCount();
    final int dCompCount = dstComp.componentCount();
    final int[] sc = new int[sCompCount];
    final int[] dcDef = new int[dCompCount];
    final int[] srcCompBitCount = srcComp.componentBitCount();
    final int[] srcCompBitMask = srcComp.componentBitMask();
    final int[] dstCompBitCount = dstComp.componentBitCount();

    // Fill w/ source values
    for (int sIdx = 0; sIdx < sCompCount; sIdx++) {
      sc[sIdx] = srcBitStream.readBits31(srcCompBitCount[sIdx]) & srcCompBitMask[sIdx];
    }
    srcBitStream.skip(srcComp.bitStride() - srcComp.bitsPerPixel());

    // Cache missing defaults
    for (int i = 0; i < dCompCount; i++) {
      dcDef[i] = dstComp.defaultValue(i, false);
    }

    if (1 == dCompCount && PixelFormat.CType.Y == dstComp.componentOrder()[0] && cmap.hasSrcRGB) {
      // RGB[A] -> Y conversion
      final int r = sc[cmap.srcRGBA[0]];
      final int g = sc[cmap.srcRGBA[1]];
      final int b = sc[cmap.srcRGBA[2]];
      final float rF = srcComp.toFloat(r, cmap.srcRGBA[0], false);
      final float gF = srcComp.toFloat(g, cmap.srcRGBA[1], false);
      final float bF = srcComp.toFloat(b, cmap.srcRGBA[2], false);
      final int a;
      final float aF;
      /**
       * if( 0 <= cmap.srcRGBA[3] ) { // disable premultiplied-alpha a = sc[cmap.srcRGBA[3]]; aF =
       * srcComp.toFloat(a, false, cmap.srcRGBA[3]); } else
       */
      {
        a = 1;
        aF = 1f;
      }
      final float lF = (rF + gF + bF) * aF / 3f;
      final int v = dstComp.fromFloat(lF, 0, false);

      dstBitStream.writeBits31(dstCompBitCount[0], v);
      dstBitStream.skip(dstComp.bitStride() - dstComp.bitsPerPixel());
      if (DEBUG) {
        if (srcBitStream.position() <= 8 * 4) {
          System.err.printf(
              "convert: rgb[a] -> Y: rgb 0x%02X 0x%02X 0x%02X 0x%02X -> %f %f %f %f"
                  + " -> %f -> dstC 0 0x%08X (%d bits: %s)%n",
              r,
              g,
              b,
              a,
              rF,
              gF,
              bF,
              aF,
              lF,
              v,
              dstCompBitCount[0],
              Bitstream.toBinString(true, v, dstCompBitCount[0]));
        }
      }
      return;
    }

    for (int dIdx = 0; dIdx < dCompCount; dIdx++) {
      int sIdx;
      if (0 <= (sIdx = cmap.dst2src[dIdx])) {
        final float f = srcComp.toFloat(sc[sIdx], sIdx, false);
        final int v = dstComp.fromFloat(f, dIdx, false);
        dstBitStream.writeBits31(dstCompBitCount[dIdx], v);
        if (DEBUG) {
          if (srcBitStream.position() <= 8 * 4) {
            System.err.printf(
                "convert: srcC %d: 0x%08X -> %f -> dstC %d 0x%08X (%d bits: %s)%n",
                sIdx,
                sc[sIdx],
                f,
                dIdx,
                v,
                dstCompBitCount[dIdx],
                Bitstream.toBinString(true, v, dstCompBitCount[dIdx]));
          }
        }
      } else {
        dstBitStream.writeBits31(dstCompBitCount[dIdx], dcDef[dIdx]);
        if (DEBUG) {
          if (srcBitStream.position() <= 8 * 4) {
            System.err.printf(
                "convert: srcC %d: undef -> dstC %d 0x%08X (%d bits: %s)%n",
                sIdx,
                dIdx,
                dcDef[dIdx],
                dstCompBitCount[dIdx],
                Bitstream.toBinString(true, dcDef[dIdx], dstCompBitCount[dIdx]));
          }
        }
      }
    }
    dstBitStream.skip(dstComp.bitStride() - dstComp.bitsPerPixel());
    return;
  }