Affine Transform, Bezier paths, cropping

  • Ok... I'm really lost. I cant seem to grasp Bezier paths and
    transform and such. I am adapting the cropping image example from
    apple to allow me to select an area in an image and then crop it.
    http://developer.apple.com/samplecode/Cropped_Image/index.html

    Everything works out except I have to make a code adjustment because
    as the example is, when you crop the image, you are returned an image
    of the same size as the original which is empty save for the area you
    selected. So in the existing code I had to adjust the new image to be
    the same size as the selection. The problem is in the path
    translation from the larger to the new smaller image. I get very off
    results from the cropping. And I don't know how to tell the system to
    adjust the coordinates for the smaller image. Mostly I am getting
    something way off center to the left. Below is the code which
    isolates the selected area and then returns the new image.

    - (NSImage *) croppedImage
      // Returns an autoreleased NSImage, consisting of the selected
    portion of the reciever's image.
      // If there's no selection, this method will return the original
    image.
    {
    NSRect
         sourceImageRect = [[self cell] rectCoveredByImageInBounds:[self
    bounds]],
      newImageBounds = NSIntersectionRect([selectionMarker selectedRect],
    sourceImageRect);

    if (!NSIsEmptyRect(newImageBounds))
      {
      NSImage
      //*newImage = [[NSImage alloc] initWithSize:sourceImageRect.size];
    (original code)
      *newImage =[[NSImage alloc] initWithSize:newImageBounds.size];(My
    change)
      NSAffineTransform
      *pathAdjustment = [NSAffineTransform transform];

      NSBezierPath
      *croppingPath = [selectionMarker selectedPath];


      //[pathAdjustment translateXBy: -NSMinX(sourceImageRect) yBy: -
    NSMinY(sourceImageRect)]; (original code)
      [pathAdjustment translateXBy: -NSMinX(newImageBounds) yBy: -NSMinY
    (newImageBounds)]; (my change)
      Above is where I know my error is I just don't understand how to
    tell it to adjust the selection for the difference between the source
    image and the new smaller image ( I tried addition, subtraction
    formulas but I must not have tried the right one.) And what I have
    found on the development site does not clear this up for me.

      croppingPath = [pathAdjustment transformBezierPath:[selectionMarker
    selectedPath]];

      [newImage lockFocus];
      if (!shouldAntiAlias)
      [[NSGraphicsContext currentContext] setShouldAntialias:NO];
      [[NSColor blackColor] set];
      [croppingPath fill];
      [[self image] compositeToPoint:NSZeroPoint
    operation:NSCompositeSourceIn];
      [newImage unlockFocus];

      return [newImage autorelease];
      }
    return [self image];
    }

    I hope that some one might be able to offer some insight here,
    Thanks
previous month october 2007 next month
MTWTFSS
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        
Go to today