iOS学习----------图片的简单处理(2)

来源:互联网 发布:mac qq五笔拼音输入法 编辑:程序博客网 时间:2024/06/10 03:05

接第一部分内容

 

//图片的大小

     CGSize ghostSize = CGSizeMake(targetGhostWidth, targetGhostWidth / ghostImageAspectRatio);

    

    //图片的位置

     CGPoint ghostOrigin = CGPointMake(inputWidth * 0.5, inputHeight *0.2);

      

      // 2.2 Scale & Get pixels of the ghost

     NSUInteger ghostBytesPerRow = bytesPerPixel * ghostSize.width;

      

     UInt32 * ghostPixels = (UInt32 *)calloc(ghostSize.width * ghostSize.height,sizeof(UInt32));

      

     CGContextRef ghostContext = CGBitmapContextCreate(ghostPixels, ghostSize.width, ghostSize.height,

                                                        bitsPerComponent, ghostBytesPerRow, colorSpace,

                                                       kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

      

     CGContextDrawImage(ghostContext, CGRectMake(0, 0, ghostSize.width, ghostSize.height),ghostCGImage);

    

      // 2.3 Blend each pixel

     NSUInteger offsetPixelCountForInput = ghostOrigin.y * inputWidth + ghostOrigin.x;

     for (NSUInteger j =0; j < ghostSize.height; j++) {

       for (NSUInteger i =0; i < ghostSize.width; i++) {

         UInt32 * inputPixel = inputPixels + j * inputWidth + i + offsetPixelCountForInput;

         UInt32 inputColor = *inputPixel;

          

         UInt32 * ghostPixel = ghostPixels + j * (int)ghostSize.width + i;

         UInt32 ghostColor = *ghostPixel;

          

          // Blend the ghost with 50% alpha

         CGFloat ghostAlpha = 0.5f * (A(ghostColor) / 255.0);

         UInt32 newR = R(inputColor) * (1 - ghostAlpha) +R(ghostColor) * ghostAlpha;

         UInt32 newG = G(inputColor) * (1 - ghostAlpha) +G(ghostColor) * ghostAlpha;

         UInt32 newB = B(inputColor) * (1 - ghostAlpha) +B(ghostColor) * ghostAlpha;

          

          //Clamp, not really useful here :p

          newR =MAX(0,MIN(255, newR));

          newG =MAX(0,MIN(255, newG));

          newB =MAX(0,MIN(255, newB));

          

          *inputPixel =RGBAMake(newR, newG, newB, A(inputColor));

        }

      }

      

      // 3. Convert the image to Black & White

     for (NSUInteger j =0; j < inputHeight; j++) {

       for (NSUInteger i =0; i < inputWidth; i++) {

         UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;

         UInt32 color = *currentPixel;

          

          // Average of RGB = greyscale

         UInt32 averageColor = (R(color) +G(color) + B(color)) /3.0;

          

          *currentPixel =RGBAMake(averageColor, averageColor, averageColor,A(color));

        }

      }


      // 4. Create a new UIImage

    //context中画好的图片转化为CGImage

     CGImageRef newCGImage = CGBitmapContextCreateImage(context);

     UIImage * processedImage = [UIImageimageWithCGImage:newCGImage];

      

      // 5. Cleanup!

     CGColorSpaceRelease(colorSpace);

     CGContextRelease(context);

     CGContextRelease(ghostContext);

     free(inputPixels);

     free(ghostPixels);

      

     return processedImage;

}

0 0
原创粉丝点击