2009-10-16 8 views
18

tengo una memoria intermedia de RGB unsigned char que me gustaría convertir en un archivo de mapa de bits, ¿alguien sabe cómo?la conversión de los datos RGB en un mapa de bits en Objective-C++ cacao

Mi flotador RGB es de la siguiente 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)] .... .

Los valores para cada unidad de datos varía de 0 a 255. alguien tiene alguna idea de cómo puedo ir sobre la fabricación de esta conversión?

+0

me puedes ayudar [aquí] (http://stackoverflow.com/questions/28310186/cgimage-grab-and-create-pixel) –

Respuesta

33

Puede utilizar CGBitmapContextCreate para hacer un contexto de mapa de bits de datos sin procesar. Luego puede crear un CGImageRef a partir del contexto del mapa de bits y guardarlo. Lamentablemente CGBitmapContextCreate es un poco exigente con el formato de los datos. No admite datos RGB de 24 bits. El ciclo al principio encapsula los datos rgb a rgba con un valor alfa de cero al final. Debe incluir y vincular con el marco 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 eres el mejor !!! tiene tanto conocimiento en el campo de la computación/procesamiento gráfico. – ReachConnection

+0

Me alegro de haber podido ayudar :) – nschmidt

+0

Muchas gracias. ¡He estado buscando un código como este por días! – Twinkle

2

Préstamos de código de nschmidt para producir un familiar, si alguien de ojos rojos imagen:

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

Entonces, addSubview:iv para obtener la imagen en su punto de vista y, por supuesto, hacer el obligatorio [releases] a mantener una casa limpia.

Cuestiones relacionadas