⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample.c

📁 DigitalImageProcessing_base_on_Matlab 基于Matlab的数字图像处理
💻 C
字号:
/* Example program for reading and writing JPEG images */

#include <stdio.h>
#include <stdlib.h>
#include "jpeg.h"

#ifndef size_t
  #define size_t unsigned
#endif

#define SUCCESSED	0
#define FAILED		1



int main(int argc, char **argv)
{
  FILE *fin, *fout;
  struct JPEG_img img_in, img_out;
  int i,j,m,n;

  if (argc != 3) {
    fprintf(stderr, "\nUsage: %s infile outfile\n", argv[0]);
    exit(-1);
  }

  if((fin = fopen(argv[1], "rb")) == NULL) {
    fprintf(stderr, "\nError: can't read from %s\n", argv[1]); 
    exit(-1);
  }

  if (read_JPEG(fin, &img_in) == FAILED) {
    fprintf(stderr, "\nError: JPEG read failed\n"); 
    fclose(fin);
    exit(-1);
  }
  fclose(fin);

  if( img_in.jpeg_type == 'g') {
    /* set up structure for output image */
    get_JPEG ( &img_out, 2*img_in.height, 2*img_in.width, 'g' );

    /* pixel replication */
    for ( i = 0; i < img_in.height; i++ )
    for ( j = 0; j < img_in.width; j++ )
      for ( m = 0; m < 2; m++ )
      for ( n = 0; n < 2; n++ ) {
        img_out.mono[2*i+m][2*j+n] = img_in.mono[i][j];
      }


  } if( (img_in.jpeg_type == 'c') ) {
    /* set up structure for output image */
    get_JPEG ( &img_out, 2*img_in.height, 2*img_in.width, 'c' );

    /* pixel replication */
    for ( i = 0; i < img_in.height; i++ )
    for ( j = 0; j < img_in.width; j++ )
      for ( m = 0; m < 2; m++ )
      for ( n = 0; n < 2; n++ ) {
        img_out.color[0][2*i+m][2*j+n] = img_in.color[0][i][j];
        img_out.color[1][2*i+m][2*j+n] = img_in.color[1][i][j];
        img_out.color[2][2*i+m][2*j+n] = img_in.color[2][i][j];
      }


  } else {
    fprintf(stderr, "\nUnknown Image type\n");
    exit(-1);
  }

  if((fout = fopen(argv[2], "wb"))==NULL) {
    fprintf(stderr,"\nError: can't write to %s\n", argv[2]); 
    exit(-1);
  }

  if( write_JPEG(fout, &img_out, 95, 0) == FAILED) { /* quality=95, smooth=0 */
     fprintf(stderr,"\nError: can't write to JPEG image %s\n", argv[2]); 
     fclose(fout);
     exit(-1);
  }
  fclose(fout);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -