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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <pipi.h>
  7. int main(int argc, char *argv[])
  8. {
  9. static double const mypal[] =
  10. {
  11. #if 1
  12. 0.800, 0.001, 0.001, /* red */
  13. 0.001, 0.700, 0.001, /* green */
  14. 0.005, 0.001, 0.600, /* blue */
  15. 0.900, 0.900, 0.900, /* white */
  16. 0.900, 0.900, 0.001, /* yellow */
  17. 0.850, 0.450, 0.001, /* orange */
  18. #else
  19. 1,1,1,
  20. 0,.05,.60,
  21. 1,0,0,
  22. .15,.76,0,
  23. .96,1,0,
  24. 1,.61,0,
  25. #endif
  26. };
  27. #define NCOLORS ((int)(sizeof(mypal)/sizeof(*mypal)/3))
  28. int x, y, w, h;
  29. pipi_image_t *src = pipi_load(argv[1]);
  30. pipi_image_t *dst = pipi_reduce(src, NCOLORS, mypal);
  31. pipi_pixels_t *p = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  32. float *data = (float *)p->pixels;
  33. w = p->w;
  34. h = p->h;
  35. for(y = 0; y < h; y++)
  36. for(x = 0; x < w; x++)
  37. {
  38. double pr, pg, pb, qr, qg, qb, er, eg, eb;
  39. pr = data[4 * (y * w + x) + 2];
  40. pg = data[4 * (y * w + x) + 1];
  41. pb = data[4 * (y * w + x)];
  42. double dist = 10.0;
  43. int n, best = -1;
  44. for(n = 0; n < NCOLORS; n++)
  45. {
  46. double dr, dg, db, d;
  47. dr = pr - mypal[n * 3];
  48. dg = pg - mypal[n * 3 + 1];
  49. db = pb - mypal[n * 3 + 2];
  50. d = dr * dr + dg * dg + db * db;
  51. //d = 0.299 * dr * dr + 0.587 * dg * dg + 0.114 * db * db;
  52. if(d < dist)
  53. {
  54. best = n;
  55. dist = d;
  56. }
  57. }
  58. qr = mypal[best * 3];
  59. qg = mypal[best * 3 + 1];
  60. qb = mypal[best * 3 + 2];
  61. er = (pr - qr) / 16;
  62. eg = (pg - qg) / 16;
  63. eb = (pb - qb) / 16;
  64. if(x < w - 1)
  65. {
  66. data[4 * (y * w + x + 1) + 2] += 7 * er;
  67. data[4 * (y * w + x + 1) + 1] += 7 * eg;
  68. data[4 * (y * w + x + 1)] += 7 * eb;
  69. }
  70. if(y < h - 1)
  71. {
  72. if(x < w - 1)
  73. {
  74. data[4 * (y * w + x + w + 1) + 2] += 1 * er;
  75. data[4 * (y * w + x + w + 1) + 1] += 1 * eg;
  76. data[4 * (y * w + x + w + 1)] += 1 * eb;
  77. }
  78. data[4 * (y * w + x + w) + 2] += 5 * er;
  79. data[4 * (y * w + x + w) + 1] += 5 * eg;
  80. data[4 * (y * w + x + w)] += 5 * eb;
  81. if(x > 0)
  82. {
  83. data[4 * (y * w + x + w - 1) + 2] += 3 * er;
  84. data[4 * (y * w + x + w - 1) + 1] += 3 * eg;
  85. data[4 * (y * w + x + w - 1)] += 3 * eb;
  86. }
  87. }
  88. data[4 * (y * w + x) + 2] = qr;
  89. data[4 * (y * w + x) + 1] = qg;
  90. data[4 * (y * w + x)] = qb;
  91. }
  92. pipi_save(dst, argv[2]);
  93. pipi_release_pixels(dst, p);
  94. return 0;
  95. }