Package javax.media.jai

Examples of javax.media.jai.LookupTableJAI


/* 252 */         table[value] = ImageUtil.clampRoundByte(binarySearch(x, yL, yH, a, b, value));
/*     */       }
/*     */
/*     */     }
/*     */
/* 258 */     this.lut = new LookupTableJAI(data);
/*     */   }
View Full Code Here


        if (cm instanceof ComponentColorModel) {
            return coverage;
        }
        final int dataType;
        final ColorSpace cs;
        final LookupTableJAI lookup;
        /*
         * If the color model is indexed. Converts to RGB or gray scale using a single "Lookup"
         * operation. Color space will be RGB or GRAY, and type will be DataBuffer.TYPE_BYTE.
         */
        if (cm instanceof IndexColorModel) {
            final IndexColorModel icm = (IndexColorModel) cm;
            final int mapSize = icm.getMapSize();
            final byte data[][];
            if (ColorUtilities.isGrayPalette(icm, false)) {
                final byte[] gray = new byte[mapSize];
                icm.getGreens(gray);
                if (icm.hasAlpha()) {
                    final byte[] alpha = new byte[mapSize];
                    icm.getAlphas(alpha);
                    data = new byte[][] { gray, alpha };
                } else {
                    data = new byte[][] { gray };
                }
                cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            } else {
                data = new byte[cm.getNumComponents()][mapSize];
                switch (data.length) {
                    default: // Should not occurs, but keep as a paranoiac check.
                    case 4:  icm.getAlphas(data[3]);
                    case 3:  icm.getBlues (data[2]);
                    case 2:  icm.getGreens(data[1]);
                    case 1:  icm.getReds  (data[0]);
                    case 0break;
                }
                cs = icm.getColorSpace();
            }
            dataType = DataBuffer.TYPE_BYTE;
            lookup = new LookupTableJAI(data);
        } else {
            lookup = null;
            cs = cm.getColorSpace();
            dataType = (cm instanceof DirectColorModel) ? DataBuffer.TYPE_BYTE :
                        image.getSampleModel().getTransferType();
View Full Code Here

         *          LookupTableFactory.create(...) to returns null.
         */
        if (transforms != null) try {
            final int sourceType = sourceModel.getDataType();
            final int targetType = targetModel.getDataType();
            LookupTableJAI table = LookupTableFactory.create(sourceType, targetType, transforms);
            if (table != null) {
                operation = "Lookup";
                param = param.add(table);
            }
        } catch (TransformException exception) {
View Full Code Here

          final byte[] lut = new byte[256];
          for (int i = 1; i < lut.length; i++)
            lut[i] = (byte) (scale * i + offset + 0.5d);

          //do the actual lookup
          final LookupTableJAI lookup = new LookupTableJAI(lut);
          final ParameterBlock pb = new ParameterBlock();
          pb.addSource(inputImage);
          pb.add(lookup);
          return JAI.create("lookup", pb, hints);         
        }
       
        ////
        //
        // General case, we use the rescale in order to stretch the values to highest and lowest dim
        //
        ////
        //get the correct dim for this data type
        final double maximum=ColorUtilities.getMaximum(dataType);
        final double minimum=ColorUtilities.getMinimum(dataType);
        if (extrema[1][0] == maximum && extrema[0][0] == minimum)
          return inputImage;
        //compute the scale factors
        final double delta = extrema[1][0] - extrema[0][0];
        final double scale = (maximum -minimum)/ delta;
        final double offset =  minimum - scale * extrema[0][0];

        //do the actual rescale
        final ParameterBlock pb = new ParameterBlock();
        pb.addSource(inputImage);
        pb.add(new double []{scale});
        pb.add(new double []{offset});
        return JAI.create("rescale", pb, hints);

      }
     
     
      // /////////////////////////////////////////////////////////////////////
      //
      // EXPONENTIAL Normalization
      //
      //
      //
      // /////////////////////////////////////////////////////////////////////     
      if (type.equalsIgnoreCase("EXPONENTIAL")) {

        if(dataType==DataBuffer.TYPE_BYTE){
          ////
          //
          // Optimisation for byte images
          //
          ////
          final byte lut[] = new byte[256];
          final double normalizationFactor=255.0;
          final double correctionFactor=255.0/(Math.E-1);
          for (int i = 1; i < lut.length; i++)
            lut[i] = (byte) (0.5f + correctionFactor * (Math.exp(i / normalizationFactor) - 1.0));
          return LookupDescriptor.create(inputImage,
              new LookupTableJAI(lut), hints);
        }
        ////
        //
        // General case, we use the piecewise1D transform
        //
        ////
        //
        // STEP 1 do the extrema
        //
        ////
        //step 1 do the extrema to get the statistics for this image
        final RenderedOp statistics = ExtremaDescriptor.create(inputImage,
            null, Integer.valueOf(1), Integer.valueOf(1), null,
            Integer.valueOf(1), null);
        final double[] minimum=(double[]) statistics.getProperty("minimum");
        final double[] maximum=(double[]) statistics.getProperty("maximum");
        final double normalizationFactor=maximum[0];
        final double correctionFactor=normalizationFactor/(Math.E-1);
       
        ////
        //
        // STEP 2 do the gamma correction by using generic piecewise
        //
        ////
        final DefaultPiecewiseTransform1DElement mainElement = DefaultPiecewiseTransform1DElement.create(
            "exponential-contrast-enhancement-transform", NumberRange.create(minimum[0],maximum[0]),
            new MathTransform1DAdapter() {

                  /*
                   * (non-Javadoc)
                   * @see org.opengis.referencing.operation.MathTransform1D#derivative(double)
                   */
                  public double derivative(double value)
                      throws TransformException {
                   
                    throw new UnsupportedOperationException(Errors.format(ErrorKeys.UNSUPPORTED_OPERATION_$1));
                  }
                  public boolean isIdentity() {
                    return false;
                  }
                  /*
                   * (non-Javadoc)
                   * @see org.opengis.referencing.operation.MathTransform1D#transform(double)
                   */
                  public double transform(double value)
                      throws TransformException {
                    value = correctionFactor*(Math.exp(value/normalizationFactor)-1);
                    return value;
                  }

            });
       
        final PiecewiseTransform1D<DefaultPiecewiseTransform1DElement> transform = new DefaultPiecewiseTransform1D<DefaultPiecewiseTransform1DElement> (
            new DefaultPiecewiseTransform1DElement[] {mainElement},0);

          final ParameterBlockJAI pbj = new ParameterBlockJAI(
              GenericPiecewise.OPERATION_NAME);
          pbj.addSource(inputImage);
          pbj.setParameter("Domain1D", transform);
          pbj.setParameter("bandIndex", Integer.valueOf(0));
          return JAI.create(
              GenericPiecewise.OPERATION_NAME, pbj);
      }     
      if (type.equalsIgnoreCase("LOGARITHMIC")) {
        // /////////////////////////////////////////////////////////////////////
        //
        // Logarithm Normalization
        //
        //
        //
        // /////////////////////////////////////////////////////////////////////
        if(dataType==DataBuffer.TYPE_BYTE){
          ////
          //
          // Optimisation for byte images m we use lookup
          //
          ////
          final byte lut[] = new byte[256];
          final double normalizationFactor=255.0;
          final double correctionFactor=100.0;
          for (int i = 1; i < lut.length; i++)
            lut[i] = (byte) (0.5f + normalizationFactor * Math.log((i * correctionFactor / normalizationFactor+ 1.0)));
          return LookupDescriptor.create(inputImage,
              new LookupTableJAI(lut), hints);       
        }
        ////
        //
        // General case
        //
        ////
        //define a specific piecewise for the logarithm

        ////
        //
        // STEP 1 do the extrema
        //
        ////
        //step 1 do the extrema to get the statistics for this image
        final RenderedOp statistics = ExtremaDescriptor.create(inputImage,
            null, Integer.valueOf(1), Integer.valueOf(1), null,
            Integer.valueOf(1), null);
        final double[] minimum=(double[]) statistics.getProperty("minimum");
        final double[] maximum=(double[]) statistics.getProperty("maximum");
        final double normalizationFactor=maximum[0];
        final double correctionFactor=100.0;
       
        ////
        //
        // STEP 2 do the gamma correction by using generic piecewise
        //
        ////
        final DefaultPiecewiseTransform1DElement mainElement = DefaultPiecewiseTransform1DElement.create(
            "logarithmic-contrast-enhancement-transform", NumberRange.create(minimum[0],maximum[0]),
            new MathTransform1DAdapter() {

                  /*
                   * (non-Javadoc)
                   * @see org.opengis.referencing.operation.MathTransform1D#derivative(double)
                   */
                  public double derivative(double value)
                      throws TransformException {
                   
                    throw new UnsupportedOperationException(Errors.format(ErrorKeys.UNSUPPORTED_OPERATION_$1));
                  }
                  public boolean isIdentity() {
                    return false;
                  }
                  /*
                   * (non-Javadoc)
                   * @see org.opengis.referencing.operation.MathTransform1D#transform(double)
                   */
                  public double transform(double value)
                      throws TransformException {
                    value =normalizationFactor*Math.log(1+(value*correctionFactor/normalizationFactor));
                    return value;
                  }

                 
            });
       
        final PiecewiseTransform1D<DefaultPiecewiseTransform1DElement>  transform = new DefaultPiecewiseTransform1D<DefaultPiecewiseTransform1DElement> (
            new DefaultPiecewiseTransform1DElement[] {mainElement},0);

          final ParameterBlockJAI pbj = new ParameterBlockJAI(
              GenericPiecewise.OPERATION_NAME);
          pbj.addSource(inputImage);
          pbj.setParameter("Domain1D", transform);
          pbj.setParameter("bandIndex", Integer.valueOf(0));
          return JAI.create(
              GenericPiecewise.OPERATION_NAME, pbj);
      }     
      if (type.equalsIgnoreCase("HISTOGRAM")) {
        // /////////////////////////////////////////////////////////////////////
        //
        // Histogram Equalization
        //
        // IT WORKS ONLY ON BYTE DATA TYPE!!!
        //
        // /////////////////////////////////////////////////////////////////////

        //convert the input image to 8 bit
        inputImage=new ImageWorker(inputImage).rescaleToBytes().getRenderedImage();
        // compute the histogram
        final RenderedOp hist = HistogramDescriptor.create(inputImage,
            null, Integer.valueOf(1), Integer.valueOf(1),
            new int[] { 256 }, new double[] { 0 },
            new double[] { 256 }, null);
        final Histogram h = (Histogram) hist.getProperty("histogram");

        // now compute the PDF and the CDF for the original image
        final byte[] cumulative = new byte[h.getNumBins(0)];

        // sum of bins (we might have excluded 0 hence we cannot really
        // optimise)
        float totalBinSum = 0;
        for (int i = 0; i < cumulative.length; i++) {
          totalBinSum += h.getBinSize(0, i);
        }

        // this is the scale factor for the histogram equalization
        // process
        final float scale = (float) (h.getHighValue(0) - 1 - h.getLowValue(0))/ totalBinSum;
        float sum = 0;
        for (int i = 1; i < cumulative.length; i++) {
          sum += h.getBinSize(0, i - 1);
          cumulative[i] = (byte) ((sum * scale + h.getLowValue(0)) + .5F);
        }

        final LookupTableJAI lookup = new LookupTableJAI(cumulative);
        final ParameterBlock pb = new ParameterBlock();
        pb.addSource(hist);
        pb.add(lookup);
        return JAI.create("lookup", pb, hints);
      }
View Full Code Here

        final byte[] lut = new byte[256];
        for (int i = 1; i < lut.length; i++)
          lut[i] = (byte) (255.0 * Math.pow(i / 255.0, gammaValue) + 0.5d);

        // apply the operation now
        final LookupTableJAI lookup = new LookupTableJAI(lut);
        final ParameterBlock pb = new ParameterBlock();
        pb.addSource(inputImage);
        pb.add(lookup);
        result = JAI.create("lookup", pb, hints);
      }
View Full Code Here

            /*
             * Checks if a table is already available in the cache. Since tables may be 64 kb big,
             * sharing tables may save a significant amount of memory if there is many images.
             */
            final LookupTableFactory key = new LookupTableFactory(sourceType, targetType, transforms);
            LookupTableJAI table = pool.get(key);
            if (table != null) {
                return table;
            }
            /*
             * Computes the table's size according the source datatype. For datatype 'short' (signed
             * or unsigned), we will create the table only if the target datatype is 'byte' in order
             * to avoid to use too much memory for the table. The memory consumed for a table from
             * source datatype 'short' to target datatype 'byte' is 64 ko.
             */
            final int length;
            final int offset;
            switch (sourceType) {
                default: {
                    return null;
                }
                case DataBuffer.TYPE_BYTE: {
                    length = 0x100;
                    offset = 0;
                    break;
                }
                case DataBuffer.TYPE_SHORT:
                case DataBuffer.TYPE_USHORT: {
                    if (targetType != DataBuffer.TYPE_BYTE) {
                        // Avoid to use too much memory for the table.
                        return null;
                    }
                    length = 0x10000;
                    offset = (sourceType == DataBuffer.TYPE_SHORT) ? Short.MIN_VALUE : 0;
                    break;
                }
            }
            /*
             * Builds the table according the target datatype.
             */
            switch (targetType) {
                default: {
                    return null;
                }
                case DataBuffer.TYPE_DOUBLE: {
                    final double[][]  data = new double[nbands][];
                    final double[]  buffer = new double[length];
                    for (int i=length; --i>=0;) {
                        buffer[i] = i;
                    }
                    for (int i=nbands; --i>=0;) {
                        final double[] array = (i==0) ? buffer : buffer.clone();
                        transforms[i].transform(array, 0, array, 0, array.length);
                        data[i] = array;
                    }
                    table = new LookupTableJAI(data, offset);
                    break;
                }
                case DataBuffer.TYPE_FLOAT: {
                    final float[][]  data = new float[nbands][];
                    final float[]  buffer = new float[length];
                    for (int i=length; --i>=0;) {
                        buffer[i] = i;
                    }
                    for (int i=transforms.length; --i>=0;) {
                        final float[] array = (i == 0) ? buffer : buffer.clone();
                        transforms[i].transform(array, 0, array, 0, length);
                        data[i] = array;
                    }
                    table = new LookupTableJAI(data, offset);
                    break;
                }
                case DataBuffer.TYPE_INT: {
                    final int[][] data = new int[nbands][];
                    for (int i=nbands; --i>=0;) {
                        final MathTransform1D tr = transforms[i];
                        final int[] array = new int[length];
                        for (int j=length; --j>=0;) {
                            array[j] = (int) min(max(round(tr.transform(j+offset)),
                                    Integer.MIN_VALUE), Integer.MAX_VALUE);
                        }
                        data[i] = array;
                    }
                    table = new LookupTableJAI(data, offset);
                    break;
                }
                case DataBuffer.TYPE_SHORT:
                case DataBuffer.TYPE_USHORT: {
                    final int minimum, maximum;
                    if (targetType == DataBuffer.TYPE_SHORT) {
                        minimum = Short.MIN_VALUE;
                        maximum = Short.MAX_VALUE;
                    } else {
                        minimum = 0;
                        maximum = 0xFFFF;
                    }
                    final short[][] data = new short[nbands][];
                    for (int i=nbands; --i>=0;) {
                        final MathTransform1D tr = transforms[i];
                        final short[] array = new short[length];
                        for (int j=length; --j>=0;) {
                            array[j] = (short) min(max(round(tr.transform(j+offset)), minimum), maximum);
                        }
                        data[i] = array;
                    }
                    table = new LookupTableJAI(data, offset, minimum!=0);
                    break;
                }
                case DataBuffer.TYPE_BYTE: {
                    final byte[][] data = new byte[nbands][];
                    for (int i=nbands; --i>=0;) {
                        final MathTransform1D tr = transforms[i];
                        final byte[] array = new byte[length];
                        for (int j=length; --j>=0;) {
                            array[j] = (byte) min(max(round(tr.transform(j+offset)), 0), 0xFF);
                        }
                        data[i] = array;
                    }
                    table = new LookupTableJAI(data, offset);
                    break;
                }
            }
            pool.put(key, table);
            return table;
View Full Code Here

            // layout.setColorModel(temp.getColorModel());
            // hints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT,layout));

            // error diffusion
            final KernelJAI ditherMask = KernelJAI.ERROR_FILTER_FLOYD_STEINBERG;
            final LookupTableJAI colorMap = ColorCube.BYTE_496;
            // (LookupTableJAI) temp.getProperty("JAI.LookupTable");
            image = ErrorDiffusionDescriptor.create(image, colorMap, ditherMask, hints);
        } else {
            // ordered dither
            final KernelJAI[] ditherMask = KernelJAI.DITHER_MASK_443;
View Full Code Here

            final int newSize = Math.max(mapSize, suggestedTransparent);
            final int newPixelSize = ColorUtilities.getBitCount(newSize);
            if (newPixelSize > 16)
                throw new IllegalArgumentException(
                        "Unable to create index color model with more than 65536 elements");
            final LookupTableJAI lookupTable;
            if (newPixelSize <= 8) {
                final byte[] table = new byte[mapSize];
                for (int i = 0; i < mapSize; i++) {
                    table[i] = (byte) ((oldCM.getAlpha(i) == 0) ? suggestedTransparent : i);
                }
                lookupTable = new LookupTableJAI(table);
            } else {
                final short[] table = new short[mapSize];
                for (int i = 0; i < mapSize; i++) {
                    table[i] = (short) ((oldCM.getAlpha(i) == 0) ? suggestedTransparent : i);
                }
                lookupTable = new LookupTableJAI(table, true);
            }
            /*
             * Now we need to perform the look up transformation. First of all we create the new color model with a bitmask transparency using the
             * transparency index specified to this method. Then we perform the lookup operation in order to prepare for the gif image.
             */
 
View Full Code Here

            final boolean alpha = icm.hasAlpha();
            /*
             * If the image is grayscale, retain only the needed bands.
             */
            final int numDestinationBands = gray ? (alpha ? 2 : 1) : (alpha ? 4 : 3);
            LookupTableJAI lut = null;

            switch (datatype) {
            case DataBuffer.TYPE_BYTE: {
                final byte data[][] = new byte[numDestinationBands][icm.getMapSize()];
                icm.getReds(data[0]);
                if (numDestinationBands >= 2)
                    // remember to optimize for grayscale images
                    if (!gray)
                        icm.getGreens(data[1]);
                    else
                        icm.getAlphas(data[1]);
                if (numDestinationBands >= 3)
                    icm.getBlues(data[2]);
                if (numDestinationBands == 4) {
                    icm.getAlphas(data[3]);
                }
                lut = new LookupTableJAI(data);

            }
                break;

            case DataBuffer.TYPE_USHORT: {
                final int mapSize = icm.getMapSize();
                final short data[][] = new short[numDestinationBands][mapSize];
                for (int i = 0; i < mapSize; i++) {
                    data[0][i] = (short) icm.getRed(i);
                    if (numDestinationBands >= 2)
                        // remember to optimize for grayscale images
                        if (!gray)
                            data[1][i] = (short) icm.getGreen(i);
                        else
                            data[1][i] = (short) icm.getAlpha(i);
                    if (numDestinationBands >= 3)
                        data[2][i] = (short) icm.getBlue(i);
                    if (numDestinationBands == 4) {
                        data[3][i] = (short) icm.getAlpha(i);
                    }
                }
                lut = new LookupTableJAI(data, datatype == DataBuffer.TYPE_USHORT);

            }
                break;

            default:
View Full Code Here

     */
    public final ImageWorker binarize(final int value0, final int value1) {
        tileCacheEnabled(false);
        binarize();
        tileCacheEnabled(true);
        final LookupTableJAI table;
        final int min = Math.min(value0, value1);
        if (min >= 0) {
            final int max = Math.max(value0, value1);
            if (max < 256) {
                table = new LookupTableJAI(new byte[] { (byte) value0, (byte) value1 });
            } else if (max < 65536) {
                table = new LookupTableJAI(new short[] { (short) value0, (short) value1 }, true);
            } else {
                table = new LookupTableJAI(new int[] { value0, value1 });
            }
        } else {
            table = new LookupTableJAI(new int[] { value0, value1 });
        }
        image = LookupDescriptor.create(image, table, getRenderingHints());
        invalidateStatistics();
        return this;
    }
View Full Code Here

TOP

Related Classes of javax.media.jai.LookupTableJAI

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.