国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C語言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法

2020-05-23 14:17:56
字體:
供稿:網(wǎng)友

這篇文章主要介紹了C語言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法,涉及C#圖片格式轉(zhuǎn)換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C語言實(shí)現(xiàn)BMP轉(zhuǎn)換JPG的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

 

 
  1. /**************************************************************************** 
  2. 名稱: jpeg.c 
  3. 功能: linux下bmp轉(zhuǎn)化為jpeg程序源代碼 
  4. 日期: 2010.01.26 
  5. 注意: 編譯時(shí)加“-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 //圖片質(zhì)量 
  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; 

希望本文所述對(duì)大家的C語言程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 依安县| 石城县| 榆中县| 孝义市| 尚义县| 峨边| 罗定市| 东平县| 紫金县| 乌审旗| 望城县| 威信县| 镇沅| 永顺县| 蒲城县| 五峰| 大荔县| 咸丰县| 嘉兴市| 桐庐县| 金阳县| 开阳县| 天祝| 饶河县| 宁陵县| 屯门区| 金山区| 平果县| 鲜城| 襄樊市| 东兴市| 五莲县| 榆社县| 苏尼特左旗| 峨山| 定陶县| 洪洞县| 凭祥市| 搜索| 迁安市| 克东县|