Or something about the image format. The libjpeg library is used to convert JPEG images to the BMP format. The decompression principle is still relatively complex and may be detailed in the future. This is just the use of the library.
First download libjpeg library, URL here: http://www.ijg.org/
Then you need to configure the environment. I used vs2010 in windows. For the compilation library, refer to this article. Compile jpeg. Lib. Of course, the actual programming also requires the corresponding header file, which is included in the downloaded file.
If you don't want to compile it, download it here: http://vdisk.weibo.com/s/jpiMs
The following is an example. Only JPG of a 24-bit color image and an 8-bit depth image can be converted to BMP.
# Include <iostream> # include <stdio. h> extern "C" {# include "expose Lib. H "};# Pragma comment (Lib," Jpeg. lib ") using namespace STD; # pragma pack (2) // two-byte alignment; otherwise, the BMP _ fileheader occupies 16 bytestruct BMP _fileheader {unsigned short bftype; // if not alignment, this will occupy 4 byte unsigned long bfsize; unsigned short blocks; 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 history;}; file * input_file; file * output_file; void encode (effeccinfo) {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); // Several Key BMP header parameters for writing. 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) // Add the palette to the grayscale image {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 * cursor; 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 (depth, 0, sizeof (unsigned char) * width * depth); point = src_buff + width * depth * (height-1); // write data backwards. The BMP format is inverted, JPG is positive for (unsigned long I = 0; I
Note: There is a memory leak in write_bmp _data () and you don't want to edit it in the source code.