Package com.lightcrafts.utils

Examples of com.lightcrafts.utils.ProgressIndicator


            folder,
            100,
            FileCacheFactory.get(folder),
            true,
            ImageDatumComparator.Name,
            new ProgressIndicator() {
                int count;
                public void incrementBy(int delta) {
                    count += delta;
                    System.out.println(count);
                }
View Full Code Here


            TIFF_PLANAR_CONFIGURATION, TIFF_PLANAR_CONFIGURATION_CHUNKY
        );
        setIntField( TIFF_ROWS_PER_STRIP, stripHeight );
        setIntField( TIFF_SAMPLES_PER_PIXEL, bands );

        final ProgressIndicator indicator;
        if ( thread != null ) {
            indicator = thread.getProgressIndicator();
            if ( indicator != null )
                indicator.setMaximum( imageHeight );
        } else
            indicator = null;

        // Allocate the output buffer only once
        final SampleModel sm =
            image.getSampleModel().createCompatibleSampleModel(
                imageWidth, stripHeight
            );
        final WritableRaster outBuffer;
        if (dataType == DataBuffer.TYPE_BYTE)
            outBuffer = new ByteInterleavedRaster(sm, new Point(0, 0));
        else
            outBuffer = new ShortInterleavedRaster(sm, new Point(0, 0));

        int stripIndex = 0;
        for ( int y = 0; y < imageHeight; y += stripHeight ) {
            if ( thread != null && thread.isCanceled() )
                return;

            final int currentStripHeight = Math.min( stripHeight, imageHeight - y );

            // Create a child raster of the out buffer for the current strip
            final WritableRaster raster = outBuffer.createWritableChild(0, 0, imageWidth, currentStripHeight, 0, y, null);

            // Prefetch tiles, uses all CPUs
            if (image instanceof PlanarImage)
                ((PlanarImage) image).getTiles(((PlanarImage) image).getTileIndices(raster.getBounds()));

            image.copyData(raster);

            int offset;
            if (raster instanceof ByteInterleavedRaster) {
                final ByteInterleavedRaster interleaved =
                    (ByteInterleavedRaster) raster;

                final int[] offsets = interleaved.getDataOffsets();
                offset = offsets[0];
                for (int i = 1; i < offsets.length; i++)
                    offset = Math.min(offset, offsets[i]);

                final DataBufferByte db = (DataBufferByte)raster.getDataBuffer();

                final int written = writeStripByte( stripIndex, db.getData(), offset, bands * imageWidth * currentStripHeight );

                if ( written != bands * imageWidth * currentStripHeight )
                    throw new LCImageLibException(
                        "something is wrong: " + written + " != " +
                        (bands * imageWidth * currentStripHeight)
                    );
            } else {
                final ShortInterleavedRaster interleaved =
                    (ShortInterleavedRaster) raster;

                final int[] offsets = interleaved.getDataOffsets();
                offset = offsets[0];
                for (int i = 1; i < offsets.length; i++)
                    offset = Math.min(offset, offsets[i]);

                final DataBufferUShort db = (DataBufferUShort) raster.getDataBuffer();

                final int written = writeStripShort( stripIndex, db.getData(), offset, 2 * bands * imageWidth * currentStripHeight );

                if ( written != 2 * bands * imageWidth * currentStripHeight )
                    throw new LCImageLibException(
                        "something is wrong: " + written + " != " + (2 * bands * imageWidth * currentStripHeight)
                    );
            }
            stripIndex++;
            if ( indicator != null )
                indicator.incrementBy( currentStripHeight );
        }

        if ( indicator != null )
            indicator.setIndeterminate( true );
    }
View Full Code Here

        setIntField(TIFF_PHOTOMETRIC_INTERPRETATION, TIFF_PHOTOMETRIC_RGB );

        setIntField(TIFF_TILE_WIDTH, image.getTileWidth());
        setIntField(TIFF_TILE_LENGTH, image.getTileHeight());

        final ProgressIndicator indicator;
        if (thread != null) {
            indicator = thread.getProgressIndicator();
            if ( indicator != null )
                indicator.setMaximum( image.getNumXTiles() * image.getNumYTiles() );
        } else
            indicator = null;

        for ( int tileX = 0; tileX < image.getNumXTiles(); tileX++ )
            for ( int tileY = 0; tileY < image.getNumYTiles(); tileY++ ) {
                if ( thread != null && thread.isCanceled() )
                    return;
                final int tileIndex = computeTile(tileX * image.getTileWidth(), tileY * image.getTileHeight(), 0, 0);
                final Raster tile = image.getTile(tileX, tileY);

                if (dataType == DataBuffer.TYPE_BYTE) {
                    final byte[] buffer = ((DataBufferByte) tile.getDataBuffer()).getData();

                    final int bytesWritten =  writeTileByte(
                        tileIndex, buffer, 0, buffer.length
                    );
                    if ( bytesWritten != buffer.length )
                        throw new LCImageLibException(
                            "something is wrong: " + bytesWritten + " != " + buffer.length
                        );
                } else {
                    final short[] buffer = ((DataBufferUShort) tile.getDataBuffer()).getData();

                    final int bytesWritten = writeTileShort(
                        tileIndex, buffer, 0, buffer.length * 2
                    );
                    if ( bytesWritten != buffer.length * 2 )
                        throw new LCImageLibException(
                            "something is wrong: " + bytesWritten + " != " + buffer.length * 2
                        );
                }
                if ( indicator != null )
                    indicator.incrementBy( 1 );
            }
        if ( indicator != null )
            indicator.setIndeterminate( true );
    }
View Full Code Here

     * @param cs The {@link ColorSpace} to use.
     */
    private void readImage( ProgressThread thread, ColorSpace cs )
        throws LCImageLibException, UserCanceledException
    {
        ProgressIndicator indicator = null;
        if ( thread != null )
            indicator = thread.getProgressIndicator();
        if ( indicator != null )
            indicator.setMaximum( m_height );

        // todo: deal with color models other than rgb and grayscale

        if ( cs == null )
            cs = (m_colorsPerPixel == 1 ? JAIContext.gray22ColorSpace :
                  m_colorsPerPixel == 3 ? JAIContext.sRGBColorSpace :
                                          JAIContext.CMYKColorSpace);

        // Color model for the image (and everything else).
        final ComponentColorModel ccm = new ComponentColorModel(
            cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE
        );

        // Sample model for the readout buffer large enough to hold a tile or a
        // strip of the image.
        final SampleModel jpegTsm = ccm.createCompatibleSampleModel(
            m_width, JAIContext.TILE_HEIGHT
        );

        // The readout buffer itself.
        final DataBuffer db = new DataBufferByte(
            m_colorsPerPixel * m_width * JAIContext.TILE_HEIGHT
        );

        // Sample model for the output image.
        final SampleModel tsm = ccm.createCompatibleSampleModel(
            JAIContext.TILE_WIDTH, JAIContext.TILE_HEIGHT
        );

        // Layout of the output image.
        final ImageLayout layout = new ImageLayout(
            0, 0, m_width, m_height, 0, 0, JAIContext.TILE_WIDTH,
            JAIContext.TILE_HEIGHT, tsm, ccm
        );

        // The output image itself, directly allocated in the file cache.
        final CachedImage image =
            new CachedImage( layout, JAIContext.fileCache );

        // Load Image Data
        int tileY = 0;
        int totalLinesRead = 0;
        while ( totalLinesRead < m_height ) {
            if ( thread != null && thread.isCanceled() )
                throw new UserCanceledException();
            final int tileHeight =
                Math.min( JAIContext.TILE_HEIGHT, m_height - totalLinesRead );

            // Wrap the data buffer with a Raster representing the input data.
            final WritableRaster raster = Raster.createWritableRaster(
                jpegTsm, db, new Point( 0, tileY * JAIContext.TILE_HEIGHT )
            );

            final int linesRead = readScanLines(
                ((DataBufferByte)db).getData(), 0, tileHeight
            );
            if ( linesRead <= 0 ) {
                System.out.println("Problem with readScanLines, returned: " + linesRead );
                break;
            }

            if ( m_hasAdobeSegment && m_colorsPerPixel == 4 ) {
                //
                // CMYK JPEG images generated by Photoshop are inverted, so we
                // have to invert the data to make it look right.
                //
                final byte data[] = ((DataBufferByte) db).getData();
                for (int i = 0; i < data.length; i++)
                    data[i] = (byte)~data[i];
            }

            totalLinesRead += linesRead;
            tileY++;

            image.setData( raster );

            if ( indicator != null )
                indicator.incrementBy( linesRead );
        }
        if ( indicator != null )
            indicator.setIndeterminate( true );
        m_image = image;
    }
View Full Code Here

        final int imageWidth = image.getWidth();
        final int imageHeight = image.getHeight();
        final Rectangle stripRect = new Rectangle();
        int stripHeight = 8;

        ProgressIndicator indicator = null;
        if ( thread != null )
            indicator = thread.getProgressIndicator();
        if ( indicator != null )
            indicator.setMaximum( imageHeight );

        final int bands = image.getSampleModel().getNumBands();
        final SampleModel sm =
            new PixelInterleavedSampleModel(
                DataBuffer.TYPE_BYTE,
                imageWidth, stripHeight, bands, bands * imageWidth,
                bands == 1 ? new int[]{ 0 } :
                    bands == 3 ? new int[]{ 0, 1, 2 } :
                        new int[]{ 0, 1, 2, 3 }
            );

        final ByteInterleavedRaster rasterBuffer = new ByteInterleavedRaster(sm, new Point(0, 0));

        for ( int y = 0; y < imageHeight; y += stripHeight ) {
            if ( thread != null && thread.isCanceled() )
                return;
            stripHeight = Math.min( stripHeight, imageHeight - y );
            stripRect.setBounds( 0, y, imageWidth, stripHeight );

            final ByteInterleavedRaster raster = (ByteInterleavedRaster) rasterBuffer.createTranslatedChild(0, y);

            // Prefetch tiles, uses all CPUs
            if (image instanceof PlanarImage)
                ((PlanarImage) image).getTiles(((PlanarImage) image).getTileIndices(raster.getBounds()));

            image.copyData(raster);

            final DataBufferByte db = (DataBufferByte)raster.getDataBuffer();

            final int[] offsets = raster.getDataOffsets();
            int offset = offsets[0];
            for (int i = 1; i < offsets.length; i++)
                offset = Math.min(offset, offsets[i]);

            if ( bands == 4 /* CMYK */ ) {
                //
                // A long-standing Photoshop bug is that CMYK images are stored
                // inverted.  To be compatible with Photoshop, we have to
                // invert CMYK images too.
                //
                final byte[] data = db.getData();
                for ( int i = 0; i < data.length; ++i )
                    data[i] = (byte)~data[i];
            }

            final int lineStride = raster.getScanlineStride();
            final int written = writeScanLines( db.getData(), offset, stripHeight, lineStride );

            if ( written != stripHeight )
                throw new LCImageLibException(
                    "something is wrong: " + written + " != " + stripHeight
                );

            if ( indicator != null )
                indicator.incrementBy( stripHeight );
        }

        if ( indicator != null )
            indicator.setIndeterminate( true );
    }
View Full Code Here

        // The output image itself, directly allocated in the file cache
        CachedImage image = new CachedImage(layout, JAIContext.fileCache);

        final int maxTileX = image.getNumXTiles();

        ProgressIndicator indicator = null;
        if ( thread != null )
            indicator = thread.getProgressIndicator();
        if ( indicator != null )
            indicator.setMaximum( tf.tiles );

        int bandList[] = new int[tf.samplesPerPixel];
        for (int i = 0; i < tf.samplesPerPixel; i++)
            bandList[i] = i;

        Rectangle imageBounds = new Rectangle(0, 0, tf.imageWidth, tf.imageHeight);

        for ( int tile = 0; tile < tf.tiles; tile++ ) {
            if ( thread != null && thread.isCanceled() )
                throw new UserCanceledException();
            final int tileX = tf.tiled ? tile % maxTileX : 0;
            final int tileY = tf.tiled ? tile / maxTileX : tile;

            // The actual tile bounds, clipping on the image bounds
            Rectangle tileBounds = new Rectangle(tileX * tf.tiffTileWidth,
                                                 tileY * tf.tiffTileHeight,
                                                 tf.tiffTileWidth,
                                                 tf.tiffTileHeight).intersection(imageBounds);

            // the corresponding tile data
            int tileData = (tf.samplesPerPixel / tf.planes) * tileBounds.width * tileBounds.height * (tf.bitsPerSample == 8 ? 1 : 2);

            /* If the TIFF image is tiled and it doesn't have an alpha channel
               then we can read directly into the destination image buffer,
               don't allocate an intermediate raster */

            WritableRaster raster =
                    !tf.tiled || tf.hasAlpha
                    ? Raster.createWritableRaster(tf.tiffTsm, db, new Point(tileBounds.x, tileBounds.y))
                    : null;

            for ( int plane = 0; plane < tf.planes; plane++ ) {
                if ( tf.tiled ) {
                    if (!tf.hasAlpha)
                        raster = image.getWritableTile( tileX, tileY );

                    if ( tf.bitsPerSample == 8 ) {
                        final byte[] buffer =
                            ((DataBufferByte)raster.getDataBuffer()).getData(
                                plane );
                        int read = readTileByte( tile + plane * tf.tiles, buffer, 0, tileData );
                        if (read != tileData)
                            throw new LCImageLibException("Broken TIFF File");
                    } else {
                        final short[] buffer =
                            ((DataBufferUShort)raster.getDataBuffer()).getData(
                                plane );
                        int read = readTileShort( tile + plane * tf.tiles, buffer, 0, tileData );
                        if (read != tileData)
                            throw new LCImageLibException("Broken TIFF File");
                    }
                } else {
                    if ( tf.bitsPerSample == 8 ) {
                        final byte[] buffer =
                            ((DataBufferByte)db).getData( plane );
                        int read = readStripByte( tileY + plane * tf.tiles, buffer, 0, tileData );
                        if (read != tileData)
                            throw new LCImageLibException("Broken TIFF File");
                    } else {
                        final short[] buffer =
                            ((DataBufferUShort)db).getData( plane );
                        int read = readStripShort( tileY + plane * tf.tiles, buffer, 0, tileData );
                        if (read != tileData)
                            throw new LCImageLibException("Broken TIFF File");
                    }
                }
            }
            if (!tf.tiled || tf.hasAlpha)
                image.setData(raster.createChild(raster.getMinX(), raster.getMinY(),
                                                 raster.getWidth(), raster.getHeight(),
                                                 raster.getMinX(), raster.getMinY(),
                                                 bandList));
            if ( indicator != null )
                indicator.incrementBy( 1 );
        }
        if ( indicator != null )
            indicator.setIndeterminate( true );

        m_image = image;
    }
View Full Code Here

TOP

Related Classes of com.lightcrafts.utils.ProgressIndicator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.