Download code
From LiteratePrograms
Back to Floyd-Steinberg_dithering_(Java)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
FloydSteinbergDither.java
1 /* Copyright (c) 2008 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Floyd-Steinberg_dithering_(Java)?action=history&offset=20080201121723 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Floyd-Steinberg_dithering_(Java)?oldid=12476 25 */ 26 27 import java.io.*; 28 29 class RGBTriple { 30 public final byte[] channels; 31 public RGBTriple() { channels = new byte[3]; } 32 public RGBTriple(int R, int G, int B) 33 { channels = new byte[]{(byte)R, (byte)G, (byte)B}; } 34 } 35 36 37 public class FloydSteinbergDither 38 { 39 private static byte plus_truncate_uchar(byte a, int b) { 40 if ((a & 0xff) + b < 0) 41 return 0; 42 else if ((a & 0xff) + b > 255) 43 return (byte)255; 44 else 45 return (byte)(a + b); 46 } 47 48 49 private static byte findNearestColor(RGBTriple color, RGBTriple[] palette) { 50 int minDistanceSquared = 255*255 + 255*255 + 255*255 + 1; 51 byte bestIndex = 0; 52 for (byte i = 0; i < palette.length; i++) { 53 int Rdiff = (color.channels[0] & 0xff) - (palette[i].channels[0] & 0xff); 54 int Gdiff = (color.channels[1] & 0xff) - (palette[i].channels[1] & 0xff); 55 int Bdiff = (color.channels[2] & 0xff) - (palette[i].channels[2] & 0xff); 56 int distanceSquared = Rdiff*Rdiff + Gdiff*Gdiff + Bdiff*Bdiff; 57 if (distanceSquared < minDistanceSquared) { 58 minDistanceSquared = distanceSquared; 59 bestIndex = i; 60 } 61 } 62 return bestIndex; 63 } 64 65 66 public static byte[][] floydSteinbergDither(RGBTriple[][] image, RGBTriple[] palette) 67 { 68 byte[][] result = new byte[image.length][image[0].length]; 69 70 71 for (int y = 0; y < image.length; y++) { 72 for (int x = 0; x < image[y].length; x++) { 73 RGBTriple currentPixel = image[y][x]; 74 byte index = findNearestColor(currentPixel, palette); 75 result[y][x] = index; 76 77 for (int i = 0; i < 3; i++) 78 { 79 int error = (currentPixel.channels[i] & 0xff) - (palette[index].channels[i] & 0xff); 80 if (x + 1 < image[0].length) { 81 image[y+0][x+1].channels[i] = 82 plus_truncate_uchar(image[y+0][x+1].channels[i], (error*7) >> 4); 83 } 84 if (y + 1 < image.length) { 85 if (x - 1 > 0) { 86 image[y+1][x-1].channels[i] = 87 plus_truncate_uchar(image[y+1][x-1].channels[i], (error*3) >> 4); 88 } 89 image[y+1][x+0].channels[i] = 90 plus_truncate_uchar(image[y+1][x+0].channels[i], (error*5) >> 4); 91 if (x + 1 < image[0].length) { 92 image[y+1][x+1].channels[i] = 93 plus_truncate_uchar(image[y+1][x+1].channels[i], (error*1) >> 4); 94 } 95 } 96 97 } 98 99 } 100 } 101 102 return result; 103 } 104 105 106 public static void main(String[] args) throws IOException { 107 RGBTriple[][] image = new RGBTriple[145][100]; 108 109 InputStream raw_in = new BufferedInputStream(new FileInputStream(args[0])); 110 for (int y = 0; y < image.length; y++) { 111 for (int x = 0; x < image[0].length; x++) { 112 image[y][x] = new RGBTriple(); 113 raw_in.read(image[y][x].channels, 0, 3); 114 } 115 } 116 raw_in.close(); 117 118 RGBTriple[] palette = { 119 new RGBTriple(149, 91, 110), 120 new RGBTriple(176, 116, 137), 121 new RGBTriple(17, 11, 15), 122 new RGBTriple(63, 47, 69), 123 new RGBTriple(93, 75, 112), 124 new RGBTriple(47, 62, 24), 125 new RGBTriple(76, 90, 55), 126 new RGBTriple(190, 212, 115), 127 new RGBTriple(160, 176, 87), 128 new RGBTriple(116, 120, 87), 129 new RGBTriple(245, 246, 225), 130 new RGBTriple(148, 146, 130), 131 new RGBTriple(200, 195, 180), 132 new RGBTriple(36, 32, 27), 133 new RGBTriple(87, 54, 45), 134 new RGBTriple(121, 72, 72) 135 }; 136 137 byte[][] result = floydSteinbergDither(image, palette); 138 139 OutputStream raw_out = new BufferedOutputStream(new FileOutputStream(args[1])); 140 for (int y = 0; y < result.length; y++) { 141 for (int x = 0; x < result[y].length; x++) { 142 raw_out.write(palette[result[y][x]].channels, 0, 3); 143 } 144 } 145 raw_out.close(); 146 } 147 148 } 149
