Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1095122
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 746
  • 用 户 组: 普通用户
  • 注册时间: 2018-06-12 20:01
个人简介

寫写code、调調bug、填填坑,僅此而已。

文章分类

全部博文(80)

文章存档

2019年(30)

2018年(50)

分类: C/C++

2019-03-22 18:56:28


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <jpeglib.h>
  3. #include <stdlib.h>

  4. /* we will be using this uninitialized pointer later to store raw, uncompressd image */
  5. unsigned char *raw_image = NULL;

  6. /* dimensions of the image we want to write */
  7. int width = 385;
  8. int height = 243;


  9. /**
  10. * read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the
  11. * raw_image buffer in an uncompressed format.
  12. *
  13. * \returns positive integer if successful, -1 otherwise
  14. * \param *filename char string specifying the file name to read from
  15. *
  16. */

  17. int read_jpeg_file( char *filename )
  18. {
  19. /* these are standard libjpeg structures for reading(decompression) */
  20. struct jpeg_decompress_struct cinfo;
  21. struct jpeg_error_mgr jerr;
  22. /* libjpeg data structure for storing one row, that is, scanline of an image */
  23. JSAMPROW row_pointer[1];

  24. FILE *infile = fopen( filename, "rb" );
  25. unsigned long location = 0;
  26. int i = 0;

  27. if ( !infile )
  28. {
  29. printf("Error opening jpeg file %s\n!", filename );
  30. return -1;
  31. }
  32. /* here we set up the standard libjpeg error handler */
  33. cinfo.err = jpeg_std_error( &jerr );
  34. /* setup decompression process and source, then read JPEG header */
  35. jpeg_create_decompress( &cinfo );
  36. /* this makes the library read from infile */
  37. jpeg_stdio_src( &cinfo, infile );
  38. /* reading the image header which contains image information */
  39. jpeg_read_header( &cinfo, TRUE );
  40. printf("orig width==%d,height=%d\n",cinfo.image_width,cinfo.image_height);
  41. /* Uncomment the following to output image information, if needed. */
  42. /*--
  43. printf( "JPEG File Information: \n" );
  44. printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
  45. printf( "Color components per pixel: %d.\n", cinfo.num_components );
  46. printf( "Color space: %d.\n", cinfo.jpeg_color_space );
  47. --*/
  48. /* Start decompression jpeg here */
  49. jpeg_start_decompress( &cinfo );

  50. /* allocate memory to hold the uncompressed image */
  51. raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
  52. /* now actually read the jpeg into the raw buffer */
  53. row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
  54. /* read one scan line at a time */
  55. while( cinfo.output_scanline < cinfo.image_height )
  56. {
  57. jpeg_read_scanlines( &cinfo, row_pointer, 1 );
  58. for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
  59. raw_image[location++] = row_pointer[0][i];
  60. }
  61. /* wrap up decompression, destroy objects, free pointers and close open files */
  62. jpeg_finish_decompress( &cinfo );
  63. jpeg_destroy_decompress( &cinfo );
  64. free( row_pointer[0] );
  65. fclose( infile );
  66. /* yup, we */
  67. return 1;
  68. }

  69. /**
  70. * write_jpeg_file Writes the raw image data stored in the raw_image buffer
  71. * to a jpeg image with default compression and smoothing options in the file
  72. * specified by *filename.
  73. *
  74. * \returns positive integer if successful, -1 otherwise
  75. * \param *filename char string specifying the file name to save to
  76. *
  77. */
  78. int write_jpeg_file( char *filename )
  79. {
  80. struct jpeg_compress_struct cinfo;
  81. struct jpeg_error_mgr jerr;

  82. /* this is a pointer to one row of image data */
  83. JSAMPROW row_pointer[1];
  84. FILE *outfile = fopen( filename, "wb" );

  85. if ( !outfile )
  86. {
  87. printf("Error opening output jpeg file %s\n!", filename );
  88. return -1;
  89. }
  90. cinfo.err = jpeg_std_error( &jerr );
  91. jpeg_create_compress(&cinfo);
  92. jpeg_stdio_dest(&cinfo, outfile);

  93. /* Setting the parameters of the output file here */
  94. cinfo.image_width = width;
  95. cinfo.image_height = height;
  96. cinfo.input_components = 3;
  97. cinfo.in_color_space = JCS_RGB;
  98. /* default compression parameters, we shouldn't be worried about these */

  99. jpeg_set_defaults( &cinfo );
  100. cinfo.num_components = 3;
  101. //cinfo.data_precision = 4;
  102. cinfo.dct_method = JDCT_FLOAT;
  103. jpeg_set_quality(&cinfo, 15, TRUE);
  104. /* Now do the compression .. */
  105. jpeg_start_compress( &cinfo, TRUE );
  106. /* like reading a file, this time write one row at a time */
  107. while( cinfo.next_scanline < cinfo.image_height )
  108. {
  109. row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
  110. jpeg_write_scanlines( &cinfo, row_pointer, 1 );
  111. }

  112. jpeg_finish_compress( &cinfo );
  113. jpeg_destroy_compress( &cinfo );
  114. fclose( outfile );

  115. return 1;
  116. }

  117. int main()
  118. {
  119. char *infilename = "1093.jpg", *outfilename = "test_out.jpg";

  120. if( read_jpeg_file( infilename ) > 0 )
  121. {
  122. if( write_jpeg_file( outfilename ) < 0 ) return -1;
  123. }
  124. else return -1;
  125. return 0;
  126. }

阅读(4348) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~