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

171 行
3.9 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * histogram.c: histogram functions
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. pipi_histogram_t* pipi_new_histogram(void)
  25. {
  26. return malloc(sizeof(pipi_histogram_t));
  27. }
  28. int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags)
  29. {
  30. int i;
  31. if(!h) return -1;
  32. uint8_t *data = (uint8_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
  33. for(i=0; i< img->w*img->h*4; i+=4)
  34. {
  35. if(flags&PIPI_COLOR_A)
  36. h->a[data[i+3]]++;
  37. if(flags&PIPI_COLOR_R)
  38. h->r[data[i+2]]++;
  39. if(flags&PIPI_COLOR_G)
  40. h->g[data[i+1]]++;
  41. if(flags&PIPI_COLOR_B)
  42. h->b[data[i]]++;
  43. if(flags&PIPI_COLOR_Y)
  44. {
  45. uint32_t p = 0.;
  46. p += 0.299 * data[i];
  47. p += 0.587 * data[i+1];
  48. p += 0.114 * data[i+2];
  49. h->y[p>255?255:p]++;
  50. }
  51. }
  52. /* Normalize dataset */
  53. unsigned int max;
  54. float n;
  55. if(flags&PIPI_COLOR_R)
  56. {
  57. max = 0;
  58. for(i=0; i<256; i++)
  59. if(h->r[i] > max) max = h->r[i];
  60. n = 255.0f / max;
  61. for(i=0; i<256; i++)
  62. h->r[i]*=n;
  63. }
  64. if(flags&PIPI_COLOR_G)
  65. {
  66. max = 0;
  67. for(i=0; i<256; i++)
  68. if(h->g[i] > max) max = h->g[i];
  69. n = 255.0f / max;
  70. for(i=0; i<256; i++)
  71. h->g[i]*=n;
  72. }
  73. if(flags&PIPI_COLOR_B)
  74. {
  75. max = 0;
  76. for(i=0; i<256; i++)
  77. if(h->b[i] > max) max = h->b[i];
  78. n = 255.0f / max;
  79. for(i=0; i<256; i++)
  80. h->b[i]*=n;
  81. }
  82. if(flags&PIPI_COLOR_A)
  83. {
  84. max = 0;
  85. for(i=0; i<256; i++)
  86. if(h->a[i] > max) max = h->a[i];
  87. n = 255.0f / max;
  88. for(i=0; i<256; i++)
  89. h->a[i]*=n;
  90. }
  91. if(flags&PIPI_COLOR_Y)
  92. {
  93. max = 0;
  94. for(i=0; i<256; i++)
  95. if(h->y[i] > max) max = h->y[i];
  96. n = 255.0f / max;
  97. for(i=0; i<256; i++)
  98. h->y[i]*=n;
  99. }
  100. return 0;
  101. }
  102. int pipi_render_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags)
  103. {
  104. if(!img || !h) return -1;
  105. int x;
  106. for(x=0; x<256; x++)
  107. {
  108. if(flags&PIPI_COLOR_R)
  109. pipi_draw_line(img,
  110. x, 255,
  111. x, 255 - h->r[x],
  112. 0x00FF0000,
  113. 0);
  114. if(flags&PIPI_COLOR_G)
  115. pipi_draw_line(img,
  116. x, 255,
  117. x, 255 - h->g[x],
  118. 0x0000FF00,
  119. 0);
  120. if(flags&PIPI_COLOR_B)
  121. pipi_draw_line(img,
  122. x, 255,
  123. x, 255 - h->b[x],
  124. 0x000000FF,
  125. 0);
  126. if(flags&PIPI_COLOR_A)
  127. pipi_draw_line(img,
  128. x, 255,
  129. x, 255 - h->a[x],
  130. 0x00000FFF,
  131. 0);
  132. if(flags&PIPI_COLOR_Y)
  133. pipi_draw_line(img,
  134. x, 255,
  135. x, 255 - h->y[x],
  136. 0x00FFFFFF,
  137. 0);
  138. }
  139. return 0;
  140. }
  141. int pipi_free_histogram(pipi_histogram_t* h)
  142. {
  143. if(h) free(h);
  144. else return -1;
  145. return 0;
  146. }