Discussion on libjpeg Image Quality Modification

Source: Internet
Author: User
Tags bmp image

Most open-source or non-open-source software to process JPG images when using libjpeg open-source library, the latest version of the libjpeg-8b, download link

Http://freshmeat.net/projects/libjpeg

There are a lot of makefile files in Linux. in Win32, you only need to set up the vc6 project. search for the vc6 suffix and set make * DSP. modify vc6 to make * DSP. DSP, all make * DSW. change vc6 to make * DSW. DSW, here get makeadsw. DSW and makejdsw. DSW, the former for all codec and TestingProgramProject, which is a simple libjpeg project. For libjpeg makejdsw, A Win32. Lib library is generated. Here, you can change the library name to libjpeg. Lib. In this way, you can refer to the encoding program to encode the image as a JPEG image.

For example, we can use GDI + to open images in any format, and then get the decoded data of the image. Then we can compile the image into a JPEG image.

PS: why do we not need to use GDI + to directly save JPG images? The quality of JPEG images generated in GDI + is not satisfactory.

 

// Initialize void write_my_jpeg () {// initialize the data Bitmap bitmap (_ T ("a.bmp"); int iwidth = bitmap. getwidth (); int iheight = bitmap. getheight (); unsigned char * pdataconv = new unsigned char [iwidth * iheight * 3]; init_data (bitmap, pdataconv); struct into _compress_struct JCs; // declare the error processor, and assigned to JCS. err domain struct performance_error_mgr JEM; JCs. err = pai_std_error (& Jem); pai_create_compress (& JCs); file * f = _ wfopen (_ T ("my.jpg"), _ T ("WB ")); if (F = NULL) {Delete [] pdataconv; return;} pai_stdio_dest (& JCs, f); JCs. image_width = iwidth; // the width and height of the image. The unit is pixel JCs. image_height = iheight; JCs. input_components = 3; // here it is 1, indicating a grayscale image. If it is a color bitmap, It is 3jcs. in_color_space = jcs_rgb; // jcs_grayscale indicates a grayscale image, and jcs_rgb indicates a colored image jpeg_set_defaults (& JCs); // specifies the brightness and color quality of JCs. q_scale_factor [0] = pai_quality_scaling (100); JCs. q_scale_factor [1] = pai_quality_scaling (100); // image sampling rate. The default value is 2*2 JCs. comp_info [0]. v_samp_factor = 1; JCs. comp_info [0]. h_samp_factor = 1; // sets the JPEG encoding quality. Optional _set_quality (& JCs, 100, true); optional _start_compress (& JCs, true); jsamprow row_pointer [1]; // a row of Bitmap int row_stride; // The number of bytes in each row row_stride = JCs. image_width * 3; // if it is not an index graph, multiply it by 3. // compress each row while (JCs. next_scanline <JCs. image_height) {row_pointer [0] = (jsamprow) (pdataconv + JCs. next_scanline * row_stride); pai_write_scanlines (& JCs, row_pointer, 1);} pai_finish_compress (& JCs); consume (& JCs); // Delete [] pdataconv ;}

// Data Conversion

 // datavoid init_data (Bitmap & bitmap, unsigned char * pdataconv) {// btimapint iwidth = bitmap. getwidth (); int iheight = bitmap. getheight (); gdiplus: bitmapdata; gdiplus: rect rectpiece (0, 0, iwidth, iheight); // lock to obtain bitmap data bitmap. lockbits (& rectpiece, gdiplus: imagelockmodewrite, pixelformat32bppargb, & bitmapdata); int ibitmappiecestride = bitmapdata. stride; unsigned Lo NG * pbitmappiecepixels = (unsigned long *) bitmapdata. scan0; // obtain the pixel data unsigned long ulcolorvaluetemp = 0; unsigned char USR, USG, USB; For (INT y = 0; y! = Iheight; ++ y) {for (INT x = 0; X! = Iwidth; ++ X) {ulcolorvaluetemp = pbitmappiecepixels [y * values/4 + X]; USR = (ulcolorvaluetemp> 16); USG = (ulcolorvaluetemp> 8 ); USB = ulcolorvaluetemp; * pdataconv = USR; ++ pdataconv; * pdataconv = USG; ++ pdataconv; * pdataconv = USB; ++ pdataconv ;}} /// memcpy (pdataconv, pbitmappiecepixels, iwidth * iheight * 3); bitmap. unlockbits (& bitmapdata) ;}

By default, common open-source libraries such as GDI +, cximage, and opencv only provide quality-setting functions, that is

 
Pai_set_quality ()

 

Under normal circumstances, this can also meet our basic needs, but for some images, such as black backgrounds, pictures written in white fonts, when compression, the basic set quality is up to 100, however, the compression quality is still unsatisfactory because of the image sampling rate during compression. When asdsee saves JPEG image formats, there are 2*1 and 1*2 of the sampling rate, the default value of libjpeg is 2*2. Therefore, the quality of ACDSee is better. If you don't mind, you can set it to 1*1. The quality will be better, however, the size of the compressed image may be greater than the size of the original BMP image. For details, refer.

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.