Download code

Jump to: navigation, search

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 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Floyd-Steinberg_dithering_(Java)?oldid=12476
 14 */
 15 
 16 import java.io.*;
 17 
 18 class RGBTriple {
 19     public final byte[] channels;
 20     public RGBTriple() { channels = new byte[3]; }
 21     public RGBTriple(int R, int G, int B)
 22     { channels = new byte[]{(byte)R, (byte)G, (byte)B}; }
 23 }
 24 
 25 
 26 public class FloydSteinbergDither
 27 {
 28     private static byte plus_truncate_uchar(byte a, int b) {
 29         if ((a & 0xff) + b < 0)
 30             return 0;
 31         else if ((a & 0xff) + b > 255)
 32             return (byte)255;
 33         else
 34             return (byte)(a + b);
 35     }
 36 
 37 
 38     private static byte findNearestColor(RGBTriple color, RGBTriple[] palette) {
 39         int minDistanceSquared = 255*255 + 255*255 + 255*255 + 1;
 40         byte bestIndex = 0;
 41         for (byte i = 0; i < palette.length; i++) {
 42             int Rdiff = (color.channels[0] & 0xff) - (palette[i].channels[0] & 0xff);
 43             int Gdiff = (color.channels[1] & 0xff) - (palette[i].channels[1] & 0xff);
 44             int Bdiff = (color.channels[2] & 0xff) - (palette[i].channels[2] & 0xff);
 45             int distanceSquared = Rdiff*Rdiff + Gdiff*Gdiff + Bdiff*Bdiff;
 46             if (distanceSquared < minDistanceSquared) {
 47                 minDistanceSquared = distanceSquared;
 48                 bestIndex = i;
 49             }
 50         }
 51         return bestIndex;
 52     }
 53 
 54     public static byte[][] floydSteinbergDither(RGBTriple[][] image, RGBTriple[] palette)
 55     {
 56         byte[][] result = new byte[image.length][image[0].length];
 57 
 58         for (int y = 0; y < image.length; y++) {
 59 	    for (int x = 0; x < image[y].length; x++) {
 60             RGBTriple currentPixel = image[y][x];
 61             byte index = findNearestColor(currentPixel, palette);
 62 	    result[y][x] = index;
 63 
 64             for (int i = 0; i < 3; i++)
 65 	    {
 66 	        int error = (currentPixel.channels[i] & 0xff) - (palette[index].channels[i] & 0xff);
 67 		if (x + 1 < image[0].length) {
 68 		    image[y+0][x+1].channels[i] =
 69 		        plus_truncate_uchar(image[y+0][x+1].channels[i], (error*7) >> 4);
 70 		}
 71 		if (y + 1 < image.length) {
 72 		    if (x - 1 > 0) {
 73 		        image[y+1][x-1].channels[i] =
 74 		            plus_truncate_uchar(image[y+1][x-1].channels[i], (error*3) >> 4);
 75 		    }
 76 		    image[y+1][x+0].channels[i] =
 77 		        plus_truncate_uchar(image[y+1][x+0].channels[i], (error*5) >> 4);
 78 		    if (x + 1 < image[0].length) {
 79 		        image[y+1][x+1].channels[i] =
 80 		            plus_truncate_uchar(image[y+1][x+1].channels[i], (error*1) >> 4);
 81 		    }
 82 		}
 83 	    }
 84             }
 85 	}
 86         return result;
 87     }
 88 
 89     public static void main(String[] args) throws IOException {
 90         RGBTriple[][] image = new RGBTriple[145][100];
 91 
 92         InputStream raw_in = new BufferedInputStream(new FileInputStream(args[0]));
 93         for (int y = 0; y < image.length; y++) {
 94             for (int x = 0; x < image[0].length; x++) {
 95                 image[y][x] = new RGBTriple();
 96                 raw_in.read(image[y][x].channels, 0, 3);
 97             }
 98         }
 99         raw_in.close();
100 
101         RGBTriple[] palette = {
102             new RGBTriple(149, 91, 110),
103             new RGBTriple(176, 116, 137),
104             new RGBTriple(17, 11, 15),
105             new RGBTriple(63, 47, 69),
106             new RGBTriple(93, 75, 112),
107             new RGBTriple(47, 62, 24),
108             new RGBTriple(76, 90, 55),
109             new RGBTriple(190, 212, 115),
110             new RGBTriple(160, 176, 87),
111             new RGBTriple(116, 120, 87),
112             new RGBTriple(245, 246, 225),
113             new RGBTriple(148, 146, 130),
114             new RGBTriple(200, 195, 180),
115             new RGBTriple(36, 32, 27),
116             new RGBTriple(87, 54, 45),
117             new RGBTriple(121, 72, 72)
118         };
119 
120         byte[][] result = floydSteinbergDither(image, palette);
121 
122         OutputStream raw_out = new BufferedOutputStream(new FileOutputStream(args[1]));
123         for (int y = 0; y < result.length; y++) {
124             for (int x = 0; x < result[y].length; x++) {
125                 raw_out.write(palette[result[y][x]].channels, 0, 3);
126             }
127         }
128         raw_out.close();
129     }
130 }


hijacker
hijacker
hijacker
hijacker