• cocos2d 创建一个黑白纹理


    @interface myGrayTexture : CCTexture2D
    // @param exposure 曝光
    +(id) textureWithFile:(NSString*) file exposure:(int) exposure;
    @end



    @implementation myGrayTexture
    typedef enum {
        ALPHA = 0,
        BLUE = 1,
        GREEN = 2,
        RED = 3
    } PIXELS;
    
    +(UIImage*) getGrayImage:(NSString*) file  exposure:(int) exposure{
        
        UIImage* img = [UIImage imageNamed:file];
        if (nil == img) {
            return nil;
        }
        
        CGSize size = [img size];
        int width = size.width;
        int height = size.height;
    	
        // the pixels will be painted to this array
        uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
    	
        // clear the pixels so any transparency is preserved
        memset(pixels, 0, width * height * sizeof(uint32_t));
    	
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    	
        // create a context with RGBA pixels
        CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
                                                     kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
    	
        // paint the bitmap to our context which will fill in the pixels array
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), [img CGImage]);
    	
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
    			
                // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
                uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
    			
                
                // set the pixels to gray
                rgbaPixel[RED] = gray + (gray > 0 ? exposure : 0);
                rgbaPixel[GREEN] = gray + (gray > 0 ? exposure : 0);
                rgbaPixel[BLUE] = gray + (gray > 0 ? exposure : 0);
            }
        }
    	
        // create a new CGImageRef from our context with the modified pixels
        CGImageRef image = CGBitmapContextCreateImage(context);
    	
        // we're done with the context, color space, and pixels
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        free(pixels);
    	
        // make a new UIImage to return
        UIImage *resultUIImage = [UIImage imageWithCGImage:image];
    	
        // we're done with image now too
        CGImageRelease(image);
    	
        return resultUIImage;
    }
    
    +(id) textureWithFile:(NSString*) file exposure:(int) exposure{
        UIImage* img = [self getGrayImage:file exposure:exposure];
        if (img) {
            return  [[[myGrayTexture alloc] initWithImage:img] autorelease];
        }
        return nil;
    }
    
    
    @end


  • 相关阅读:
    网络层-数据平面:路由器工作原理
    文件系统和目录:目录
    文件系统与目录:文件系统
    运输层-可靠数据传输原理:选择重传 Selective Repeat Protocol
    运输层-可靠数据传输原理:回退N步 Go-Back_N Protocol
    JAVA学习笔记之多态
    约瑟夫环的递归解法C
    题目:汉诺塔问题
    题目:在同一坐标中输出sinx和cosx两条曲线
    n!的溢出问题及处理
  • 原文地址:https://www.cnblogs.com/iapp/p/3631719.html
Copyright © 2020-2023  润新知