bool ImageIODecoder::readHeader()
{
    CFURLRef         imageURLRef;
    CGImageSourceRef sourceRef;
    // diciu, if ReadHeader is called twice in a row make sure to release the previously allocated imageRef
    if (imageRef != NULL)
        CGImageRelease(imageRef);
    imageRef = NULL;

    imageURLRef = CFURLCreateFromFileSystemRepresentation( NULL,
        (const UInt8*)m_filename.c_str(), m_filename.size(), false );

    sourceRef = CGImageSourceCreateWithURL( imageURLRef, NULL );
    CFRelease( imageURLRef );
    if ( !sourceRef )
        return false;

    imageRef = CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL );
    CFRelease( sourceRef );
    if( !imageRef )
        return false;

    m_width = CGImageGetWidth( imageRef );
    m_height = CGImageGetHeight( imageRef );

    CGColorSpaceRef colorSpace = CGImageGetColorSpace( imageRef );
    if( !colorSpace )
        return false;

    m_type = CGColorSpaceGetNumberOfComponents( colorSpace ) > 1 ? CV_8UC3 : CV_8UC1;

    return true;
}
Пример #2
0
CGColorTransformRef CGColorTransformCreate(CGColorSpaceRef space, CFDictionaryRef theDict)
{
	CGColorTransformRef colorTransform;
	CGColorTransformRef colTransformTmp;
	size_t numComponents;
	CGColorSpaceType spaceType;
	CFIndex extraBytes;
	CGColorSpaceRef space2;
	unsigned char* md5;

	if (!space || !CGColorSpaceSupportsOutput(space))
		return NULL;

	numComponents = CGColorSpaceGetNumberOfComponents(space);
	extraBytes = (numComponents <= 4) ? 328 : 16; // 0x148 : 0x10
	
	colorTransform = (CGColorTransformRef)_CFRuntimeCreateInstance(NULL, CGColorTransformGetTypeID(), extraBytes, NULL);
	if (!colorTransform)
		return NULL;

	spaceType = CGColorSpaceGetType(space);
	switch(spaceType)
	{
	case kCGColorSpaceTypeDeviceGray:
		space2 = CGColorSpaceCreateDisplayGray();
		break;
	case kCGColorSpaceTypeDeviceRGB:
		space2 = CGColorSpaceCreateDisplayRGB();
		break;
	case kCGColorSpaceTypeDeviceCMYK:
		space2 = CGColorSpaceCreateSystemDefaultCMYK();
		break;
	}

	if (space2)
	{
		pthread_mutex_lock(&cacheMutex);
		if (baseCache)
		{
			md5 = CGColorSpaceGetMD5Digest(space2);
			for (int i=0; i < CFArrayGetCount(baseCache); i++)
			{
				/*colTransformTmp = (CGColorTransformRef) CFArrayGetValueAtIndex(baseCache, i);
				 if (!memcmp((unsigned char *)CFArrayGetValueAtIndex(baseCache, i), md5, 16))
				 {

				 }*/
			}
		}
	}
	else
	{
		CFRelease(colorTransform);
		colorTransform = NULL;
	}

	return colorTransform;
}
Пример #3
0
uint32_t 
d2d_InitializeFormat(CGBitmapContextInfoRef bitmapContextInfo)
{
	size_t numOfComponents;

	numOfComponents = CGColorSpaceGetNumberOfComponents(bitmapContextInfo->colorspace);

	//if (bitmapContextInfo->bitmapInfo & kCGBitmapByteOrderMask)

	return ((uint32_t)0);
}
Пример #4
0
vl::Image
vl::ImageReader::readDimensions(const char * fileName)
{
  // intermediate buffer
  char unsigned * rgba = NULL ;

  // Core graphics
  CFURLRef url = NULL ;
  CGImageSourceRef imageSourceRef = NULL ;
  CGImageRef imageRef = NULL ;
  CGColorSpaceRef colorSpaceRef = NULL ;

  // initialize the image as null
  Image image ;
  image.width = 0 ;
  image.height = 0 ;
  image.depth = 0 ;
  image.memory = NULL ;
  image.error = 0 ;

  // get file
  url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)fileName, strlen(fileName), false) ;
  check(url) ;

  // get image source from file
  imageSourceRef = CGImageSourceCreateWithURL(url, NULL) ;
  check(imageSourceRef) ;

  // get image from image source
  imageRef = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, NULL);
  check(imageRef) ;

  colorSpaceRef = CGColorSpaceCreateDeviceRGB();
  check(colorSpaceRef) ;

  image.width = CGImageGetWidth(imageRef);
  image.height = CGImageGetHeight(imageRef);
  image.depth = CGColorSpaceGetNumberOfComponents(colorSpaceRef) ;
  check(image.depth == 1 || image.depth == 3) ;

done:
  if (colorSpaceRef) { CFRelease(colorSpaceRef) ; }
  if (imageRef) { CFRelease(imageRef) ; }
  if (imageSourceRef) { CFRelease(imageSourceRef) ; }
  if (url) { CFRelease(url) ; }
  return image ;
}
Пример #5
0
CGContextDelegateRef 
__CGBitmapContextDelegateCreate(CGBitmapContextInfoRef bitmapContextInfo, 
								CFDictionaryRef theDict)
{
	uint32_t format, depth;
	size_t numOfComponents;

	format = d2d_InitializeFormat(bitmapContextInfo);
	depth = D2DLayerDepthForFormat(format);
	
	if (format == -1 || depth == 0) {
		numOfComponents = CGColorSpaceGetNumberOfComponents(bitmapContextInfo->colorspace);
		CGPostError("Unsupported pixel description - %lu components, %lu bits-per-com",
			numOfComponents, bitmapContextInfo->bitsPerComponent);
		return NULL;
	}

	d2d_Initialize(NULL);

	return _ripc_globals->ctxDelegate;
}
Пример #6
0
vl::Image
vl::ImageReader::read(const char * fileName, float * memory)
{
  // intermediate buffer
  char unsigned * pixels = NULL ;
  int bytesPerPixel ;
  int bytesPerRow ;

  // Core graphics
  CGBitmapInfo bitmapInfo ;
  CFURLRef url = NULL ;
  CGImageSourceRef imageSourceRef = NULL ;
  CGImageRef imageRef = NULL ;
  CGContextRef contextRef = NULL ;
  CGColorSpaceRef sourceColorSpaceRef = NULL ;
  CGColorSpaceRef colorSpaceRef = NULL ;

  // initialize the image as null
  Image image ;
  image.width = 0 ;
  image.height = 0 ;
  image.depth = 0 ;
  image.memory = NULL ;
  image.error = 0 ;

  // get file
  url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)fileName, strlen(fileName), false) ;
  check(url) ;

  // get image source from file
  imageSourceRef = CGImageSourceCreateWithURL(url, NULL) ;
  check(imageSourceRef) ;

  // get image from image source
  imageRef = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, NULL);
  check(imageRef) ;

  sourceColorSpaceRef = CGImageGetColorSpace(imageRef) ;
  check(sourceColorSpaceRef) ;

  image.width = CGImageGetWidth(imageRef);
  image.height = CGImageGetHeight(imageRef);
  image.depth = CGColorSpaceGetNumberOfComponents(sourceColorSpaceRef) ;
  check(image.depth == 1 || image.depth == 3) ;

  // decode image to L (8 bits per pixel) or RGBA (32 bits per pixel)
  switch (image.depth) {
    case 1:
      colorSpaceRef = CGColorSpaceCreateDeviceGray();
      bytesPerPixel = 1 ;
      bitmapInfo = kCGImageAlphaNone ;
      break ;

    case 3:
      colorSpaceRef = CGColorSpaceCreateDeviceRGB();
      bytesPerPixel = 4 ;
      bitmapInfo = kCGImageAlphaPremultipliedLast || kCGBitmapByteOrder32Big ;
      /* this means
       pixels[0] = R
       pixels[1] = G
       pixels[2] = B
       pixels[3] = A
       */
      break ;

  }
  check(colorSpaceRef) ;

  bytesPerRow = image.width * bytesPerPixel ;
  pixels = (char unsigned*)malloc(image.height * bytesPerRow) ;
  check(pixels) ;

  contextRef = CGBitmapContextCreate(pixels,
                                     image.width, image.height,
                                     8, bytesPerRow,
                                     colorSpaceRef,
                                     bitmapInfo) ;
  check(contextRef) ;

  CGContextDrawImage(contextRef, CGRectMake(0, 0, image.width, image.height), imageRef);

  // copy pixels to MATLAB format
  if (memory == NULL) {
    image.memory = (float*)malloc(image.height * image.width * image.depth * sizeof(float)) ;
    check(image.memory) ;
  } else {
    image.memory = memory ;
  }
  switch (image.depth) {
    case 3:
      vl::impl::imageFromPixels<impl::pixelFormatRGBA>(image, pixels, image.width * bytesPerPixel) ;
      break ;
    case 1:
      vl::impl::imageFromPixels<impl::pixelFormatL>(image, pixels, image.width * bytesPerPixel) ;
      break ;
  }

done:
  if (pixels) { free(pixels) ; }
  if (contextRef) { CFRelease(contextRef) ; }
  if (colorSpaceRef) { CFRelease(colorSpaceRef) ; }
  if (imageRef) { CFRelease(imageRef) ; }
  if (imageSourceRef) { CFRelease(imageSourceRef) ; }
  if (url) { CFRelease(url) ; }
  return image ;
}
Пример #7
0
CGFloat *decodeValuesFromImageDictionary(CGPDFDictionaryRef dict, CGColorSpaceRef cgColorSpace, int bitsPerComponent) {
    CGFloat *decodeValues = NULL;
    CGPDFArrayRef decodeArray = NULL;
	
    if (CGPDFDictionaryGetArray(dict, "Decode", &decodeArray)) {
        size_t count = CGPDFArrayGetCount(decodeArray);
        decodeValues = malloc(sizeof(CGFloat) * count);
        CGPDFReal realValue;
        int i;
        for (i = 0; i < count; i++) {
            CGPDFArrayGetNumber(decodeArray, i, &realValue);
            decodeValues[i] = realValue;
        }
    } else {
        size_t n;
		int i;
        switch (CGColorSpaceGetModel(cgColorSpace)) {
            case kCGColorSpaceModelMonochrome:
                decodeValues = malloc(sizeof(CGFloat) * 2);
                decodeValues[0] = 0.0;
                decodeValues[1] = 1.0;
                break;
            case kCGColorSpaceModelRGB:
                decodeValues = malloc(sizeof(CGFloat) * 6);
                for (i = 0; i < 6; i++) {
                    decodeValues[i] = i % 2 == 0 ? 0 : 1;
                }
                break;
            case kCGColorSpaceModelCMYK:
                decodeValues = malloc(sizeof(CGFloat) * 8);
                for (i = 0; i < 8; i++) {
                    decodeValues[i] = i % 2 == 0 ? 0.0 :
                    1.0;
                }
                break;
            case kCGColorSpaceModelLab:
                // ????
                break;
            case kCGColorSpaceModelDeviceN:
                n =
                CGColorSpaceGetNumberOfComponents(cgColorSpace) * 2;
                decodeValues = malloc(sizeof(CGFloat) * (n *
                                                         2));
				i = 0;
                for (; i < n; i++) {
                    decodeValues[i] = i % 2 == 0 ? 0.0 :
                    1.0;
                }
                break;
            case kCGColorSpaceModelIndexed:
                decodeValues = malloc(sizeof(CGFloat) * 2);
                decodeValues[0] = 0.0;
                decodeValues[1] = pow(2.0,(double)bitsPerComponent) - 1;
                break;
            default:
                break;
        }
    }
	
    return (CGFloat *)CFMakeCollectable(decodeValues);
}
Пример #8
0
void myOperator_Do(CGPDFScannerRef s, void *info)
{
    // Check to see if this is an image or not.
    const char *name;
    CGPDFObjectRef xobject;
    CGPDFDictionaryRef dict;
    CGPDFStreamRef stream;
    CGPDFContentStreamRef cs = CGPDFScannerGetContentStream(s);
    
    // The Do operator takes a name. Pop the name off the
    // stack. If this fails then the argument to the 
    // Do operator is not a name and is therefore invalid!
    if(!CGPDFScannerPopName(s, &name)){
		fprintf(stderr, "Couldn't pop name off stack!\n"); 
		return;
    }
    // Get the resource with type "XObject" and the name
    // obtained from the stack.
    xobject = CGPDFContentStreamGetResource(cs, "XObject", name);
    if(!xobject){
		fprintf(stderr, "Couldn't get XObject with name %s\n", name);
		return;
    }

    // An XObject must be a stream so obtain the value from the xobject
    // as if it were a stream. If this fails, the PDF is malformed.
    if (!CGPDFObjectGetValue(xobject, kCGPDFObjectTypeStream, &stream)){
		fprintf(stderr, "XObject '%s' is not a stream!\n", name);
		return;
    }
    // Streams consist of a dictionary and the data associated
    // with the stream. This code only cares about the dictionary.
    dict = CGPDFStreamGetDictionary(stream);
    if(!dict){
		fprintf(stderr, 
			"Couldn't obtain dictionary from stream %s!\n", name);
		return;
    }
    // An XObject dict has a Subtype that indicates what kind it is.
    if(!CGPDFDictionaryGetName(dict, "Subtype", &name)){
		fprintf(stderr, "Couldn't get SubType of dictionary object!\n");
		return;
    }
	
	
    // This code is interested in the "Image" Subtype of an XObject.
    // Check whether this object has Subtype of "Image".
	printf("%s\n",name);
    if(strcmp(name, "Image") != 0){
		// The Subtype is not "Image" so this must be a form 
		// or other type of XObject.
		return;
    } else {
		CGPDFArrayRef colorSpaceArray;
		CGPDFDictionaryGetArray(dict,"ColorSpace" ,&colorSpaceArray);
		CGColorSpaceRef colorSpace=NULL;
		colorSpace=colorSpaceFromPDFArray(colorSpaceArray);
		
		CGPDFDataFormat format;
		const char *name = NULL, *colorSpaceName = NULL,*renderingIntentName = NULL;;
		CFDataRef data=CGPDFStreamCopyData(stream,&format);
		if (format == CGPDFDataFormatRaw){
			CGColorSpaceRef cgColorSpace;
			CGPDFInteger width, height, bps, spp;
			CGColorRenderingIntent renderingIntent;
			CGPDFBoolean interpolation = 0;
			CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data);
			
			if (!CGPDFDictionaryGetInteger(dict, "Width", &width))
				return ;
			
			if (!CGPDFDictionaryGetInteger(dict, "Height", &height))
				return ;
			
			if (!CGPDFDictionaryGetInteger(dict, "BitsPerComponent", &bps))
				return ;
			
			if (!CGPDFDictionaryGetBoolean(dict, "Interpolate", &interpolation))
				interpolation = 0;
			
			if (!CGPDFDictionaryGetName(dict, "Intent", &renderingIntentName))
				renderingIntent = kCGRenderingIntentDefault;
			else{
				renderingIntent = kCGRenderingIntentDefault;
				//      renderingIntent = renderingIntentFromName(renderingIntentName);
			}
			
			if (CGPDFDictionaryGetArray(dict, "ColorSpace", &colorSpaceArray)) {
				cgColorSpace = CGColorSpaceCreateDeviceRGB();
				//      cgColorSpace = colorSpaceFromPDFArray(colorSpaceArray);
				spp = CGColorSpaceGetNumberOfComponents(cgColorSpace);
			} else if (CGPDFDictionaryGetName(dict, "ColorSpace", &colorSpaceName)) {
				if (strcmp(colorSpaceName, "DeviceRGB") == 0) {
					cgColorSpace = CGColorSpaceCreateDeviceRGB();
					//          CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
					spp = 3;
				} else if (strcmp(colorSpaceName, "DeviceCMYK") == 0) {     
					cgColorSpace = CGColorSpaceCreateDeviceCMYK();
					//          CGColorSpaceCreateWithName(kCGColorSpaceGenericCMYK);
					spp = 4;
				} else if (strcmp(colorSpaceName, "DeviceGray") == 0) {
					cgColorSpace = CGColorSpaceCreateDeviceGray();
					//          CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
					spp = 1;
				} else if (bps == 1) { // if there's no colorspace entry, there's still one we can infer from bps
					cgColorSpace = CGColorSpaceCreateDeviceGray();
					//          colorSpace = NSDeviceBlackColorSpace;
					spp = 1;
				}
			}
			 CGFloat *decodeValues = NULL;
			decodeValues = decodeValuesFromImageDictionary(dict, cgColorSpace, bps);
			
			int rowBits = bps * spp * width;
			int rowBytes = rowBits / 8;
			// pdf image row lengths are padded to byte-alignment
			if (rowBits % 8 != 0)
				++rowBytes;
			CGImageRef sourceImage = CGImageCreate(width, height, bps, bps * spp, rowBytes, cgColorSpace, 0, dataProvider, decodeValues, interpolation, renderingIntent);
			CGDataProviderRelease(dataProvider);
			
			
			CGDataProviderRef dataProvider2 = CGImageGetDataProvider(sourceImage);
			CFDataRef data = CGDataProviderCopyData(dataProvider2);
			int fd;
			findex;
			char file[256];
			memset(file,0,sizeof(file));
			sprintf(file,"%d.jpg",findex);	   
			fd=open(file, O_RDWR|O_CREAT);
			write(fd, CFDataGetBytePtr(data), CFDataGetLength(data));
			findex++;
			close(fd);
			
			//[[NSImage alloc] initWithCGImage:sourceImage size:NSMakeSize(0, 0)];
		}else {
			int fd;
			findex;
			char file[256];
			memset(file,0,sizeof(file));
			sprintf(file,"%d.jpg",findex);	   
			fd=open(file, O_RDWR|O_CREAT);
			write(fd, CFDataGetBytePtr(data), CFDataGetLength(data));
			findex++;
			close(fd);
		}


		
	}

    
    // This is an Image so figure out what variety of image it is.
    checkImageType(dict, (MyDataScan *)info);
    
}