Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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