use of javax.imageio.stream.MemoryCacheImageInputStream in project pdfbox by apache.
the class Type5ShadingContext method collectTriangles.
@SuppressWarnings("squid:S1166")
private List<ShadedTriangle> collectTriangles(PDShadingType5 latticeTriangleShadingType, AffineTransform xform, Matrix matrix) throws IOException {
COSDictionary dict = latticeTriangleShadingType.getCOSObject();
if (!(dict instanceof COSStream)) {
return Collections.emptyList();
}
PDRange rangeX = latticeTriangleShadingType.getDecodeForParameter(0);
PDRange rangeY = latticeTriangleShadingType.getDecodeForParameter(1);
if (Float.compare(rangeX.getMin(), rangeX.getMax()) == 0 || Float.compare(rangeY.getMin(), rangeY.getMax()) == 0) {
return Collections.emptyList();
}
int numPerRow = latticeTriangleShadingType.getVerticesPerRow();
PDRange[] colRange = new PDRange[numberOfColorComponents];
for (int i = 0; i < numberOfColorComponents; ++i) {
colRange[i] = latticeTriangleShadingType.getDecodeForParameter(2 + i);
}
List<Vertex> vlist = new ArrayList<>();
long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
COSStream cosStream = (COSStream) dict;
try (ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream())) {
boolean eof = false;
while (!eof) {
Vertex p;
try {
p = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
vlist.add(p);
} catch (EOFException ex) {
eof = true;
}
}
}
int sz = vlist.size(), rowNum = sz / numPerRow;
Vertex[][] latticeArray = new Vertex[rowNum][numPerRow];
List<ShadedTriangle> list = new ArrayList<>();
if (rowNum < 2) {
// must have at least two rows; if not, return empty list
return list;
}
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < numPerRow; j++) {
latticeArray[i][j] = vlist.get(i * numPerRow + j);
}
}
for (int i = 0; i < rowNum - 1; i++) {
for (int j = 0; j < numPerRow - 1; j++) {
Point2D[] ps = new Point2D[] { latticeArray[i][j].point, latticeArray[i][j + 1].point, latticeArray[i + 1][j].point };
float[][] cs = new float[][] { latticeArray[i][j].color, latticeArray[i][j + 1].color, latticeArray[i + 1][j].color };
list.add(new ShadedTriangle(ps, cs));
ps = new Point2D[] { latticeArray[i][j + 1].point, latticeArray[i + 1][j].point, latticeArray[i + 1][j + 1].point };
cs = new float[][] { latticeArray[i][j + 1].color, latticeArray[i + 1][j].color, latticeArray[i + 1][j + 1].color };
list.add(new ShadedTriangle(ps, cs));
}
}
return list;
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project limelight by slagyr.
the class ImagePanel method setData.
public void setData(byte[] bytes) throws Exception {
ImageInputStream imageInput = new MemoryCacheImageInputStream(new ByteArrayInputStream(bytes));
setImage(ImageIO.read(imageInput));
filename = "[DATA]";
markAsNeedingLayout();
getParent().markAsNeedingLayout();
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project zm-mailbox by Zimbra.
the class NativeFormatter method getResizedImageData.
/**
* If the image stored in the {@code MimePart} exceeds the given width,
* shrinks the image and returns the shrunk data. If the
* image width is smaller than {@code maxWidth} or resizing is not supported,
* returns {@code null}.
*/
public static byte[] getResizedImageData(InputStream in, String contentType, String fileName, Integer maxWidth, Integer maxHeight) throws IOException {
ImageReader reader = null;
ImageWriter writer = null;
if (maxWidth == null)
maxWidth = LC.max_image_size_to_resize.intValue();
if (maxHeight == null)
maxHeight = LC.max_image_size_to_resize.intValue();
try {
// Get ImageReader for stream content.
reader = ImageUtil.getImageReader(contentType, fileName);
if (reader == null) {
log.debug("No ImageReader available.");
return null;
}
// Read message content.
reader.setInput(new MemoryCacheImageInputStream(in));
BufferedImage img = reader.read(0);
int width = img.getWidth(), height = img.getHeight();
if (width <= maxWidth && height <= maxHeight) {
log.debug("Image %dx%d is less than max %dx%d. Not resizing.", width, height, maxWidth, maxHeight);
return RETURN_CODE_NO_RESIZE.getBytes();
}
// Resize.
writer = ImageIO.getImageWriter(reader);
if (writer == null) {
log.debug("No ImageWriter available.");
return null;
}
double ratio = Math.min((double) maxWidth / width, (double) maxHeight / height);
width *= ratio;
height *= ratio;
BufferedImage small = ImageUtil.resize(img, width, height);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.setOutput(new MemoryCacheImageOutputStream(out));
writer.write(small);
return out.toByteArray();
} finally {
ByteUtil.closeStream(in);
if (reader != null) {
reader.dispose();
}
if (writer != null) {
writer.dispose();
}
}
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project OsmAnd-tools by osmandapp.
the class NativeJavaRendering method renderImage.
public BufferedImage renderImage(RenderingImageContext ctx) throws IOException {
ByteBuffer bitmapBuffer = render(ctx);
InputStream inputStream = new InputStream() {
int nextInd = 0;
@Override
public int read() throws IOException {
if (nextInd >= bitmapBuffer.capacity()) {
return -1;
}
byte b = bitmapBuffer.get(nextInd++);
if (b < 0) {
return b + 256;
} else {
return b;
}
}
};
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");
ImageReader reader = readers.next();
reader.setInput(new MemoryCacheImageInputStream(inputStream), true);
BufferedImage img = reader.read(0);
return img;
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project openj9 by eclipse.
the class ZipFileResolver method getLibrary.
public LibraryDataSource getLibrary(String fileName, boolean silent) throws FileNotFoundException {
if (zipfile == null) {
throw new FileNotFoundException(fileName);
}
ZipEntry entry = null;
try {
zip = new ZipFile(zipfile);
if (entryNames == null) {
entryNames = new ArrayList<String>();
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry nextEntry = entries.nextElement();
entryNames.add(nextEntry.getName());
if (nextEntry.getName().equals(fileName)) {
// we found the name we were asked to resolve
entry = nextEntry;
}
}
if (entry == null) {
// didn't find the named entry whilst building the cache
throw new FileNotFoundException(fileName);
}
} else {
if (entryNames.contains(fileName)) {
entry = zip.getEntry(fileName);
} else {
throw new FileNotFoundException(fileName);
}
}
MemoryCacheImageInputStream stream = new MemoryCacheImageInputStream(zip.getInputStream(entry));
log.fine("Resolved library " + fileName + " in zip file " + zipfile.getAbsolutePath());
return new LibraryDataSource(fileName, stream);
} catch (IOException e) {
log.log(Level.FINE, "Error resolving library in zip file " + zipfile.getAbsolutePath(), e);
throw new FileNotFoundException(fileName + " (due to underlying IOException)");
}
}
Aggregations