List of usage examples for java.awt.image BufferedImage getColorModel
public ColorModel getColorModel()
From source file:ome.logic.AWTScaleService.java
public BufferedImage scaleBufferedImage(BufferedImage image, float xScale, float yScale) { int thumbHeight = (int) (image.getHeight() * yScale); int thumbWidth = (int) (image.getWidth() * xScale); log.info("Scaling to: " + thumbHeight + "x" + thumbWidth); // Create the required compatible (thumbnail) buffered image to avoid // potential errors from Java's ImagingLib. ColorModel cm = image.getColorModel(); WritableRaster r = cm.createCompatibleWritableRaster(thumbWidth, thumbHeight); BufferedImage thumbImage = new BufferedImage(cm, r, false, null); // Do the actual scaling and return the result Graphics2D graphics2D = thumbImage.createGraphics(); // graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, // RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null); return thumbImage; }
From source file:GraphicsUtil.java
/** * Copies data from one bufferedImage to another paying attention * to the state of AlphaPreMultiplied./*from ww w.j a v a 2s . com*/ * * @param src The source * @param srcRect The Rectangle of source data to be copied * @param dst The destination * @param destP The Place for the upper left corner of srcRect in dst. */ public static void copyData(BufferedImage src, Rectangle srcRect, BufferedImage dst, Point destP) { /* if (srcCS != dstCS) throw new IllegalArgumentException ("Images must be in the same ColorSpace in order "+ "to copy Data between them"); */ boolean srcAlpha = src.getColorModel().hasAlpha(); boolean dstAlpha = dst.getColorModel().hasAlpha(); // System.out.println("Src has: " + srcAlpha + // " is: " + src.isAlphaPremultiplied()); // // System.out.println("Dst has: " + dstAlpha + // " is: " + dst.isAlphaPremultiplied()); if (srcAlpha == dstAlpha) if (!srcAlpha || src.isAlphaPremultiplied() == dst.isAlphaPremultiplied()) { // They match one another so just copy everything... copyData(src.getRaster(), dst.getRaster()); return; } // System.out.println("Using Slow CopyData"); int[] pixel = null; Raster srcR = src.getRaster(); WritableRaster dstR = dst.getRaster(); int bands = dstR.getNumBands(); int dx = destP.x - srcRect.x; int dy = destP.y - srcRect.y; int w = srcRect.width; int x0 = srcRect.x; int y0 = srcRect.y; int y1 = y0 + srcRect.height - 1; if (!srcAlpha) { // Src has no alpha dest does so set alpha to 1.0 everywhere. // System.out.println("Add Alpha"); int[] oPix = new int[bands * w]; int out = (w * bands) - 1; // The 2 skips alpha channel while (out >= 0) { // Fill alpha channel with 255's oPix[out] = 255; out -= bands; } int b, in; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = w * (bands - 1) - 1; out = (w * bands) - 2; // The 2 skips alpha channel on last pix switch (bands) { case 4: while (in >= 0) { oPix[out--] = pixel[in--]; oPix[out--] = pixel[in--]; oPix[out--] = pixel[in--]; out--; } break; default: while (in >= 0) { for (b = 0; b < bands - 1; b++) oPix[out--] = pixel[in--]; out--; } } dstR.setPixels(x0 + dx, y + dy, w, 1, oPix); } } else if (dstAlpha && dst.isAlphaPremultiplied()) { // Src and dest have Alpha but we need to multiply it for dst. // System.out.println("Mult Case"); int a, b, alpha, in, fpNorm = (1 << 24) / 255, pt5 = 1 << 23; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = bands * w - 1; switch (bands) { case 4: while (in >= 0) { a = pixel[in]; if (a == 255) in -= 4; else { in--; alpha = fpNorm * a; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; } } break; default: while (in >= 0) { a = pixel[in]; if (a == 255) in -= bands; else { in--; alpha = fpNorm * a; for (b = 0; b < bands - 1; b++) { pixel[in] = (pixel[in] * alpha + pt5) >>> 24; in--; } } } } dstR.setPixels(x0 + dx, y + dy, w, 1, pixel); } } else if (dstAlpha && !dst.isAlphaPremultiplied()) { // Src and dest have Alpha but we need to divide it out for dst. // System.out.println("Div Case"); int a, b, ialpha, in, fpNorm = 0x00FF0000, pt5 = 1 << 15; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = (bands * w) - 1; switch (bands) { case 4: while (in >= 0) { a = pixel[in]; if ((a <= 0) || (a >= 255)) in -= 4; else { in--; ialpha = fpNorm / a; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; } } break; default: while (in >= 0) { a = pixel[in]; if ((a <= 0) || (a >= 255)) in -= bands; else { in--; ialpha = fpNorm / a; for (b = 0; b < bands - 1; b++) { pixel[in] = (pixel[in] * ialpha + pt5) >>> 16; in--; } } } } dstR.setPixels(x0 + dx, y + dy, w, 1, pixel); } } else if (src.isAlphaPremultiplied()) { int[] oPix = new int[bands * w]; // Src has alpha dest does not so unpremult and store... // System.out.println("Remove Alpha, Div Case"); int a, b, ialpha, in, out, fpNorm = 0x00FF0000, pt5 = 1 << 15; for (int y = y0; y <= y1; y++) { pixel = srcR.getPixels(x0, y, w, 1, pixel); in = (bands + 1) * w - 1; out = (bands * w) - 1; while (in >= 0) { a = pixel[in]; in--; if (a > 0) { if (a < 255) { ialpha = fpNorm / a; for (b = 0; b < bands; b++) oPix[out--] = (pixel[in--] * ialpha + pt5) >>> 16; } else for (b = 0; b < bands; b++) oPix[out--] = pixel[in--]; } else { in -= bands; for (b = 0; b < bands; b++) oPix[out--] = 255; } } dstR.setPixels(x0 + dx, y + dy, w, 1, oPix); } } else { // Src has unpremult alpha, dest does not have alpha, // just copy the color channels over. Rectangle dstRect = new Rectangle(destP.x, destP.y, srcRect.width, srcRect.height); for (int b = 0; b < bands; b++) copyBand(srcR, srcRect, b, dstR, dstRect, b); } }
From source file:com.aurel.track.attachment.AttachBL.java
private static boolean hasAlpha(Image image) { // If buffered image, the color model is readily available if (image instanceof BufferedImage) { BufferedImage bimage = (BufferedImage) image; return bimage.getColorModel().hasAlpha(); }//from w w w . ja va 2 s. c o m // grabbing a single pixel is usually sufficient PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { pg.grabPixels(); } catch (InterruptedException e) { } // Get the image's color model ColorModel cm = pg.getColorModel(); return cm.hasAlpha(); }
From source file:Java2DExample.java
public BufferedImage processImage(BufferedImage image) { float[][] colorMatrix = { { 1f, 0f, 0f }, { 0.5f, 1.0f, 0.5f }, { 0.2f, 0.4f, 0.6f } }; BandCombineOp changeColors = new BandCombineOp(colorMatrix, null); Raster sourceRaster = image.getRaster(); WritableRaster displayRaster = sourceRaster.createCompatibleWritableRaster(); changeColors.filter(sourceRaster, displayRaster); return new BufferedImage(image.getColorModel(), displayRaster, true, null); }
From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java
/** * scales bands to min = 0 & max = 2^8 or 2^16 * * @param image//from ww w. j av a2s .co m * @param min int[] containing minima per band * @param max * @return */ public static BufferedImage scaleIntensities(BufferedImage image, int[] min, int[] max) throws IllegalArgumentException { WritableRaster raster = image.getRaster(); if ((min.length != max.length) || min.length != raster.getNumBands()) throw new IllegalArgumentException( "Please ensure consistency of min and max arrays and number of bands in image"); int width = image.getWidth(); int height = image.getHeight(); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { for (int band = 0; band < raster.getNumBands(); band++) { int pixel = raster.getSample(col, row, band); if (pixel < min[band]) pixel = min[band]; if (pixel > max[band]) pixel = max[band]; pixel = ((int) (((((double) pixel - min[band]) / (max[band] - min[band])) * ((int) Math.pow(2, image.getColorModel().getPixelSize()) - 1)) + 0.5)); raster.setSample(col, row, band, pixel); } } } return image; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage convertToARGB(BufferedImage img) { if (img == null) { return null; }//w ww. j a va 2 s.co m BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB); ColorConvertOp cco = new ColorConvertOp(img.getColorModel().getColorSpace(), out.getColorModel().getColorSpace(), null); cco.filter(img, out); return out; }
From source file:org.codice.alliance.plugin.nitf.NitfPostIngestPlugin.java
private byte[] renderToJpeg2k(final BufferedImage bufferedImage) throws IOException { BufferedImage imageToCompress = bufferedImage; if (bufferedImage.getColorModel().getNumComponents() == ARGB_COMPONENT_COUNT) { imageToCompress = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = imageToCompress.createGraphics(); g.drawImage(bufferedImage, 0, 0, null); }//from ww w. j av a 2 s . c o m ByteArrayOutputStream os = new ByteArrayOutputStream(); J2KImageWriter writer = new J2KImageWriter(new J2KImageWriterSpi()); J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam(); writeParams.setLossless(false); writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); writeParams.setCompressionType("JPEG2000"); writeParams.setCompressionQuality(0.0f); ImageOutputStream ios = new MemoryCacheImageOutputStream(os); writer.setOutput(ios); writer.write(null, new IIOImage(imageToCompress, null, null), writeParams); writer.dispose(); ios.close(); return os.toByteArray(); }
From source file:com.dianping.imcaptcha.strategy.WaveFilter.java
public BufferedImage filter(BufferedImage src, BufferedImage dst) { int width = src.getWidth(); int height = src.getHeight(); int type = src.getType(); WritableRaster srcRaster = src.getRaster(); originalSpace = new Rectangle(0, 0, width, height); transformedSpace = new Rectangle(0, 0, width, height); transformSpace(transformedSpace);/*w w w. j a v a 2s .co m*/ if (dst == null) { ColorModel dstCM = src.getColorModel(); dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null); } WritableRaster dstRaster = dst.getRaster(); int[] inPixels = getRGB(src, 0, 0, width, height, null); if (interpolation == NEAREST_NEIGHBOUR) return filterPixelsNN(dst, width, height, inPixels, transformedSpace); int srcWidth = width; int srcHeight = height; int srcWidth1 = width - 1; int srcHeight1 = height - 1; int outWidth = transformedSpace.width; int outHeight = transformedSpace.height; int outX, outY; int index = 0; int[] outPixels = new int[outWidth]; float radius = srcHeight * 1.0f / 2 / (float) Math.PI; outX = transformedSpace.x; outY = transformedSpace.y; float[] out = new float[2]; for (int y = 0; y < outHeight; y++) { for (int x = 0; x < outWidth; x++) { transformInverse(outX + x, outY + y, out, radius); int srcX = (int) Math.floor(out[0]); int srcY = (int) Math.floor(out[1]); float xWeight = out[0] - srcX; float yWeight = out[1] - srcY; int nw, ne, sw, se; if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) { // Easy case, all corners are in the image int i = srcWidth * srcY + srcX; nw = inPixels[i]; ne = inPixels[i + 1]; sw = inPixels[i + srcWidth]; se = inPixels[i + srcWidth + 1]; } else { // Some of the corners are off the image nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight); ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight); sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight); se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight); } outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se); } setRGB(dst, 0, y, transformedSpace.width, 1, outPixels); } return dst; }
From source file:com.liusoft.dlog4j.photo.FileSystemSaver.java
public Photo save(HttpContext context, FormFile imgFile, boolean autoRotate) throws IOException { String extendName = StringUtils.getFileExtend(imgFile.getFileName()).toLowerCase(); String[] urls = this.createNewPhotoURI(context, extendName); if (urls == null) return null; String origionalPath = urls[0]; String previewPath = urls[1]; //?// ww w. j av a2 s . c o m { ImageUtils.writeToFile(imgFile, origionalPath); } Photo photo = new Photo(); if (ImageUtils.isJPG(extendName)) { try { ImageUtils.fillExifInfo(origionalPath, photo); int orient = photo.getOrientation(); if (autoRotate && orient > 0 && orient <= 8) { //Rotate the image ImageUtils.rotateImage(origionalPath, orient); } } catch (Exception e) { log.error("Exception occur when reading EXIF of " + origionalPath, e); } } else if (ImageUtils.isBMP(extendName)) { String jpgName = ImageUtils.BMP_TO_JPG(origionalPath); if (jpgName != null) { //bmp if (new File(origionalPath).delete()) origionalPath = jpgName; } } //??? File fOrigionalImage = new File(origionalPath); photo.setSize((int) fOrigionalImage.length()); BufferedImage oldImage = (BufferedImage) ImageIO.read(fOrigionalImage); int old_width = oldImage.getWidth(); int old_height = oldImage.getHeight(); photo.setWidth(old_width); photo.setHeight(old_height); photo.setColorBit(oldImage.getColorModel().getPixelSize()); photo.setFileName(imgFile.getFileName()); { //?1024*768 int ori_width = MAX_WIDTH, ori_height = MAX_HEIGHT; boolean regenerate_img = true; if (old_width <= MAX_WIDTH && old_height <= MAX_HEIGHT) { ori_width = old_width; ori_height = old_height; regenerate_img = false; } else if (old_width > MAX_WIDTH && old_height > MAX_HEIGHT) { ori_width = MAX_WIDTH; ori_height = old_height * ori_width / old_width; } else if (old_width > MAX_WIDTH && old_height <= MAX_HEIGHT) { ori_width = MAX_WIDTH; ori_height = old_height; } else if (old_width <= MAX_WIDTH && old_height > MAX_HEIGHT) { ori_height = MAX_HEIGHT; ori_width = old_width * ori_height / old_height; } if (regenerate_img) { photo.setWidth(ori_width); photo.setHeight(ori_height); ImageUtils.createPreviewImage(new FileInputStream(origionalPath), origionalPath, ori_width, ori_height); photo.setSize((int) new File(origionalPath).length()); } } //? int preview_width, preview_height; preview_width = Math.min(PREVIEW_WIDTH, photo.getWidth()); if (photo.getHeight() <= PREVIEW_HEIGHT) preview_height = photo.getHeight(); else { //? preview_height = photo.getHeight() * preview_width / photo.getWidth(); } if (preview_width == photo.getWidth() && preview_height == photo.getHeight()) { //??? previewPath = origionalPath; } else { //? if (ImageUtils.isImage(extendName)) { previewPath = ImageUtils.createPreviewImage(new FileInputStream(origionalPath), previewPath, preview_width, preview_height); } else { photo = null; return null; } } //url //String contextPath = context.getRequest().getContextPath() + "/"; String uploadPath = this.getUploadPath(context); String path1 = origionalPath.substring(uploadPath.length()); String path2 = previewPath.substring(uploadPath.length()); photo.setImageURL(getPhotoBaseURI(context) + StringUtils.replace(path1, File.separator, "/")); photo.setPreviewURL(getPhotoBaseURI(context) + StringUtils.replace(path2, File.separator, "/")); return photo; }
From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java
private void createImageStream(PDDocument doc, BufferedImage bi) throws IOException { BufferedImage alphaImage = null; BufferedImage rgbImage = null; int width = bi.getWidth(); int height = bi.getHeight(); if (bi.getColorModel().hasAlpha()) { // extract the alpha information WritableRaster alphaRaster = bi.getAlphaRaster(); ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); alphaImage = new BufferedImage(cm, alphaRaster, false, null); // create a RGB image without alpha rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); Graphics2D g = rgbImage.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(bi, 0, 0, null);// w w w .ja va 2 s. c o m } else { rgbImage = bi; } java.io.OutputStream os = null; try { int numberOfComponents = rgbImage.getColorModel().getNumComponents(); if (numberOfComponents == 3) { setColorSpace(PDDeviceRGB.INSTANCE); } else { if (numberOfComponents == 1) { setColorSpace(new PDDeviceGray()); } else { throw new IllegalStateException(); } } byte[] outData = new byte[width * height * numberOfComponents]; rgbImage.getData().getDataElements(0, 0, width, height, outData); // add FlateDecode compression getPDStream().addCompression(); os = getCOSStream().createUnfilteredStream(); os.write(outData); COSDictionary dic = getCOSStream(); dic.setItem(COSName.FILTER, COSName.FLATE_DECODE); dic.setItem(COSName.SUBTYPE, COSName.IMAGE); dic.setItem(COSName.TYPE, COSName.XOBJECT); if (alphaImage != null) { PDPixelMap smask = new PDPixelMap(doc, alphaImage); dic.setItem(COSName.SMASK, smask); } setBitsPerComponent(8); setHeight(height); setWidth(width); } finally { os.close(); } }