imageblocker.java

来自「用java写的jt-jpeg。jt表示java time package」· Java 代码 · 共 95 行

JAVA
95
字号
package jpeg;
import jcp.*;
import jcp.clocked.*;

/**
 *  ImageBlocker.java
 *  A component that converts an image into a serial stream of 8x8 image blocks. The
 *  image can then be reconstructed using the ImageUnblocker component.
 *
 *  Input(s):
 *      image in - An WxH matrix of integers representing an image.
 *  Ouput(s):
 *      block out - An 8x8 matrix of integers. The first block emitted corresponds to
 *                  the upper-left block of the image. The remainder of the image is
 *                  scanned out in left-right, top-bottom order. Thus, the final block
 *                  corresponds to the bottom-right block of the image.
 *      width - The width of the input image.
 *      height - The height of the input image.
 *      done - A Boolean that is set true after the final block has been emitted.
 *
 *  @see IntMatrix
 *  @see ImageUnblocker
 */

/**
 *  @author James S. Young, based on original source by Florian Raemy &
 *  LCAV, Swiss Federal Institute of Technology, Lausanne, Switzerland
 */

public class ImageBlocker extends Synchronous
{
    private int[][] image;
    private Port img_p, blk_p, w_p, h_p, done_p;

    public ImageBlocker() {
        setName("Image Blocker");
        img_p = addPort(true,"image in");
        blk_p = addPort(false,"block out");
        w_p = addPort(false,"width");
        h_p = addPort(false,"height");
        done_p = addPort(false,"done");
    }

    public void run() {

        while(true) {
            waitClock();    // TICK!

            int[][] block = new int[8][8];
            int width = 0;
            int height = 0;

            emit(Signal.FALSE,done_p);  // Not done yet..

            Object s = img_p.signal();
            if (s instanceof IntMatrix) {
                image = ((IntMatrix)s).get_matrix();
                width = image.length;
                height = image[0].length;

                int blockW = width/8;
                int blockH = height/8;

                emit(new Integer(blockW*8),w_p);
                emit(new Integer(blockH*8),h_p);

                waitClock();    // TICK!

                for (int i=0; i < (height)/8; i++) {
                    for (int j=0; j < (width)/8; j++) {

                        int xpos = j * 8;
                        int ypos = i * 8;

                        for (int a=0; a < 8; a++) {
                            for (int b=0; b < 8; b++) {
                                block[a][b] = image[xpos+a][ypos+b];
                            }
                        }

                        // Output an image block:
                        emit(new IntMatrix(block),blk_p);
                        waitClock();    // TICK!
                    }
                }

                emit(Signal.TRUE,done_p); // Now we're done
                waitClock();
                emit(Signal.FALSE,done_p);  // Not done..
                waitClock();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?