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

📄 test_xform.c

📁 JPEG2000 EBCOT算法源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company                                   */
/* All rights reserved                                                       */
/* Author: David Taubman                                                     */
/* Version: V2.0                                                             */
/* Last Revised: 9/22/98                                                     */
/*****************************************************************************/
#include <local_heap.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <line_block_ifc.h>
#include <test_xform_config.h>

#define MAX_COMPONENTS 256

/*****************************************************************************/
/* STATIC                         local_fopen                                */
/*****************************************************************************/

static FILE *
  local_fopen(char *fname, char *mode)

 /* Same as `fopen', but accepts `-' to identify std-output or std-input. */

{
  if (strcmp(fname,"-") == 0)
    {
#ifdef WIN32
      if (strchr(mode,'b') != NULL)
        {
          fprintf(stderr,"The UNIX \"-\" convention for std-input and/or "
                  "std-output\n  should not be used to open binary files on "
                  "WIN32 platforms!\n");
          exit(-1);
        }
#endif
      if (strchr(mode,'r') != NULL)
        return(stdin);
      else
        return(stdout);
    }
  return(fopen(fname,mode));
}

/*****************************************************************************/
/* STATIC                         local_fclose                               */
/*****************************************************************************/

static void
  local_fclose(FILE *fp)
{
  if ((fp != stdin) && (fp != stdout) && (fp != stderr))
    fclose(fp);
}

/*****************************************************************************/
/* STATIC                          print_usage                               */
/*****************************************************************************/

static void
  print_usage(int default_levels_in, int default_levels_out,
              int default_components_out, char *prog_name,
              FILE *dest)
{
  filter_info_ref info;
  analysis_ref analysis;
  synthesis_ref synthesis;

  fprintf(dest,"\n"
          "Usage: \"%s args...\",\n"
          "       where the following arguments are recognized:\n",prog_name);
  fprintf(dest,
          "  -i <input image file> (mandatory; PGM or PPM for now)\n");
  fprintf(dest,
          "  -o <output image file> (mandatory; PGM or PPM for now)\n");
  fprintf(dest,
          "  -lev <transform levels> (default = %d) -- must be <= 16.\n",
          default_levels_in);
  fprintf(dest,
          "  -rec <reconstructed levels> (default = %d) -- must be <= 16.\n",
          default_levels_out);
  fprintf(dest,
          "  -comp <max reconstructed components> (default = %d).\n",
          default_components_out);
  fprintf(dest,
          "  -fil <built-in filter set> (default = 0)\n"
          "      -- must lie in range 0 to 14.\n"
          "         \"0\" -> Daubechies 9/7 filters.\n");
  fprintf(dest,
          "  -loc <file name for local filter set> "
          "(default = built-in filter set)\n");
  info = info_creator();
  info->print_usage(info,dest);
  info->terminate(info);
  analysis = analysis_creator();
  analysis->print_usage(analysis,dest);
  analysis->terminate(analysis);
  synthesis = synthesis_creator();
  synthesis->print_usage(synthesis,dest);
  synthesis->terminate(synthesis);
  fprintf(dest,
          "  -u [<output file>]\n"
          "      -- print usage statement & exit.  Overrides other args.\n"
          "         Prints to std-error unless output file given.\n"
          "         Accepts UNIX \"-\" convention to identify std-output.\n");
}

/*****************************************************************************/
/* STATIC                    parse_common_arguments                          */
/*****************************************************************************/

static void
  parse_common_arguments(int argc, char *argv[], int *levels_in,
                         int *levels_out, int *components_out,
                         int *filter_set, char **local_filter_name)
{
  for (argc--, argv++; argc > 0; argc--, argv++)
    if (strcmp(*argv,"-lev") == 0)
      {
        *(argv++) = ""; argc--;
        if ((argc == 0) || (sscanf(*argv,"%d",levels_in) == 0) ||
            (*levels_in > 16))
          {
            fprintf(stderr,"\nThe `-lev' argument requires a non-negative "
                    "number of resolution\n  levels, no larger than 16!\n");
            exit(-1);
          }
        *argv = "";
      }
    else if (strcmp(*argv,"-rec") == 0)
      {
        *(argv++) = ""; argc--;
        if ((argc == 0) || (sscanf(*argv,"%d",levels_out) == 0) ||
            (*levels_out > 16))
          {
            fprintf(stderr,"\nThe `-rec' argument requires a non-negative "
                    "number of resolution\n  levels, no larger than 16!\n");
            exit(-1);
          }
        *argv = "";
      }
    else if (strcmp(*argv,"-fil") == 0)
      {
        *(argv++) = ""; argc--;
        if ((argc == 0) || (sscanf(*argv,"%d",filter_set) == 0) ||
            (*filter_set < 0) || (*filter_set > 14))
          {
            fprintf(stderr,"\nThe `-fil' argument requires a built-in "
                    "filter set number\n  in the range 0 to 14!\n");
            exit(-1);
          }
        *argv = "";
      }
    else if (strcmp(*argv,"-loc") == 0)
      {
        *(argv++) = ""; argc--;
        if ((argc == 0) || (strlen(*argv) <= 1))
          {
            fprintf(stderr,"\nThe `-loc' argument requires the name of "
                    "a local filter\n  set file; the name must be at least "
                    "2 characters long!\n");
            exit(-1);
          }
        *local_filter_name = *argv;
        *argv = "";
        *filter_set = 15;
      }
    else if (strcmp(*argv,"-comp") == 0)
      {
        *(argv++) = ""; argc--;
        if ((argc == 0) || (sscanf(*argv,"%d",components_out) == 0))
          {
            fprintf(stderr,"\nThe `-comp' argument requires the number of "
                    "output components!\n");
            exit(-1);
          }
        *argv = "";
      }
}

/*****************************************************************************/
/* STATIC                    check_all_arguments_used                        */
/*****************************************************************************/

static void
  check_all_arguments_used(int argc, char *argv[])
{
  int i;

  for (i=1; i < argc; i++)
    if (*(argv[i]) != '\0')
      fprintf(stderr,"\nWarning:  The command-line argument, \"%s\", does "
              "not\n  appear to have been recognized by any object in the "
              "compression system!\n",argv[i]);
}

/*****************************************************************************/
/* STATIC                          eat_white                                 */
/*****************************************************************************/

static void
  eat_white(FILE *fp)
{
  int ch;

  while ((ch=getc(fp)) != EOF)
    if (ch == '#')
      do {
        ch = getc(fp);
      } while ((ch != '\n') && (ch != EOF));
    else if ((ch != ' ')  && (ch != '\t') && (ch != '\n'))
      {
        ungetc(ch,fp);
        return;
      }
}

/*****************************************************************************/
/* STATIC                       open_input_file                              */
/*****************************************************************************/

static FILE *
  open_input_file(int argc, char *argv[], int *num_rows, int *num_cols,
                  int *num_planes)

 /* Works only for PNM files. */

{
  int i, rows, cols, planes, depth, ch;
  char *name;
  FILE *fp;

  for (name=NULL, i=1; i < (argc-1); i++)
    if (strcmp(argv[i],"-i") == 0)
      {
        name = argv[i+1];
        argv[i] = argv[i+1] = "";
      }
  if (name == NULL)
    {
      fprintf(stderr,"Must supply input file name via `-i' switch!\n");
      exit(-1);
    }
  fp = local_fopen(name,"rb");
  if (fp == NULL)
    {
      fprintf(stderr,"Cannot open input file, \"%s\"!\n",name);
      exit(-1);
    }

  if ((fgetc(fp) != 'P') || (((ch = fgetc(fp)) != '5')  && (ch != '6')))
    {
      fprintf(stderr,"The image file, \"%s\", does not have a PGM or PPM "
              "format!\n",name);
      exit(-1);
    }
  if (ch == '5')
    planes = 1;
  else
    planes = 3;
  eat_white(fp);
  if ((fscanf(fp,"%d",&cols) != 1) || (cols < 1))
    {
      fprintf(stderr,"Invalid PNM header in input image, \"%s\"!\n",name);
      exit(-1);
    }
  eat_white(fp);
  if ((fscanf(fp,"%d",&rows) != 1) || (rows < 1))
    {
      fprintf(stderr,"Invalid PNM header in input image, \"%s\"!\n",name);
      exit(-1);
    }
  eat_white(fp);
  if ((fscanf(fp,"%d",&depth) != 1) || (depth > 255) || (depth < 0))
    {
      fprintf(stderr,"Invalid PNM header in input image, \"%s\"!\n",name);
      exit(-1);
    }
  fgetc(fp);
  *num_rows = rows;
  *num_cols = cols;
  *num_planes = planes;
  return(fp);
}

/*****************************************************************************/
/* STATIC                       open_output_file                             */
/*****************************************************************************/

static FILE *
  open_output_file(int argc, char *argv[], int num_rows, int num_cols,
                   int num_planes)
{

⌨️ 快捷键说明

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