楼主: Lisrelchen
1297 0

[Case Study]C语言实现BMP转换JPG的方法 [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-11-26 09:42:52 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. /****************************************************************************
  2. 名称:  jpeg.c
  3. 功能:  linux下bmp转化为jpeg程序源代码
  4. 日期:  2010.01.26
  5. 注意:  编译时加“-ljpeg”(gcc -o bmp2jpg jpeg.c -ljpeg)
  6. *****************************************************************************/
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/mman.h>
  12. #include <linux/videodev.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <signal.h>
  19. #include <sys/timeb.h>
  20. #include <jpeglib.h>
  21. #define JPEG_QUALITY 95 //图片质量
  22. int Bmp2Jpg(const char *bmp_file, const char *jeg_file, const int width, const int height)
  23. {
  24.   FILE *fd;
  25.   int ret;
  26.   unsigned char *data;
  27.   long sizeImage;
  28.   int depth = 3;
  29.   JSAMPROW * row_pointer;
  30.   long rgb_index = 0;
  31.   int i=0;
  32.   struct jpeg_compress_struct cinfo;
  33.   struct jpeg_error_mgr jerr;
  34.   FILE *outfile;            
  35.   ;
  36.   // Read bmp image data
  37.   sizeImage = width*height*3;
  38.   data = (unsigned char*)malloc(sizeImage);
  39.   fd = fopen(bmp_file, "rb");
  40.   if(!fd)
  41.   {
  42.     printf("ERROR1: Can not open the image.\n");
  43.     free(data);
  44.     return -1;
  45.   }
  46.   fseek(fd, 54, SEEK_SET);
  47.   ret = fread(data, sizeof(unsigned char)*sizeImage, 1, fd);
  48.   if(ret == 0)
  49.   {
  50.     if(ferror(fd))
  51.     {
  52.       printf("\nERROR2: Can not read the pixel data.\n");
  53.       free(data);
  54.       fclose(fd);
  55.       return -1;
  56.     }
  57.   }
  58.   //Convert BMP to JPG
  59.   cinfo.err = jpeg_std_error(&jerr);
  60.   //* Now we can initialize the JPEG compression object.
  61.   jpeg_create_compress(&cinfo);
  62.   if ((outfile = fopen(jeg_file, "wb")) == NULL)
  63.   {
  64.     fprintf(stderr, "can't open %s\n", jeg_file);
  65.     return -1;
  66.   }
  67.   jpeg_stdio_dest(&cinfo, outfile);
  68.   cinfo.image_width = width;
  69.   //* image width and height, in pixels
  70.   cinfo.image_height = height;
  71.   cinfo.input_components = depth;
  72.   //* # of color components per pixel
  73.   cinfo.in_color_space = JCS_RGB;
  74.   //* colorspace of input image
  75.   jpeg_set_defaults(&cinfo);
  76.    //Now you can set any non-default parameters you wish to.
  77.    //Here we just illustrate the use of quality (quantization table) scaling:
  78.   jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE );
  79.   //* limit to baseline-JPEG values
  80.   jpeg_start_compress(&cinfo, TRUE);
  81.   //一次写入
  82.   int j=0;
  83.   row_pointer = malloc(height*width*3);
  84.   char * line[300];
  85.   for(i=0;i<height;i++)
  86.   {  
  87.     unsigned char * lineData = NULL;
  88.     lineData = malloc(width*3);
  89.     line[i]=lineData;
  90.     for(j=0;j<width;j++)
  91.     {
  92.       lineData[j*3+2] = data[rgb_index];
  93.       rgb_index ++;
  94.       lineData[j*3+1] = data[rgb_index];
  95.       rgb_index ++;
  96.       lineData[j*3+0] = data[rgb_index];
  97.       rgb_index ++;
  98.     }
  99.     row_pointer[height-i-1] = lineData;   
  100.   }
  101.   jpeg_write_scanlines(&cinfo, row_pointer, height);
  102.   jpeg_finish_compress(&cinfo);
  103.   jpeg_destroy_compress(&cinfo);
  104.   for (i=0; i<height; i++)
  105.   {
  106.     free(line[i]);
  107.   }
  108.   free(row_pointer);
  109.   free(data);
  110.   fclose(fd);
  111.   fclose(outfile);
  112.   return 0;
  113. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Case study study Case JPG CAS C语言

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 08:18