Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 
  */
 package org.apache.batik.svggen;
 
 
Utility class that converts a LookupOp object into an SVG filter descriptor. The SVG filter corresponding to a LookupOp is an feComponentTransfer, with a type set to 'table', the tableValues set to the content of the lookup table.

Author(s):
Vincent Hardy
Version:
$Id: SVGLookupOp.java 501495 2007-01-30 18:00:36Z dvholten $
See also:
SVGBufferedImageOp
 
 public class SVGLookupOp extends AbstractSVGFilterConverter {

    
Gamma for linear to sRGB convertion
 
     private static final double GAMMA = 1.0/2.4;

    
Lookup table for linear to sRGB value forward and backward mapping
 
     private static final int[] linearToSRGBLut = new int[256];
     private static final int[] sRGBToLinear = new int[256];
 
     static {
         for(int i=0; i<256; i++) {
             // linear to sRGB
             float value = i/255f;
             if (value <= 0.0031308) {
                 value *= 12.92f;
             } else {
                 value = 1.055f * ((float) Math.pow(value)) - 0.055f;
             }
             [i] = Math.round(value*255);
 
             // sRGB to linear
             value = i/255f;
             if(value <= 0.04045){
                 value /= 12.92f;
             } else {
                 value = (float)Math.pow((value + 0.055f)/1.055f, 1/);
             }
 
             [i] = Math.round(value*255);
         }
     }

    

Parameters:
generatorContext used to build Elements
 
     public SVGLookupOp(SVGGeneratorContext generatorContext) {
         super(generatorContext);
     }

    
Converts a Java 2D API BufferedImageOp into a set of attribute/value pairs and related definitions

Parameters:
filter BufferedImageOp filter to be converted
filterRect Rectangle, in device space, that defines the area to which filtering applies. May be null, meaning that the area is undefined.
Returns:
descriptor of the attributes required to represent the input filter
See also:
SVGFilterDescriptor
 
     public SVGFilterDescriptor toSVG(BufferedImageOp filter,
                                      Rectangle filterRect) {
        if (filter instanceof LookupOp)
            return toSVG((LookupOp)filter);
        else
            return null;
    }

    

Parameters:
lookupOp the LookupOp to be converted
Returns:
a description of the SVG filter corresponding to lookupOp. The definition of the feComponentTransfer filter in put in feComponentTransferDefSet
    public SVGFilterDescriptor toSVG(LookupOp lookupOp) {
        // Reuse definition if lookupOp has already been converted
        SVGFilterDescriptor filterDesc =
            (SVGFilterDescriptor).get(lookupOp);
        Document domFactory = .;
        if (filterDesc == null) {
            //
            // First time filter is converted: create its corresponding
            // SVG filter
            //
            Element filterDef = domFactory.createElementNS(,
                                                           );
            Element feComponentTransferDef =
                domFactory.createElementNS(,
                                           );
            // Append transfer function for each component, setting
            // the attributes corresponding to the scale and offset.
            // Because we are using a LookupOp as a BufferedImageOp,
            // the number of lookup table must be:
            // + 1, in which case the same lookup is applied to the
            //   Red, Green and Blue components,
            // + 3, in which case the lookup tables apply to the
            //   Red, Green and Blue components
            // + 4, in which case the lookup tables apply to the
            //   Red, Green, Blue and Alpha components
            String[] lookupTables = convertLookupTables(lookupOp);
            Element feFuncR = domFactory.createElementNS(,
                                                         );
            Element feFuncG = domFactory.createElementNS(,
                                                         );
            Element feFuncB = domFactory.createElementNS(,
                                                         );
            Element feFuncA = null;
            String type = ;
            if(lookupTables.length == 1){
                feFuncR.setAttributeNS(nulltype);
                feFuncG.setAttributeNS(nulltype);
                feFuncB.setAttributeNS(nulltype);
                feFuncR.setAttributeNS(null,
                                       lookupTables[0]);
                feFuncG.setAttributeNS(null,
                                       lookupTables[0]);
                feFuncB.setAttributeNS(null,
                                       lookupTables[0]);
            }
            else if(lookupTables.length >= 3){
                feFuncR.setAttributeNS(nulltype);
                feFuncG.setAttributeNS(nulltype);
                feFuncB.setAttributeNS(nulltype);
                feFuncR.setAttributeNS(null,
                                       lookupTables[0]);
                feFuncG.setAttributeNS(null,
                                       lookupTables[1]);
                feFuncB.setAttributeNS(null,
                                       lookupTables[2]);
                if(lookupTables.length == 4){
                    feFuncA = domFactory.createElementNS(,
                                                         );
                    feFuncA.setAttributeNS(nulltype);
                    feFuncA.setAttributeNS(null,
                                           lookupTables[3]);
                }
            }
            feComponentTransferDef.appendChild(feFuncR);
            feComponentTransferDef.appendChild(feFuncG);
            feComponentTransferDef.appendChild(feFuncB);
            if(feFuncA != null)
                feComponentTransferDef.appendChild(feFuncA);
            filterDef.appendChild(feComponentTransferDef);
            filterDef.
                setAttributeNS(null,
                               ..
                               generateID());
            //
            // Create a filter descriptor
            //
            // Process filter attribute
//            StringBuffer filterAttrBuf = new StringBuffer(URL_PREFIX);
//            filterAttrBuf.append(SIGN_POUND);
//            filterAttrBuf.append(filterDef.getAttributeNS(null, SVG_ID_ATTRIBUTE));
//            filterAttrBuf.append(URL_SUFFIX);
            String filterAttrBuf =  +  + filterDef.getAttributeNS(null) + ;
            filterDesc = new SVGFilterDescriptor(filterAttrBuffilterDef);
            .add(filterDef);
            .put(lookupOpfilterDesc);
        }
        return filterDesc;
    }

    
Converts the filter's LookupTable into an array of corresponding SVG table strings
    private String[] convertLookupTables(LookupOp lookupOp){
        LookupTable lookupTable = lookupOp.getTable();
        int nComponents = lookupTable.getNumComponents();
        if((nComponents != 1) && (nComponents != 3) && (nComponents != 4))
        StringBuffer[] lookupTableBuf = new StringBuffer[nComponents];
        for(int i=0; i<nComponentsi++)
            lookupTableBuf[i] = new StringBuffer();
        if(!(lookupTable instanceof ByteLookupTable)){
            int[] src = new int[nComponents];
            int[] destnew int[nComponents];
            int offset = lookupTable.getOffset();
            // Offsets are used for constrained sources. Therefore,
            // the lookup values should never be used under offset.
            // There is no SVG equivalent for this behavior.
            // These values are mapped to identity.
            for(int i=0; i<offseti++){
                // Fill in string buffers
                for(int j=0; j<nComponentsj++){
                    // lookupTableBuf[j].append(Integer.toString(i));
                    lookupTableBuf[j].append(doubleString(i/255.0)).append();
                }
            }
            for(int i=offseti<=255; i++){
                // Fill in source array
                Arrays.fillsrci );
                // Get destination values
                lookupTable.lookupPixel(srcdest);
                // Fill in string buffers
                for(int j=0; j<nComponentsj++){
                    lookupTableBuf[j].append(doubleStringdest[j]/255.0) ).append();
                }
            }
        }
        else{
            byte[] src = new byte[nComponents];
            byte[] dest = new byte[nComponents];
            int offset = lookupTable.getOffset();
            // Offsets are used for constrained sources. Therefore,
            // the lookup values should never be used under offset.
            // There is no SVG equivalent for this behavior.
            // These values are mapped to identity.
            for(int i=0; i<offseti++){
                // Fill in string buffers
                for(int j=0; j<nComponentsj++){
                    // lookupTableBuf[j].append(Integer.toString(i));
                    lookupTableBuf[j].appenddoubleString(i/255.0) ).append();
                }
            }
            for(int i=0; i<=255; i++){
                // Fill in source array
                Arrays.fillsrc, (byte)(0xff & i) );
                // Get destination values
                ((ByteLookupTable)lookupTable).lookupPixel(srcdest);
                // Fill in string buffers
                for(int j=0; j<nComponentsj++){
                    lookupTableBuf[j].appenddoubleString( (0xff & dest[j])/255.0) ).append();
                }
            }
        }
        String[] lookupTables = new String[nComponents];
        for(int i=0; i<nComponentsi++)
            lookupTables[i] = lookupTableBuf[i].toString().trim();
        /*for(int i=0; i<lookupTables.length; i++){
            System.out.println(lookupTables[i]);
            }*/
        return lookupTables;
    }
New to GrepCode? Check out our FAQ X