private static ImageLayout layoutHelper(Vector sources,
                                            ImageLayout il) {

        ImageLayout layout = (il == null) ? new ImageLayout() : (ImageLayout)il.clone();

        int numSources = sources.size();

        // dest data type is the maximum of transfertype of source image
        // utilizing the monotonicity of data types.

        // dest number of bands = sum of source bands
        int destNumBands = totalNumBands(sources);

        int destDataType = DataBuffer.TYPE_BYTE;  // initialize
        RenderedImage srci = (RenderedImage)sources.get(0);
        Rectangle destBounds = new Rectangle(srci.getMinX(),  srci.getMinY(),
                                             srci.getWidth(), srci.getHeight());                                             
        for ( int i = 0; i < numSources; i++ ) {
            srci = (RenderedImage)sources.get(i);
            destBounds = destBounds.intersection(new Rectangle(srci.getMinX(), srci.getMinY(),
                                                 srci.getWidth(), srci.getHeight()));

            int typei = srci.getSampleModel().getTransferType();

            // NOTE: this depends on JDK ordering
            destDataType = typei > destDataType ? typei : destDataType;
        }

        SampleModel sm = layout.getSampleModel((RenderedImage)sources.get(0));

        if ( sm.getNumBands() < destNumBands ) {
            int[] destOffsets = new int[destNumBands];

            for(int i=0; i < destNumBands; i++) {
                destOffsets[i] = i;
            }

            // determine the proper width and height to use
            int destTileWidth = sm.getWidth();
            int destTileHeight = sm.getHeight();
            if(layout.isValid(ImageLayout.TILE_WIDTH_MASK))
            {
                destTileWidth =
                    layout.getTileWidth((RenderedImage)sources.get(0));
            }
            if(layout.isValid(ImageLayout.TILE_HEIGHT_MASK))
            {
                destTileHeight =
                    layout.getTileHeight((RenderedImage)sources.get(0));
            }
            
            sm = RasterFactory.createComponentSampleModel(sm,
                                                          destDataType,
                                                          destTileWidth,
                                                          destTileHeight,
                                                          destNumBands);


            layout.setSampleModel(sm);
        }

        ColorModel cm = layout.getColorModel(null);

        if ( cm != null &&
             !JDKWorkarounds.areCompatibleDataModels(sm, cm)) {
            // Clear the mask bit if incompatible.
            layout.unsetValid(ImageLayout.COLOR_MODEL_MASK);
        }

        return layout;
    }