Using Libjpeg for picture reading

Source: Internet
Author: User
1 Introduction

Libjpeg a picture decoding library, in the project need to read into the picture, but do not want to rely on OPENCV interface, this can libjpeg complete decoding. Libjpeg has two versions, one of the original Libjpeg, the other is Libjpeg-turbo, which is a decoding library using SIMD instruction acceleration, about 3 times times the speed of Libjpeg, code see https://github.com/ Libjpeg-turbo/libjpeg-turbo 2 compile download source code, cmake,nasm reference (https://github.com/libjpeg-turbo/libjpeg-turbo/blob/ MASTER/BUILDING.MD) 3 code decoding module

struct Jpeginfo {unsigned int width;
    unsigned int height;
    ColorType ColorType;
unsigned char* dstimg;
}; /* * Enter picture name, output unsigned char* picture data * */int readjpeg (std::string file_name, jpeginfo &jpeginfo) {struct JPEG_DE
    Compress_struct Cinfo;
    struct My_error_mgr jerr;
    FILE * infile;
    Jsamparray buffer;
    int row_stride;
    errno_t err; if (err = fopen_s (&infile, File_name.c_str (), "RB"))! = 0) {fprintf (stderr, "can ' t open%s\n", file_name
        );
    return-1;
    } Cinfo.err = Jpeg_std_error (&jerr.pub);
    Jerr.pub.error_exit = My_error_exit;
        if (setjmp (Jerr.setjmp_buffer)) {jpeg_destroy_decompress (&cinfo);
        Fclose (infile);
    return-1;
    } jpeg_create_decompress (&cinfo);

    JPEG_STDIO_SRC (&cinfo, infile);

    (void) Jpeg_read_header (&cinfo, TRUE);
    (void) jpeg_start_decompress (&cinfo);

    Row_stride = Cinfo.output_width * cinfo.output_components; int cols= Cinfo.output_width;

    int rows = Cinfo.output_height;


    Buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, Jpool_image, Row_stride, 1);
    Jpeginfo.width = Cinfo.output_width;
    Jpeginfo.height = Cinfo.output_height;
        if (cinfo.output_components = = 3) {jpeginfo.colortype = RGB;

    jpeginfo.dstimg = new unsigned char[jpeginfo.width*jpeginfo.height * 3+1];
        }//gray else{jpeginfo.colortype = Gray;
    jpeginfo.dstimg = new unsigned char[jpeginfo.width * jpeginfo.height+1];
        } while (Cinfo.output_scanline < cinfo.output_height) {(void) jpeg_read_scanlines (&cinfo, buffer, 1); for (size_t i = 0; i < cinfo.output_width; i++) {//rgb if (jpeginfo.colortype = = RGB) {jpeginfo.dstimg[(cinfo.output_scanline-1) *jpeginfo.width*3 + i*3] = Buffer[0][i *
                3]; jpeginfo.dstimg[(cinfo.output_scanline-1) *jpeginfo.width*3 + i*3+ 1] = buffer[0][i * 3 + 1];
            jpeginfo.dstimg[(cinfo.output_scanline-1) *jpeginfo.width *3+ i*3 + 2] = buffer[0][i * 3 + 2]; } else if (Jpeginfo.colortype = = Gray) {jpeginfo.dstimg[(Cinfo.output_scanline-
            1) *jpeginfo.width + i] = buffer[0][i];

    }}} (void) jpeg_finish_decompress (&cinfo);
    Jpeg_destroy_decompress (&cinfo);
    Fclose (infile);

return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.