org.jpedal.parser.image.mask.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-2016 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.mask;
import java.awt.image.*;
import org.jpedal.color.ColorSpaces;
import org.jpedal.color.GenericColorSpace;
import org.jpedal.io.ColorSpaceConvertor;
import org.jpedal.objects.raw.PdfDictionary;
import org.jpedal.objects.raw.PdfObject;
import org.jpedal.parser.image.ImageCommands;
import org.jpedal.parser.image.data.ImageData;
/**
*
* @author markee
*/
public class MaskDecoder {
/**
* apply the Mask to image data directly as a component on argb
*
* @param imageData
* @param decodeColorData
* @return
*/
public static byte[] applyMask(final ImageData imageData,final GenericColorSpace decodeColorData, final PdfObject newMask, final PdfObject XObject, byte[] maskDataSream) {
int[] maskArray=XObject.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();
if(objectData == null && d == 8){
objectData = new byte[w*h];
}else if( objectData != null && d == 1 && decodeColorData.getID() == ColorSpaces.DeviceGray){ //refer to case 25110
objectData = ColorSpaceConvertor.normaliseTo8Bit(d, w, h, objectData);
d = 8;
}
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(objectData,maskDataSream,imageData, 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
* @return
*/
static byte[] applyMaskStream(byte[] objectData,byte[] maskData, final ImageData imageData,final PdfObject newMask, final PdfObject XObject) {
//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);
int maskD=newMask.getInt(PdfDictionary.BitsPerComponent);
final boolean isImageMask=newMask.getBoolean(PdfDictionary.ImageMask); //for example, see Case 22754
if(isImageMask){
maskD=1;
}
//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;i1){
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;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy