参考自: 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 实现的更多相关文章

  1. cpuimage 开源之

    前年学习opengl做的一个小东西. 原本计划将gpuimage 的算法一个一个转写成cpu版本 c,c++ 版本. gpuimage 项目参考: https://github.com/BradLar ...

随机推荐

  1. CoolBlog开发笔记第1课:项目分析

    首先说一下CoolBlog开发笔记是我制作的<Django实战项目>系列教程基础篇的内容,使用Django来开发一个酷炫的个人博客,涉及的知识包括项目的分析,环境的搭建,模型和视图定义等等 ...

  2. Bash On Windows的学习

    Bash On Windows的学习 Bash On Windows的卸载 删除软件和设置:在 cmd 运行lxrun /uninstall 删除所有文件:在cmd中运行lxrun /uninstal ...

  3. oracle学习笔记(1)-三级模式SCHEMA

    oracle三级模式及二级映像 模式(schema)是数据库的一个名词,大部分的数据库在结构上都有三级模式的特征,了解下基本的概念,有助于后续深入的学习. 用老罗坚果pro发布会的话说就是,不罗嗦,先 ...

  4. php隔行换色输出表格

    <?php header("Content-type:text/html;charset=utf-8"); $str=''; $str.='<table border= ...

  5. PHP htmlspecialchars和htmlspecialchars_decode(函数)

    htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体. 函数原型:htmlspecialchars(string,quotestyle,character-set) 预定 ...

  6. [leetcode-581-Shortest Unsorted Continuous Subarray]

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  7. ajax数据请求2(json格式)

    ajax数据请求2(json格式) <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  8. Linux之用户管理--初级上

    管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 同adduser命令,执行此命令可在系统中添加用户.(更改4个用户文件) userdel删 执行此命令可删除用户及相关用户的配 ...

  9. HTML DOM元素关系与操作

    <html> <head><title>DOM元素关系与操作</title></head> <body> <!-- div ...

  10. 【转】iOS 9 Storyboard 教程(一上)

    转自:http://blog.csdn.net/yangmeng13930719363/article/details/49886547 Storyboard是在iOS5之后新增的一个令人兴奋的功能, ...