Guest User

Untitled

a guest
Sep 29th, 2012
209
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <jpeglib.h>
  3. #include <stdlib.h>
  4.  
  5. /* we will be using this uninitialized pointer later to store raw, uncompressd image */
  6. unsigned char *raw_image = NULL;
  7.  
  8. /* dimensions of the image we want to write */
  9. int width = 1600;
  10. int height = 1200;
  11. int bytes_per_pixel = 3; /* or 1 for GRACYSCALE images */
  12. J_COLOR_SPACE color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
  13.  
  14. /**
  15. * read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the
  16. * raw_image buffer in an uncompressed format.
  17. *
  18. * \returns positive integer if successful, -1 otherwise
  19. * \param *filename char string specifying the file name to read from
  20. *
  21. */
  22.  
  23. int read_jpeg_file( char *filename )
  24. {
  25. /* these are standard libjpeg structures for reading(decompression) */
  26. struct jpeg_decompress_struct cinfo;
  27. struct jpeg_error_mgr jerr;
  28. /* libjpeg data structure for storing one row, that is, scanline of an image */
  29. JSAMPROW row_pointer[1];
  30.  
  31. FILE *infile = fopen( filename, "rb" );
  32. unsigned long location = 0;
  33. int i = 0;
  34.  
  35. if ( !infile )
  36. {
  37. printf("Error opening jpeg file %s\n!", filename );
  38. return -1;
  39. }
  40. /* here we set up the standard libjpeg error handler */
  41. cinfo.err = jpeg_std_error( &jerr );
  42. /* setup decompression process and source, then read JPEG header */
  43. jpeg_create_decompress( &cinfo );
  44. /* this makes the library read from infile */
  45. jpeg_stdio_src( &cinfo, infile );
  46. /* reading the image header which contains image information */
  47. jpeg_read_header( &cinfo, TRUE );
  48. /* Uncomment the following to output image information, if needed. */
  49. /*--
  50. printf( "JPEG File Information: \n" );
  51. printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
  52. printf( "Color components per pixel: %d.\n", cinfo.num_components );
  53. printf( "Color space: %d.\n", cinfo.jpeg_color_space );
  54. --*/
  55. /* Start decompression jpeg here */
  56. jpeg_start_decompress( &cinfo );
  57.  
  58. /* allocate memory to hold the uncompressed image */
  59. raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
  60. /* now actually read the jpeg into the raw buffer */
  61. row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
  62. /* read one scan line at a time */
  63. while( cinfo.output_scanline < cinfo.image_height )
  64. {
  65. jpeg_read_scanlines( &cinfo, row_pointer, 1 );
  66. for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
  67. raw_image[location++] = row_pointer[0][i];
  68. }
  69. /* wrap up decompression, destroy objects, free pointers and close open files */
  70. jpeg_finish_decompress( &cinfo );
  71. jpeg_destroy_decompress( &cinfo );
  72. free( row_pointer[0] );
  73. fclose( infile );
  74. /* yup, we succeeded! */
  75. return 1;
  76. }
  77.  
  78. /**
  79. * write_jpeg_file Writes the raw image data stored in the raw_image buffer
  80. * to a jpeg image with default compression and smoothing options in the file
  81. * specified by *filename.
  82. *
  83. * \returns positive integer if successful, -1 otherwise
  84. * \param *filename char string specifying the file name to save to
  85. *
  86. */
  87. int write_jpeg_file( char *filename )
  88. {
  89. struct jpeg_compress_struct cinfo;
  90. struct jpeg_error_mgr jerr;
  91.  
  92. /* this is a pointer to one row of image data */
  93. JSAMPROW row_pointer[1];
  94. FILE *outfile = fopen( filename, "wb" );
  95.  
  96. if ( !outfile )
  97. {
  98. printf("Error opening output jpeg file %s\n!", filename );
  99. return -1;
  100. }
  101. cinfo.err = jpeg_std_error( &jerr );
  102. jpeg_create_compress(&cinfo);
  103. jpeg_stdio_dest(&cinfo, outfile);
  104.  
  105. /* Setting the parameters of the output file here */
  106. cinfo.image_width = width;
  107. cinfo.image_height = height;
  108. cinfo.input_components = bytes_per_pixel;
  109. cinfo.in_color_space = color_space;
  110. /* default compression parameters, we shouldn't be worried about these */
  111. jpeg_set_defaults( &cinfo );
  112. /* Now do the compression .. */
  113. jpeg_start_compress( &cinfo, TRUE );
  114. /* like reading a file, this time write one row at a time */
  115. while( cinfo.next_scanline < cinfo.image_height )
  116. {
  117. row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
  118. jpeg_write_scanlines( &cinfo, row_pointer, 1 );
  119. }
  120. /* similar to read file, clean up after we're done compressing */
  121. jpeg_finish_compress( &cinfo );
  122. jpeg_destroy_compress( &cinfo );
  123. fclose( outfile );
  124. /* success code is 1! */
  125. return 1;
  126. }
  127.  
  128. int main()
  129. {
  130. char *infilename = "test.jpg", *outfilename = "test_out.jpg";
  131.  
  132. /* Try opening a jpeg*/
  133. if( read_jpeg_file( infilename ) > 0 )
  134. {
  135. /* then copy it to another file */
  136. if( write_jpeg_file( outfilename ) < 0 ) return -1;
  137. }
  138. else return -1;
  139. return 0;
  140. }
RAW Paste Data