public BufferedImage executeForOneLayer(BufferedImage src) { BufferedImage dest = null; if (createDefaultDestBuffer()) { if (copyContents()) { dest = ImageUtils.copyImage(src); } else { dest = ImageUtils.createCompatibleDest(src); } } dest = transform(src, dest); runCount++; // System.out.println(String.format("Filter::executeForOneLayer: transformed '%s'", // getName())); if (dest == null) { if (Build.CURRENT == Build.DEVELOPMENT) { System.out.println( String.format("Filter::executeForOneLayer: '%s' returned null dest", getName())); } } assert dest != null; return dest; }
@Override public BufferedImage doTransform(BufferedImage src, BufferedImage dest) { int hueP = hue.getValue(); int satP = saturation.getValue(); int briP = brightness.getValue(); if ((hueP == 0) && (satP == 0) && (briP == 0)) { return src; } float satShift = saturation.getValueAsPercentage(); float briShift = brightness.getValueAsPercentage(); float hueShift = hue.getValueAsFloat() / 360.0f; int[] srcData = ImageUtils.getPixelsAsArray(src); int[] destData = ImageUtils.getPixelsAsArray(dest); int length = srcData.length; assert length == destData.length; float[] tmpHSBArray = {0.0f, 0.0f, 0.0f}; for (int i = 0; i < length; i++) { int rgb = srcData[i]; int a = rgb & 0xFF000000; int r = (rgb >>> 16) & 0xFF; int g = (rgb >>> 8) & 0xFF; int b = (rgb) & 0xFF; tmpHSBArray = Color.RGBtoHSB(r, g, b, tmpHSBArray); float shiftedHue = tmpHSBArray[0] + hueShift; float shiftedSat = tmpHSBArray[1] + satShift; float shiftedBri = tmpHSBArray[2] + briShift; if (shiftedSat < 0.0f) { shiftedSat = 0.0f; } if (shiftedSat > 1.0f) { shiftedSat = 1.0f; } if (shiftedBri < 0.0f) { shiftedBri = 0.0f; } if (shiftedBri > 1.0f) { shiftedBri = 1.0f; } int newRGB = Color.HSBtoRGB(shiftedHue, shiftedSat, shiftedBri); // alpha is 255 here newRGB &= 0x00FFFFFF; // set alpha to 0 destData[i] = a | newRGB; // add the real alpha } return dest; }
@Override public BufferedImage doTransform(BufferedImage src, BufferedImage dest) { dest = ImageUtils.copyImage(src); Color color = colorParam.getColor(); int width = widthParam.getValue(); int spacing = spacingParam.getValue(); boolean emptyIntersections = emptyIntersectionsParam.isChecked(); Graphics2D g = dest.createGraphics(); int destWidth = dest.getWidth(); int destHeight = dest.getHeight(); float opacity = opacityParam.getValueAsPercentage(); boolean rotated = !rotateResult.isSetToDefault(); int maxX = destWidth; int maxY = destHeight; // if(rotated) { // maxX = (int) (Math.max(destWidth, destHeight) * 1.3); // maxY = maxX; // } // we draw the grid first on an image with transparent background BufferedImage tmp = ImageUtils.getGridImageOnTransparentBackground( color, maxX, maxY, width, spacing, width, spacing, emptyIntersections); g.setComposite(AlphaComposite.getInstance(SRC_ATOP, opacity)); if (rotated) { g.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); g.setRenderingHint(KEY_INTERPOLATION, VALUE_INTERPOLATION_BILINEAR); g.translate(maxX / 2, maxY / 2); g.rotate(rotateResult.getValueInRadians()); int xDiff = maxX - destWidth; int yDiff = maxY - destHeight; g.translate(-xDiff / 2, -yDiff / 2); g.translate(-maxX / 2 / 2, -maxY / 2 / 2); } g.drawImage(tmp, 0, 0, null); g.dispose(); return dest; }
@Override public BufferedImage doTransform(BufferedImage src, BufferedImage dest) { if (radius.getValue() == 0) { return src; } if (filter == null) { filter = new GaussianFilter(); } filter.setRadius(radius.getValueAsFloat()); filter.setPremultiplyAlpha(false); dest = filter.filter(src, dest); if (hpSharpening.isChecked()) { dest = ImageUtils.getHighPassSharpenedImage(src, dest); } return dest; }
@Override public BufferedImage doTransform(BufferedImage src, BufferedImage dest) { if (filter == null) { filter = new CheckFilter(); } // filter.setAaRes(aaRes.getValue()); filter.setFuzziness(fuzziness.getValue()); filter.setAngle((float) angle.getValueInRadians()); filter.setBackground(color1.getColor().getRGB()); filter.setForeground(color2.getColor().getRGB()); filter.setXScale(size.getValue(0)); filter.setYScale(size.getValue(1)); dest = filter.filter(src, dest); if (bumpMap.isChecked()) { return ImageUtils.bumpMap(src, dest); } return dest; }
public static Composition readOpenRaster(File file) throws IOException, ParserConfigurationException, SAXException { boolean DEBUG = System.getProperty("openraster.debug", "false").equals("true"); String stackXML = null; Map<String, BufferedImage> images = new HashMap<>(); try (ZipFile zipFile = new ZipFile(file)) { Enumeration<? extends ZipEntry> fileEntries = zipFile.entries(); while (fileEntries.hasMoreElements()) { ZipEntry entry = fileEntries.nextElement(); String name = entry.getName(); if (name.equalsIgnoreCase("stack.xml")) { stackXML = extractString(zipFile.getInputStream(entry)); } else if (name.equalsIgnoreCase("mergedimage.png")) { // no need for that } else { String extension = FileExtensionUtils.getFileExtension(name); if ("png".equalsIgnoreCase(extension)) { BufferedImage image = ImageIO.read(zipFile.getInputStream(entry)); images.put(name, image); if (DEBUG) { System.out.println( String.format( "OpenRaster::readOpenRaster: found png image in zip file at the path '%s'", name)); } } } } } if (stackXML == null) { throw new IllegalStateException("No stack.xml found."); } if (DEBUG) { System.out.println(String.format("OpenRaster::readOpenRaster: stackXML = '%s'", stackXML)); } Document doc = loadXMLFromString(stackXML); Element documentElement = doc.getDocumentElement(); documentElement.normalize(); String documentElementNodeName = documentElement.getNodeName(); if (!documentElementNodeName.equals("image")) { throw new IllegalStateException( String.format( "stack.xml root element is '%s', expected: 'image'", documentElementNodeName)); } String w = documentElement.getAttribute("w"); int compositionWidth = Integer.parseInt(w); String h = documentElement.getAttribute("h"); int compositionHeight = Integer.parseInt(h); if (DEBUG) { System.out.println( String.format( "OpenRaster::readOpenRaster: w = '%s', h = '%s', compositionWidth = %d, compositionHeight = %d", w, h, compositionWidth, compositionHeight)); } Composition comp = Composition.empty(compositionWidth, compositionHeight); comp.setFile(file); NodeList layers = documentElement.getElementsByTagName("layer"); for (int i = layers.getLength() - 1; i >= 0; i--) { // stack.xml contains layers in reverse order Node node = layers.item(i); Element element = (Element) node; String layerName = element.getAttribute("name"); String layerVisibility = element.getAttribute("visibility"); String layerVisible = element.getAttribute("visible"); String layerBlendingMode = element.getAttribute("composite-op"); String layerOpacity = element.getAttribute("opacity"); String layerImageSource = element.getAttribute("src"); String layerX = element.getAttribute("x"); String layerY = element.getAttribute("y"); BufferedImage image = images.get(layerImageSource); image = ImageUtils.toCompatibleImage(image); if (DEBUG) { int imageWidth = image.getWidth(); int imageHeight = image.getHeight(); System.out.println( "OpenRaster::readOpenRaster: imageWidth = " + imageWidth + ", imageHeight = " + imageHeight); // Utils.debugImage(image, layerImageSource); } if (layerVisibility == null || layerVisibility.isEmpty()) { // workaround: paint.net exported files use "visible" attribute instead of "visibility" layerVisibility = layerVisible; } boolean visibility = layerVisibility == null ? true : layerVisibility.equals("visible"); ImageLayer layer = new ImageLayer(comp, image, layerName, null); layer.setVisible(visibility, AddToHistory.NO); BlendingMode blendingMode = BlendingMode.fromSVGName(layerBlendingMode); if (DEBUG) { System.out.println("OpenRaster::readOpenRaster: blendingMode = " + blendingMode); } layer.setBlendingMode(blendingMode, UpdateGUI.NO, AddToHistory.NO, false); float opacity = Utils.parseFloat(layerOpacity, 1.0f); layer.setOpacity(opacity, UpdateGUI.NO, AddToHistory.NO, false); int translationX = Utils.parseInt(layerX, 0); int translationY = Utils.parseInt(layerY, 0); // TODO assuming that there is no layer mask layer.setTranslation(translationX, translationY); if (DEBUG) { System.out.println( String.format( "OpenRaster::readOpenRaster: opacity = %.2f, translationX = %d, translationY = %d", opacity, translationX, translationY)); } comp.addLayerNoGUI(layer); } comp.setActiveLayer(comp.getLayer(0), AddToHistory.NO); return comp; }