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

175 行
4.2 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. uint8_t *data;
  31. float n;
  32. unsigned int max;
  33. int i;
  34. if(!h) return -1;
  35. data = (uint8_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
  36. memset(h->a, 0, 256*(sizeof(unsigned int)));
  37. memset(h->r, 0, 256*(sizeof(unsigned int)));
  38. memset(h->g, 0, 256*(sizeof(unsigned int)));
  39. memset(h->b, 0, 256*(sizeof(unsigned int)));
  40. memset(h->y, 0, 256*(sizeof(unsigned int)));
  41. for(i=0; i< img->w*img->h*4; i+=4)
  42. {
  43. if(flags&PIPI_COLOR_A)
  44. h->a[data[i+3]]++;
  45. if(flags&PIPI_COLOR_R)
  46. h->r[data[i+2]]++;
  47. if(flags&PIPI_COLOR_G)
  48. h->g[data[i+1]]++;
  49. if(flags&PIPI_COLOR_B)
  50. h->b[data[i]]++;
  51. if(flags&PIPI_COLOR_Y)
  52. {
  53. uint32_t p = 0.;
  54. p += 0.299 * data[i];
  55. p += 0.587 * data[i+1];
  56. p += 0.114 * data[i+2];
  57. h->y[p>255?255:p]++;
  58. }
  59. }
  60. /* Normalize dataset */
  61. if(flags&PIPI_COLOR_R)
  62. {
  63. max = 0;
  64. for(i=0; i<256; i++)
  65. if(h->r[i] > max) max = h->r[i];
  66. n = 255.0f / max;
  67. for(i=0; i<256; i++)
  68. h->r[i]*=n;
  69. }
  70. if(flags&PIPI_COLOR_G)
  71. {
  72. max = 0;
  73. for(i=0; i<256; i++)
  74. if(h->g[i] > max) max = h->g[i];
  75. n = 255.0f / max;
  76. for(i=0; i<256; i++)
  77. h->g[i]*=n;
  78. }
  79. if(flags&PIPI_COLOR_B)
  80. {
  81. max = 0;
  82. for(i=0; i<256; i++)
  83. if(h->b[i] > max) max = h->b[i];
  84. n = 255.0f / max;
  85. for(i=0; i<256; i++)
  86. h->b[i]*=n;
  87. }
  88. if(flags&PIPI_COLOR_A)
  89. {
  90. max = 0;
  91. for(i=0; i<256; i++)
  92. if(h->a[i] > max) max = h->a[i];
  93. n = 255.0f / max;
  94. for(i=0; i<256; i++)
  95. h->a[i]*=n;
  96. }
  97. if(flags&PIPI_COLOR_Y)
  98. {
  99. max = 0;
  100. for(i=0; i<256; i++)
  101. if(h->y[i] > max) max = h->y[i];
  102. n = 255.0f / max;
  103. for(i=0; i<256; i++)
  104. h->y[i]*=n;
  105. }
  106. return 0;
  107. }
  108. int pipi_render_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags)
  109. {
  110. int x;
  111. if(!img || !h) return -1;
  112. for(x=0; x<256; x++)
  113. {
  114. if(flags&PIPI_COLOR_R)
  115. pipi_draw_line(img,
  116. x, 255,
  117. x, 255 - h->r[x],
  118. 0x00FF0000,
  119. 0);
  120. if(flags&PIPI_COLOR_G)
  121. pipi_draw_line(img,
  122. x, 255,
  123. x, 255 - h->g[x],
  124. 0x0000FF00,
  125. 0);
  126. if(flags&PIPI_COLOR_B)
  127. pipi_draw_line(img,
  128. x, 255,
  129. x, 255 - h->b[x],
  130. 0x000000FF,
  131. 0);
  132. if(flags&PIPI_COLOR_A)
  133. pipi_draw_line(img,
  134. x, 255,
  135. x, 255 - h->a[x],
  136. 0x00000FFF,
  137. 0);
  138. if(flags&PIPI_COLOR_Y)
  139. pipi_draw_line(img,
  140. x, 255,
  141. x, 255 - h->y[x],
  142. 0x00FFFFFF,
  143. 0);
  144. }
  145. return 0;
  146. }
  147. int pipi_free_histogram(pipi_histogram_t* h)
  148. {
  149. if(h) free(h);
  150. else return -1;
  151. return 0;
  152. }