Download code
From LiteratePrograms
Back to Floyd-Steinberg_dithering_(C)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
floyd_steinberg_dither_sample.c
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_(C)?action=history&offset=20080916082812 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_(C)?oldid=14630 25 */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include "floyd_steinberg_dither.h" 30 31 int main(int argc, char* argv[]) { 32 int x, y; 33 RGBImage image; 34 RGBPalette palette; 35 PalettizedImage result; 36 FILE * raw_in, * raw_out; 37 38 image.width = 100; 39 image.height = 145; 40 image.pixels = malloc(sizeof(RGBTriple) * 41 image.width * image.height); 42 43 raw_in = fopen(argv[1], "rb"); 44 for(y = 0; y < image.height; y++) { 45 for(x = 0; x < image.width; x++) { 46 fread(&image.pixels[x + y*image.width], 47 3, 1, raw_in); 48 } 49 } 50 fclose(raw_in); 51 52 palette.size = 16; 53 palette.table = malloc(sizeof(RGBTriple) * 16); 54 palette.table[0].R = 149; 55 palette.table[0].G = 91; 56 palette.table[0].B = 110; 57 palette.table[1].R = 176; 58 palette.table[1].G = 116; 59 palette.table[1].B = 137; 60 palette.table[2].R = 17; 61 palette.table[2].G = 11; 62 palette.table[2].B = 15; 63 palette.table[3].R = 63; 64 palette.table[3].G = 47; 65 palette.table[3].B = 69; 66 palette.table[4].R = 93; 67 palette.table[4].G = 75; 68 palette.table[4].B = 112; 69 palette.table[5].R = 47; 70 palette.table[5].G = 62; 71 palette.table[5].B = 24; 72 palette.table[6].R = 76; 73 palette.table[6].G = 90; 74 palette.table[6].B = 55; 75 palette.table[7].R = 190; 76 palette.table[7].G = 212; 77 palette.table[7].B = 115; 78 palette.table[8].R = 160; 79 palette.table[8].G = 176; 80 palette.table[8].B = 87; 81 palette.table[9].R = 116; 82 palette.table[9].G = 120; 83 palette.table[9].B = 87; 84 palette.table[10].R = 245; 85 palette.table[10].G = 246; 86 palette.table[10].B = 225; 87 palette.table[11].R = 148; 88 palette.table[11].G = 146; 89 palette.table[11].B = 130; 90 palette.table[12].R = 200; 91 palette.table[12].G = 195; 92 palette.table[12].B = 180; 93 palette.table[13].R = 36; 94 palette.table[13].G = 32; 95 palette.table[13].B = 27; 96 palette.table[14].R = 87; 97 palette.table[14].G = 54; 98 palette.table[14].B = 45; 99 palette.table[15].R = 121; 100 palette.table[15].G = 72; 101 palette.table[15].B = 72; 102 103 result = FloydSteinbergDither(image, palette); 104 105 raw_out = fopen(argv[2], "wb"); 106 for(y = 0; y < result.height; y++) { 107 for(x = 0; x < result.width; x++) { 108 fwrite(&palette.table[result.pixels[x + y*image.width]], 109 3, 1, raw_out); 110 } 111 } 112 fclose(raw_out); 113 114 return 0; 115 } 116
floyd_steinberg_dither.c
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_(C)?action=history&offset=20080916082812 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_(C)?oldid=14630 25 */ 26 27 #include <stdlib.h> 28 29 #include "floyd_steinberg_dither.h" 30 31 #define plus_truncate_uchar(a, b) \ 32 if (((int)(a)) + (b) < 0) \ 33 (a) = 0; \ 34 else if (((int)(a)) + (b) > 255) \ 35 (a) = 255; \ 36 else \ 37 (a) += (b); 38 39 40 static 41 unsigned char FindNearestColor(RGBTriple color, RGBPalette palette) { 42 int i, distanceSquared, minDistanceSquared, bestIndex = 0; 43 minDistanceSquared = 255*255 + 255*255 + 255*255 + 1; 44 for (i=0; i<palette.size; i++) { 45 int Rdiff = ((int)color.R) - palette.table[i].R; 46 int Gdiff = ((int)color.G) - palette.table[i].G; 47 int Bdiff = ((int)color.B) - palette.table[i].B; 48 distanceSquared = Rdiff*Rdiff + Gdiff*Gdiff + Bdiff*Bdiff; 49 if (distanceSquared < minDistanceSquared) { 50 minDistanceSquared = distanceSquared; 51 bestIndex = i; 52 } 53 } 54 return bestIndex; 55 } 56 57 58 #define compute_disperse(channel) \ 59 error = ((int)(currentPixel->channel)) - palette.table[index].channel; \ 60 if (x + 1 < image.width) { \ 61 plus_truncate_uchar(image.pixels[(x+1) + (y+0)*image.width].channel, (error*7) >> 4); \ 62 } \ 63 if (y + 1 < image.height) { \ 64 if (x - 1 > 0) { \ 65 plus_truncate_uchar(image.pixels[(x-1) + (y+1)*image.width].channel, (error*3) >> 4); \ 66 } \ 67 plus_truncate_uchar(image.pixels[(x+0) + (y+1)*image.width].channel, (error*5) >> 4); \ 68 if (x + 1 < image.width) { \ 69 plus_truncate_uchar(image.pixels[(x+1) + (y+1)*image.width].channel, (error*1) >> 4); \ 70 } \ 71 } 72 73 74 PalettizedImage FloydSteinbergDither(RGBImage image, RGBPalette palette) 75 76 { 77 PalettizedImage result; 78 result.width = image.width; 79 result.height = image.height; 80 result.pixels = malloc(sizeof(unsigned char) * result.width * result.height); 81 82 83 { 84 int x, y; 85 for(y = 0; y < image.height; y++) { 86 for(x = 0; x < image.width; x++) { 87 RGBTriple* currentPixel = &(image.pixels[x + y*image.width]); 88 unsigned char index = FindNearestColor(*currentPixel, palette); 89 result.pixels[x + y*result.width] = index; 90 91 { 92 int error; 93 compute_disperse(R); 94 compute_disperse(G); 95 compute_disperse(B); 96 } 97 98 } 99 } 100 } 101 102 return result; 103 } 104 105 106
floyd_steinberg_dither.h
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_(C)?action=history&offset=20080916082812 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_(C)?oldid=14630 25 */ 26 27 #ifndef _FLOYD_STEINBERG_DITHER_H_ 28 29 typedef struct { 30 unsigned char R, G, B; 31 } RGBTriple; 32 33 typedef struct { 34 int size; 35 RGBTriple* table; 36 } RGBPalette; 37 38 typedef struct { 39 int width, height; 40 RGBTriple* pixels; 41 } RGBImage; 42 43 typedef struct { 44 int width, height; 45 unsigned char* pixels; 46 } PalettizedImage; 47 48 PalettizedImage FloydSteinbergDither(RGBImage image, RGBPalette palette) 49 ; 50 51 #endif /* #ifndef _FLOYD_STEINBERG_DITHER_H_ */ 52
