2009-10-16 8 views
18

Ho un buffer di char senza segno RGB che vorrei convertire in un file bitmap, qualcuno sa come?Conversione di dati RGB in una bitmap in Objective-C++ Cocoa

mio galleggiante RGB è del seguente formato

R [(0,0)], G [(0,0)], B [(0,0)], R [(0,1) ], G [(0,1)], B [(0,1)], R [(0,2)], G [(0,2)], B [(0,2)] ....

I valori per ciascuna unità di dati vanno da 0 a 255. Qualcuno ha qualche idea su come posso fare questa conversione?

+0

puoi aiutarmi [qui] (http://stackoverflow.com/questions/28310186/cgimage-grab-and-create-pixel) per favore –

risposta

33

È possibile utilizzare CGBitmapContextCreate per creare un contesto bitmap dai dati non elaborati. Quindi puoi creare un CGImageRef dal contesto bitmap e salvarlo. Sfortunatamente CGBitmapContextCreate è un po 'schizzinoso sul formato dei dati. Non supporta i dati RGB a 24 bit. Il loop all'inizio trasforma i dati rgb in rgba con un valore alfa di zero alla fine. Devi includere e collegare con il framework ApplicationServices.

char* rgba = (char*)malloc(width*height*4); 
for(int i=0; i < width*height; ++i) { 
    rgba[4*i] = myBuffer[3*i]; 
    rgba[4*i+1] = myBuffer[3*i+1]; 
    rgba[4*i+2] = myBuffer[3*i+2]; 
    rgba[4*i+3] = 0; 
} 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba, 
    width, 
    height, 
    8, // bitsPerComponent 
    4*width, // bytesPerRow 
    colorSpace, 
    kCGImageAlphaNoneSkipLast); 

CFRelease(colorSpace); 

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext); 
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("image.png"), kCFURLPOSIXPathStyle, false); 

CFStringRef type = kUTTypePNG; // or kUTTypeBMP if you like 
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0); 

CGImageDestinationAddImage(dest, cgImage, 0); 

CFRelease(cgImage); 
CFRelease(bitmapContext); 
CGImageDestinationFinalize(dest); 
free(rgba); 
+0

nschmidt sei il migliore !!! hai così tanta conoscenza nel campo dell'elaborazione/elaborazione grafica. – ReachConnection

+0

Felice di poter aiutare :) – nschmidt

+0

Grazie mille. Ho cercato un codice come questo per giorni! – Twinkle

2

Prestito dal codice di nschmidt per produrre un familiare, se qualcuno con gli occhi rossi immagine:

int width = 11; 
int height = 8; 

Byte r[8][11]={ 
    {000,000,255,000,000,000,000,000,255,000,000}, 
    {000,000,000,255,000,000,000,255,000,000,000}, 
    {000,000,255,255,255,255,255,255,255,000,000}, 
    {000,255,255,255,255,255,255,255,255,255,000}, 
    {255,255,255,255,255,255,255,255,255,255,255}, 
    {255,000,255,255,255,255,255,255,255,000,255}, 
    {255,000,255,000,000,000,000,000,255,000,255}, 
    {000,000,000,255,255,000,255,255,000,000,000}}; 

Byte g[8][11]={ 
    {000,000,255,000,000,000,000,000,255,000,000}, 
    {000,000,000,255,000,000,000,255,000,000,000}, 
    {000,000,255,255,255,255,255,255,255,000,000}, 
    {000,255,255,000,255,255,255,000,255,255,000}, 
    {255,255,255,255,255,255,255,255,255,255,255}, 
    {255,000,255,255,255,255,255,255,255,000,255}, 
    {255,000,255,000,000,000,000,000,255,000,255}, 
    {000,000,000,255,255,000,255,255,000,000,000}}; 

Byte b[8][11]={ 
    {000,000,255,000,000,000,000,000,255,000,000}, 
    {000,000,000,255,000,000,000,255,000,000,000}, 
    {000,000,255,255,255,255,255,255,255,000,000}, 
    {000,255,255,000,255,255,255,000,255,255,000}, 
    {255,255,255,255,255,255,255,255,255,255,255}, 
    {255,000,255,255,255,255,255,255,255,000,255}, 
    {255,000,255,000,000,000,000,000,255,000,255}, 
    {000,000,000,255,255,000,255,255,000,000,000}}; 

char* rgba = (char*)malloc(width*height*4); 
int offset=0; 
for(int i=0; i < height; ++i) 
{ 
    for (int j=0; j < width; j++) 
    { 
     rgba[4*offset] = r[i][j]; 
     rgba[4*offset+1] = g[i][j]; 
     rgba[4*offset+2] = b[i][j]; 
     rgba[4*offset+3] = 0; 
     offset ++; 
    } 
} 


CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmapContext = CGBitmapContextCreate(
                rgba, 
                width, 
                height, 
                8, // bitsPerComponent 
                4*width, // bytesPerRow 
                colorSpace, 
                kCGImageAlphaNoneSkipLast); 

CFRelease(colorSpace); 

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext); 

free(rgba); 

UIImage *newUIImage = [UIImage imageWithCGImage:cgImage]; 

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 11,8)]; 

[iv setImage:newUIImage]; 

Poi, addSubview:iv per ottenere l'immagine nella vostra vista e, naturalmente, fare l'obbligatorio [releases] a mantenere una casa pulita.

Problemi correlati