linux 下 读取 jpeg 图片 - 代码片段

static unsigned char* copyScanline(unsigned char *currPtr, unsigned char *from, int cnt)
{
    memcpy((void*)currPtr, (void*)from, cnt);
    currPtr -= cnt;
    return currPtr;
}


unsigned char* Image::loadJpegFile(const char *filename)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;


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


    FILE *infile = fopen(filename, "rb");

    if( infile  == NULL)
    {
        fprintf(stderr, "can't open %s\n", filename) ;
        return NULL;
    }

    jpeg_stdio_src(&cinfo, infile);

    jpeg_read_header(&cinfo, TRUE);

    cinfo.out_color_space = JCS_RGB;

    jpeg_start_decompress(&cinfo);

    int row_stride = cinfo.output_width * cinfo.output_components;

    JSAMPARRAY rowbuffer = (*cinfo.mem->alloc_sarray)
            ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

    this->width = cinfo.output_width;
    this->height = cinfo.output_height;
    this->components = cinfo.output_components;
    this->size = this->height * this->width * this->components;
    this->pixels = (unsigned char *) malloc(this->size);
    unsigned char *currPtr = this->pixels;

    currPtr = this->pixels + row_stride * (cinfo.output_height-1);

    while(cinfo.output_scanline < cinfo.image_height)
    {
        jpeg_read_scanlines(&cinfo, rowbuffer, 1);
        currPtr = copyScanline(currPtr, rowbuffer[0], row_stride);
    }

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

    fclose(infile);
    return this->pixels;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值