您当前的位置: 首页 > 

柳鲲鹏

暂无认证

  • 0浏览

    0关注

    4642博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

将RGB转换为JPG格式到内存的代码

柳鲲鹏 发布时间:2018-09-08 10:34:13 ,浏览量:0

  前面有文章,将RGB格式转换为JPG文件。近来工作需要,需要RGB转换为JPG格式到内存。这事……


int rgb2jpgAction(struct jpeg_compress_struct* pCinfo, const char *pRgbData, const int width, const int height)
{
    int depth = RGB_COUNT;
    JSAMPROW row_pointer[1];
 
    pCinfo->image_width      = width;
    pCinfo->image_height     = height;
    pCinfo->input_components = depth;
    pCinfo->in_color_space   = JCS_RGB;

    jpeg_set_defaults(  pCinfo); 
    jpeg_set_quality(   pCinfo, JPEG_QUALITY, TRUE );
    jpeg_start_compress(pCinfo, TRUE);

    int row_stride = width * depth;
    while (pCinfo->next_scanline < pCinfo->image_height)
    {
        row_pointer[0] = (JSAMPROW)(pRgbData + pCinfo->next_scanline * row_stride);
        jpeg_write_scanlines(pCinfo, row_pointer, 1);
    }

    jpeg_finish_compress( pCinfo);
    jpeg_destroy_compress(pCinfo);

    return 0;
}

/**
 这里特别说明jpeg_mem_dest的第二个参数,buffer。
 如果在rgb2jpg声明指针或者缓冲区,然后试图复制到pDest,直接崩溃;或者取不到数据。
 研究了半天不行。必须是如下的写法。
 如果缓冲区不够怎么办?那就开大一点。

 char pDest[512*1024];
 int  size=512*1024;
 然后再传递过来。

 */
int rgb2jpg(const char *pRgbData, const int width, const int height, int type, char* pDest, int* pSize)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    FILE* pOutFile = NULL;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

    if (type)
    {
        if ((pOutFile = fopen(pDest, "wb")) == NULL)
        {
            return -1;
        }
        jpeg_stdio_dest(&cinfo, pOutFile);
    }
    else
    {
        jpeg_mem_dest(&cinfo, (unsigned char **)&pDest, (long unsigned int *)pSize);
    }

    rgb2jpgAction(&cinfo, pRgbData, width, height);
    if (type)
    {
        fclose(pOutFile);
    }

    return 0;
}

 

关注
打赏
1665724893
查看更多评论
立即登录/注册

微信扫码登录

0.8504s