ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1321
1321
EMPFEHLEN
-
27.03.10 19:09 #1
Zum aktuellen Coding-Quiz habe ich eine Java-Implementierung geschrieben:
Das Programm läuft ohne GUI, man muss in den gleichen Ordner ein Bild mit Dateiname "Bild.png" und die angehängte Datei "Cubes.png" legen, erzeugt werden dann folgende Dateien:Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
import java.awt.Color; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; public class PixelToDice { public static final int steps = 6; public static final int step = (256/steps)+1; public static final boolean dither = true; public static final int cubeWidth = 6; public static Image cubeImg; public PixelToDice() { System.out.print("Laden... "); ImageIcon cubes = new ImageIcon("Cubes.png"); cubeImg = cubes.getImage(); ImageIcon icon = new ImageIcon("Bild.png"); BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage newImg = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); BufferedImage newImg2 = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); img.getGraphics().drawImage(icon.getImage(), 0, 0, icon.getImageObserver()); System.out.println("Fertig!"); System.out.print("Berechnen... "); maximize_blackWhite_dither(img, newImg, newImg2); System.out.println("Fertig!"); System.out.print("Speichern... "); try { ImageIO.write(newImg, "png", new File("Bild_dithered.png")); ImageIO.write(newImg2, "png", new File("Bild_withDice.png")); System.out.println("Fertig!"); } catch (IOException e) { e.printStackTrace(); } } public static BufferedImage maximize_blackWhite_dither(BufferedImage img, BufferedImage dest, BufferedImage dest2){ int x, y, x2 = 1, y2 = 1, highest=0, lowest=255, aktValue, colorSum, err, light; Color oldCol, newCol, aktCol; //Bestimmen der höchsten Farbwerte: for(y = 0; y<img.getHeight(); y++){ for(x = 0; x<img.getWidth(); x++){ oldCol = new Color(img.getRGB(x, y)); aktValue = (int)oldCol.getRed(); if(aktValue>highest){ highest = aktValue; } if(aktValue<lowest){ lowest = aktValue; } aktValue = (int)oldCol.getGreen(); if(aktValue>highest){ highest = aktValue; } if(aktValue<lowest){ lowest = aktValue; } aktValue = (int)oldCol.getBlue(); if(aktValue>highest){ highest = aktValue; } if(aktValue<lowest){ lowest = aktValue; } } } //Erhöhen des Kontrasts: for(y = 0; y<img.getHeight(); y++){ for(x = 0; x<img.getWidth(); x++){ oldCol = new Color(img.getRGB(x, y)); newCol = new Color((oldCol.getRed()-lowest)*highest/255, (oldCol.getGreen()-lowest)*highest/255, (oldCol.getBlue()-lowest)*highest/255); img.setRGB(x, y, newCol.getRGB()); } } //Umwandlung des Bildes: for(y = 0; y<img.getHeight(); y += cubeWidth){ for(x = 0; x<img.getWidth(); x += cubeWidth){ colorSum = 0; //Schwarzweiß-Durchschnitt: for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ aktCol = new Color(img.getRGB(x2, y2)); colorSum += aktCol.getRed(); colorSum += aktCol.getGreen(); colorSum += aktCol.getBlue(); } } colorSum /= x2-x; colorSum /= y2-y; colorSum /= 3; oldCol = new Color(colorSum, colorSum, colorSum); newCol = new Color(compress(colorSum), compress(colorSum), compress(colorSum)); //Zeichnen der Pixel-Version: for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ dest.setRGB(x2, y2, newCol.getRGB()); } } //Zeichnen der Würfel-Version: light = 5-(int)(colorSum/step); dest2.getGraphics().drawImage(cubeImg, x, y, x+cubeWidth, y+cubeWidth, 0, cubeWidth*light, cubeWidth, cubeWidth*(light+1), null); //Dithering des Bildes mit dem Floyd-Steinberg-Algorythmus: if(dither == true){ err = oldCol.getRed()-newCol.getRed(); try{ for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ aktCol = new Color(img.getRGB(x+cubeWidth, y)); for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ img.setRGB(x2+cubeWidth, y2, new Color(aktCol.getRed()+7*err/16, aktCol.getGreen()+7*err/16, aktCol.getBlue()+7*err/16).getRGB()); } } }catch(Exception e){ } try{ for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ aktCol = new Color(img.getRGB(x-cubeWidth, y+cubeWidth)); for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ img.setRGB(x2-cubeWidth, y2+cubeWidth, new Color(aktCol.getRed()+(3*err/16), aktCol.getGreen()+(3*err/16), aktCol.getBlue()+(3*err/16)).getRGB()); } } }catch(Exception e){ } try{ for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ aktCol = new Color(img.getRGB(x, y+cubeWidth)); for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ img.setRGB(x2, y2+cubeWidth, new Color(aktCol.getRed()+5*err/16, aktCol.getGreen()+5*err/16, aktCol.getBlue()+5*err/16).getRGB()); } } }catch(Exception e){ } try{ for(y2 = y; y2<y+cubeWidth && y2<img.getHeight(); y2++){ aktCol = new Color(img.getRGB(x+cubeWidth, y+cubeWidth)); for(x2 = x; x2<x+cubeWidth && x2<img.getWidth(); x2++){ img.setRGB(x2+cubeWidth, y2+cubeWidth, new Color(aktCol.getRed()+1*err/16, aktCol.getGreen()+1*err/16, aktCol.getBlue()+1*err/16).getRGB()); } } }catch(Exception e){ } } if(x>=img.getWidth()||y>=img.getHeight()){ break; } } } return img; } public static int compress(int color){ return check((int) ((Math.round(color/step)+0.5)*step)); } public static int check(int color){ return (color<0?0:(color>255?255:color)); } public static void main(String[] args) { new PixelToDice(); } }
"Bild_dithered.png": Das Bild in 6 Graustufen (jeweils 6x6 Pixel große Flächen)
"Bild_withDice.png": Das Bild in 6x6 Pixel große Würfelchen
Ein Beispiel:
Ein (relativ großes) Bild von Obama: (Von Tutorials.de leider verkleinert, daher als jpg)
![[QUIZ#14] Jellysheep (Java)-bild.jpg](http://www.tutorials.de/attachments/archiv/51604d1322823100t-bild.jpg)
Die erzeugte Pixelversion:
![[QUIZ#14] Jellysheep (Java)-bild_dithered.png](http://www.tutorials.de/attachments/archiv/51605d1322823100t-bild_dithered.png)
Und schließlich das Würfel-Bild:
Geändert von Jellysheep (27.03.10 um 22:03 Uhr)
Ähnliche Themen
-
Quiz XML mit Java DOM
Von Simon Berger im Forum XML TechnologienAntworten: 1Letzter Beitrag: 16.06.10, 08:20 -
[QUIZ#15] Jellysheep (C++) [2]
Von Jellysheep im Forum ArchivAntworten: 2Letzter Beitrag: 12.04.10, 15:40 -
[QUIZ#15] Jellysheep (C++)
Von Jellysheep im Forum ArchivAntworten: 2Letzter Beitrag: 11.04.10, 20:23 -
Quiz mit Java?
Von fraus im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 13.07.01, 10:22





Login





