@Override public void paint(Graphics g) { if (g == null) throw new NullPointerException(); if (offscreenBuffer == null) { offscreenBuffer = createImage(this.getWidth(), this.getHeight()); offscreenGraphics = offscreenBuffer.getGraphics(); } for (int x = 0; x < widthInCharacters; x++) { for (int y = 0; y < heightInCharacters; y++) { if (oldBackgroundColors[x][y] == backgroundColors[x][y] && oldForegroundColors[x][y] == foregroundColors[x][y] && oldChars[x][y] == chars[x][y]) continue; Color bg = backgroundColors[x][y]; Color fg = foregroundColors[x][y]; LookupOp op = setColors(bg, fg); BufferedImage img = op.filter(glyphs[chars[x][y]], null); // <--- MAGIA NEGRA!! offscreenGraphics.drawImage(img, x * charWidth, y * charHeight, null); oldBackgroundColors[x][y] = backgroundColors[x][y]; oldForegroundColors[x][y] = foregroundColors[x][y]; oldChars[x][y] = chars[x][y]; } } g.drawImage(offscreenBuffer, 0, 0, this); }
private Image invert(Image src) { BufferedImage biSrc = toBufferedImage(src); BufferedImage biDst = new BufferedImage(biSrc.getWidth(), biSrc.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); byte invert[] = new byte[256]; for (int j = 0; j < 256; j++) { invert[j] = (byte) (255 - j); } byte[][] ftable = {invert}; LookupOp lo = new LookupOp(new ByteLookupTable(0, ftable), null); lo.filter(biSrc, biDst); return Toolkit.getDefaultToolkit().createImage(biDst.getSource()); }
// MenuItem Event Posterize private void jMenuItem11ActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jMenuItem11ActionPerformed if (gambarEdit == null) { return; } byte posterize[] = new byte[256]; for (int i = 0; i < 256; i++) { posterize[i] = (byte) (i - (i % 32)); } ByteLookupTable table = new ByteLookupTable(0, posterize); LookupOp op = new LookupOp(table, null); BufferedImage filteredImage = new BufferedImage(gambarEdit.getWidth(), gambarEdit.getHeight(), gambarEdit.getType()); op.filter(gambarEdit, filteredImage); gambarEdit = filteredImage; jLabel2.setIcon(null); jLabel2.setIcon(new ImageIcon(gambarEdit)); } // GEN-LAST:event_jMenuItem11ActionPerformed
// MenuItem Event Threshold private void jMenuItem13ActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jMenuItem13ActionPerformed if (gambarEdit == null) { return; } jMenuItem9ActionPerformed(null); byte threshold[] = new byte[256]; for (int i = 0; i < 256; i++) { threshold[i] = (i < 128) ? (byte) 0 : (byte) 255; } ByteLookupTable table = new ByteLookupTable(0, threshold); LookupOp op = new LookupOp(table, null); BufferedImage filteredImage = new BufferedImage(gambarEdit.getWidth(), gambarEdit.getHeight(), gambarEdit.getType()); op.filter(gambarEdit, filteredImage); gambarEdit = filteredImage; jLabel2.setIcon(null); jLabel2.setIcon(new ImageIcon(gambarEdit)); jMenuItem9ActionPerformed(null); } // GEN-LAST:event_jMenuItem13ActionPerformed
/** * Paint the image onto a Graphics object. The painting is performed tile-by-tile, and includes a * grey region covering the unused portion of image tiles as well as the general background. At * this point the image must be byte data. */ public synchronized void paintComponent(Graphics g) { Graphics2D g2D = null; if (g instanceof Graphics2D) { g2D = (Graphics2D) g; } else { return; } // if source is null, it's just a component if (source == null) { g2D.setColor(getBackground()); g2D.fillRect(0, 0, componentWidth, componentHeight); return; } int transX = -originX; int transY = -originY; // Get the clipping rectangle and translate it into image coordinates. Rectangle clipBounds = g.getClipBounds(); if (clipBounds == null) { clipBounds = new Rectangle(0, 0, componentWidth, componentHeight); } // clear the background (clip it) [minimal optimization here] if (transX > 0 || transY > 0 || transX < (componentWidth - source.getWidth()) || transY < (componentHeight - source.getHeight())) { g2D.setColor(getBackground()); g2D.fillRect(0, 0, componentWidth, componentHeight); } clipBounds.translate(-transX, -transY); // Determine the extent of the clipping region in tile coordinates. int txmin, txmax, tymin, tymax; int ti, tj; txmin = XtoTileX(clipBounds.x); txmin = Math.max(txmin, minTileX); txmin = Math.min(txmin, maxTileX); txmax = XtoTileX(clipBounds.x + clipBounds.width - 1); txmax = Math.max(txmax, minTileX); txmax = Math.min(txmax, maxTileX); tymin = YtoTileY(clipBounds.y); tymin = Math.max(tymin, minTileY); tymin = Math.min(tymin, maxTileY); tymax = YtoTileY(clipBounds.y + clipBounds.height - 1); tymax = Math.max(tymax, minTileY); tymax = Math.min(tymax, maxTileY); Insets insets = getInsets(); // Loop over tiles within the clipping region for (tj = tymin; tj <= tymax; tj++) { for (ti = txmin; ti <= txmax; ti++) { int tx = TileXtoX(ti); int ty = TileYtoY(tj); Raster tile = source.getTile(ti, tj); if (tile != null) { DataBuffer dataBuffer = tile.getDataBuffer(); WritableRaster wr = tile.createWritableRaster(sampleModel, dataBuffer, null); BufferedImage bi = new BufferedImage(colorModel, wr, colorModel.isAlphaPremultiplied(), null); // correctly handles band offsets if (brightnessEnabled == true) { SampleModel sm = sampleModel.createCompatibleSampleModel(tile.getWidth(), tile.getHeight()); WritableRaster raster = RasterFactory.createWritableRaster(sm, null); BufferedImage bimg = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); // don't move this code ByteLookupTable lutTable = new ByteLookupTable(0, lutData); LookupOp lookup = new LookupOp(lutTable, null); lookup.filter(bi, bimg); g2D.drawImage(bimg, biop, tx + transX + insets.left, ty + transY + insets.top); } else { AffineTransform transform; transform = AffineTransform.getTranslateInstance( tx + transX + insets.left, ty + transY + insets.top); g2D.drawRenderedImage(bi, transform); } } } } }