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
cocos2d 创建一个黑白纹理的更多相关文章
- Cocos2d-android (01) 创建一个简单的cocos2d应用程序
下载Cocos2d-android的源代码:cocos2d-android-1 git@github.com:ZhouWeikuan/cocos2d.git 将项目导入到eclipse中.运行实例: ...
- cocos2d环境及创建一个自己的项目
一. mac环境: 1.在终端操作,准备好mac系统下的sdk,adt,ndk,ant文件,放在自己的目录中 2.进入mac终端,输入: vim ~/.bash_profile 然后回 ...
- 【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- PS网页设计教程XXVI——如何在PS中创建一个专业的网页布局
作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...
- PS网页设计教程XXX——在PS中创建一个漫画书主题网页布局
作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...
- (7)nehe教程1 创建一个OpenGL窗口:
不要用那个nehe ndk了 误人子弟! 转自: 一个窗口,代码可真多啊 http://www.yakergong.net/nehe/ 在这个教程里,我将教你在Windows环境中创建OpenGL程序 ...
- cocos3.2中如何创建一个场景
1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...
- 【淡墨Unity3D Shader计划】一间 创建一个游戏场景 & 第一Shader写作
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- 用three.js创建一个简易的天空盒
本文创建的天空盒是用六张图片来创建的.笔者会论述两种方法来创建,都是最简单基本的方法,不涉及着色器的使用.一种是创建一个盒子,然后将图片作为盒子6个面的纹理贴上来创建.另一种则是简单的将纹理作为场景的 ...
随机推荐
- 从源代码角度分析ViewStub 疑问与原理
一.提出疑问 ViewStub比較简单.之前文章都提及到<Android 性能优化 三 布局优化ViewStub标签的使用>.可是在使用过程中有一个疑惑,究竟是ViewStub上设 ...
- Disable root 账号登陆
1. 改动文件: /etc/ssh/sshd_config #PermitRootLogin yes 改动为: PermitRootLogin no 2. 重新启动ssh进程 /etc/init.d/ ...
- Linux Shell 之 Shell中的函数调用
说起函数调用,相信大家也不会陌生,然而对于初学Shell的我来说,Shell中函数调用方式却有点让我不太习惯,自己也走了不少的弯路,因为传递参数时出了一个很“自然”的错误,也让我吃了不少的苦头,所以总 ...
- SQL ---指令实例语句
1 1 create database+数据库名字 创建数据库 2 2 create table+表的名字 创建表 3 表中的操作: 4 3 insert into 表名 (列名1,列名2··)val ...
- [Android学习笔记]Context简单理解
一.Context是什么?上下文对象,可以理解为一个程序的运行的环境,从中可以获取当前程序的资源:getResources,getAssets 二.常见的Context有哪些?Application ...
- centos下chm阅读器
xchm kchmviewer chmsee
- drupal 7 模块开发,hook_form
因为不是系统学习,只能把每天自己学习到的东西零碎的记录下来. 一来方便自己记忆,二来可供大家查阅. 后续有精力再去做进一步的整理. 1 开发一个模块分为有下面几个文件 hook.admin.inc h ...
- NavigationBar 隐藏底部边线,阴影
NavigationBar 底部默认有一条边线 假设项目中须要隐藏何以採用这个库 https://github.com/samwize/UINavigationBar-Addition/
- UIImagePickerController本地化控件文字
在使用UIImagePickerController时候,你会发如今选择照片或者拍照的时候,界面的很多控件都是英文的,比方"Cancel","Choose"等. ...
- 使用xml和java代码混合控制UI界面
main.xml.................... <?xml version="1.0" encoding="utf-8"?> <Li ...