You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ostromoukhov.c 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * ostromoukhov.c: Ostromoukhov dithering functions
  16. *
  17. * This module implements Ostromoukhov's simple error diffusion algorithm,
  18. * as introduced in the paper "A Simple and Efficient Error-Diffusion
  19. * Algorithm", Proceedings of SIGGRAPH 2001, in ACM Computer Graphics,
  20. * Annual Conference Series, pp. 567--572, 2001.
  21. *
  22. * TODO: the table is actually a piecewise linear function, so it should
  23. * be easy to change it into something that works perfectly with floats.
  24. */
  25. #include "config.h"
  26. #include "common.h"
  27. #include "pipi.h"
  28. #include "pipi_internals.h"
  29. static int const table[][3] =
  30. {
  31. {13, 0, 5}, {13, 0, 5}, {21, 0, 10}, {7, 0, 4},
  32. {8, 0, 5}, {47, 3, 28}, {23, 3, 13}, {15, 3, 8},
  33. {22, 6, 11}, {43, 15, 20}, {7, 3, 3}, {501, 224, 211},
  34. {249, 116, 103}, {165, 80, 67}, {123, 62, 49}, {489, 256, 191},
  35. {81, 44, 31}, {483, 272, 181}, {60, 35, 22}, {53, 32, 19},
  36. {237, 148, 83}, {471, 304, 161}, {3, 2, 1}, {481, 314, 185},
  37. {354, 226, 155}, {1389, 866, 685}, {227, 138, 125}, {267, 158, 163},
  38. {327, 188, 220}, {61, 34, 45}, {627, 338, 505}, {1227, 638, 1075},
  39. {20, 10, 19}, {1937, 1000, 1767}, {977, 520, 855}, {657, 360, 551},
  40. {71, 40, 57}, {2005, 1160, 1539}, {337, 200, 247}, {2039, 1240, 1425},
  41. {257, 160, 171}, {691, 440, 437}, {1045, 680, 627}, {301, 200, 171},
  42. {177, 120, 95}, {2141, 1480, 1083}, {1079, 760, 513}, {725, 520, 323},
  43. {137, 100, 57}, {2209, 1640, 855}, {53, 40, 19}, {2243, 1720, 741},
  44. {565, 440, 171}, {759, 600, 209}, {1147, 920, 285}, {2311, 1880, 513},
  45. {97, 80, 19}, {335, 280, 57}, {1181, 1000, 171}, {793, 680, 95},
  46. {599, 520, 57}, {2413, 2120, 171}, {405, 360, 19}, {2447, 2200, 57},
  47. {11, 10, 0}, {158, 151, 3}, {178, 179, 7}, {1030, 1091, 63},
  48. {248, 277, 21}, {318, 375, 35}, {458, 571, 63}, {878, 1159, 147},
  49. {5, 7, 1}, {172, 181, 37}, {97, 76, 22}, {72, 41, 17},
  50. {119, 47, 29}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  51. {4, 1, 1}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  52. {4, 1, 1}, {4, 1, 1}, {65, 18, 17}, {95, 29, 26},
  53. {185, 62, 53}, {30, 11, 9}, {35, 14, 11}, {85, 37, 28},
  54. {55, 26, 19}, {80, 41, 29}, {155, 86, 59}, {5, 3, 2},
  55. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  56. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  57. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {5, 3, 2},
  58. {305, 176, 119}, {155, 86, 59}, {105, 56, 39}, {80, 41, 29},
  59. {65, 32, 23}, {55, 26, 19}, {335, 152, 113}, {85, 37, 28},
  60. {115, 48, 37}, {35, 14, 11}, {355, 136, 109}, {30, 11, 9},
  61. {365, 128, 107}, {185, 62, 53}, {25, 8, 7}, {95, 29, 26},
  62. {385, 112, 103}, {65, 18, 17}, {395, 104, 101}, {4, 1, 1}
  63. };
  64. pipi_image_t *pipi_ostromoukhov(pipi_image_t *src, pipi_scan_t scan)
  65. {
  66. pipi_image_t *dst;
  67. pipi_pixels_t *srcp, *dstp;
  68. float *srcdata, *dstdata;
  69. int x, y, w, h;
  70. w = src->w;
  71. h = src->h;
  72. srcp = pipi_getpixels(src, PIPI_PIXELS_Y_F);
  73. srcdata = (float *)srcp->pixels;
  74. dst = pipi_new(w, h);
  75. dstp = pipi_getpixels(dst, PIPI_PIXELS_Y_F);
  76. dstdata = (float *)dstp->pixels;
  77. for(y = 0; y < h; y++)
  78. {
  79. int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
  80. for(x = 0; x < w; x++)
  81. {
  82. float p, q, e;
  83. int x2, s, i;
  84. x2 = reverse ? w - 1 - x : x;
  85. s = reverse ? -1 : 1;
  86. p = srcdata[y * w + x2];
  87. q = p < 0.5 ? 0. : 1.;
  88. dstdata[y * w + x2] = q;
  89. e = p - q;
  90. i = p * 255.9999;
  91. if(i > 127) i = 255 - i;
  92. if(i < 0) i = 0; /* XXX: no "else" here */
  93. e /= table[i][0] + table[i][1] + table[i][2];
  94. if(x < w - 1)
  95. srcdata[y * w + x2 + s] += e * table[i][0];
  96. if(y < h - 1)
  97. {
  98. if(x > 0)
  99. srcdata[(y + 1) * w + x2 - s] += e * table[i][1];
  100. srcdata[(y + 1) * w + x2] += e * table[i][2];
  101. }
  102. }
  103. }
  104. return dst;
  105. }