// Dithering // G. Landini at bham. ac. auk // 17/ May 2009 Dialog.create("Dithering"); items=newArray("Floyd-Steinberg", "Atkinson", "Jarvis-Judice-Ninke","Stucki", "Bayer_2x2", "Bayer_4x4", "Bayer_8x8", "Clustered_4x4", "Random"); doColour = false; Dialog.addChoice("Method", items); if (bitDepth==24) Dialog.addCheckbox("Colour dithering", false); Dialog.show() ; method=Dialog.getChoice(); if (bitDepth==24) doColour=Dialog.getCheckbox(); setBatchMode(true); w=getWidth(); h=getHeight(); run("Duplicate...", "title="+method); if (bitDepth==24 && doColour) { run("RGB Stack"); for (i=1;i<4;i++){ showProgress(i/4); setSlice(i); dither(method); } run("RGB Color"); } else{ if (bitDepth>8 && !doColour) run("8-bit"); dither(method); } setBatchMode(false); function dither(method) { if (method=="Floyd-Steinberg"){ w1=7/16; w2=3/16; w3=5/16; w4=1/16; for (y=0;y=d[(y%2*2+x%2)]) newpixel = 255; else newpixel=0; setPixel(x,y, newpixel); } } } else if (method=="Bayer_4x4"){ d= newArray(0.1250, 1.0000, 0.1875, 0.8125, 0.6250, 0.3750, 0.6875, 0.4375,0.2500, 0.8750, 0.0625, 0.9375, 0.7500, 0.5000, 0.5625, 0.3125); for (i=0;i=d[(y%4*4+x%4)]) newpixel = 255; else newpixel=0; setPixel(x,y, newpixel); } } } else if (method=="Bayer_8x8"){ d= newArray(1, 33, 9, 41, 3, 35, 11, 43, 49, 17, 57, 25, 51, 19, 59, 27, 13, 45, 5, 37, 15, 47, 7, 39, 61, 29, 53, 21, 63, 31, 55, 23, 4, 36, 12, 44, 2, 34, 10, 42, 52, 20, 60, 28, 50, 18, 58, 26, 16, 48, 8, 40, 14, 46, 6, 38, 64, 32, 56, 24, 62, 30, 54, 22); for (i=0;i=d[(y%8*8+x%8)]) newpixel = 255; else newpixel=0; setPixel(x,y, newpixel); } } } else if (method=="Clustered_4x4"){ d= newArray(0.7500, 0.3750, 0.6250, 0.2500, 0.0625, 1.0000, 0.8750, 0.4375, 0.5000, 0.8125, 0.9375, 0.1250, 0.1875, 0.5625, 0.3125, 0.6875); for (i=0;i=d[(y%4*4+x%4)]) newpixel = 255; else newpixel=0; setPixel(x,y, newpixel); } } } else if (method=="Random"){ w1=1/8; for (y=0;y random*255) setPixel(x,y, 255); else setPixel(x,y, 0); } } } }