File: MyDocument.m

package info (click to toggle)
price.app 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 1,824 kB
  • sloc: objc: 8,414; ansic: 192; makefile: 18
file content (558 lines) | stat: -rw-r--r-- 20,510 bytes parent folder | download | duplicates (3)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//
//  MyDocument.m
//  PRICE
//
//  Created by Riccardo Mottola on Thu Dec 12 2002.
//  Copyright (c) 2002-2015 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 "MyDocument.h"
#include <limits.h>

#import "PRGrayscaleFilter.h"
#import "PRInvert.h"
#import "PRConvolve55.h"
#import "PRTransforms.h"
#import "PRFourier.h"
#import "PRDFTLowPass.h"
#import "PRDFTHighPass.h"
#import "PREqualize.h"
#import "PRTraceEdges.h"
#import "PRCustTraceEdges.h"
#import "PRMedian.h"
#import "PRScale.h"
#import "PRCrop.h"
#import "PRBriCon.h"
#import "PRCurves.h"


/* changeSaveType is an undocument API call, not exported in AppKit headers */
@interface NSDocument(Hidden)
- (void)changeSaveType:(id)sender;
@end


@implementation MyDocument


- (NSData *)dataRepresentationOfType:(NSString *)aType
{
  NSData *dataOfRep;
  NSDictionary *repProperties;

  dataOfRep = nil;
  if ([aType isEqualToString:@"TIFF"] || [aType isEqualToString:@"public.tiff"])
    {
      NSLog(@"data representation of type TIFF");
      repProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:NSTIFFCompressionLZW] forKey:NSImageCompressionMethod];
      dataOfRep = [[activeImage bitmapRep] representationUsingType: NSTIFFFileType properties:repProperties];
    }
  else if ([aType isEqualToString:@"JPEG"]  || [aType isEqualToString:@"public.jpeg"])
    {
      float level;

      level = [windowController compressionLevel];
      NSLog(@"data representation of type JPEG, %f", level);
      repProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:level] forKey:NSImageCompressionFactor];
      dataOfRep = [[activeImage bitmapRep] representationUsingType: NSJPEGFileType properties:repProperties];
    }
    return dataOfRep;
}

- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
  PRImage *tempImage;
    
  tempImage = [[PRImage alloc] initWithData:data];
  if (tempImage != nil)
    {
      NSBitmapImageRep *tmpImageRep;
      BOOL convertColorSpace;
      BOOL convertPlanar;
      NSMutableDictionary *imgProps;

#if !defined (GNUSTEP) &&  (MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_3)
      /* since 10.4 we have different Alpha format position, let's convert it */
      tmpImageRep = [tempImage bitmapRep];
      if ([tmpImageRep bitmapFormat] != 0)
        {
          NSInteger x, y;
          NSInteger w, h;
          BOOL alphaFirst;
          BOOL nonPremultipliedA;
          BOOL floatingPoint;
          PRImage *destImage;
          NSBitmapImageRep *destImageRep;
          NSInteger           srcBytesPerRow;
          NSInteger           destBytesPerRow;
          NSInteger           srcBytesPerPixel;
          NSInteger           destBytesPerPixel;
          

          NSLog(@"We have a non-standard format, let's try to convert it");
          alphaFirst = [tmpImageRep bitmapFormat] & NSAlphaFirstBitmapFormat;
          nonPremultipliedA = [tmpImageRep bitmapFormat] & NSAlphaNonpremultipliedBitmapFormat;
          floatingPoint = [tmpImageRep bitmapFormat] & NSFloatingPointSamplesBitmapFormat;
          if ([tmpImageRep bitsPerSample] == 8)
            {
              unsigned char    *srcData;
              unsigned char    *destData;
              unsigned char    *p1;
              unsigned char    *p2;

              /* swap Alpha is hopefully only for chunky images */
              if (alphaFirst)
                {
                  imgProps = [[NSMutableDictionary alloc] init];
                  [imgProps setValue:[tmpImageRep valueForProperty:NSImageCompressionMethod] forKey:NSImageCompressionMethod];
                  [imgProps setValue:[tmpImageRep valueForProperty:NSImageCompressionFactor] forKey:NSImageCompressionFactor];
                  [imgProps setValue:[tmpImageRep valueForProperty:NSImageEXIFData] forKey:NSImageEXIFData];

                  w = [tmpImageRep pixelsWide];
                  h = [tmpImageRep pixelsHigh];

                  srcBytesPerRow = [tmpImageRep bytesPerRow];
                  srcBytesPerPixel = [tmpImageRep bitsPerPixel] / 8;
                  destImage = [[PRImage alloc] initWithSize:NSMakeSize(w, h)];
                  destImageRep = [[NSBitmapImageRep alloc]
                                  initWithBitmapDataPlanes:NULL
                                                pixelsWide:w
                                                pixelsHigh:h
                                             bitsPerSample:8
                                           samplesPerPixel:[tmpImageRep samplesPerPixel]
                                                  hasAlpha:[tmpImageRep hasAlpha]
                                                  isPlanar:NO
                                            colorSpaceName:[tmpImageRep colorSpaceName]
                                               bytesPerRow:0
                                              bitsPerPixel:0];
                  
                  destBytesPerRow = [destImageRep bytesPerRow];
                  destBytesPerPixel = [destImageRep bitsPerPixel] / 8;
                  srcData = [tmpImageRep bitmapData];
                  destData = [destImageRep bitmapData];
                  if (![tempImage hasColor])
                    {
                      for (y = 0; y < h; y++)
                        for (x = 0; x < w; x++)
                          {
                            p1 = srcData + srcBytesPerRow * y + srcBytesPerPixel * x;
                            p2 = destData + destBytesPerRow * y + destBytesPerPixel * x;
                            p2[0] = p1[1];
                            p2[1] = p1[0];
                          }
                    }
                  else
                    {
                      for (y = 0; y < h; y++)
                        for (x = 0; x < w; x++)
                          {
                            p1 = srcData + srcBytesPerRow * y + srcBytesPerPixel * x;
                            p2 = destData + destBytesPerRow * y + destBytesPerPixel * x;
                            p2[0] = p1[1];
                            p2[1] = p1[2];
                            p2[2] = p1[3];
                            p2[3] = p1[0];
                          }
                    }
                  [destImageRep setProperty:NSImageEXIFData withValue:[imgProps objectForKey:NSImageEXIFData]];
                  [destImage setBitmapRep:destImageRep];
                  [destImageRep release];
                  [tempImage release];
                  tempImage = destImage;
                  [imgProps release];
                }
            }
          else /* for 16 bit */
            {
            }
        }
#endif /* Image format conversion */

      /* if the loaded image is in BlackColorSpace we convert it to WhiteColorSpace */
      /* which is the only TIFF rep used internally and generated by PRICE */
      /* we also need to convert Planar images into a meshed configuration */
      tmpImageRep = [tempImage bitmapRep];

      imgProps = [[NSMutableDictionary alloc] init];
      [imgProps setValue:[tmpImageRep valueForProperty:NSImageCompressionMethod] forKey:NSImageCompressionMethod];
      [imgProps setValue:[tmpImageRep valueForProperty:NSImageCompressionFactor] forKey:NSImageCompressionFactor];
      [imgProps setValue:[tmpImageRep valueForProperty:NSImageEXIFData] forKey:NSImageEXIFData];

        
      NSLog(@"Properties: %@", imgProps);
      convertColorSpace = [[tmpImageRep colorSpaceName] isEqualToString: NSCalibratedBlackColorSpace] || [[tmpImageRep colorSpaceName] isEqualToString: NSDeviceBlackColorSpace];
      convertPlanar = [tmpImageRep isPlanar];
      
      if (convertColorSpace || convertPlanar)
        {
          unsigned char *dataPtr;
          unsigned char *dataPtr2;
          NSInteger k;
          NSInteger w, h;
          PRImage *newImage;
          NSBitmapImageRep *newImageRep;
          NSInteger destSamplesPerPixel;

          
          NSLog(@"Converting color space");
          w = [tmpImageRep pixelsWide];
          h = [tmpImageRep pixelsHigh];
          destSamplesPerPixel = [tmpImageRep samplesPerPixel];
          /* converting the colorspace */
          newImage = [[PRImage alloc] initWithSize:NSMakeSize(w, h)];
          newImageRep = [[NSBitmapImageRep alloc]
                          initWithBitmapDataPlanes:NULL
                                        pixelsWide:w
                                        pixelsHigh:h
                                     bitsPerSample:8
                                   samplesPerPixel:destSamplesPerPixel
                                          hasAlpha:NO
                                          isPlanar:NO
                                    colorSpaceName:NSCalibratedWhiteColorSpace
                                       bytesPerRow:w*destSamplesPerPixel
                                      bitsPerPixel:0];
          dataPtr = [tmpImageRep bitmapData];
          dataPtr2 = [newImageRep bitmapData];
          if (convertPlanar)
            {
              NSInteger x, y;
              NSInteger xp;
              if (convertColorSpace)
                {
                  for (y = 0; y < h; y++)
                    {
                      xp = 0;
                      for (x = 0; x < w*3; x += 3)
                        {
                          dataPtr2[y*(w*3) + x] = UCHAR_MAX - dataPtr[y*w + x];
                          dataPtr2[y*(w*3) + x + 1] = UCHAR_MAX - dataPtr[y*w*2 + x];
                          dataPtr2[y*(w*3) + x + 2] = UCHAR_MAX - dataPtr[y*w*3 + x];
                          xp++;
                        }
                    }                            
                }
              else
                {
                  for (y = 0; y < h; y++)
                    {
                      xp = 0;
                      for (x = 0; x < w*3; x += 3)
                        {
                          dataPtr2[y*(w*3) + x] = dataPtr[y*w + x];
                          dataPtr2[y*(w*3) + x + 1] = dataPtr[y*w*2 + x];
                          dataPtr2[y*(w*3) + x + 2] = dataPtr[y*w*3 + x];
                          xp++;
                        }
                    }
                }
            }
          else
            {
              if (convertColorSpace)
                {
                  NSInteger s;
                  
                  s = w * h;
                  for (k = 0; k < s; k++)
                    *dataPtr2++ = UCHAR_MAX - *dataPtr++;
                } else
                {
                  /* shall never happen */
                  NSLog(@"Internal error: tried to convert image when it wasn't necessary");
                }                
            }
          // FIXME: should we remove the color space from the EXIF data?
          [newImageRep setColorSpaceName:NSCalibratedWhiteColorSpace];
          [newImageRep setProperty:NSImageEXIFData withValue:[imgProps objectForKey:NSImageEXIFData]];
          [newImage setBitmapRep:newImageRep];
          [newImageRep release];
          [tempImage release];
          tempImage = newImage;
        }
      [imgProps release];
    }

    oldImage = nil;
    /* returns a bool to be able to know if loading was successul */
    if (tempImage != nil)
    {
        /* we setActiveImage won't set the image info yet (why?)  */
        [self setActiveImage:tempImage];
        [tempImage release];
        return YES;
    } else
        return NO;
}


- (void)makeWindowControllers
/* instantiate PRWindowController */
{
    windowController = [[PRWindowController alloc] initWithWindowNibName:@"PRWindow"];
    [self addWindowController:windowController];
    
    /* set undo levels */
    [[self undoManager] setLevelsOfUndo:1];
}

- (NSWindow *)window
{
    return [windowController window];
}

- (NSView *)view
{
    return [windowController view];
}

- (PRImage *)activeImage
/* method to access the active image */
{
    return activeImage;
}

- (void)setActiveImage: (PRImage *)theImage
/* method to set the active image */
{
    if (activeImage != nil)
        [activeImage release];

    activeImage = [theImage retain];
    NSLog(@"set active, per pixel: %d %d", [[activeImage bitmapRep] bitsPerSample], [[activeImage bitmapRep] bitsPerPixel]);

    /* window controller is still nil here the first time we load an image
     * thus the info must be manually set after the nib finished loading */
    [windowController setImageToDraw:activeImage];
}

- (void)copy:(id)sender
{
    NSPasteboard *pboard;

    pboard = [NSPasteboard generalPasteboard];

    [pboard declareTypes:[NSArray arrayWithObjects:NSTIFFPboardType, nil] owner:nil];
    [pboard setData:[activeImage TIFFRepresentation] forType:NSTIFFPboardType];
}

- (void)paste:(id)sender
{
    NSUndoManager *uMgr;
    NSPasteboard  *pboard;
    NSString      *type;
    NSData        *tempData;
    PRImage       *tempImage;

    pboard = [NSPasteboard generalPasteboard];
    type = [pboard availableTypeFromArray:[NSArray arrayWithObjects:NSTIFFPboardType, nil]];

    if (type != nil)
    {
        if ([type isEqualToString:NSTIFFPboardType])
        {
            /* get the clipboard data */
            tempData = [pboard dataForType:NSTIFFPboardType];
            if (tempData != nil)
            {
                uMgr = [self undoManager];
                /* save the method on the undo stack */
                [[uMgr prepareWithInvocationTarget: self] restoreLastImage];
                [uMgr setActionName:@"Paste"];

                /* save the current image */
                [self saveCurrentImage];

                tempImage = [[PRImage alloc] initWithData:tempData];
                [self setActiveImage: tempImage];
                [tempImage release];
                [[windowController view] setFrameSize:[activeImage size]];
                [[windowController view] setNeedsDisplay:YES];
            } else
            {
                /* guidelines say I should put a panel */
                /* #### fixme */
                NSLog(@"something went wrong in paste");
            }
        } else
            NSLog(@"received a paste of unhandled type: %@", type);
    }
}

- (void)runFilter:(PRFilter *)filter with:(NSArray *)parameters
{
    NSUndoManager *uMgr;
    PRCProgress   *filterProgr;
    NSMutableDictionary *imgProps;
    id tempVal;
    NSLog(@"before running filter, per pixel: %d %d", [[activeImage bitmapRep] bitsPerSample], [[activeImage bitmapRep] bitsPerPixel]);

    uMgr = [self undoManager];
    /* save the method on the undo stack */
    [[uMgr prepareWithInvocationTarget: self] restoreLastImage];
    [uMgr setActionName:[filter actionName]];
    
    filterProgr = nil;
    if ([filter displayProgress])
    {
        filterProgr = [[PRCProgress alloc] init];
        [filterProgr showProgress:self];
        [filterProgr setTitle: [filter actionName]];
    }
    
    /* save the current image */
    [self saveCurrentImage];
    
    /* save image properties */
    imgProps = [[NSMutableDictionary alloc] init];
    tempVal = [[activeImage bitmapRep] valueForProperty:NSImageCompressionMethod];
    if (tempVal)
      [imgProps setObject:tempVal forKey:NSImageCompressionMethod];
    tempVal = [[activeImage bitmapRep] valueForProperty:NSImageCompressionFactor];
    if (tempVal)
      [imgProps setObject:tempVal forKey:NSImageCompressionFactor];
    tempVal = [[activeImage bitmapRep] valueForProperty:NSImageEXIFData];
    if (tempVal)
      [imgProps setObject:tempVal forKey:NSImageEXIFData];
    NSLog(@"beforeFilter: %@", [[activeImage bitmapRep] valueForProperty:NSImageEXIFData]);

    /* instantiate and run the filter */
    [self setActiveImage: [filter filterImage: activeImage with:parameters progressPanel:filterProgr]];
    
    /* reset image properties */
    if ([imgProps objectForKey:NSImageEXIFData] != nil)
      {
        NSMutableDictionary *exifDict;

        exifDict = [NSMutableDictionary dictionaryWithDictionary:[imgProps objectForKey:NSImageEXIFData]];
        NSLog(@"we have EXIF Data: %@", exifDict);
        if ([exifDict objectForKey:@"PixelXDimension"])
          {
            NSNumber *w = [NSNumber numberWithInt:[activeImage width]];
            [exifDict setObject:w forKey:@"PixelXDimension"];
          }
        if ([exifDict objectForKey:@"PixelYDimension"])
          {
            NSNumber *w = [NSNumber numberWithInt:[activeImage height]];
            [exifDict setObject:w forKey:@"PixelYDimension"];
          }
        NSLog(@"New EXIF Data: %@", exifDict);
        [imgProps setObject:exifDict forKey:NSImageEXIFData];
      }
    [[activeImage bitmapRep] setProperty:NSImageEXIFData withValue:[imgProps objectForKey:NSImageEXIFData]];
    [imgProps release];
    NSLog(@"afterFilter: %@", [[activeImage bitmapRep] valueForProperty:NSImageEXIFData]);

    [filterProgr release];
    
    /* reset the selected zoom ration, this will also cause a view update */
    [windowController scaleFromMenu:nil];
}

- (void)restoreLastImage
{
    PRImage *tempImage;
    
    tempImage = [activeImage copy];
    [self setActiveImage: oldImage];
    [oldImage release];
    oldImage = tempImage;
    [[[self undoManager] prepareWithInvocationTarget: self] restoreLastImage];
    
    [windowController scaleFromMenu:nil];
}

- (void)saveCurrentImage
{
    if (activeImage != nil)
    {
        if (oldImage != nil)
            [oldImage release];
      NSLog(@"save active, per pixel: %d %d", [[activeImage bitmapRep] bitsPerSample], [[activeImage bitmapRep] bitsPerPixel]);
        oldImage = [activeImage copy];
        NSLog(@"saved, per pixel: %d %d", [[oldImage bitmapRep] bitsPerSample], [[oldImage bitmapRep] bitsPerPixel]);

    }
}

- (void)dealloc
{
    [activeImage release];
    [windowController release];
    [super dealloc];
}


- (void)setPrintInfo:(NSPrintInfo *)anObject
{
    if (printInfo != anObject)
    {
        [printInfo autorelease];
        printInfo = [anObject copyWithZone:[self zone]];
    }
}

- (NSPrintInfo *)printInfo
{
    if (printInfo == nil)
    {
        [self setPrintInfo:[NSPrintInfo sharedPrintInfo]];
        [printInfo setHorizontallyCentered:YES];
        [printInfo setVerticallyCentered:YES];
        [printInfo setLeftMargin:5.0];
        [printInfo setRightMargin:5.0];
        [printInfo setTopMargin:5.0];
        [printInfo setBottomMargin:5.0];
    }
    return printInfo;
}


- (void)printShowingPrintPanel:(BOOL)showPanels
{
    NSPrintOperation *op = [NSPrintOperation printOperationWithView:[windowController view] printInfo:[self printInfo]];
    [op setShowPanels:showPanels];
    [op runOperationModalForWindow:[self window] delegate:nil didRunSelector:NULL contextInfo:NULL];
}

/* file panel methods */

/* undocumented API call which works fine for our purpose to intercept
   the change of the filetype and update the view information accordingly */
- (void)changeSaveType:(id)sender
{
  NSLog(@"MyDocument changeSaveType");
  [super changeSaveType:sender];
  
  [windowController changeSaveType:sender];
}


/* we override this for GNUstep */
#if defined(GNUSTEP_GUI_VERSION) && GNU_GNUSTEP_GUI_MAJOR_VERSION == 0 && GNUSTEP_GUI_MINOR_VERSION <= 22
- (NSInteger) runModalSavePanel: (NSSavePanel*)savePanel withAccessoryView: (NSView*)accessoryView

{
  NSLog(@"runModalSavePanel: withAccessoryView. We should see this only on GS gui <= 0.22");
  [windowController prepareSavePanel: savePanel];
  [windowController setWritableFileTypes:[MyDocument writableTypes]];
  [windowController setCompressionType:[self fileType]];

  /* we finally call super, but reget the accessory view since we changed it */
  return [super runModalSavePanel:savePanel withAccessoryView:[savePanel accessoryView]];
}
#endif

/* we override this for Cocoa */
- (BOOL) prepareSavePanel:(NSSavePanel *) panel
{
  BOOL r;

  r = [windowController prepareSavePanel: panel];
  [windowController setWritableFileTypes:[MyDocument writableTypes]];
  [windowController setCompressionType:[self fileType]];
  return r;
}

@end