关闭

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

563人阅读 评论(0) 收藏 举报
分类:
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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:231577次
    • 积分:3133
    • 等级:
    • 排名:第11050名
    • 原创:163篇
    • 转载:40篇
    • 译文:4篇
    • 评论:19条
    博客专栏
    最新评论