File: PRGrayscaleFilter.m

package info (click to toggle)
price.app 0.8.3%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,808 kB
  • ctags: 46
  • sloc: objc: 3,480; ansic: 284; makefile: 41
file content (96 lines) | stat: -rw-r--r-- 3,190 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
//
//  PRGrayscaleFilter.m
//  PRICE
//
//  Created by Riccardo Mottola on Mon Dec 23 2002.
//  Copyright (c) 2002-2007 Carduus. All rights reserved.
//
// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
// 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 "PRGrayscaleFilter.h"
#import <math.h>


@implementation PRGrayscaleFilter

- (PRImage *)filterImage:(PRImage *)srcImage :(int)method
{
    NSBitmapImageRep *srcImageRep;
    PRImage          *destImage;
    NSBitmapImageRep *destImageRep;
    int              w, h;
    int              x, y;
    unsigned char    *srcData;
    unsigned char    *destData;
    unsigned char    *p1;
    int              bytesPerPixel;
    
    
    /* get source image representation and associated information */
    srcImageRep = [srcImage tiffRep];
    
    w = [srcImageRep pixelsWide];
    h = [srcImageRep pixelsHigh];
    bytesPerPixel = [srcImageRep bitsPerPixel] /8;
    
    /* if the image is already greyscale... */
    if ([srcImageRep hasAlpha])
    {
        if ([srcImageRep samplesPerPixel] == 2)
            return srcImage;
    }
    else
    {
        if ([srcImageRep samplesPerPixel] == 1)
            return srcImage;
    }
    
    /* allocate destination image and its representation */
    destImage = [[PRImage alloc] initWithSize:NSMakeSize(w, h)];
    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 (method == METHOD_AVERAGE)
    {
        /* execute the actual filtering (R+G+B)/3 */
    for (y = 0; y < h; y++)
        for (x = 0; x < w; x++)
        {
            p1 = srcData + bytesPerPixel * (y * w + x);
            destData[y*w + x] = (unsigned char)rint((p1[0] + p1[1] + p1[2]) / 3);
        }
    } else if (method == METHOD_LUMINANCE)
    {
        /* execute the actual filtering
         * JPEG-YCbCr (601) from "digital 8-bit R'G'B'  "
         * Y' =       + 0.299    * R'd + 0.587    * G'd + 0.114    * B'd
         */
        for (y = 0; y < h; y++)
            for (x = 0; x < w; x++)
            {
                p1 = srcData + bytesPerPixel * (y * w + x);
                destData[y*w + x] = (unsigned char)rintf(0.2990f*p1[0] + 0.5870f*p1[1] + 0.1140f*p1[2]);
            }
                
    }
    [destImage addRepresentation:destImageRep];
    [destImageRep release];
    [destImage autorelease];
    return destImage;
}

@end