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.

autocontrast.c 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * autocontrast.c: autocontrast functions
  16. * TODO: the current approach is naive; we should use the histogram in order
  17. * to decide how to change the contrast.
  18. */
  19. #include "config.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include "pipi.h"
  25. #include "pipi_internals.h"
  26. pipi_image_t *pipi_autocontrast(pipi_image_t *src)
  27. {
  28. pipi_image_t *dst;
  29. pipi_pixels_t *srcp, *dstp;
  30. float *srcdata, *dstdata;
  31. float min = 1.0, max = 0.0, t;
  32. int x, y, w, h, gray;
  33. w = src->w;
  34. h = src->h;
  35. gray = (src->last_modified == PIPI_PIXELS_Y_F32);
  36. srcp = gray ? pipi_get_pixels(src, PIPI_PIXELS_Y_F32)
  37. : pipi_get_pixels(src, PIPI_PIXELS_RGBA_F32);
  38. srcdata = (float *)srcp->pixels;
  39. for(y = 0; y < h; y++)
  40. {
  41. for(x = 0; x < w; x++)
  42. {
  43. if(gray)
  44. {
  45. if(srcdata[y * w + x] < min)
  46. min = srcdata[y * w + x];
  47. if(srcdata[y * w + x] > max)
  48. max = srcdata[y * w + x];
  49. }
  50. else
  51. {
  52. if(srcdata[4 * (y * w + x)] < min)
  53. min = srcdata[4 * (y * w + x)];
  54. if(srcdata[4 * (y * w + x)] > max)
  55. max = srcdata[4 * (y * w + x)];
  56. if(srcdata[4 * (y * w + x) + 1] < min)
  57. min = srcdata[4 * (y * w + x) + 1];
  58. if(srcdata[4 * (y * w + x) + 1] > max)
  59. max = srcdata[4 * (y * w + x) + 1];
  60. if(srcdata[4 * (y * w + x) + 2] < min)
  61. min = srcdata[4 * (y * w + x) + 2];
  62. if(srcdata[4 * (y * w + x) + 2] > max)
  63. max = srcdata[4 * (y * w + x) + 2];
  64. }
  65. }
  66. }
  67. if(min >= max)
  68. return pipi_copy(src);
  69. t = 1. / (max - min);
  70. dst = pipi_new(w, h);
  71. dstp = gray ? pipi_get_pixels(dst, PIPI_PIXELS_Y_F32)
  72. : pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32);
  73. dstdata = (float *)dstp->pixels;
  74. for(y = 0; y < h; y++)
  75. {
  76. for(x = 0; x < w; x++)
  77. {
  78. if(gray)
  79. {
  80. dstdata[y * w + x] = (srcdata[y * w + x] - min) * t;
  81. }
  82. else
  83. {
  84. dstdata[4 * (y * w + x)]
  85. = (srcdata[4 * (y * w + x)] - min) * t;
  86. dstdata[4 * (y * w + x) + 1]
  87. = (srcdata[4 * (y * w + x) + 1] - min) * t;
  88. dstdata[4 * (y * w + x) + 2]
  89. = (srcdata[4 * (y * w + x) + 2] - min) * t;
  90. dstdata[4 * (y * w + x) + 3]
  91. = srcdata[4 * (y * w + x) + 3];
  92. }
  93. }
  94. }
  95. return dst;
  96. }