org.jpedal.parser.image.MaskDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of OpenViewerFX Show documentation
Show all versions of OpenViewerFX Show documentation
An Open Source JavaFX PDF Viewer
/*
* ===========================================
* 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
*
* ---------------
* MaskDecoder.java
* ---------------
*/
package org.jpedal.parser.image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import org.jpedal.color.ColorSpaces;
import org.jpedal.color.GenericColorSpace;
import org.jpedal.io.ColorSpaceConvertor;
import org.jpedal.objects.GraphicsState;
import org.jpedal.objects.raw.PdfDictionary;
import org.jpedal.objects.raw.PdfObject;
import org.jpedal.parser.image.data.ImageData;
import org.jpedal.render.DynamicVectorRenderer;
/**
*
* @author markee
*/
public class MaskDecoder {
/**
* apply the Mask to image data directly as a component on argb
*
* @param imageData
* @param decodeColorData
* @param newSMask
* @return
*/
static byte[] applyMask(final ImageData imageData,final GenericColorSpace decodeColorData, final PdfObject newMask, final PdfObject XObject, byte[] maskDataSream) {
int[] maskArray=newMask.getIntArray(PdfDictionary.Mask);
if(maskArray!=null){
maskArray=convertToRGB(maskArray,decodeColorData);
}
byte[] objectData=imageData.getObjectData();
/*
* Image data
*/
int w=imageData.getWidth();
int h=imageData.getHeight();
int d=imageData.getDepth();
objectData = MaskDataDecoder.convertData(decodeColorData, objectData, w, h, imageData, d, 1, null);
XObject.setIntNumber(PdfDictionary.BitsPerComponent, 8);
if(maskArray!=null){
objectData=applyMaskArray(w, h, objectData,maskArray);
}else{
objectData=applyMaskStream(maskDataSream,imageData,decodeColorData, newMask, XObject);
}
// img= ColorSpaceConvertor.createARGBImage( XObject.getInt(PdfDictionary.Width), XObject.getInt(PdfDictionary.Height), objectData);
//
// try{
// ImageIO.write(img, "PNG", new java.io.File("/Users/markee/Desktop/mixed.png"));
// }catch(Exception e){}
return objectData;
}
private static byte[] applyMaskArray(final int w, final int h, final byte[] objectData, final int[] maskArray) {
int pixels=w*h*4;
int rgbPtr=0;
byte[] combinedData=new byte[w*h*4];
final int rawDataSize=objectData.length;
float[] diff=new float[3];
if(maskArray!=null){
for(int a=0;a<3;a++){
diff[a]=maskArray[1]-maskArray[0];
if(diff[a]>1f){
diff[a] /= 255f;
}
}
}
try{
for(int i=0;i0){
combinedData[i+comp]=(byte) (objectData[rgbPtr]*diff[comp]);
}else{
combinedData[i+comp]=objectData[rgbPtr];
}
}
rgbPtr++;
}
//opacity
combinedData[i+3]=(byte)255;
}
}
}catch(Exception e){
e.printStackTrace();
}
return combinedData;
}
/**
* apply the Mask streamto image data directly as a component on argb
*
* @param imageData
* @param decodeColorData
* @param newSMask
* @return
*/
static byte[] applyMaskStream(byte[] maskData, final ImageData imageData,final GenericColorSpace decodeColorData, final PdfObject newMask, final PdfObject XObject) {
byte[] objectData=imageData.getObjectData();
/*
* Image data
*/
int w=imageData.getWidth();
int h=imageData.getHeight();
//int d=imageData.getDepth();
/*
* mask data (ASSUME single component at moment)
*/
final int maskW=newMask.getInt(PdfDictionary.Width);
final int maskH=newMask.getInt(PdfDictionary.Height);
final int maskD=newMask.getInt(PdfDictionary.BitsPerComponent);
//needs to be 'normalised to 8 bit'
if(maskD!=8){
maskData=ColorSpaceConvertor.normaliseTo8Bit(maskD, maskW, maskH, maskData);
}
final float[] maskDecodeArray=newMask.getFloatArray(PdfDictionary.Decode);
if(maskDecodeArray!=null){
float diff=maskDecodeArray[1]-maskDecodeArray[0];
if(diff==-1){
for(int i=0;i=20 || imageData.getMode()!=ImageCommands.ID){ //not worth it for inline image
hasObjectBehind = current.hasObjectsBehind(gs.CTM);
}
//remove empty images in some files
boolean isBlank=false,keepNonTransparent=false;
if(imageMask && d==1 && decodeColorData.getID()==ColorSpaces.DeviceRGB && maskCol[0]==0 && maskCol[1]==0 && maskCol[2]==0){
//see if blank (assume true and disprove) and remove as totally see-through
isBlank=true;
for(int aa=0;aa1){
rawColorData[a] /= 255f;
}
decodeColorData.setColor(rawColorData, comps);
}
int foreground=decodeColorData.getColor().getRGB();
for(int a=0;a>16) & 0xFF);
rgbArray[1+(3*values)]=(byte) ((foreground>>8) & 0xFF);
rgbArray[2+(3*values)]=(byte) ((foreground) & 0xFF);
}
}
return rgbArray;
}
}