RGB565 YUV转换程序

由于RGB565为16位数据,故每个像素点占用两个字节大小,所以在使用第一个函数时,需要将图片像素的当前地址和下一位的地址都送到函数中,或者可以使用第二个封装后的函数,只需要设定图片的显示范围即可

#define CLIP(X) ((X) > 255 ? 255 : (X) < 0 ? 0 : X)
// RGB -> YUV
#define RGB2Y(R, G, B) CLIP(((66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
#define RGB2U(R, G, B) CLIP(((-38 * (R)-74 * (G) + 112 * (B) + 128) >> 8) + 128)
#define RGB2V(R, G, B) CLIP(((112 * (R)-94 * (G)-18 * (B) + 128) >> 8) + 128)

void RGB5652YUV(uint16_t source1, uint16_t source2, uint16_t *dest1, uint16_t *dest2)
{
    uint8_t R, G, B, Y0, Y1, U0, V0, U1, V1;
    R      = ((source1 & RGB565_RED) >> 11) << 3;
    G      = ((source1 & RGB565_GREEN) >> 5) << 2;
    B      = ((source1 & RGB565_BLUE)) << 3;
    Y0     = RGB2Y(R, G, B);
    U0     = RGB2U(R, G, B);
    V0     = RGB2V(R, G, B);
    R      = ((source2 & RGB565_RED) >> 11) << 3;
    G      = ((source2 & RGB565_GREEN) >> 5) << 2;
    B      = ((source2 & RGB565_BLUE)) << 3;
    Y1     = RGB2Y(R, G, B);
    U1     = RGB2U(R, G, B);
    V1     = RGB2V(R, G, B);
    U0     = (U0 + U1) / 2;
    V0     = (V0 + V1) / 2;
    *dest2 = (Y1 << 8) | V0;
    *dest1 = (Y0 << 8) | U0;
}

void ConvertRGB2YUV(uint16_t *sourceLcdBuffer, uint16_t *destLcdBuffer)
{
    int line, col;

    for (line = 0; line < LCD_HEIGHT; line++)
        for (col = 0; col < LCD_WIDTH - 1; col = col + 2)
        {
            RGB5652YUV(*(sourceLcdBuffer + line * LCD_WIDTH + col), *(sourceLcdBuffer + line * LCD_WIDTH + col + 1),
                       (destLcdBuffer + line * LCD_WIDTH + col), (destLcdBuffer + line * LCD_WIDTH + col + 1));
        }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在OpenGL ES中将YUV数据转换RGB格式需要进行以下步骤: 1. 创建一个纹理对象,用于存储转换后的RGB数据。 2. 将YUV数据绑定到一个纹理单元上。 3. 创建一个着色器程序,用于将YUV数据转换RGB格式。 4. 将纹理对象绑定到FBO(Frame Buffer Object),并将着色器程序与FBO绑定。 5. 渲染FBO以将YUV数据转换RGB格式。 下面是一个简单的代码示例: 1. 创建纹理对象 ``` GLuint textureID; glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); ``` 2. 绑定YUV数据到纹理单元 ``` glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, yData); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, width / 2, height / 2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, uvData); ``` 3. 创建着色器程序 ``` const char* vertexShader = "attribute vec4 position;\n" "attribute vec2 texCoord;\n" "varying vec2 v_texCoord;\n" "void main()\n" "{\n" " gl_Position = position;\n" " v_texCoord = texCoord;\n" "}\n"; const char* fragmentShader = "precision highp float;\n" "varying vec2 v_texCoord;\n" "uniform sampler2D yTexture;\n" "uniform sampler2D uvTexture;\n" "void main()\n" "{\n" " float y = texture2D(yTexture, v_texCoord).r;\n" " float u = texture2D(uvTexture, v_texCoord).r - 0.5;\n" " float v = texture2D(uvTexture, v_texCoord).a - 0.5;\n" " float r = y + 1.13983 * v;\n" " float g = y - 0.39465 * u - 0.58060 * v;\n" " float b = y + 2.03211 * u;\n" " gl_FragColor = vec4(r, g, b, 1.0);\n" "}\n"; GLuint programID = glCreateProgram(); GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShaderID, 1, &vertexShader, NULL); glCompileShader(vertexShaderID); glAttachShader(programID, vertexShaderID); GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShaderID, 1, &fragmentShader, NULL); glCompileShader(fragmentShaderID); glAttachShader(programID, fragmentShaderID); glLinkProgram(programID); glUseProgram(programID); ``` 4. 绑定纹理对象到FBO,并将着色器程序与FBO绑定 ``` GLuint fboID; glGenFramebuffers(1, &fboID); glBindFramebuffer(GL_FRAMEBUFFER, fboID); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0); glViewport(0, 0, width, height); glUseProgram(programID); glUniform1i(glGetUniformLocation(programID, "yTexture"), 0); glUniform1i(glGetUniformLocation(programID, "uvTexture"), 1); ``` 5. 渲染FBO ``` GLfloat vertices[] = { -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f }; GLubyte indices[] = {0, 1, 2, 3}; glVertexAttribPointer(glGetAttribLocation(programID, "position"), 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), vertices); glVertexAttribPointer(glGetAttribLocation(programID, "texCoord"), 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), vertices + 2); glEnableVertexAttribArray(glGetAttribLocation(programID, "position")); glEnableVertexAttribArray(glGetAttribLocation(programID, "texCoord")); glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices); glDisableVertexAttribArray(glGetAttribLocation(programID, "position")); glDisableVertexAttribArray(glGetAttribLocation(programID, "texCoord")); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值