Example usage for com.google.gwt.typedarrays.shared TypedArrays createUint8Array

List of usage examples for com.google.gwt.typedarrays.shared TypedArrays createUint8Array

Introduction

In this page you can find the example usage for com.google.gwt.typedarrays.shared TypedArrays createUint8Array.

Prototype

public static Uint8Array createUint8Array(int length) 

Source Link

Document

Create a Uint8Array instance of length elements, backed by a new ArrayBuffer .

Usage

From source file:com.lizardtech.djvu.BSInputStream.java

License:Open Source License

/**
 * Creates a new BSInputStream object.//from w w w  .  jav  a  2  s  .c  om
* @param bsInputStream 
 */
public BSInputStream(BSInputStream toCopy) {
    this.zp = new ZPCodec(toCopy.zp);
    for (int i = 0; i < toCopy.ctx.length; i++) {
        if (toCopy.ctx[i] != null) {
            ctx[i] = new BitContext((short) (toCopy.ctx[i].get() & 0xFF));
        }
    }
    if (toCopy.data != null) {
        data = TypedArrays.createUint8Array(toCopy.data.length());
        data.set(toCopy.data);
    }
    eof = toCopy.eof;
    blocksize = toCopy.blocksize;
    bptr = toCopy.bptr;
    size = toCopy.size;
}

From source file:com.lizardtech.djvu.BSInputStream.java

License:Open Source License

/**
   * Called to decode data.// w w w . j a  v  a2s  .com
   *
   * @return the number of bytes decoded
   *
   * @throws IOException if an error occurs
   */
private int decode() throws IOException {
    /////////////////////////////////
    ////////////  Decode input stream
    size = decode_raw(24);

    if (size == 0) {
        return 0;
    }

    if (size > (MAXBLOCK * 1024)) {
        throw new IOException("ByteStream.corrupt");
    }

    // Allocate
    if (blocksize < size) {
        blocksize = size;
        data = TypedArrays.createUint8Array(blocksize);
    } else if (data == null) {
        data = TypedArrays.createUint8Array(blocksize);
    }

    // Decode Estimation Speed
    int fshift = 0;

    if (zp.decoder() != 0) {
        fshift++;

        if (zp.decoder() != 0) {
            fshift++;
        }
    }

    // Prepare Quasi MTF
    byte[] mtf = Arrays.copyOf(MTF, MTF.length);

    int[] freq = new int[FREQMAX];

    for (int i = 0; i < FREQMAX; freq[i++] = 0) {
        ;
    }

    int fadd = 4;

    // Decode
    int mtfno = 3;
    int markerpos = -1;

    for (int i = 0; i < size; i++) {
        int ctxid = CTXIDS - 1;

        if (ctxid > mtfno) {
            ctxid = mtfno;
        }

        int ctxoff = 0;

        switch (0) {
        default:

            if (zp.decoder(ctx[ctxoff + ctxid]) != 0) {
                mtfno = 0;
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += CTXIDS;

            if (zp.decoder(ctx[ctxoff + ctxid]) != 0) {
                mtfno = 1;
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += CTXIDS;

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 2 + decode_binary(ctxoff + 1, 1);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 1);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 4 + decode_binary(ctxoff + 1, 2);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 3);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 8 + decode_binary(ctxoff + 1, 3);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 7);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 16 + decode_binary(ctxoff + 1, 4);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 15);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 32 + decode_binary(ctxoff + 1, 5);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 31);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 64 + decode_binary(ctxoff + 1, 6);
                data.set(i, mtf[mtfno]);

                break;
            }

            ctxoff += (1 + 63);

            if (zp.decoder(ctx[ctxoff + 0]) != 0) {
                mtfno = 128 + decode_binary(ctxoff + 1, 7);
                data.set(i, mtf[mtfno]);

                break;
            }

            mtfno = 256;
            data.set(i, 0);
            markerpos = i;

            continue;
        }

        // Rotate mtf according to empirical frequencies (new!)
        // Adjust frequencies for overflow
        int k;
        fadd = fadd + (fadd >> fshift);

        if (fadd > 0x10000000) {
            fadd >>= 24;
            freq[0] >>= 24;
            freq[1] >>= 24;
            freq[2] >>= 24;
            freq[3] >>= 24;

            for (k = 4; k < FREQMAX; k++) {
                freq[k] >>= 24;
            }
        }

        // Relocate new char according to new freq
        int fc = fadd;

        if (mtfno < FREQMAX) {
            fc += freq[mtfno];
        }

        for (k = mtfno; k >= FREQMAX; k--) {
            mtf[k] = mtf[k - 1];
        }

        for (; (k > 0) && ((0xffffffffL & fc) >= (0xffffffffL & freq[k - 1])); k--) {
            mtf[k] = mtf[k - 1];
            freq[k] = freq[k - 1];
        }

        mtf[k] = (byte) data.get(i);
        freq[k] = fc;
    }

    /////////////////////////////////
    ////////// Reconstruct the string
    if ((markerpos < 1) || (markerpos >= size)) {
        throw new IOException("ByteStream.corrupt");
    }

    // Allocate pointers
    int[] pos = new int[size];

    for (int j = 0; j < size; pos[j++] = 0) {
        ;
    }

    // Prepare count buffer
    int[] count = new int[256];

    for (int i = 0; i < 256; count[i++] = 0) {
        ;
    }

    // Fill count buffer
    for (int i = 0; i < markerpos; i++) {
        byte c = (byte) data.get(i);
        pos[i] = (c << 24) | (count[0xff & c] & 0xffffff);
        count[0xff & c]++;
    }

    for (int i = markerpos + 1; i < size; i++) {
        byte c = (byte) data.get(i);
        pos[i] = (c << 24) | (count[0xff & c] & 0xffffff);
        count[0xff & c]++;
    }

    // Compute sorted char positions
    int last = 1;

    for (int i = 0; i < 256; i++) {
        int tmp = count[i];
        count[i] = last;
        last += tmp;
    }

    // Undo the sort transform
    int j = 0;
    last = size - 1;

    while (last > 0) {
        int n = pos[j];
        byte c = (byte) (pos[j] >> 24);
        data.set(--last, c);
        j = count[0xff & c] + (n & 0xffffff);
    }

    // Free and check
    if (j != markerpos) {
        throw new IOException("ByteStream.corrupt");
    }

    return size;
}

From source file:com.lizardtech.djvu.GMap.java

License:Open Source License

protected void setImageData(ImageData imageData) {
    this.imageData = imageData;
    Uint8Array imageArray = (Uint8Array) imageData.getData();
    // image array is clamped by default, we need non-clamped
    data = TypedArrays.createUint8Array(imageArray.buffer());
}

From source file:com.lizardtech.djvu.IWPixmap.java

License:Open Source License

/**
 * DOCUMENT ME!//from  w w  w  .  ja v a 2  s  .c o  m
 *
 * @return DOCUMENT ME!
 */
public GPixmap getPixmap() {
    if (ymap == null) {
        return null;
    }

    final int w = ymap.iw;
    final int h = ymap.ih;
    final int pixsep = 4;
    final int rowsep = w * pixsep;
    Uint8Array bytes = TypedArrays.createUint8Array(h * rowsep);

    ymap.image(0, bytes, rowsep, pixsep, false);

    if ((crmap != null) && (cbmap != null) && (crcb_delay >= 0)) {
        cbmap.image(1, bytes, rowsep, pixsep, crcb_half);
        crmap.image(2, bytes, rowsep, pixsep, crcb_half);
    }

    // Convert image to RGB
    final GPixmap ppm = new GPixmap().init(bytes, h, w);
    final GPixelReference pixel = ppm.createGPixelReference(0);

    for (int i = 0; i < h;) {
        pixel.setOffset(i++, 0);

        if ((crmap != null) && (cbmap != null) && (crcb_delay >= 0)) {
            pixel.YCC_to_RGB(w);
        } else {
            for (int x = w; x-- > 0; pixel.incOffset()) {
                pixel.setGray(127 - pixel.getBlue());
            }
        }
    }

    return ppm;
}

From source file:org.plugination.connect.html.HtmlWebSocket.java

License:Apache License

@Override
public void send(ByteBuffer data) {
    int limit = data.limit();
    ArrayBuffer buffer = TypedArrays.createArrayBuffer(limit);
    Uint8Array arrayBuffer = TypedArrays.createUint8Array(buffer);
    for (int i = 0; i < limit; i++) {
        arrayBuffer.set(i, data.get(i));
    }//from  w w  w .ja va 2 s  .  c om
    ws.send(buffer);
}

From source file:thothbot.parallax.core.client.textures.DataTexture.java

License:Open Source License

public void generateDataTexture(Color color) {
    int size = width * height;
    Uint8Array data = TypedArrays.createUint8Array(3 * size);

    int r = (int) Math.floor(color.getR() * 255);
    int g = (int) Math.floor(color.getG() * 255);
    int b = (int) Math.floor(color.getB() * 255);

    for (int i = 0; i < size; i++) {
        data.set(i * 3, r);/*from   ww  w  .j  a v  a 2 s  .  com*/
        data.set(i * 3 + 1, g);
        data.set(i * 3 + 2, b);

    }

    setData(data);
    setFormat(PixelFormat.RGB);
    setNeedsUpdate(true);
}