org.jpedal.images.StandardImageLibrary 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
Open Source (LGPL) JavaFX PDF Viewer for NetBeans plugin
/*
* ===========================================
* 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-2017 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
@LICENSE@
*
* ---------------
* StandardImageLibrary.java
* ---------------
*/
package org.jpedal.images;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.jpedal.color.GenericColorSpace;
import org.jpedal.exception.PdfException;
import org.jpedal.utils.LogWriter;
/**
*
* @author markee
*/
public class StandardImageLibrary implements ImageLibrary {
@Override
public byte[] JBIGDecode(final byte[] data, final byte[] globalData, final String temp_dir) throws Exception {
return null;
}
@Override
public int[] convertCMYKtoRGB(final int cc, final int mm, final int yy, final int kk) {
return null;
}
@Override
public byte[] convertCMYK2RGB(final int w, final int h, final int pixelCount, final byte[] data) {
return null;
}
@Override
public void processJPEG(final int dim, final byte[] data, final int p, final int[] maskArray, final int[] output) {
}
/**
* convert byte[] datastream JPEG to an image in RGB
*
* @throws PdfException
*/
@Override
public BufferedImage JPEG2000ToRGBImage(final byte[] data) throws Exception {
return null;
}
public BufferedImage getDicomImage(final byte[] data) throws Exception {
return null;
}
@Override
public byte[] getBytesFromJPEG(final boolean isInverted, final byte[] data, final boolean isMask) throws Exception {
return null;
}
@Override
public byte[] getUnconvertedBytesFromJPEG2000(final byte[] data) throws Exception {
return null;
}
public byte[] getUnconvertedBytesFromJPEG(final byte[] data) throws Exception {
return null;
}
@Override
public byte[] getUnconvertedBytesFromJPEG(final byte[] data, final int adobeColorTransform) throws Exception {
return null;
}
@Override
public byte[] getBytesFromJPEG(final byte[] data) throws Exception {
final Raster ras = getRasterFromJPEG2000(data);
return ((DataBufferByte) ras.getDataBuffer()).getData();
}
private static Raster getRasterFromJPEG2000(final byte[] data) {
final ByteArrayInputStream in;
ImageReader iir = null;
final ImageInputStream iin;
Raster ras = null;
try {
//read the image data
in = new ByteArrayInputStream(data);
//suggestion from Carol
final Iterator iterator = ImageIO.getImageReadersByFormatName("JPEG2000");
while (iterator.hasNext()) {
final Object o = iterator.next();
iir = (ImageReader) o;
if (iir.canReadRaster()) {
break;
}
}
ImageIO.setUseCache(false);
iin = ImageIO.createImageInputStream((in));
iir.setInput(iin, true);
ras = iir.read(0).getRaster();
in.close();
iir.dispose();
iin.close();
} catch (final Exception ee) {
LogWriter.writeLog("Problem closing " + ee);
}
return ras;
}
@Override
public BufferedImage getTiffImage(final int tiffImageToLoad, final String file) {
return null;
}
public BufferedImage getPsdImage(final String fileName) {
return null;
}
@Override
public int getTiffPageCount(final String file) {
return 0;
}
@Override
public void write(final BufferedImage image, final String type, final String file_name, final boolean fasterPNG) throws IOException {
ImageIO.write(image, type, new File(file_name));
}
public int getDicomFrameCount(final String fn) {
return 0;
}
public static BufferedImage getDicomImage(final String fn, final int frameNumber) throws Exception {
return null;
}
public byte[] getScaledBytes(final byte[] src, final int srcWidth, final int srcHeight, final int dstWidth, final int dstHeight, final int nComp) {
return null;
}
public BufferedImage getScaledImage(final BufferedImage srcImage, final int dstWidth, final int dstHeight) {
return null;
}
public BufferedImage getSGIImage(final byte[] data) throws Exception {
return null;
}
public BufferedImage getSGIImage(final File data) throws Exception {
return null;
}
@Override
public BufferedImage read(final File f) throws Exception {
return ImageIO.read(f);
}
@Override
public BufferedImage convert(final Raster ras, final int w, final int h) {
final byte[] rasData = ((DataBufferByte) ras.getDataBuffer()).getData();
return decode(rasData, w, h);
}
@Override
public BufferedImage convert(final byte[] cmyk, final int w, final int h) {
return decode(cmyk, w, h);
}
private static BufferedImage decode(final byte[] rasData, final int w, final int h) {
final BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
final int[] imageData = ((java.awt.image.DataBufferInt) image.getRaster().getDataBuffer()).getData();
final java.awt.color.ColorSpace CMYK = GenericColorSpace.getColorSpaceInstance();
final int dim = w * h;
int p = 0;
int c, m, y, k;
for (int i = 0; i < dim; i++) {
c = rasData[p++] & 0xff;
m = rasData[p++] & 0xff;
y = rasData[p++] & 0xff;
k = rasData[p++] & 0xff;
final float[] RGB = CMYK.toRGB(new float[]{c / 255f, m / 255f, y / 255f, k / 255f});
final int r = (int) (RGB[0] * 255);
final int g = (int) (RGB[1] * 255);
final int b = (int) (RGB[2] * 255);
imageData[i] = (r << 16) | (g << 8) | b;
}
return image;
}
}