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

来源:互联网 发布:徕卡全站仪数据导出 编辑:程序博客网 时间:2024/05/05 17:16
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