Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 153 lines (120 sloc) 4.999 kb
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
// TODO:
// * change ifDifferent... to ignore format. Only the size, to implement...
// * forcing format of the output image (needed for laplace which requires output to be 16 or 32 bits)

#import "QCCVAbstractInputImageProcessingPlugIn.h"

@implementation QCCVAbstractInputImageProcessingPlugIn

@dynamic inputImage;
@dynamic outputImage;

+ (QCPlugInExecutionMode) executionMode {
return kQCPlugInExecutionModeProcessor;
}

+ (QCPlugInTimeMode) timeMode {
return kQCPlugInTimeModeNone;
}

- (id) init {
  if (self = [super init]) {
    outputIplImageProvider = [[QCCVIplImageProvider alloc] initWithIplImageReference: &outputIplImage];
  }
  return self;
}

- (BOOL) ifDifferentUpdateInputIplImageWithInputImage: (id<QCPlugInInputImageSource>) image {
  BOOL didChange = NO;
  if (![QCCVPlugIn isIplImage: inputIplImage sameSizeAndFormatAsInputImageSource: image]) {
    if (inputIplImage != NULL)
      cvReleaseImageHeader(&inputIplImage);
    inputIplImage = [QCCVPlugIn createIplImageHeaderWithInputImageSource: image];
    
    // Recreate image intensity if we're using it as well
    if (useImageIntensity) {
      if (inputIplImageIntensity != NULL)
        cvReleaseImage(&inputIplImageIntensity);
      inputIplImageIntensity = cvCreateImage(cvSize(inputIplImage->width, inputIplImage->height), 8, 1);
    }
    
    didChange = YES;
  }
  if (inputIplImage)
    inputIplImage->imageData = (char *)[image bufferBaseAddress];
  return didChange;
}

- (BOOL) ifDifferentUpdateOutputIplImageWithCloneOfInputImage: (id<QCPlugInInputImageSource>) image {
  BOOL didChange = NO;
  if (![QCCVPlugIn isIplImage: outputIplImage sameSizeAndFormatAsInputImageSource: image]) {
    if (outputIplImage != NULL)
      cvReleaseImage(&outputIplImage);
    outputIplImage = [QCCVPlugIn cloneIplImageWithInputImageSource: image];
    
    // Recreate output for image intensity if we're using it
    if (useImageIntensity) {
      if (outputIplImageIntensity != NULL)
        cvReleaseImage(&outputIplImageIntensity);
      outputIplImageIntensity = cvCreateImage(cvSize(inputIplImage->width, inputIplImage->height), 8, 1);
    }
    
    didChange = YES;
  }
  return didChange;
}

@end

@implementation QCCVAbstractInputImageProcessingPlugIn (ImageProcessingExecution)

- (BOOL) executeImageProcessingWith: (id<QCPlugInContext>) context atTime: (NSTimeInterval) time withArguments: (NSDictionary*) arguments {
  return YES;
}

@end


@implementation QCCVAbstractInputImageProcessingPlugIn (Execution)

- (BOOL) startExecution: (id<QCPlugInContext>) context {
return YES;
}

- (void) enableExecution: (id<QCPlugInContext>) context {
}

- (BOOL) execute: (id<QCPlugInContext>) context atTime: (NSTimeInterval) time withArguments: (NSDictionary *) arguments {
  BOOL r = NO;
  if ([self didValueForInputKeyChange: @"inputImage"]) {
    id<QCPlugInInputImageSource> inputImage_ = self.inputImage;
    if (inputImage_ == nil) {
      [context logMessage: @"Input image is nil"];
      r = YES;
    } else {
      
      NSString *pixelFormat;
      CGColorSpaceRef colorSpace = [inputImage_ imageColorSpace];
      if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
        pixelFormat = QCPlugInPixelFormatI8;
      } else if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelRGB) {
#if __BIG_ENDIAN__
        pixelFormat = QCPlugInPixelFormatARGB8;
#else
        pixelFormat = QCPlugInPixelFormatBGRA8;
#endif
      } else {
        [context logMessage: @"Unable to get proper pixel format to lock buffer representation"];
        return NO;
      }
      
      if (![inputImage_ lockBufferRepresentationWithPixelFormat: pixelFormat
                                                     colorSpace: [inputImage_ imageColorSpace]
                                                      forBounds: [inputImage_ imageBounds]]) {
        [context logMessage:
         @"Can't lock buffer representation with pixel format %@, color space %@ and image bounds %@",
         [inputImage_ bufferPixelFormat],
         [inputImage_ imageColorSpace],
         [inputImage_ imageBounds]];
        r = NO;
      } else {
        
        [self ifDifferentUpdateInputIplImageWithInputImage: inputImage_];
        
        // Update image intensity if we're using it
        if (useImageIntensity)
          [QCCVPlugIn convertRGBA: inputIplImage toGRAY: inputIplImageIntensity];
        
        [self ifDifferentUpdateOutputIplImageWithCloneOfInputImage: inputImage_];
        
        //NSLog(@" * %p %p", inputIplImage, outputIplImage);
        
        r = [self executeImageProcessingWith: context atTime: time withArguments: arguments];
        
        if (r == NO) {
          [context logMessage: @"OpenCV patch says NO!", inputImage_];
        }
        
        [inputImage_ unlockBufferRepresentation];
      }
    }
  } else {
    r = YES;
  }
return r;
}

- (void) disableExecution:(id<QCPlugInContext>)context {
}

- (void) stopExecution:(id<QCPlugInContext>)context {
}

@end
Something went wrong with that request. Please try again.