The method of realizing BMP transform JPG by C language _c language

Source: Internet
Author: User
Tags bmp image

This article describes the C language implementation of BMP conversion JPG method. Share to everyone for your reference. The implementation method is as follows:

/**************************************************************************** Name: jpeg.c function: Linux under BMP conversion to the JPEG program source code Date: 2010.01.26 Note: Compile-time plus "-ljpeg" (Gcc-o bmp2jpg jpeg.c-ljpeg) ******************************************************** /#include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <linux/videodev.h> #include <fcntl.h> #include
<unistd.h> #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <signal.h> #include <sys/timeb.h> #include <jpeglib.h> #define JPEG_QUALITY 95//Picture quality int bmp2jpg (const char *bmp_file, C
  Onst char *jeg_file, const int width, const int height) {file *fd;
  int ret;
  unsigned char *data;
  Long Sizeimage;
  int depth = 3;
  Jsamprow * Row_pointer;
  Long Rgb_index = 0;
  int i=0;
  struct Jpeg_compress_struct cinfo;
  struct Jpeg_error_mgr jerr;         FILE *outfile;   
  ;
  Read BMP image Data Sizeimage = width*height*3;
  data = (unsigned char*) malloc (sizeimage);
  FD = fopen (Bmp_file, "RB");
    if (!FD) {printf ("Error1:can not open the image.\n");
    Free (data);
  return-1; 
  } fseek (fd, si, seek_set);
  RET = fread (data, sizeof (unsigned char) *sizeimage, 1, FD);
      if (ret = 0) {if (ferror (FD)) {printf ("\nerror2:can not read the pixel data.\n");
      Free (data);
      Fclose (FD);
    return-1;
  }//convert BMP to JPG cinfo.err = Jpeg_std_error (&jerr);
  * Now we can initialize the JPEG compression object.
  Jpeg_create_compress (&cinfo);
    if (outfile = fopen (Jeg_file, "wb") = = NULL) {fprintf (stderr, "can ' t open%s\n", jeg_file);
  return-1;
  } jpeg_stdio_dest (&cinfo, outfile);
  Cinfo.image_width = width;
  * Image width and height, in pixels cinfo.image_height = height;
  cinfo.input_components = depth; * # of color components per pixel Cinfo.in_color_spaCE = Jcs_rgb;
   * ColorSpace of input image jpeg_set_defaults (&cinfo);
   Now your can set any Non-default parameters you wish to. Here we are just illustrate the use of quality (quantization table) scaling:jpeg_set_quality (&cinfo, jpeg_quality, TR
  UE);
  * Limit to Baseline-jpeg values jpeg_start_compress (&cinfo, TRUE);
  write int j=0 once;
  Row_pointer = malloc (height*width*3);
  char * line[300];
    for (i=0;i 

I hope this article will help you with your C language program.

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.