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

org.jpedal.parser.image.downsample.EightBitDownSampler 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-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


 *
 * ---------------
 * EightBitDownSampler.java
 * ---------------
 */

package org.jpedal.parser.image.downsample;

import org.jpedal.color.ColorSpaces;
import org.jpedal.color.DeviceRGBColorSpace;
import org.jpedal.color.GenericColorSpace;
import org.jpedal.parser.image.data.ImageData;

/**
 *
 * @author markee
 */
class EightBitDownSampler {
    
    static GenericColorSpace downSample(final ImageData imageData, GenericColorSpace decodeColorData, int sampling) {
        
        byte[] index=decodeColorData.getIndexedMap();
        
        final boolean hasIndex=decodeColorData.getIndexedMap()!=null &&
                (decodeColorData.getID()==ColorSpaces.DeviceRGB || decodeColorData.getID()==ColorSpaces.CalRGB ||
                decodeColorData.getID()==ColorSpaces.DeviceCMYK || decodeColorData.getID()==ColorSpaces.ICC || decodeColorData.getID()==ColorSpaces.DeviceN);
        
        int comp;
        
        int indexCount=1;
        
        if(hasIndex){ //convert to sRGB
            comp=1;
            
            imageData.setCompCount(3);
            indexCount=3;
            index=decodeColorData.convertIndexToRGB(index);
            
            //actually sRGB now so reset colorspace
            decodeColorData=new DeviceRGBColorSpace();
            
        }else{
            comp=decodeColorData.getColorComponentCount();
        }
        
        
        int newW=imageData.getWidth()/sampling;
        int newH=imageData.getHeight()/sampling;
        byte[] data=imageData.getObjectData();
        
        final int oldSize=data.length;
        
        int x,y,xx,yy,jj;
        
        int origLineLength;
        //black and white
        if(imageData.getWidth()*imageData.getHeight()==oldSize || decodeColorData.getID()==ColorSpaces.DeviceGray) {
            comp = 1;
        }
        
        final byte[] newData;
        
        if(hasIndex){ //hard-coded to 3 values
            newData=new byte[newW*newH*indexCount];
            origLineLength= imageData.getWidth();
        }else{
            newData=new byte[newW*newH*comp];
            origLineLength= imageData.getWidth()*comp;
        }
        //scan all pixels and down-sample
        for(y=0;ywGapLeft) {
                    wCount = wGapLeft;
                }
                if(hCount>hGapLeft) {
                    hCount = hGapLeft;
                }
                
                int[] indexAv;
                for(jj=0;jj0){
                        newPtr=jj+(x*comp)+(newW*y*comp);
                        newData[newPtr]=(byte)((byteTotal)/count);
                    }
                }
            }
        }
        
        imageData.setObjectData(newData);
        imageData.setWidth(newW);
        imageData.setHeight(newH);
        
        return decodeColorData;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy