Search in sources :

Example 1 with Kdu_coords

use of kdu_jni.Kdu_coords in project imageio-ext by geosolutions-it.

the class JP2KKakaduImageReader method setInput.

public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
    // //
    // 
    // Reset the reader prior to do anything with it
    // 
    // //
    reset();
    // //
    if (input == null)
        throw new NullPointerException("The provided input is null!");
    // Checking if the provided input is a File
    if (input instanceof File) {
        inputFile = (File) input;
    // Checking if the provided input is a FileImageInputStreamExt
    } else if (input instanceof FileImageInputStreamExt) {
        inputFile = ((FileImageInputStreamExt) input).getFile();
    // Checking if the provided input is a URL
    } else if (input instanceof URL) {
        final URL tempURL = (URL) input;
        if (tempURL.getProtocol().equalsIgnoreCase("file")) {
            inputFile = Utilities.urlToFile(tempURL);
        }
    } else if (input instanceof ImageInputStream) {
        try {
            inputFile = File.createTempFile("buffer", ".j2c");
            FileOutputStream fos = new FileOutputStream(inputFile);
            final byte[] buff = new byte[TEMP_BUFFER_SIZE];
            int bytesRead = 0;
            while ((bytesRead = ((ImageInputStream) input).read(buff)) != -1) fos.write(buff, 0, bytesRead);
            fos.close();
            input = inputFile;
            deleteInputFile = true;
        } catch (IOException ioe) {
            throw new RuntimeException("Unable to create a temp file", ioe);
        }
    }
    if (this.inputFile == null)
        throw new IllegalArgumentException("Invalid source provided.");
    fileName = inputFile.getAbsolutePath();
    // //
    // 
    // Open it up
    // 
    // //
    // Must be disposed last
    Kdu_simple_file_source rawSource = null;
    // Dispose
    final Jp2_family_src familySource = new Jp2_family_src();
    // last
    // Dispose in the
    final Jpx_source wrappedSource = new Jpx_source();
    // middle
    Kdu_codestream codestream = new Kdu_codestream();
    try {
        // Open input file as raw codestream or a JP2/JPX file
        familySource.Open(fileName);
        final int success = wrappedSource.Open(familySource, true);
        if (success < 0) {
            // //
            // 
            // Must open as raw file
            // 
            // //
            familySource.Close();
            wrappedSource.Close();
            rawSource = new Kdu_simple_file_source(fileName);
            if (rawSource != null) {
                if (fileName != null) {
                    FileInputStream fis = new FileInputStream(new File(fileName));
                    byte[] jp2SocMarker = new byte[2];
                    fis.read(jp2SocMarker);
                    if (jp2SocMarker[0] != (byte) 0xFF || jp2SocMarker[1] != (byte) 0x4F)
                        throw new IllegalArgumentException("Not a JP2K source.");
                    fis.close();
                }
                isRawSource = true;
                if (LOGGER.isLoggable(Level.FINE))
                    LOGGER.fine("Detected raw source");
                numImages = 1;
            }
        } else {
            // we have a valid jp2/jpx file
            isRawSource = false;
            // //
            // 
            // get the number of codestreams in this jpeg2000 file
            // 
            // //
            final int[] count = new int[1];
            if (wrappedSource.Count_codestreams(count))
                numImages = count[0];
            else
                numImages = 0;
        }
        if (!isRawSource)
            fileWalker = new JP2KFileWalker(this.fileName);
        for (int cs = 0; cs < numImages; cs++) {
            if (isRawSource) {
                codestream.Create(rawSource);
            } else {
                final Jpx_codestream_source stream = wrappedSource.Access_codestream(cs);
                final Jpx_input_box inputbox = stream.Open_stream();
                codestream.Create(inputbox);
            }
            final JP2KCodestreamProperties codestreamP = new JP2KCodestreamProperties();
            // //
            // 
            // Initializing size-related properties (Image dims, Tiles)
            // 
            // //
            final Kdu_dims imageDims = new Kdu_dims();
            codestream.Access_siz().Finalize_all();
            codestream.Get_dims(-1, imageDims, false);
            final Kdu_dims tileSize = new Kdu_dims();
            codestream.Get_tile_dims(new Kdu_coords(0, 0), -1, tileSize);
            int tileWidth = tileSize.Access_size().Get_x();
            int tileHeight = tileSize.Access_size().Get_y();
            // maximum integer
            if ((float) tileWidth * tileHeight >= Integer.MAX_VALUE) {
                // TODO: Customize these settings
                tileHeight = 1024;
                tileWidth = 1024;
            }
            codestreamP.setTileWidth(tileWidth);
            codestreamP.setTileHeight(tileHeight);
            codestreamP.setWidth(imageDims.Access_size().Get_x());
            codestreamP.setHeight(imageDims.Access_size().Get_y());
            final int nComponents = codestream.Get_num_components();
            int maxBitDepth = -1;
            int[] componentIndexes = new int[nComponents];
            int[] bitsPerComponent = new int[nComponents];
            boolean isSigned = false;
            for (int i = 0; i < nComponents; i++) {
                // TODO: FIX THIS
                bitsPerComponent[i] = codestream.Get_bit_depth(i);
                if (maxBitDepth < bitsPerComponent[i]) {
                    maxBitDepth = bitsPerComponent[i];
                }
                isSigned |= codestream.Get_signed(i);
                componentIndexes[i] = i;
            }
            codestreamP.setNumComponents(nComponents);
            codestreamP.setBitsPerComponent(bitsPerComponent);
            codestreamP.setComponentIndexes(componentIndexes);
            codestreamP.setMaxBitDepth(maxBitDepth);
            codestreamP.setSigned(isSigned);
            // Initializing Resolution levels and Quality Layers
            final int sourceDWTLevels = codestream.Get_min_dwt_levels();
            codestreamP.setSourceDWTLevels(sourceDWTLevels);
            codestreamP.setMaxSupportedSubSamplingFactor(1 << sourceDWTLevels);
            Kdu_coords tileCoords = new Kdu_coords();
            tileCoords.Set_x(0);
            tileCoords.Set_y(0);
            codestream.Open_tile(tileCoords);
            codestreamP.setMaxAvailableQualityLayers(codestream.Get_max_tile_layers());
            initializeSampleModelAndColorModel(codestreamP);
            codestream.Destroy();
            multipleCodestreams.add(codestreamP);
            if (isRawSource)
                break;
        }
    } catch (KduException e) {
        throw new RuntimeException("Error caused by a Kakadu exception during creation of key objects!", e);
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Exception occurred during kakadu reader initialization", e);
    } catch (IOException e) {
        throw new RuntimeException("Exception occurred during kakadu reader initialization", e);
    } finally {
        if (!isRawSource && wrappedSource != null) {
            try {
                if (wrappedSource.Exists())
                    wrappedSource.Close();
            } catch (Throwable e) {
            // yeah I am eating this
            }
            try {
                wrappedSource.Native_destroy();
            } catch (Throwable e) {
            // yeah I am eating this
            }
            if (familySource != null) {
                try {
                    if (familySource.Exists())
                        familySource.Close();
                } catch (Throwable e) {
                // yeah I am eating this
                }
                try {
                    familySource.Native_destroy();
                } catch (Throwable e) {
                // yeah I am eating this
                }
            }
        } else if (isRawSource && rawSource != null) {
            try {
                if (wrappedSource.Exists())
                    wrappedSource.Close();
            } catch (Throwable e) {
            // yeah I am eating this
            }
        }
    }
    // //
    // 
    // Setting input for superclass
    // 
    // //
    super.setInput(input, seekForwardOnly, ignoreMetadata);
}
Also used : FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) Jpx_source(kdu_jni.Jpx_source) Kdu_dims(kdu_jni.Kdu_dims) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) Kdu_codestream(kdu_jni.Kdu_codestream) KduException(kdu_jni.KduException) Kdu_simple_file_source(kdu_jni.Kdu_simple_file_source) Jpx_codestream_source(kdu_jni.Jpx_codestream_source) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) Kdu_coords(kdu_jni.Kdu_coords) Jpx_input_box(kdu_jni.Jpx_input_box) Jp2_family_src(kdu_jni.Jp2_family_src) File(java.io.File)

Aggregations

FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 Jp2_family_src (kdu_jni.Jp2_family_src)1 Jpx_codestream_source (kdu_jni.Jpx_codestream_source)1 Jpx_input_box (kdu_jni.Jpx_input_box)1 Jpx_source (kdu_jni.Jpx_source)1 KduException (kdu_jni.KduException)1 Kdu_codestream (kdu_jni.Kdu_codestream)1 Kdu_coords (kdu_jni.Kdu_coords)1 Kdu_dims (kdu_jni.Kdu_dims)1 Kdu_simple_file_source (kdu_jni.Kdu_simple_file_source)1