CPUImageRGBFilter 实现
参考自: https://github.com/BradLarson/GPUImage
GPUImageRGBFilter: Adjusts the individual RGB channels of an image
- red: Normalized values by which each color channel is multiplied. The range is from 0.0 up, with 1.0 as the default.
- green:
- blue:
对应的Shader:
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform highp float redAdjustment;
uniform highp float greenAdjustment;
uniform highp float blueAdjustment;
void main()
{
highp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
gl_FragColor = vec4(textureColor.r * redAdjustment, textureColor.g * greenAdjustment, textureColor.b * blueAdjustment, textureColor.a);
}
简单解释一下以上Shader.
uniform 声明为只读变量。
varying 声明为读写变量。
highp 为计算精度。这里为高精度。
textureCoordinate为对应的纹理坐标位置即(x,y)
inputImageTexture为输入的纹理图。
texture2D 取对应纹理图所在坐标的像素颜色。
所以整个算法很简单,就是将对应纹理图所在坐标的像素进行相应的 红绿蓝 权重调整。
权重的取值范围为[0,1]
根据以上Shader进行CPU代码转换。
#ifndef ClampToByte
#define ClampToByte(v)(((unsigned) int(v)) < (255) ? (v) : (v < 0) ? int(0) : int(255))
#endif
void CPUImageRGBFilter(unsigned char* Input, unsigned char* Output, int Width, int Height, int Stride, float redAdjustment = 1.0f, float greenAdjustment = 1.0f, float blueAdjustment = 1.0f)
{
int Channels = Stride / Width;
) return;
int numberOfPixels = Width*Height;
unsigned char* scanLineIn = Input;
unsigned char* scanLineOut = Output;
; pixel < numberOfPixels; pixel++) {
scanLineOut[] = ClampToByte(scanLineIn[] * redAdjustment);
scanLineOut[] = ClampToByte(scanLineIn[] * greenAdjustment);
scanLineOut[] = ClampToByte(scanLineIn[] * blueAdjustment);
scanLineIn += Channels;
scanLineOut += Channels;
}
}
转换后可进行建表优化。
即:
void CPUImageRGBFilter(unsigned char* Input, unsigned char* Output, int Width, int Height, int Stride, float redAdjustment = 1.0f, float greenAdjustment = 1.0f, float blueAdjustment = 1.0f)
{
int Channels = Stride / Width;
) return;
unsigned ] = { };
unsigned ] = { };
unsigned ] = { };
; pixel < ; pixel++)
{
AdjustMapR[pixel] = pixel*redAdjustment;
AdjustMapG[pixel] = pixel*greenAdjustment;
AdjustMapB[pixel] = pixel*blueAdjustment;
}
; Y < Height; Y++)
{
unsigned char* scanLineOut = Output + (Y * Stride);
unsigned char* scanLineIn = Input + (Y * Stride);
; X < Width; X++)
{
scanLineOut[] = AdjustMapR[scanLineIn[]];
scanLineOut[] = AdjustMapG[scanLineIn[]];
scanLineOut[] = AdjustMapB[scanLineIn[]];
scanLineIn += Channels;
scanLineOut += Channels;
}
}
}
代码比较简单,一看就明了。
待后续有时间,再通过simd指令集以及thread等方式进行改写。
CPUImageRGBFilter 实现的更多相关文章
- cpuimage 开源之
前年学习opengl做的一个小东西. 原本计划将gpuimage 的算法一个一个转写成cpu版本 c,c++ 版本. gpuimage 项目参考: https://github.com/BradLar ...
随机推荐
- 原生js实现Ajax的原理。
Ajax(Asynchronous JavaScript and XML)表示异步的js与xml. 有别于传统web的同步开发方式. 原理:通过XMLHttpRequest对象向服务器发送异步请求,从 ...
- selenium webDriver给隐藏域赋值 input hidden set value
//直接这样无法给input hidden赋值// driver.findElement(By.id("image_default")).sendKeys("a1112. ...
- object覆盖的div解决办法
最近做个web项目,因为里面有个<object>的插件,弹出<div>对话框会被其遮盖,我做了个<iframe>标签,在弹框时,把<object>覆盖掉 ...
- Swift计算字符数量
通过调用全局 countElements 函数并将字符串作为参数进行传递可以获取该字符串的字符数量. let unusualMenagerie = "Koala
- 在ashx和静态文件中使用Session
在ashx页面中如果想使用可读可写的Session,必须要实现一个接口“IRequiresSessionState”,在这个接口中没有定义任何方法,这样的接口被称为“标识接口”. public int ...
- 一只猿:使用flask来做一个小应用
上周 @萍姐 问我如何抓取天猫上面店铺的评分,看了下挺简单的,于是花了点时间写了个Python脚本,加上web.py做成一个web服务,使用起来还不错,今天来看的时候发现当时为了方便直接用web.py ...
- ActiveMQ 503错误
问题描述: 在Linux系统下安装ActiveMQ,启动服务 正常启动后,通过浏览器进行访问 可以正常显示home页面,但是点击其他菜单,如Queues,Topics等,都会出现503错误,如图 问题 ...
- jdk和jre有什么区别?
简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境.SDK是Software Development Kit 一般指软件开发包,可以包括函数库.编译程序等. JDK就是Ja ...
- Servlet 中为多项选择题判分---String类的indexOf()方法妙用
首先来看一下String类的indexOf()方法的用法: public class FirstDemo1 { /** *API中String的常用方法 */ // 查找指定字符串是否存在 publi ...
- 基于Node.js的微信JS-SDK后端接口实现
做了一个网站,放到线上,用微信打开,点击分享,可是分享后发给朋友的链接卡片是微信默认自带的,如下: 这标题,描述以及图片是默认自带的,丑不说,分享给别人还以为是盗号网站呢,而接入微信的JSSDK后,分 ...