- 论坛徽章:
- 0
|
如何将BMP转成JPG
已经搞定了,看了libjpeg的源代码
使用libjpeg的静态库,编译的时候增加 -ljpeg
/*
*filename -- jpeg filename
*bits -------- the value of RGB
*width ------jpeg width
*height -----jpeg height
*depth------color components per pixel
*/
- #include <jpeglib.h>;
- int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth)
- {
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- FILE * outfile; /* target file */
- JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
- int row_stride; /* physical row width in image buffer */
- cinfo.err = jpeg_std_error(&jerr);
- /* Now we can initialize the JPEG compression object. */
- jpeg_create_compress(&cinfo);
- if ((outfile = fopen(filename, "wb")) == NULL) {
- fprintf(stderr, "can't open %s\n", filename);
- 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 you can set any non-default parameters you wish to.
- * Here we just illustrate the use of quality (quantization table) scaling:
- */
- jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE /* limit to baseline-JPEG values */);
- jpeg_start_compress(&cinfo, TRUE);
- row_stride = width * 3; /* JSAMPLEs per row in image_buffer */
- while (cinfo.next_scanline < cinfo.image_height) {
- row_pointer[0] = & bits[cinfo.next_scanline * row_stride];
- (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
- }
- jpeg_finish_compress(&cinfo);
- fclose(outfile);
- jpeg_destroy_compress(&cinfo);
- return 0;
- }
复制代码 |
|