qq_36769014
未_定
采纳率50%
2019-04-23 11:17 阅读 304

调用jpeg库解压多张图片,第一张图片正常,第二张图片出错

5
    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 未_定 2019-04-23 12:19

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

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2019-04-23 11:35

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

    点赞 评论 复制链接分享

相关推荐