Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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