调用jpeg库解压多张图片,第一张图片正常,第二张图片出错
    int i, j,k;
    ICON_INFO *info;
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * input_file;
    JDIMENSION num_scanlines;
    int row_stride;
    JSAMPARRAY buffer;
    unsigned char * dst_buffer;
    unsigned short *dst;
    unsigned char * src;
    /*jpeg decode for logo*/ 
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    /* init icon */
    info = icon_info;
    j = sizeof(icon_info)/sizeof(icon_info[0]);
    printf("=======j=%d=====\n",j);
    for (i = 0; i < j; i++, info++)
    {
        printf("=======i=%d=====\n",i);
        printf(info->name);
        if ((input_file = fopen(info->name, "rb")) == NULL) {
            fprintf(stderr, "can't open %s\n",info->name);
            exit(EXIT_FAILURE);
        }

        /* Specify data source for decompression */
        jpeg_stdio_src(&cinfo, input_file);
        printf("************1\n");
        /* Read file header, set default decompression parameters */
        (void) jpeg_read_header(&cinfo, TRUE);
        printf("************2\n");
        /* Start decompressor */
        (void) jpeg_start_decompress(&cinfo);
        printf("************3\n");
        printf("cinfo.output_width:%d\n",cinfo.output_width);
        printf("cinfo.output_height:%d\n",cinfo.output_height);
        printf("cinfo.output_components:%d\n",cinfo.output_components);

        dst_buffer = malloc(cinfo.output_width*cinfo.output_height*(cinfo.output_components + 1));
        if(NULL == dst_buffer) {
            printf("failed to malloc for icon_logo.jpg \n");
            return -1;
        }
        dst = (unsigned short *)dst_buffer;
        icon_tbl[i] = (int)dst_buffer;

        row_stride = cinfo.output_width * cinfo.output_components;
        buffer = (*cinfo.mem->alloc_sarray)
            ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
        /* Process data */

        while (cinfo.output_scanline < cinfo.output_height) {
            num_scanlines = jpeg_read_scanlines(&cinfo, buffer, 1);

            //argb8888
            src = (unsigned char *)buffer[0];
            for(k=0; k<cinfo.output_width; k++) {
                *dst_buffer = *(src + 2);
                *(dst_buffer + 1) = *(src + 1);
                *(dst_buffer + 2) = *src;
                src += 3;
                dst_buffer +=3;
                *dst_buffer++ = 0xE0;
            }


#endif
        }

        /*end jpeg decode */
    }

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

图片说明

出错是在图片说明

2个回答

是我没有关闭之前打开的文件以及没有调用结束解压的函数才会出这个问题

颠倒下图的顺序,看看是不是图片本身的格式有问题

qq_36769014
善信静思 不是的,问题已经解决了。谢谢回答
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问