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.