You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
3.1 KiB

  1. /*
  2. * libpipi Proper image processing implementation 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 "common.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include "pipi.h"
  26. #include "pipi_internals.h"
  27. pipi_image_t *pipi_autocontrast(pipi_image_t *src)
  28. {
  29. pipi_image_t *dst;
  30. pipi_pixels_t *srcp, *dstp;
  31. float *srcdata, *dstdata;
  32. float min = 1.0, max = 0.0, t;
  33. int x, y, w, h, gray;
  34. w = src->w;
  35. h = src->h;
  36. gray = (src->last_modified == PIPI_PIXELS_Y_F);
  37. srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F)
  38. : pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  39. srcdata = (float *)srcp->pixels;
  40. for(y = 0; y < h; y++)
  41. {
  42. for(x = 0; x < w; x++)
  43. {
  44. if(gray)
  45. {
  46. if(srcdata[y * w + x] < min)
  47. min = srcdata[y * w + x];
  48. if(srcdata[y * w + x] > max)
  49. max = srcdata[y * w + x];
  50. }
  51. else
  52. {
  53. if(srcdata[4 * (y * w + x)] < min)
  54. min = srcdata[4 * (y * w + x)];
  55. if(srcdata[4 * (y * w + x)] > max)
  56. max = srcdata[4 * (y * w + x)];
  57. if(srcdata[4 * (y * w + x) + 1] < min)
  58. min = srcdata[4 * (y * w + x) + 1];
  59. if(srcdata[4 * (y * w + x) + 1] > max)
  60. max = srcdata[4 * (y * w + x) + 1];
  61. if(srcdata[4 * (y * w + x) + 2] < min)
  62. min = srcdata[4 * (y * w + x) + 2];
  63. if(srcdata[4 * (y * w + x) + 2] > max)
  64. max = srcdata[4 * (y * w + x) + 2];
  65. }
  66. }
  67. }
  68. if(min >= max)
  69. return pipi_copy(src);
  70. t = 1. / (max - min);
  71. dst = pipi_new(w, h);
  72. dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F)
  73. : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  74. dstdata = (float *)dstp->pixels;
  75. for(y = 0; y < h; y++)
  76. {
  77. for(x = 0; x < w; x++)
  78. {
  79. if(gray)
  80. {
  81. dstdata[y * w + x] = (srcdata[y * w + x] - min) * t;
  82. }
  83. else
  84. {
  85. dstdata[4 * (y * w + x)]
  86. = (srcdata[4 * (y * w + x)] - min) * t;
  87. dstdata[4 * (y * w + x) + 1]
  88. = (srcdata[4 * (y * w + x) + 1] - min) * t;
  89. dstdata[4 * (y * w + x) + 2]
  90. = (srcdata[4 * (y * w + x) + 2] - min) * t;
  91. }
  92. }
  93. }
  94. return dst;
  95. }