CheckAlignmentOpImage.java Source code

Java tutorial

Introduction

Here is the source code for CheckAlignmentOpImage.java

Source

/*
    
Java Media APIs: Cross-Platform Imaging, Media and Visualization
Alejandro Terrazas
Sams, Published November 2002, 
ISBN 0672320940
*/

import javax.media.jai.ImageLayout;
import javax.media.jai.PointOpImage;
import java.awt.image.RenderedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

public class CheckAlignmentOpImage extends PointOpImage {

    public CheckAlignmentOpImage(RenderedImage s1, RenderedImage s2, int sp, ImageLayout layout,
            java.util.Map configuration, boolean cobbleSources) {

        super(s1, s2, layout, configuration, cobbleSources);
        source1 = s1;
        source2 = s2;
        samplingPeriod = sp;
    }

    public Raster computeTile(int x, int y) {
        Raster r1 = source1.getTile(x, y);
        Raster r2 = source2.getTile(x, y);

        int xBounds = r1.getWidth();
        if (r2.getWidth() < xBounds)
            xBounds = r2.getWidth();
        int yBounds = r1.getHeight();
        if (r2.getHeight() < yBounds)
            yBounds = r2.getHeight();

        WritableRaster wr;
        wr = r1.createCompatibleWritableRaster(xBounds, yBounds);

        int tmpi;
        int tmpj;
        for (int i = 0; i < wr.getWidth(); i++)
            for (int j = 0; j < wr.getHeight(); j++) {
                tmpi = i / samplingPeriod;
                tmpj = j / samplingPeriod;
                if ((tmpi % 2 == 0) && (tmpj % 2 == 0))
                    wr.setDataElements(i, j, r2.getDataElements(i, j, null));
                else if ((tmpi % 2 != 0) && (tmpj % 2 != 0))
                    wr.setDataElements(i, j, r2.getDataElements(i, j, null));
                else
                    wr.setDataElements(i, j, r1.getDataElements(i, j, null));
            }
        return wr;
    }

    private RenderedImage source1, source2;

    private int samplingPeriod;
}