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 | /* |
---|
16 | * autocontrast.c: autocontrast functions |
---|
17 | * TODO: the current approach is naive; we should use the histogram in order |
---|
18 | * to decide how to change the contrast. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <string.h> |
---|
26 | #include <math.h> |
---|
27 | |
---|
28 | #include "pipi.h" |
---|
29 | #include "pipi_internals.h" |
---|
30 | |
---|
31 | pipi_image_t *pipi_autocontrast(pipi_image_t *src) |
---|
32 | { |
---|
33 | pipi_image_t *dst; |
---|
34 | pipi_pixels_t *srcp, *dstp; |
---|
35 | float *srcdata, *dstdata; |
---|
36 | float min = 1.0, max = 0.0, t; |
---|
37 | int x, y, w, h, gray; |
---|
38 | |
---|
39 | w = src->w; |
---|
40 | h = src->h; |
---|
41 | |
---|
42 | gray = (src->last_modified == PIPI_PIXELS_Y_F); |
---|
43 | |
---|
44 | srcp = gray ? pipi_getpixels(src, PIPI_PIXELS_Y_F) |
---|
45 | : pipi_getpixels(src, PIPI_PIXELS_RGBA_F); |
---|
46 | srcdata = (float *)srcp->pixels; |
---|
47 | |
---|
48 | for(y = 0; y < h; y++) |
---|
49 | { |
---|
50 | for(x = 0; x < w; x++) |
---|
51 | { |
---|
52 | if(gray) |
---|
53 | { |
---|
54 | if(srcdata[y * w + x] < min) |
---|
55 | min = srcdata[y * w + x]; |
---|
56 | if(srcdata[y * w + x] > max) |
---|
57 | max = srcdata[y * w + x]; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | if(srcdata[4 * (y * w + x)] < min) |
---|
62 | min = srcdata[4 * (y * w + x)]; |
---|
63 | if(srcdata[4 * (y * w + x)] > max) |
---|
64 | max = srcdata[4 * (y * w + x)]; |
---|
65 | if(srcdata[4 * (y * w + x) + 1] < min) |
---|
66 | min = srcdata[4 * (y * w + x) + 1]; |
---|
67 | if(srcdata[4 * (y * w + x) + 1] > max) |
---|
68 | max = srcdata[4 * (y * w + x) + 1]; |
---|
69 | if(srcdata[4 * (y * w + x) + 2] < min) |
---|
70 | min = srcdata[4 * (y * w + x) + 2]; |
---|
71 | if(srcdata[4 * (y * w + x) + 2] > max) |
---|
72 | max = srcdata[4 * (y * w + x) + 2]; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if(min >= max) |
---|
78 | return pipi_copy(src); |
---|
79 | |
---|
80 | t = 1. / (max - min); |
---|
81 | |
---|
82 | dst = pipi_new(w, h); |
---|
83 | dstp = gray ? pipi_getpixels(dst, PIPI_PIXELS_Y_F) |
---|
84 | : pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
85 | dstdata = (float *)dstp->pixels; |
---|
86 | |
---|
87 | for(y = 0; y < h; y++) |
---|
88 | { |
---|
89 | for(x = 0; x < w; x++) |
---|
90 | { |
---|
91 | if(gray) |
---|
92 | { |
---|
93 | dstdata[y * w + x] = (srcdata[y * w + x] - min) * t; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | dstdata[4 * (y * w + x)] |
---|
98 | = (srcdata[4 * (y * w + x)] - min) * t; |
---|
99 | dstdata[4 * (y * w + x) + 1] |
---|
100 | = (srcdata[4 * (y * w + x) + 1] - min) * t; |
---|
101 | dstdata[4 * (y * w + x) + 2] |
---|
102 | = (srcdata[4 * (y * w + x) + 2] - min) * t; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | return dst; |
---|
108 | } |
---|
109 | |
---|