All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.parser.image.MaskUtils Maven / Gradle / Ivy

There is a newer version: 20151002
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2015 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
     This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 *
 * ---------------
 * MaskUtils.java
 * ---------------
 */
package org.jpedal.parser.image;

import org.jpedal.exception.PdfException;
import org.jpedal.io.ObjectStore;
import org.jpedal.io.PdfObjectReader;
import org.jpedal.objects.GraphicsState;
import org.jpedal.objects.raw.PdfDictionary;
import org.jpedal.objects.raw.PdfObject;
import org.jpedal.parser.ParserOptions;
import org.jpedal.parser.PdfStreamDecoder;
import org.jpedal.parser.ValueTypes;
import org.jpedal.render.*;
import org.jpedal.utils.LogWriter;

import java.awt.*;
import java.awt.image.BufferedImage;

public class MaskUtils {


    /**
     *
     * @param XObject
     * @param name
     * @param newSMask
     * @param gs
     * @param current
     * @param currentPdfFile
     * @param parserOptions
     * @param formLevel
     * @param multiplyer
     * @param useTransparancy
     * @throws PdfException
     */
    public static void createMaskForm(final PdfObject XObject, final String name, final PdfObject newSMask, final GraphicsState gs,
                                      final DynamicVectorRenderer current, final PdfObjectReader currentPdfFile,
                                      final ParserOptions parserOptions, final int formLevel, final float multiplyer, final boolean useTransparancy) {

        final float[] BBox= XObject.getFloatArray(PdfDictionary.BBox);

        //System.out.println("x,y="+gs.CTM[2][0]+" "+gs.CTM[2][1]+" "+BBox[0]+" "+BBox[1]+" "+BBox[2]+" "+BBox[3]+" "+newSMask);
        
        /**get form as an image*/
        final int fx=(int)BBox[0];
        final int fw=(int)BBox[2];
        final int fy=(int)BBox[1];
        final int fh=(int)BBox[3];
        
        final int iw,ih;
        final float scaling=4f;

        BufferedImage image = PDFObjectToImage.getImageFromPdfObject(XObject, fx, fw, fy, fh, currentPdfFile, parserOptions, formLevel, multiplyer,(newSMask==null && useTransparancy),scaling); 

        if(newSMask!=null){ //apply SMask to image
            
            final BufferedImage smaskImage = PDFObjectToImage.getImageFromPdfObject(newSMask, fx, fw, fy, fh, currentPdfFile, parserOptions, formLevel, multiplyer,false,scaling);

            image= SMask.applySmask(image, smaskImage,true);

            if(smaskImage!=null){
                smaskImage.flush();
            }
        }
        
        iw=image.getWidth()/4;
        ih=image.getHeight()/4;

        final GraphicsState gs1; //add in gs

        boolean isChanged=false;
        if(newSMask==null && gs.getAlphaMax(GraphicsState.FILL)<1f){

            isChanged=true;

            gs1 =new GraphicsState(); //add in gs
            gs1.setMaxAlpha(GraphicsState.FILL, gs.getAlphaMax(GraphicsState.FILL));
            gs1.setMaxAlpha(GraphicsState.STROKE, gs.getAlphaMax(GraphicsState.STROKE));

            current.setGraphicsState(GraphicsState.STROKE, gs.getAlpha(GraphicsState.STROKE), PdfDictionary.Normal);
            current.setGraphicsState(GraphicsState.FILL, gs.getAlpha(GraphicsState.FILL), PdfDictionary.Normal);

        }else {
            if(formLevel==1){
                gs1 = new GraphicsState(gs); //add in gs
            }else{
                gs1 = new GraphicsState(); //add in gs
            }
        }

        gs1.CTM=new float[][]{{iw,0,1},{0,ih,1},{0,0,0}};

        //different formula needed if flattening forms
        if(parserOptions.isFlattenedForm()){
            gs1.x= parserOptions.getflattenX();
            gs1.y= parserOptions.getflattenY();
        }else{

            gs1.x = fx;
            
            if(fy fh){
            hh= fy - fh;
         //   diff=fh;
        }

        //as we make it image, make it bigger to retain quality
        final int scaling=4;

        //get bit underneath and merge in
        image=new BufferedImage(scaling*fw,scaling*hh, BufferedImage.TYPE_INT_ARGB);

        final Graphics2D formG2=image.createGraphics();

        formG2.setColor(Color.WHITE);
        formG2.fillRect(0, 0, scaling*fw, scaling*hh);
        formG2.translate(0, scaling*hh);
        formG2.scale(1, -1);
        formG2.scale(scaling, scaling);

        //current.paint(formG2,null,null,null,false,true);

        glyphDisplay.setG2(formG2);
        glyphDisplay.paint(null,null,null);

        localStore.flush();

        return image;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy