2010-03-02 3 views
0

J'ai affaire à une application iPhone qui choisirait un pixel spécifique de couleur de l'image et le remplacer par d'autres nuances de couleurs que je choisis dans le menu de couleur. Le problème est que le code que j'ai implémenté fonctionne bien sur le simulateur, mais quand je cours le même code sur le périphérique tout ce que je reçois est que, les pixels de l'image sont remplacés par seulement la couleur blanche. Je suis coller le code ci-dessous, Si quelqu'un a la moindre idée comme la façon de la mettre en œuvre, alors il serait une grande aidePixel de remplacement de couleur fonctionne bien sur le simulateur mais pas sur l'iPhone

// This is data buffer details of whole image's pixels 

CGImageRef imageRef = [image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height,bitsPerComponent, bytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

//Second image into buffer 
//This is data buffer of the pixel to be replaced that i am selecting from image 
CGImageRef imageRefs = [selectedColorImage.image CGImage]; 
NSUInteger widths = CGImageGetWidth(imageRefs); 
NSUInteger heights = CGImageGetHeight(imageRefs); 
CGColorSpaceRef colorSpaces = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawDatas = malloc(heights * widths * 4); 
NSUInteger bytesPerPixels = 4; 
NSUInteger bytesPerRows = bytesPerPixels * widths; 
NSUInteger bitsPerComponents = 8; 
CGContextRef contexts = CGBitmapContextCreate(rawDatas, widths, heights,bitsPerComponents, bytesPerRows, colorSpaces,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpaces); 
CGContextDrawImage(contexts, CGRectMake(0, 0, widths, heights), imageRefs); 
CGContextRelease(contexts); 


// Now your rawData contains the image data in the RGBA8888 pixel format. 
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
int byteIndexs = (bytesPerRows * 0) + 0 * bytesPerPixels; 
int i=0; 
for (int ii = 0 ; ii < count ; ++ii) 
    { 
     CGFloat redb = (rawData[byteIndex] * 1.0)/255.0; 
     CGFloat greenb = (rawData[byteIndex + 1] * 1.0)/255.0; 
     CGFloat blueb = (rawData[byteIndex + 2] * 1.0)/255.0; 
     CGFloat alphab = (rawData[byteIndex + 3] * 1.0)/255.0; 

     CGFloat reds = (rawDatas[byteIndexs] * 1.0)/255.0; 
     CGFloat greens = (rawDatas[byteIndexs + 1] * 1.0)/255.0; 
     CGFloat blues = (rawDatas[byteIndexs + 2] * 1.0)/255.0; 
     CGFloat alphas = (rawDatas[byteIndexs + 3] * 1.0)/255.0; 
     /* CGColorRef ref=[[shapeButton backgroundColor] CGColor]; 
     switch(CGColorSpaceGetModel(CGColorGetColorSpace(ref))) 
     { 
      case kCGColorSpaceModelMonochrome: 
       // For grayscale colors, the luminance is the color value 
       //luminance = components[0]; 
       break; 

      case kCGColorSpaceModelRGB: 
       // For RGB colors, we calculate luminance assuming sRGB Primaries as per 
       // http://en.wikipedia.org/wiki/Luminance_(relative) 
       //luminance = 0.2126 * components[0] + 0.7152 * components[1] + 0.0722 * components[2]; 
       break; 
      case kCGColorSpaceModelCMYK: 
      case kCGColorSpaceModelLab: 
      case kCGColorSpaceModelDeviceN: 
      case kCGColorSpaceModelIndexed: 
       break; 
      case kCGColorSpaceModelUnknown: 
       break; 
      case kCGColorSpaceModelPattern: 
       break; 
      //default: 
       // We don't implement support for non-gray, non-rgb colors at this time. 
       // Since our only consumer is colorSortByLuminance, we return a larger than normal 
       // value to ensure that these types of colors are sorted to the end of the list. 
       //luminance = 2.0; 
     } 
     int numComponents = CGColorGetNumberOfComponents(ref); 
     if (numComponents == 4) 
     { 
      const CGFloat *components = CGColorGetComponents(ref); 
      CGFloat red = components[0]; 
      CGFloat green = components[1]; 
      CGFloat blue = components[2]; 
      CGFloat alpha = components[3]; 
     }*/ 
     if((redb==red/255.0f)&&(greenb=green/255.0f)&&(blueb=blue/255.0f)&&(alphab==alpha/255.0f)) 
     { 
      if(button_tag ==1) 
      { 
      NSLog(@"color matching %d",i);//done 
      i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+999999999; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+000000800.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+999999000.0; 
       rawData[byteIndex+3]=(alphas*255.0)/255.0; 

      } 
      if(button_tag ==2) 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+899989989; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+898998999.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+899989900.0; 
       rawData[byteIndex+3]=(alphas*255.0)/255.0; 

      } 
      if(button_tag ==3) 
      { 
       NSLog(@"color matching %d",i);//done 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+999999999; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+990000800.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+999999000.0; 
       rawData[byteIndex+3]=(alphas*255.0)/10.0; 

      } 
      if(button_tag ==4)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+50.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+50.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+50.0; 
       rawData[byteIndex+3]=(alphas*0.0)/0.0; 


      } 
      if(button_tag ==5)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+255000000.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+000000000.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+000000255.0; 
       rawData[byteIndex+3]=(alphas*255.0)/10.0; 

      } 
      if(button_tag ==6)// done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+0.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+1.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+0.0; 
       rawData[byteIndex+3]=(alphas*255.0)/0.0; 

      } 
      if(button_tag ==7)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+255255255.0f; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+000255255.0f; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+255255255.0f; 
       rawData[byteIndex+3]=(alphas*255.0)/255.0; 

      } 
      if(button_tag ==8)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 


       rawData[byteIndex]=(reds*255.0)/1.0+200.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+200.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+200.0; 
       rawData[byteIndex+3]=(alphas*0.0)/0.0; 
      } 
      if(button_tag ==9)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+1.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+0.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+0.0; 
       rawData[byteIndex+3]=(alphas*255.0)/0.0; 

      } 
      if(button_tag ==10)//done 
      { 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+999999999; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+990000888.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+999999000.0; 
       rawData[byteIndex+3]=(alphas*255.0)/10.0; 

      } 
      if(button_tag ==11)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+900000000; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+990000800.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+999999000.0; 
       rawData[byteIndex+3]=(alphas*255.0)/255.0; 

      } 
      if(button_tag ==12)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+150.0f; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+150.0f; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+150.0f; 
       rawData[byteIndex+3]=(alphas*255.0)/255.0f; 

      } 
      if(button_tag ==13)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+0.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+0.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+0.0; 
       rawData[byteIndex+3]=(alphas*255.0)/0.0; 

      } 
      if(button_tag ==14)//done 
      { 
       NSLog(@"color matching %d",i); 
       i++; 
       rawData[byteIndex]=(reds*255.0)/1.0+10.0; 
       rawData[byteIndex+1]=(blues*255.0)/1.0+10.0; 
       rawData[byteIndex+2]=(greens*255.0)/1.0+10.0; 
       rawData[byteIndex+3]=(alphas*255.0)/1.0; 

      } 

     } 
     byteIndex += 4; 
     //byteIndexs += 4; 
    } 
CGSize size=CGSizeMake(320, 330); 
UIImage *newImage= [self imageWithBits:rawData withSize:size]; 

[backgroundImage setImage:newImage]; 
[self HideLoadingIndicator]; 

//free(rawData); 
//free(rawDatas); 

Merci à l'avance :)

+0

Neuf questions, zéro accepte. Chic. –

Répondre

0

flotteurs seront toujours Comparer inexactes. Utilisez plutôt les valeurs entières originales; ils sont plus faciles à travailler et seront plus rapides.

modifier: quelque chose de semblable à cela devrait fonctionner pour remplacer blanc transparent:

uint32_t *pixels = (pointer to image data); 
uint32_t sourceColor = 0xffffffff; 
uint32_t destColor = 0x00000000; 
size_t pixelCount = width * height; 
for (int i = 0; i < pixelCount; i++) 
    if (pixels[i] == sourceColor) 
     pixels[i] = destColor; 
0

Bien que je ne peux pas vous dire exactement ce que votre problème est, je remarque beaucoup de code semblable à:

rawData[byteIndex]=(reds*255.0)/1.0+999999999; 

qui, en écrivant à un seul octet, va atteindre la valeur maximale (255). Faire cela quatre fois sur un pixel RGBA8 le rendra blanc et opaque.

Questions connexes