1//
2//  PRCrop.m
3//  PRICE
4//
5//  Created by Riccardo Mottola on Fri Jan 28 2005.
6//  Copyright (c) 2005-2014 Carduus. All rights reserved.
7//
8// 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.
9// 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.
10
11#include "PRCrop.h"
12
13@implementation PRCrop
14
15- (PRImage *)filterImage:(PRImage *)image with:(NSArray *)parameters progressPanel:(PRCProgress *)progressPanel
16{
17    int pixTop;
18    int pixBottom;
19    int pixRight;
20    int pixLeft;
21
22    /* interpret the parameters */
23    pixTop = [[parameters objectAtIndex:0] intValue];
24    pixBottom = [[parameters objectAtIndex:1] intValue];
25    pixLeft = [[parameters objectAtIndex:2] intValue];
26    pixRight = [[parameters objectAtIndex:3] intValue];
27
28    return [self cropImage:image :pixTop :pixBottom :pixLeft :pixRight];
29}
30
31- (NSString *)actionName
32{
33    return @"Crop";
34}
35
36- (PRImage *)cropImage :(PRImage *)srcImage :(int)pixTop :(int)pixBottom :(int)pixLeft :(int)pixRight
37{
38  NSBitmapImageRep *srcImageRep;
39  PRImage *destImage;
40  NSBitmapImageRep *destImageRep;
41  NSInteger origW, origH;
42  NSInteger newW, newH;
43  NSInteger x, y;
44  NSInteger i;
45  unsigned char *srcData;
46  unsigned char *destData;
47  NSInteger srcSamplesPerPixel;
48  NSInteger destSamplesPerPixel;
49  register NSInteger srcBytesPerPixel;
50  register NSInteger destBytesPerPixel;
51  register NSInteger srcBytesPerRow;
52  register NSInteger destBytesPerRow;
53
54    /* some trace */
55    NSLog(@"top: %d left:%d right:%d bottom:%d", pixTop, pixLeft, pixRight, pixBottom);
56
57
58    /* get source image representation and associated information */
59    srcImageRep = [srcImage bitmapRep];
60    origW = [srcImageRep pixelsWide];
61    origH = [srcImageRep pixelsHigh];
62    srcBytesPerRow = [srcImageRep bytesPerRow];
63    srcSamplesPerPixel = [srcImageRep samplesPerPixel];
64    destSamplesPerPixel = srcSamplesPerPixel;
65    srcBytesPerPixel = [srcImageRep bitsPerPixel] / 8;
66
67    newW = origW - pixLeft - pixRight;
68    newH = origH - pixTop - pixBottom;
69
70    /* allocate destination image and its representation */
71    destImage = [[PRImage alloc] initWithSize:NSMakeSize(newW, newH)];
72    destImageRep = [[NSBitmapImageRep alloc]
73                     initWithBitmapDataPlanes:NULL
74                                   pixelsWide:newW
75                                   pixelsHigh:newH
76                                bitsPerSample:[srcImageRep bitsPerSample]
77                              samplesPerPixel:destSamplesPerPixel
78                                     hasAlpha:[srcImageRep hasAlpha]
79                                     isPlanar:NO
80                               colorSpaceName:[srcImageRep colorSpaceName]
81                                  bytesPerRow:0
82                                 bitsPerPixel:0];
83    srcData = [srcImageRep bitmapData];
84    destData = [destImageRep bitmapData];
85    destBytesPerRow = [destImageRep bytesPerRow];
86    destBytesPerPixel = [destImageRep bitsPerPixel] / 8;
87
88
89    for (y = 0; y < newH; y++)
90      for (x = 0; x < newW; x++)
91        for (i = 0; i < srcSamplesPerPixel; i++)
92          {
93            NSInteger sX, sY;
94
95            sX = x + pixLeft;
96            sY = y + pixTop;
97            if ((sX >= 0 && sX < origW) && (sY >= 0 && sY < origH))
98              {
99                destData[destBytesPerRow * y + destBytesPerPixel * x + i] = srcData[srcBytesPerRow * sY + srcBytesPerPixel * sX + i];
100              }
101            else
102              {
103                destData[destBytesPerRow * y + destBytesPerPixel * x + i] = 0;
104              }
105          }
106    [destImage setBitmapRep:destImageRep];
107    [destImageRep release];
108    [destImage autorelease];
109    return destImage;
110}
111
112@end
113