当前位置: 首页>>代码示例>>C++>>正文


C++ CGImageGetWidth函数代码示例

本文整理汇总了C++中CGImageGetWidth函数的典型用法代码示例。如果您正苦于以下问题:C++ CGImageGetWidth函数的具体用法?C++ CGImageGetWidth怎么用?C++ CGImageGetWidth使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了CGImageGetWidth函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Clear

bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
{
    Clear();

    if ((pBuf == NULL) || (nSize == 0))
        return false;

    Handle picHandle = NewHandle( nSize );
    memcpy( *picHandle, pBuf, nSize );
    m_pictHandle = picHandle;
    CGImageRef cgImageRef = 0;

    CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) pBuf, nSize, kCFAllocatorNull);
    CGImageSourceRef source = CGImageSourceCreateWithData( data, NULL );
    if ( source )
    {
        cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
        CFRelease( source );
    }
    CFRelease( data );

    if ( cgImageRef )
    {
        m_bitmap.Create( CGImageGetWidth(cgImageRef)  , CGImageGetHeight(cgImageRef) );
        CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef)  , CGImageGetHeight(cgImageRef) );
        // since our context is upside down we dont use CGContextDrawImage
        wxMacDrawCGImage( (CGContextRef) m_bitmap.GetHBITMAP() , &r, cgImageRef ) ;
        CGImageRelease(cgImageRef);
        cgImageRef = NULL;
    }

    return m_bitmap.IsOk();
}
开发者ID:lukesingh24,项目名称:wxWidgets,代码行数:33,代码来源:dataobj.cpp

示例2: UNUSED_PARAM

CGImageRef ImageSource::createFrameAtIndex(size_t index, float* scale)
{
    UNUSED_PARAM(scale);

    if (!initialized())
        return 0;

#if !PLATFORM(IOS)
    UNUSED_PARAM(scale);
    RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata)));
#else
    // Subsampling can be 1, 2 or 3, which means quarter-, sixteenth- and sixty-fourth-size, respectively.
    // A zero or negative value means no subsampling.
    int subsampling = scale ? static_cast<int>(log2f(1.0f / std::max(0.1f, std::min(1.0f, *scale)))) : -1;
    RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata, subsampling)));

    // <rdar://problem/7371198> - CoreGraphics changed the default caching behaviour in iOS 4.0 to kCGImageCachingTransient
    // which caused a performance regression for us since the images had to be resampled/recreated every time we called
    // CGContextDrawImage. We now tell CG to cache the drawn images. See also <rdar://problem/14366755> -
    // CoreGraphics needs to un-deprecate kCGImageCachingTemporary since it's still not the default.
#if COMPILER(CLANG)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
    CGImageSetCachingFlags(image.get(), kCGImageCachingTemporary);
#if COMPILER(CLANG)
#pragma clang diagnostic pop
#endif
    if (scale) {
        if (subsampling > 0)
            *scale = static_cast<float>(CGImageGetWidth(image.get())) / size(DoNotRespectImageOrientation).width();
        else {
            ASSERT(static_cast<int>(CGImageGetWidth(image.get())) == size(DoNotRespectImageOrientation).width());
            *scale = 1;
        }
    }
#endif // !PLATFORM(IOS)
    CFStringRef imageUTI = CGImageSourceGetType(m_decoder);
    static const CFStringRef xbmUTI = CFSTR("public.xbitmap-image");
    if (!imageUTI || !CFEqual(imageUTI, xbmUTI))
        return image.leakRef();
    
    // If it is an xbm image, mask out all the white areas to render them transparent.
    const CGFloat maskingColors[6] = {255, 255,  255, 255, 255, 255};
    RetainPtr<CGImageRef> maskedImage = adoptCF(CGImageCreateWithMaskingColors(image.get(), maskingColors));
    if (!maskedImage)
        return image.leakRef();

    return maskedImage.leakRef();
}
开发者ID:MYSHLIFE,项目名称:webkit,代码行数:50,代码来源:ImageSourceCG.cpp

示例3: DrawUsingCGImage

void DrawUsingCGImage( void )
{
	OSErr  err = noErr;
	Handle hOpenTypeList = NewHandle(0);
	long   numTypes = 0;
	FSSpec theFSSpec;
	Rect   bounds = { 45, 10, 100, 100 };
	GraphicsImportComponent importer = 0;
    CGImageRef imageRef = 0;
    CGContextRef context = NULL;
    CGRect rect;
	
	BuildGraphicsImporterValidFileTypes( hOpenTypeList, &numTypes );
	HLock( hOpenTypeList );

	err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL);
	DisposeHandle( hOpenTypeList );
	if ( err ) return;
	
	// locate and open a graphics importer component which can be used to draw the
	// selected file. If a suitable importer is not found the ComponentInstance
	// is set to NULL.
	err = GetGraphicsImporterForFile( &theFSSpec,	// specifies the file to be drawn
									  &importer );	// pointer to the returned GraphicsImporterComponent
                                      
	window = NewCWindow( NULL, &bounds, "\pDraw Using CGImage", false, documentProc, (WindowPtr)-1, true, 0);
    
    // import the image as a CGImage
    err = GraphicsImportCreateCGImage( importer, &imageRef, kGraphicsImportCreateCGImageUsingCurrentSettings );
    if (err) return;
    
    SizeWindow( window, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), false );
    ShowWindow(window);
    
    // create a Core Graphics Context from the window port
	err = QDBeginCGContext(GetWindowPort(window), &context);
	if (err) return;

    // make a rectangle designating the location and dimensions in user space of the bounding box in which to draw the image
    rect = CGRectMake( 0, 0, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ) );
    
    // draw the image
    CGContextDrawImage( context, rect, imageRef );
    
    // end the the context we had for the port
	QDEndCGContext(GetWindowPort(window), &context);
	
	// close the importer instance
	CloseComponent( importer );
}
开发者ID:fruitsamples,项目名称:ImproveYourImage,代码行数:50,代码来源:DrawUsingCGImage.c

示例4: PoofItGood

void	PoofItGood( Point centerPt )
{
	CGRect				box;
	WindowRef			window;
	Rect				bounds;
	CGContextRef		context;
	CGImageRef			image;
	float				windowWidth;
	float				windowHeight;
	int					i;
	
	image = GetThePoofImage();
	if ( image == NULL ) goto Bail;

	windowWidth		= CGImageGetWidth( image ) / NUMBER_OF_POOF_ANIM_FRAMES;
	windowHeight	= CGImageGetHeight( image );
	
	// Start our animation bounds at the first item in the animation strip
	box.origin.x	= box.origin.y	= 0;
	box.size.width	= CGImageGetWidth( image );
	box.size.height	= CGImageGetHeight( image );

	bounds.top		= centerPt.v - (SInt16)(windowHeight / 2);
	bounds.left		= centerPt.h - (SInt16)(windowWidth / 2);
	bounds.bottom	= bounds.top + (SInt16)windowHeight;
	bounds.right	= bounds.left + (SInt16)windowWidth;
	
	CreateNewWindow( kOverlayWindowClass, 0, &bounds, &window );

	CreateCGContextForPort( GetWindowPort( window ), &context );
	ShowWindow( window );

	for ( i = 1; i <= NUMBER_OF_POOF_ANIM_FRAMES; i++ )
	{
		CGContextClearRect( context, box );
		CGContextDrawImage( context, box, image );
		CGContextFlush( context );
		
		Delay( EventTimeToTicks( POOF_ANIMATION_DELAY ), NULL );
		box.origin.x -= windowWidth;
	}
	
	CGContextRelease( context );
	CGImageRelease( image );
	DisposeWindow( window );
	
Bail:
	return;
}
开发者ID:fruitsamples,项目名称:WindowFun,代码行数:49,代码来源:WindowFun.c

示例5: CreateBitmapContextForImage

DataSourceSurfaceCG::DataSourceSurfaceCG(CGImageRef aImage)
{
  mFormat = SurfaceFormat::B8G8R8A8;
  mImage = aImage;
  mCg = CreateBitmapContextForImage(aImage);
  if (mCg == nullptr) {
    // error creating context
    return;
  }

  // Get image width, height. We'll use the entire image.
  CGFloat w = CGImageGetWidth(aImage);
  CGFloat h = CGImageGetHeight(aImage);
  CGRect rect = {{0,0},{w,h}};

  // Draw the image to the bitmap context. Once we draw, the memory
  // allocated for the context for rendering will then contain the
  // raw image data in the specified color space.
  CGContextDrawImage(mCg, rect, aImage);

  // Now we can get a pointer to the image data associated with the bitmap
  // context.
  mData = CGBitmapContextGetData(mCg);
  assert(mData);
}
开发者ID:Belxjander,项目名称:tenfourfox,代码行数:25,代码来源:SourceSurfaceCG.cpp

示例6: CGImageGetWidth

bool GiCanvasIos::drawImage(CGImageRef image, const Point2d& centerM, bool autoScale)
{
    CGContextRef context = m_draw->getContext();
    bool ret = false;
    
    if (context && image) {
        Point2d ptD = centerM * m_draw->xf().modelToDisplay();
        float w = CGImageGetWidth(image);
        float h = CGImageGetHeight(image);
        
        if (autoScale) {
            w *= m_draw->xf().getViewScale();
            h *= m_draw->xf().getViewScale();
        }
        
        CGAffineTransform af = CGAffineTransformMake(1, 0, 0, -1, 0, m_draw->height());
        af = CGAffineTransformTranslate(af, ptD.x - w * 0.5f, 
                                        m_draw->height() - (ptD.y + h * 0.5f));
        
        CGContextConcatCTM(context, af);
        CGContextDrawImage(context, CGRectMake(0, 0, w, h), image);
        CGContextConcatCTM(context, CGAffineTransformInvert(af));
        ret = true;
    }
    
    return ret;
}
开发者ID:huangzongwu,项目名称:touchvg,代码行数:27,代码来源:ioscanvas.cpp

示例7: CGImageRelease

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;
}
开发者ID:AlexandreFreitas,项目名称:danfreve-blinkdetection,代码行数:33,代码来源:grfmt_imageio.cpp

示例8: DrawTheMTView

//-----------------------------------------------------------------------------------
static void DrawTheMTView(CGContextRef ctx, MTViewData* data)
{
    CGRect dstRect;

#if CG_COORDINATES
    TransformHIViewToCG(ctx, data->theView);
#endif

    // Draw the image first, before stroking the path; otherwise the path gets overwritten
    if (data->theImage != NULL)
    {
	dstRect = CGRectMake(0, 0, CGImageGetWidth(data->theImage), CGImageGetHeight(data->theImage));
#if CG_COORDINATES
	CGContextDrawImage(ctx, dstRect, data->theImage);
#else
	HIViewDrawCGImage(ctx, &dstRect, data->theImage);
#endif
    }
    
    if (data->thePath != NULL)
    {
	CGPathApply(data->thePath, (void*)ctx, MyCGPathApplier);
	CGContextStrokePath(ctx);
    }
}   // DrawTheMTView
开发者ID:fruitsamples,项目名称:MouseTracking,代码行数:26,代码来源:MouseTrackingView-CG.c

示例9: SkCreateBitmapFromCGImage

bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleToFit) {
    const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth(image));
    const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHeight(image));
    SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);

    SkBitmap tmp;
    if (!tmp.allocPixels(info)) {
        return false;
    }

    if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), image)) {
        return false;
    }

    CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image);
    switch (cgInfo) {
        case kCGImageAlphaNone:
        case kCGImageAlphaNoneSkipLast:
        case kCGImageAlphaNoneSkipFirst:
            SkASSERT(SkBitmap::ComputeIsOpaque(tmp));
            tmp.setAlphaType(kOpaque_SkAlphaType);
            break;
        default:
            // we don't know if we're opaque or not, so compute it.
            if (SkBitmap::ComputeIsOpaque(tmp)) {
                tmp.setAlphaType(kOpaque_SkAlphaType);
            }
    }

    *dst = tmp;
    return true;
}
开发者ID:venkatarajasekhar,项目名称:Qt,代码行数:32,代码来源:SkCreateCGImageRef.cpp

示例10: CGImageToUncompressedImage

static UncompressedImage
CGImageToUncompressedImage(CGImageRef image)
{
  if (image == nullptr)
    return UncompressedImage();
  
  size_t width = CGImageGetWidth(image);
  size_t height = CGImageGetHeight(image);
  
  if ((0 == width) || (0 == height))
    return UncompressedImage();
  
  size_t bits_per_pixel = CGImageGetBitsPerPixel(image);
  size_t bits_per_component = CGImageGetBitsPerComponent(image);
  CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
  
  size_t row_size;
  UncompressedImage::Format format;
  CGColorSpaceRef bitmap_colorspace;
  CGBitmapInfo bitmap_info;
  
  if ((8 == bits_per_pixel) &&
      (8 == bits_per_component) &&
      (CGColorSpaceGetModel(colorspace) == kCGColorSpaceModelMonochrome)) {
    row_size = width;
    format = UncompressedImage::Format::GRAY;
    static CGColorSpaceRef grey_colorspace = CGColorSpaceCreateDeviceGray();
    bitmap_colorspace = grey_colorspace;
    bitmap_info = 0;
  } else {
    static CGColorSpaceRef rgb_colorspace = CGColorSpaceCreateDeviceRGB();
    bitmap_colorspace = rgb_colorspace;
    if ((24 == bits_per_pixel) && (8 == bits_per_component)) {
      row_size = width * 3;
      format = UncompressedImage::Format::RGB;
      bitmap_info = kCGBitmapByteOrder32Big;
    } else {
      row_size = width * 4;
      format = UncompressedImage::Format::RGBA;
      bitmap_info = kCGImageAlphaPremultipliedLast |
                    kCGBitmapByteOrder32Big;
    }
  }
  
  std::unique_ptr<uint8_t[]> uncompressed(new uint8_t[height * row_size]);
  
  CGContextRef bitmap = CGBitmapContextCreate(uncompressed.get(), width, height,
                                              8, row_size, bitmap_colorspace,
                                              bitmap_info);
  if (nullptr == bitmap) {
    return UncompressedImage();
  }

  AtScopeExit(bitmap) { CFRelease(bitmap); };

  CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), image);
  
  return UncompressedImage(format, row_size, width, height,
                           std::move(uncompressed));
}
开发者ID:Exadios,项目名称:xcsoar-exp,代码行数:60,代码来源:ImageDecoder.cpp

示例11: frameAtIndex

void BitmapImage::checkForSolidColor()
{
    m_checkedForSolidColor = true;
    if (frameCount() > 1)
        m_isSolidColor = false;
    else {
        CGImageRef image = frameAtIndex(0);
        
        // Currently we only check for solid color in the important special case of a 1x1 image.
        if (image && CGImageGetWidth(image) == 1 && CGImageGetHeight(image) == 1) {
            unsigned char pixel[4]; // RGBA
            CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
            CGContextRef bmap = CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), space,
                kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
            if (bmap) {
                GraphicsContext(bmap).setCompositeOperation(CompositeCopy);
                CGRect dst = { {0, 0}, {1, 1} };
                CGContextDrawImage(bmap, dst, image);
                if (pixel[3] == 0)
                    m_solidColor = Color(0, 0, 0, 0);
                else
                    m_solidColor = Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]);
                m_isSolidColor = true;
                CFRelease(bmap);
            } 
            CFRelease(space);
        }
    }
}
开发者ID:boyliang,项目名称:ComponentSuperAccessor,代码行数:29,代码来源:ImageCG.cpp

示例12: CFDataCreateWithBytesNoCopy

void GL::Image::load(const unsigned char *buf, size_t bufSize)
{
    CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8*)buf, (CFIndex)bufSize, kCFAllocatorNull);
    if (data != NULL) {
        CGImageSourceRef imageSource = CGImageSourceCreateWithData(data, NULL);
        if (imageSource != NULL) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
            if (img != NULL) {
                width_ = (int)CGImageGetWidth(img);
                height_ = (int)CGImageGetHeight(img);
                CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                if (colorSpace != NULL) {
                    char *texData = (char*)calloc(width_ * height_ * 4, sizeof(char));
                    CGContextRef ctx = CGBitmapContextCreate(texData, width_, height_, 8, width_ * 4, colorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst);
                    if (ctx != NULL) {
                        CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, width_, height_), img);
                        CGContextRelease(ctx);
                        loadTextureData_(texData);
                    }
                    free(texData);
                    CGColorSpaceRelease(colorSpace);
                }
                CGImageRelease(img);
            }
            CFRelease(imageSource);
        }
        CFRelease(data);
    }
}
开发者ID:MaddTheSane,项目名称:Glypha,代码行数:29,代码来源:GLImage_CoreImage.cpp

示例13: Image

BitmapImage::BitmapImage(CGImageRef cgImage, ImageObserver* observer)
    : Image(observer)
    , m_currentFrame(0)
    , m_frames(0)
    , m_frameTimer(0)
    , m_repetitionCount(cAnimationNone)
    , m_repetitionCountStatus(Unknown)
    , m_repetitionsComplete(0)
    , m_isSolidColor(false)
    , m_checkedForSolidColor(false)
    , m_animationFinished(true)
    , m_allDataReceived(true)
    , m_haveSize(true)
    , m_sizeAvailable(true)
    , m_decodedSize(0)
    , m_haveFrameCount(true)
    , m_frameCount(1)
{
    initPlatformData();
    
    CGFloat width = CGImageGetWidth(cgImage);
    CGFloat height = CGImageGetHeight(cgImage);
    m_decodedSize = width * height * 4;
    m_size = IntSize(width, height);

    m_frames.grow(1);
    m_frames[0].m_frame = cgImage;
    m_frames[0].m_hasAlpha = true;
    m_frames[0].m_haveMetadata = true;
    checkForSolidColor();
}
开发者ID:dslab-epfl,项目名称:warr,代码行数:31,代码来源:ImageCG.cpp

示例14: SkStreamToCGImageSource

bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
    CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);

    if (NULL == imageSrc) {
        return false;
    }
    SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);

    CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL);
    if (NULL == image) {
        return false;
    }
    SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image);

    const int width = SkToInt(CGImageGetWidth(image));
    const int height = SkToInt(CGImageGetHeight(image));

    bm->setInfo(SkImageInfo::MakeN32Premul(width, height));
    if (SkImageDecoder::kDecodeBounds_Mode == mode) {
        return true;
    }

    if (!this->allocPixelRef(bm, NULL)) {
        return false;
    }

    SkAutoLockPixels alp(*bm);

    if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), image)) {
        return false;
    }

    CGImageAlphaInfo info = CGImageGetAlphaInfo(image);
    switch (info) {
        case kCGImageAlphaNone:
        case kCGImageAlphaNoneSkipLast:
        case kCGImageAlphaNoneSkipFirst:
            SkASSERT(SkBitmap::ComputeIsOpaque(*bm));
            bm->setAlphaType(kOpaque_SkAlphaType);
            break;
        default:
            // we don't know if we're opaque or not, so compute it.
            if (SkBitmap::ComputeIsOpaque(*bm)) {
                bm->setAlphaType(kOpaque_SkAlphaType);
            }
    }
    if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) {
        // CGBitmapContext does not support unpremultiplied, so the image has been premultiplied.
        // Convert to unpremultiplied.
        for (int i = 0; i < width; ++i) {
            for (int j = 0; j < height; ++j) {
                uint32_t* addr = bm->getAddr32(i, j);
                *addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr);
            }
        }
        bm->setAlphaType(kUnpremul_SkAlphaType);
    }
    return true;
}
开发者ID:Adenilson,项目名称:skia,代码行数:59,代码来源:SkImageDecoder_CG.cpp

示例15: os_image_load_from_file

unsigned char* os_image_load_from_file(const char* filename, int* outWidth, int* outHeight, int* outChannels, int unused) {
  const int fileHandle = open(filename, O_RDONLY);
  struct stat statBuffer;
  fstat(fileHandle, &statBuffer);
  const size_t bytesInFile = (size_t)(statBuffer.st_size);
  uint8_t* fileData = (uint8_t*)(mmap(NULL, bytesInFile, PROT_READ, MAP_SHARED, fileHandle, 0));
  if (fileData == MAP_FAILED) {
    fprintf(stderr, "Couldn't open file '%s' with mmap\n", filename);
    return NULL;
  }
  CFDataRef fileDataRef = CFDataCreateWithBytesNoCopy(NULL, fileData, bytesInFile, kCFAllocatorNull);
  CGDataProviderRef imageProvider = CGDataProviderCreateWithCFData(fileDataRef);

  const char* suffix = strrchr(filename, '.');
  if (!suffix || suffix == filename) {
    suffix = "";
  }
  CGImageRef image;
  if (strcasecmp(suffix, ".png") == 0) {
    image = CGImageCreateWithPNGDataProvider(imageProvider, NULL, true, kCGRenderingIntentDefault);
  } else if ((strcasecmp(suffix, ".jpg") == 0) ||
    (strcasecmp(suffix, ".jpeg") == 0)) {
    image = CGImageCreateWithJPEGDataProvider(imageProvider, NULL, true, kCGRenderingIntentDefault);
  } else {
    munmap(fileData, bytesInFile);
    close(fileHandle);
    CFRelease(imageProvider);
    CFRelease(fileDataRef);
    fprintf(stderr, "Unknown suffix for file '%s'\n", filename);
    return NULL;
  }

  const int width = (int)CGImageGetWidth(image);
  const int height = (int)CGImageGetHeight(image);
  const int channels = 4;
  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  const int bytesPerRow = (width * channels);
  const int bytesInImage = (bytesPerRow * height);
  uint8_t* result = (uint8_t*)(malloc(bytesInImage));
  const int bitsPerComponent = 8;
  CGContextRef context = CGBitmapContextCreate(result, width, height,
    bitsPerComponent, bytesPerRow, colorSpace,
    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  CGColorSpaceRelease(colorSpace);
  CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
  CGContextRelease(context);
  CFRelease(image);

  munmap(fileData, bytesInFile);
  close(fileHandle);
  CFRelease(imageProvider);
  CFRelease(fileDataRef);

  *outWidth = width;
  *outHeight = height;
  *outChannels = channels;

  return result;
}
开发者ID:anshulnsit,项目名称:MDig,代码行数:59,代码来源:os_image_load.cpp


注:本文中的CGImageGetWidth函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。