RGB数据剪切后保存为JPG格式文件的代码(使用jpeglib)

简介: RGB数据剪切后保存为JPG格式文件的代码(使用jpeglib)

从一个大的RGBA数据中,剪切部分为RGB格式:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gh_rgba2jpg.h"
#include <jpeglib.h>
int clipRgbaToJpgFile(const char *pFileName, const char* pRgbaData, const int nWidth, const int nHeight, const int nClipLeft, const int nClipTop, const int nClipWidth, const int nClipHeight)
{
    char* pClipSource     = NULL;
    char* pClipData       = NULL;
    int pixcelBytes       = nClipWidth*nClipHeight*3;
    int i = 0;
    int j = 0;
    pClipSource = malloc(pixcelBytes);
    if (!pClipSource)
    {
        return -1;
    }
    //移动到制定位置
    pRgbaData += nClipTop * nWidth * 4;
    pRgbaData += nClipLeft * 4;
    pClipData = pClipSource;
    for (i=0; i<nClipHeight; i++)
    {
        for (j=0; j<nClipWidth; j++)
        {
            //这样性能如何?
            memcpy(pClipData, pRgbaData, 3);
            pRgbaData += 4;
            pClipData += 3;
        }
        pRgbaData += (nWidth-nClipWidth)    * 4;
    }
    rgb2jpg(pFileName, pClipSource, nClipWidth, nClipHeight);
    //释放资源
    free(pClipSource);
    return 0;
}



将剪切后的RGB保存为JPG文件:

int rgb2jpg(char *jpg_file, char *pdata, int width, int height)
{
    int depth = 3;
    JSAMPROW row_pointer[1];
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *outfile;
    if ((outfile = fopen(jpg_file, "wb")) == NULL)
    {
        return -1;
    }
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo); 
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width      = width;
    cinfo.image_height     = height;
    cinfo.input_components = depth;
    cinfo.in_color_space   = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE );
    jpeg_start_compress(&cinfo, TRUE);
    int row_stride = width * depth;
    while (cinfo.next_scanline < cinfo.image_height)
    {
        row_pointer[0] = (JSAMPROW)(pdata + cinfo.next_scanline * row_stride);
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose(outfile);
    return 0;
}


目录
相关文章
|
10月前
|
监控 PHP Apache
优化 PHP-FPM 参数配置:实现服务器性能提升
优化PHP-FPM的参数配置可以显著提高服务器的性能和稳定性。通过合理设置 `pm.max_children`、`pm.start_servers`、`pm.min_spare_servers`、`pm.max_spare_servers`和 `pm.max_requests`等参数,并结合监控和调优措施,可以有效应对高并发和负载波动,确保Web应用程序的高效运行。希望本文提供的优化建议和配置示例能够帮助您实现服务器性能的提升。
518 3
|
5月前
|
存储 Unix Shell
Shell 输出命令完全指南:echo 与 printf 的深度剖析
本文深入解析了 Shell 编程中 `echo` 和 `printf` 两个核心输出命令的用法与区别。`echo` 简单易用,适合基础输出;`printf` 功能强大,支持复杂格式化。文章从语法、转义序列、高级技巧到实际应用场景(如日志记录、进度显示)逐一讲解,并对比两者的性能与适用场景,帮助开发者根据需求灵活选择。最后通过进阶技巧和常见问题解答,进一步提升对两者的掌握程度。
262 1
|
SQL 数据库 Python
【Python】已完美解决:(156, b“Incorrect syntax near the keyword ‘group’.DB-Lib error message 20018, severity
【Python】已完美解决:(156, b“Incorrect syntax near the keyword ‘group’.DB-Lib error message 20018, severity
351 0
|
NoSQL Java Redis
深入理解Servlet Filter及其限流实践
深入理解Servlet Filter及其限流实践
276 44
|
Linux C++ iOS开发
VLC源码解析:视频播放速度控制背后的技术
VLC源码解析:视频播放速度控制背后的技术
1062 0
|
存储 数据库 数据库管理
SQLite支持哪些数据类型?
【7月更文挑战第31天】SQLite支持哪些数据类型?
1133 8
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp微信小程序的考研资料分享系统的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的考研资料分享系统的详细设计和实现
162 0
|
存储 SQL 人工智能
从零开始搭建医药领域知识图谱实现智能问答与分析服务(含码源):含Neo4j基于垂直网站数据的医药知识图谱构建、医药知识图谱的自动问答等
从零开始搭建医药领域知识图谱实现智能问答与分析服务(含码源):含Neo4j基于垂直网站数据的医药知识图谱构建、医药知识图谱的自动问答等
从零开始搭建医药领域知识图谱实现智能问答与分析服务(含码源):含Neo4j基于垂直网站数据的医药知识图谱构建、医药知识图谱的自动问答等
|
弹性计算 监控 新能源
新能源电动车监控数据上报
本场景介绍了阿里云原生数据库Lindorm定位智能海量数据存储场景,可提供车辆数据实时监控,助力建立驾驶行为分析等服务。