JPG to BMP (using Libjpeg)

Source: Internet
Author: User
jpg to BMP (using Libjpeg)

Or something about the image format. The Libjpeg library is used to convert JPEG images to BMP format. Decompression principle is still relatively complex, in the future opportunity may be introduced in detail. This is just the use of the library.

First need to download the Libjpeg library, the URL is here: http://www.ijg.org/

Then you need to configure the environment, I was under windows with VS2010, the compilation library can refer to this article. It's OK to compile the jpeg.lib. Of course, the actual programming also requires the corresponding header file, the header file in the downloaded file.

Download it if you don't want to compile it: http://vdisk.weibo.com/s/jpiMs

The following is the appropriate routine that converts only 24-bit color and 8-bit depth-map jpg to BMP.

#include <iostream> #include <stdio.h> extern "C" {#include "jpeglib.h"};

#pragma comment (lib, "Jpeg.lib") using namespace std;        #pragma pack (2)//Two byte alignment, otherwise Bmp_fileheader will account for 16Byte struct Bmp_fileheader {unsigned short bftype;
    If not aligned, this will account for 4Byte unsigned long bfsize;
    unsigned short bfReverved1;
    unsigned short bfReverved2;
unsigned long bfoffbits;

};
    struct Bmp_infoheader {unsigned long bisize;
    unsigned long biwidth;
    unsigned long biheight;
    unsigned short biplanes;
    unsigned short bibitcount;
    unsigned long bicompression;
    unsigned long bisizeimage;
    unsigned long bixpelspermeter;
    unsigned long biypelspermeter;
    unsigned long biclrused;
unsigned long biclrimportant;

};
FILE *input_file;

FILE *output_file;
    void Write_bmp_header (j_decompress_ptr cinfo) {struct Bmp_fileheader bfh;

    struct Bmp_infoheader BiH;
    unsigned long width; unsigned long Height
    unsigned short depth;
    unsigned long headersize;

    unsigned long filesize;
    width=cinfo->output_width;
    height=cinfo->output_height;

    depth=cinfo->output_components;
        if (depth==1) {headersize=14+40+256*4;
    Filesize=headersize+width*height;
        } if (depth==3) {headersize=14+40;
    filesize=headersize+width*height*depth;
    } memset (&bfh,0,sizeof (struct bmp_fileheader));
    
    memset (&bih,0,sizeof (struct bmp_infoheader));
    Write a few key BMP head parameters bfh.bftype=0x4d42;
    Bfh.bfsize=filesize;

    Bfh.bfoffbits=headersize;
    bih.bisize=40;
    Bih.biwidth=width;
    Bih.biheight=height;
    Bih.biplanes=1;
    bih.bibitcount= (unsigned short) depth*8;

    bih.bisizeimage=width*height*depth;
    Fwrite (&bfh,sizeof (struct bmp_fileheader), 1,output_file);

    Fwrite (&bih,sizeof (struct bmp_infoheader), 1,output_file); if (depth==1)//Grayscale image to add palette {unsigned char *Platte;
        platte=new unsigned char[256*4];
        unsigned char j=0;
            for (int i=0;i<1024;i+=4) {platte[i]=j;
            Platte[i+1]=j;
            Platte[i+2]=j;
            platte[i+3]=0;
        j + +;
        } fwrite (platte,sizeof (unsigned char) *1024,1,output_file);
    Delete[] Platte;
    } void Write_bmp_data (J_decompress_ptr cinfo,unsigned char *src_buff) {unsigned char *dst_width_buff;

    unsigned char *point;
    unsigned long width;
    unsigned long height;

    unsigned short depth;
    width=cinfo->output_width;
    height=cinfo->output_height;

    depth=cinfo->output_components;
    dst_width_buff=new unsigned char[width*depth];

    memset (dst_width_buff,0,sizeof (unsigned char) *width*depth);    point=src_buff+width*depth* (height-1); Write the data backwards, BMP format is inverted, JPG is positive for (unsigned long i=0;i

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.