首页 > 编程 > C++ > 正文

C语言实现BMP转换JPG的方法

2020-05-23 14:17:56
字体:
来源:转载
供稿:网友

这篇文章主要介绍了C语言实现BMP转换JPG的方法,涉及C#图片格式转换的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C语言实现BMP转换JPG的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  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. FILE *fd; 
  24. int ret; 
  25. unsigned char *data; 
  26. long sizeImage; 
  27. int depth = 3; 
  28. JSAMPROW * row_pointer; 
  29. long rgb_index = 0; 
  30. int i=0; 
  31. struct jpeg_compress_struct cinfo; 
  32. struct jpeg_error_mgr jerr; 
  33. FILE *outfile;  
  34. // Read bmp image data 
  35. sizeImage = width*height*3; 
  36. data = (unsigned char*)malloc(sizeImage); 
  37. fd = fopen(bmp_file, "rb"); 
  38. if(!fd) 
  39. printf("ERROR1: Can not open the image./n"); 
  40. free(data); 
  41. return -1; 
  42. fseek(fd, 54, SEEK_SET);  
  43. ret = fread(data, sizeof(unsigned char)*sizeImage, 1, fd); 
  44. if(ret == 0) 
  45. if(ferror(fd)) 
  46. printf("/nERROR2: Can not read the pixel data./n"); 
  47. free(data); 
  48. fclose(fd); 
  49. return -1; 
  50. //Convert BMP to JPG 
  51. cinfo.err = jpeg_std_error(&jerr); 
  52. //* Now we can initialize the JPEG compression object. 
  53. jpeg_create_compress(&cinfo); 
  54. if ((outfile = fopen(jeg_file, "wb")) == NULL) 
  55. fprintf(stderr, "can't open %s/n", jeg_file); 
  56. return -1; 
  57. jpeg_stdio_dest(&cinfo, outfile); 
  58. cinfo.image_width = width; 
  59. //* image width and height, in pixels 
  60. cinfo.image_height = height; 
  61. cinfo.input_components = depth; 
  62. //* # of color components per pixel 
  63. cinfo.in_color_space = JCS_RGB; 
  64. //* colorspace of input image 
  65. jpeg_set_defaults(&cinfo); 
  66. //Now you can set any non-default parameters you wish to. 
  67. //Here we just illustrate the use of quality (quantization table) scaling: 
  68. jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE ); 
  69. //* limit to baseline-JPEG values 
  70. jpeg_start_compress(&cinfo, TRUE); 
  71. //一次写入 
  72. int j=0; 
  73. row_pointer = malloc(height*width*3); 
  74. char * line[300]; 
  75. for(i=0;i<height;i++) 
  76. {  
  77. unsigned char * lineData = NULL; 
  78. lineData = malloc(width*3); 
  79. line[i]=lineData; 
  80. for(j=0;j<width;j++) 
  81. lineData[j*3+2] = data[rgb_index]; 
  82. rgb_index ++; 
  83. lineData[j*3+1] = data[rgb_index]; 
  84. rgb_index ++; 
  85. lineData[j*3+0] = data[rgb_index]; 
  86. rgb_index ++; 
  87. row_pointer[height-i-1] = lineData;  
  88. jpeg_write_scanlines(&cinfo, row_pointer, height); 
  89. jpeg_finish_compress(&cinfo); 
  90. jpeg_destroy_compress(&cinfo); 
  91. for (i=0; i<height; i++) 
  92. free(line[i]); 
  93. }  
  94. free(row_pointer); 
  95. free(data);  
  96. fclose(fd); 
  97. fclose(outfile); 
  98. return 0; 

希望本文所述对大家的C语言程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表