File: PREqualize.m

package info (click to toggle)
price.app 0.8.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,708 kB
  • ctags: 41
  • sloc: objc: 3,280; ansic: 264; makefile: 41
file content (190 lines) | stat: -rw-r--r-- 6,962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//  PREqualize.m
//  PRICE
//  Image level equalization
//
//  Created by Riccardo Mottola on Fri Dec 05 2003.
//  Copyright (c) 2003-2004 Carduus. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under the terms of the version 2 of the GNU General Public License as published by the Free Software Foundation.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

#import <math.h>
#import <limits.h>

#import "PREqualize.h"


@implementation PREqualize

- (PRImage *)equalizeImage:(PRImage *)srcImage
{
    NSBitmapImageRep *srcImageRep;
    PRImage          *destImage;
    NSBitmapImageRep *destImageRep;
    int              w, h;
    int              x, y;
    int              i;
    unsigned char    *srcData;
    unsigned char    *destData;
    int              bytesPerPixel;
    int              pixNum;
    BOOL             isColor;
    
    /* some trace */
    printf("levels: %d\n", UCHAR_MAX);
    
    /* get source image representation and associated information */
    srcImageRep = [srcImage tiffRep];
    
    w = [srcImageRep pixelsWide];
    h = [srcImageRep pixelsHigh];
    pixNum = h * w;
    printf("pixels: %d\n", pixNum);
    bytesPerPixel = [srcImageRep bitsPerPixel] /8;
    
    /* check bith depth and color/greyscale image */
    if ([srcImageRep hasAlpha])
    {
        if ([srcImageRep samplesPerPixel] == 2)
        {
            printf("Grayscale image\n");
            isColor = NO;
        }
        else
        {
            printf("Color image\n");
            isColor = YES;
        }
    }
    else
    {
        if ([srcImageRep samplesPerPixel] == 1)
        {
            printf("Grayscale image\n");
            isColor = NO;
        }
        else
        {
            printf("Color image\n");
            isColor = YES;
        }
    }
    
    /* allocate destination image and its representation */
    destImage = [[PRImage alloc] initWithSize:NSMakeSize(w, h)];
    if (isColor)
    {
        destImageRep = [[NSBitmapImageRep alloc]
                    initWithBitmapDataPlanes:NULL
                    pixelsWide:w
                    pixelsHigh:h
                    bitsPerSample:8
                    samplesPerPixel:3
                    hasAlpha:NO
                    isPlanar:NO
                    colorSpaceName:NSCalibratedRGBColorSpace
                    bytesPerRow:0
                    bitsPerPixel:0];
    } else
    {
        destImageRep = [[NSBitmapImageRep alloc]
                    initWithBitmapDataPlanes:NULL
                    pixelsWide:w
                    pixelsHigh:h
                    bitsPerSample:8
                    samplesPerPixel:1
                    hasAlpha:NO
                    isPlanar:NO
                    colorSpaceName:NSCalibratedWhiteColorSpace
                    bytesPerRow:0
                    bitsPerPixel:0];
    }
    srcData = [srcImageRep bitmapData];
    destData = [destImageRep bitmapData];
    
    if (isColor)
    {
        unsigned long int histogramDenormR[UCHAR_MAX+1]; /* not normalized pixel count for each level */
        unsigned long int histogramDenormG[UCHAR_MAX+1]; /* not normalized pixel count for each level */
        unsigned long int histogramDenormB[UCHAR_MAX+1]; /* not normalized pixel count for each level */
        float histogramR[UCHAR_MAX+1];                   /* normalized histogram */
        float histogramG[UCHAR_MAX+1];                   /* normalized histogram */
        float histogramB[UCHAR_MAX+1];                   /* normalized histogram */
        float cumulativeHistogramR[UCHAR_MAX+1];         /* cumulative histogram */
        float cumulativeHistogramG[UCHAR_MAX+1];         /* cumulative histogram */
        float cumulativeHistogramB[UCHAR_MAX+1];         /* cumulative histogram */
        
        /* calculate the histogram */
        for (i = 0; i <= UCHAR_MAX; i++)
            histogramDenormR[i] = histogramDenormG[i] = histogramDenormB[i] =  0;
        for (y = 0; y < h; y++)
            for (x = 0; x < w*3; x += 3)
            {
                histogramDenormR[srcData[y*(w*3) + x]]++;
                histogramDenormG[srcData[y*(w*3) + x + 1]]++;
                histogramDenormB[srcData[y*(w*3) + x + 2]]++;
            }
    
        /* normalize histogram */
        for (i = 0; i <= UCHAR_MAX; i++)
        {
            histogramR[i] = (float)histogramDenormR[i] / (float)pixNum;
            histogramG[i] = (float)histogramDenormG[i] / (float)pixNum;
            histogramB[i] = (float)histogramDenormB[i] / (float)pixNum;
        }
        
        /* cumulative histogram */
        cumulativeHistogramR[0] = histogramR[0];
        cumulativeHistogramG[0] = histogramG[0];
        cumulativeHistogramB[0] = histogramB[0];
        for (i = 1; i <= UCHAR_MAX; i++)
        {
            cumulativeHistogramR[i] = cumulativeHistogramR[i-1] + histogramR[i];
            cumulativeHistogramG[i] = cumulativeHistogramG[i-1] + histogramG[i];
            cumulativeHistogramB[i] = cumulativeHistogramB[i-1] + histogramB[i];
        }
        
        /* equalize */
        for (y = 0; y < h; y++)
            for (x = 0; x < w*3; x += 3)
            {
                destData[y*(w*3) + x] = floor((UCHAR_MAX+0.9)*cumulativeHistogramR[srcData[y*(w*3) + x]]);
                destData[y*(w*3) + x + 1] = floor((UCHAR_MAX+0.9)*cumulativeHistogramG[srcData[y*(w*3) + x + 1]]);
                destData[y*(w*3) + x + 2] = floor((UCHAR_MAX+0.9)*cumulativeHistogramB[srcData[y*(w*3) + x + 2]]);
            }
    } else
    {
        unsigned long int histogramDenorm[UCHAR_MAX+1]; /* not normalized pixel count for each level */
        float histogram[UCHAR_MAX+1];                   /* normalized histogram */
        float cumulativeHistogram[UCHAR_MAX+1];         /* cumulative histogram */
        /* calculate the histogram */
        for (i = 0; i <= UCHAR_MAX; i++)
            histogramDenorm[i] = 0;
        for (y = 0; y < h; y++)
            for (x = 0; x < w; x++)
                histogramDenorm[srcData[y*w + x]]++;
    
        /* normalize histogram */
        for (i = 0; i <= UCHAR_MAX; i++)
            histogram[i] = (float)histogramDenorm[i] / (float)pixNum;
        
        /* cumulative histogram */
        cumulativeHistogram[0] = histogram[0];
        for (i = 1; i <= UCHAR_MAX; i++)
            cumulativeHistogram[i] = cumulativeHistogram[i-1] + histogram[i];
        
        /* equalize */
        for (y = 0; y < h; y++)
            for (x = 0; x < w; x++)
                destData[y*w + x] = floor((UCHAR_MAX+0.9)*cumulativeHistogram[srcData[y*w + x]]);
    }
    
    [destImage addRepresentation:destImageRep];
    [destImageRep release];
    [destImage autorelease];
    return destImage;
}


@end