選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

119 行
3.0 KiB

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