Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

118 řádky
4.3 KiB

  1. /*
  2. * libpipi Pathetic image processing interface 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 "pipi.h"
  27. #include "pipi_internals.h"
  28. static int const table[][3] =
  29. {
  30. {13, 0, 5}, {13, 0, 5}, {21, 0, 10}, {7, 0, 4},
  31. {8, 0, 5}, {47, 3, 28}, {23, 3, 13}, {15, 3, 8},
  32. {22, 6, 11}, {43, 15, 20}, {7, 3, 3}, {501, 224, 211},
  33. {249, 116, 103}, {165, 80, 67}, {123, 62, 49}, {489, 256, 191},
  34. {81, 44, 31}, {483, 272, 181}, {60, 35, 22}, {53, 32, 19},
  35. {237, 148, 83}, {471, 304, 161}, {3, 2, 1}, {481, 314, 185},
  36. {354, 226, 155}, {1389, 866, 685}, {227, 138, 125}, {267, 158, 163},
  37. {327, 188, 220}, {61, 34, 45}, {627, 338, 505}, {1227, 638, 1075},
  38. {20, 10, 19}, {1937, 1000, 1767}, {977, 520, 855}, {657, 360, 551},
  39. {71, 40, 57}, {2005, 1160, 1539}, {337, 200, 247}, {2039, 1240, 1425},
  40. {257, 160, 171}, {691, 440, 437}, {1045, 680, 627}, {301, 200, 171},
  41. {177, 120, 95}, {2141, 1480, 1083}, {1079, 760, 513}, {725, 520, 323},
  42. {137, 100, 57}, {2209, 1640, 855}, {53, 40, 19}, {2243, 1720, 741},
  43. {565, 440, 171}, {759, 600, 209}, {1147, 920, 285}, {2311, 1880, 513},
  44. {97, 80, 19}, {335, 280, 57}, {1181, 1000, 171}, {793, 680, 95},
  45. {599, 520, 57}, {2413, 2120, 171}, {405, 360, 19}, {2447, 2200, 57},
  46. {11, 10, 0}, {158, 151, 3}, {178, 179, 7}, {1030, 1091, 63},
  47. {248, 277, 21}, {318, 375, 35}, {458, 571, 63}, {878, 1159, 147},
  48. {5, 7, 1}, {172, 181, 37}, {97, 76, 22}, {72, 41, 17},
  49. {119, 47, 29}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  50. {4, 1, 1}, {4, 1, 1}, {4, 1, 1}, {4, 1, 1},
  51. {4, 1, 1}, {4, 1, 1}, {65, 18, 17}, {95, 29, 26},
  52. {185, 62, 53}, {30, 11, 9}, {35, 14, 11}, {85, 37, 28},
  53. {55, 26, 19}, {80, 41, 29}, {155, 86, 59}, {5, 3, 2},
  54. {5, 3, 2}, {5, 3, 2}, {5, 3, 2}, {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. {305, 176, 119}, {155, 86, 59}, {105, 56, 39}, {80, 41, 29},
  58. {65, 32, 23}, {55, 26, 19}, {335, 152, 113}, {85, 37, 28},
  59. {115, 48, 37}, {35, 14, 11}, {355, 136, 109}, {30, 11, 9},
  60. {365, 128, 107}, {185, 62, 53}, {25, 8, 7}, {95, 29, 26},
  61. {385, 112, 103}, {65, 18, 17}, {395, 104, 101}, {4, 1, 1}
  62. };
  63. pipi_image_t *pipi_dither_ostromoukhov(pipi_image_t *img, pipi_scan_t scan)
  64. {
  65. pipi_image_t *dst;
  66. pipi_pixels_t *dstp;
  67. float *dstdata;
  68. int x, y, w, h;
  69. w = img->w;
  70. h = img->h;
  71. dst = pipi_copy(img);
  72. dstp = pipi_get_pixels(dst, PIPI_PIXELS_Y_F32);
  73. dstdata = (float *)dstp->pixels;
  74. for(y = 0; y < h; y++)
  75. {
  76. int reverse = (y & 1) && (scan == PIPI_SCAN_SERPENTINE);
  77. for(x = 0; x < w; x++)
  78. {
  79. float p, q, e;
  80. int x2, s, i;
  81. x2 = reverse ? w - 1 - x : x;
  82. s = reverse ? -1 : 1;
  83. p = dstdata[y * w + x2];
  84. q = p < 0.5 ? 0. : 1.;
  85. dstdata[y * w + x2] = q;
  86. e = p - q;
  87. i = p * 255.9999;
  88. if(i > 127) i = 255 - i;
  89. if(i < 0) i = 0; /* XXX: no "else" here */
  90. e /= table[i][0] + table[i][1] + table[i][2];
  91. if(x < w - 1)
  92. dstdata[y * w + x2 + s] += e * table[i][0];
  93. if(y < h - 1)
  94. {
  95. if(x > 0)
  96. dstdata[(y + 1) * w + x2 - s] += e * table[i][1];
  97. dstdata[(y + 1) * w + x2] += e * table[i][2];
  98. }
  99. }
  100. }
  101. return dst;
  102. }