Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

169 Zeilen
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. 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. for(i=0; i< img->w*img->h*4; i+=4)
  37. {
  38. if(flags&PIPI_COLOR_A)
  39. h->a[data[i+3]]++;
  40. if(flags&PIPI_COLOR_R)
  41. h->r[data[i+2]]++;
  42. if(flags&PIPI_COLOR_G)
  43. h->g[data[i+1]]++;
  44. if(flags&PIPI_COLOR_B)
  45. h->b[data[i]]++;
  46. if(flags&PIPI_COLOR_Y)
  47. {
  48. uint32_t p = 0.;
  49. p += 0.299 * data[i];
  50. p += 0.587 * data[i+1];
  51. p += 0.114 * data[i+2];
  52. h->y[p>255?255:p]++;
  53. }
  54. }
  55. /* Normalize dataset */
  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. int x;
  106. if(!img || !h) return -1;
  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. }