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! */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值