@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

cocos2d 创建一个黑白纹理的更多相关文章

  1. Cocos2d-android (01) 创建一个简单的cocos2d应用程序

    下载Cocos2d-android的源代码:cocos2d-android-1 git@github.com:ZhouWeikuan/cocos2d.git 将项目导入到eclipse中.运行实例: ...

  2. cocos2d环境及创建一个自己的项目

    一. mac环境:     1.在终端操作,准备好mac系统下的sdk,adt,ndk,ant文件,放在自己的目录中     2.进入mac终端,输入: vim ~/.bash_profile 然后回 ...

  3. 【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  4. PS网页设计教程XXVI——如何在PS中创建一个专业的网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  5. PS网页设计教程XXX——在PS中创建一个漫画书主题网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  6. (7)nehe教程1 创建一个OpenGL窗口:

    不要用那个nehe ndk了 误人子弟! 转自: 一个窗口,代码可真多啊 http://www.yakergong.net/nehe/ 在这个教程里,我将教你在Windows环境中创建OpenGL程序 ...

  7. cocos3.2中如何创建一个场景

    1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...

  8. 【淡墨Unity3D Shader计划】一间 创建一个游戏场景 &amp; 第一Shader写作

    本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨)  ...

  9. 用three.js创建一个简易的天空盒

    本文创建的天空盒是用六张图片来创建的.笔者会论述两种方法来创建,都是最简单基本的方法,不涉及着色器的使用.一种是创建一个盒子,然后将图片作为盒子6个面的纹理贴上来创建.另一种则是简单的将纹理作为场景的 ...

随机推荐

  1. win8vs2012创建自带sqlServer数据库出错

    以前写程序的时候,一直使用的sqlite,今天心血来潮,想用vs2012连接自身带的数据库,结果就出现错误: 啊,看到这个错误,咱赶紧上网搜搜,啊,有关的日志也是比较少的,经过一番苦战之后,终于好了, ...

  2. Swift - 搜索条(UISearchBar)的用法

    1,搜索条Options属性还可设置如下功能样式: Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件. Shows Bookmark ...

  3. NET5

    ASP.NET5(RC1) - 翻译 前言 ASP.NET 5 是一次令人惊叹的对于ASP.NET的创新革命. 他将构建目标瞄准了 .NET Core CLR, 同时ASP.NET又是对于云服务进行优 ...

  4. linux cent os putty 问题彻底解决办法

    出现乱码的根本原因: linux系统和putty使用的编码格式不一致. 解决办法: 1.首先使用命令查看linux当前使用的是什么编码格式 echo $LANG 返回的结果有如下几种情况:1)zh_C ...

  5. Linux Force DHCP Client (dhclient) to Renew IP Address

    http://www.cyberciti.biz/faq/howto-linux-renew-dhcp-client-ip-address/‘m using Ubuntu Linux. How to ...

  6. SLIC superpixel实现分析

    http://infoscience.epfl.ch/record/149300这是SLIC算法的官网,网站有和SLIC相关的资源. SLIC主要运用K-means聚类算法进行超像素的处理,聚类算法中 ...

  7. java学习笔记05--字符串 .

    java学习笔记05--字符串 . 一.String类 由字符所组成的一串文字符号被称之为字符串.在java中字符串不仅仅是字符数组,而且是String类的一个实例,可以使用String类来构建. 字 ...

  8. 为什么 string.find()返回值是-1

    之前好像在哪里见到过这个问题,时间有点久,想不起来了,今天写字符串又碰到这个问题,书上给出的定义是当string.find()没有找到时返回的是一个非常大的值,网上有人说是-1,两种说法都对,由于整数 ...

  9. 1.SQL统计某张表的列数。

    select   count(syscolumns.name)    from   syscolumns   ,   sysobjects       where   syscolumns.id    ...

  10. hdu4223(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4223 由于n范围较小,完全可暴力... #include <cstdio> #includ ...