RGBtoJPEG

參考很多人的程序,使用JPEGlib  源代碼:

===========================================================================

#include <stdio.h>
#include <unistd.h>
#include <jpeglib.h>

#include "head.h"   //IMGWIDTH and IMGHEIGHT

void rgb_to_jpeg (char *filename, unsigned char *img, /
 int quality, int gray)
{
     struct jpeg_compress_struct jcoms;
     struct jpeg_error_mgr jerr;  /* More stuff */

     FILE *fp;  /* target file */

     JSAMPROW row_pointer[1];   /* pointer to JSAMPLE row[s] */

     int line_length, m;    /* physical row width in image buffer */
     unsigned char *line;

     if(debug) 
          printf("begin to start compress!/n");
 
     jcoms.err = jpeg_std_error(&jerr);


     jpeg_create_compress(&jcoms);
     if(debug) 
          printf("create compress OK!/n");
     if ((fp = fopen(filename, "w")) == NULL)
      {
          fprintf(stderr, "can't open %s/n", filename);
          exit(1);
        }

 

     jpeg_stdio_dest(&jcoms, fp);

     jcoms.image_width = IMGWIDTH; 
     jcoms.image_height = IMGHEIGHT;
     jcoms.input_components = gray ? 1 : 3;//3 colour

     jcoms.in_color_space = gray ? /
      JCS_GRAYSCALE : JCS_RGB;/*colorspace*/

 

    jpeg_set_defaults(&jcoms);
     jpeg_set_quality(&jcoms, quality, TRUE);/* limit to baseline-JPEG values */
     if(debug) 
          printf("set quality OK!/n");

     jpeg_start_compress(&jcoms, TRUE);

      if(debug)
          printf("start compress OK!/n");

     line_length = gray ? IMGWIDTH : IMGWIDTH * 3;

 /* JSAMPLEs per row in image_buffer */
     if(debug) 
          printf("begin to write scanlines/n");
 
     for (m = 0, line = img;  m < IMGHEIGHT; /
          m++, line +=line_length)
      jpeg_write_scanlines(&jcoms, &line, 1);

/* Step 6: Finish compression */
     jpeg_finish_compress(&jcoms);
     if (debug) 
      printf("finish compress OK!/n");

/* to read the jpg data to the image buffer*/

    /* After finish_compress, we can close the output file. */
     fclose(fp);

     jpeg_destroy_compress(&jcoms);

     if(debug) 
          printf("destroy compress OK!/n");
//return statbuf.st_size;
 /* And we're done! */
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章