2009-10-16 16 views
18

J'ai un buffer de caractère non signé RGB que je voudrais convertir en un fichier bitmap, est-ce que quelqu'un sait comment?Conversion de données RVB en bitmap dans Objective-C++ Cocoa

Mon flotteur RGB est le format suivant

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)] ....

Les valeurs de chaque unité de données vont de 0 à 255. Quelqu'un at-il des idées sur la façon dont je peux procéder à cette conversion?

+0

pouvez-vous m'aider [ici] (http://stackoverflow.com/questions/28310186/cgimage-grab-and-create-pixel) s'il vous plaît –

Répondre

33

Vous pouvez utiliser CGBitmapContextCreate pour créer un contexte bitmap à partir de vos données brutes. Ensuite, vous pouvez créer un CGImageRef à partir du contexte bitmap et l'enregistrer. Malheureusement CGBitmapContextCreate est un peu difficile sur le format des données. Il ne prend pas en charge les données RVB 24 bits. La boucle au début fait basculer les données rgb vers rgba avec une valeur alpha de zéro à la fin. Vous devez inclure et lier avec le 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 vous êtes le meilleur !!! vous avez tellement de connaissances dans le domaine de l'informatique graphique/traitement. – ReachConnection

+0

Content de pouvoir aider :) – nschmidt

+0

Merci beaucoup. J'ai cherché un code comme celui-ci pendant des jours! – Twinkle

2

emprunts à partir du code de nschmidt pour produire un familier, si quelqu'un l'image aux yeux rouges:

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]; 

Ensuite, addSubview:iv pour obtenir l'image dans votre point de vue et, bien sûr, faire le [releases] obligatoire garder une maison propre.