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

📄 calc_dct.c

📁 JPEG2000实现的源码
💻 C
字号:
/*****************************************************************************/
/* Copyright 2000, Eastman Kodak Company                                     */
/* All rights reserved                                                       */
/* File: "calc_dct.c"                                                        */
/* Description: Main driver code for DCT calculation.                        */
/* Author: Austin Lan                                                        */
/* Affiliation: Eastman Kodak Company                                        */
/* Version: VM8.5                                                            */
/* Last Revised: 11 September, 2000                                          */
/*****************************************************************************/
#include <local_services.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <ifc.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/* ========================================================================= */
/* -------------------------- Internal Functions --------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* STATIC                        allocate_1d                                 */
/*****************************************************************************/

static void allocate_1d(void **ptr, int dim1, int byte_depth)
{
   void *mem_p;

   if ((mem_p = (void *)local_malloc("KLT", (dim1 * byte_depth))) == NULL) {
      local_error("allocate_1d():  calloc failure.");
   }
   memset((void *)mem_p, 0, (dim1 * byte_depth));

   *ptr = mem_p;
}

/*****************************************************************************/
/* STATIC                        allocate_2d                                 */
/*****************************************************************************/

static void allocate_2d(void ***ptr, int dim1, int dim2, int byte_depth)
{
   int i;
   void **mem_p;

   if ((mem_p = (void **)local_malloc("KLT", (dim1 * sizeof(void *)))) == NULL) {
      local_error("allocate_2d():  calloc failure (1).");
   }
   if ((mem_p[0] = (void *)local_malloc("KLT", (dim1 * dim2 * byte_depth))) == NULL) {
      local_error("allocate_2d():  calloc failure (2).");
   }
   for (i=1; i<dim1; i++)
      mem_p[i] = (char *)mem_p[0] + (i * dim2 * byte_depth);
   memset((void *)mem_p[0], 0, (dim1 * dim2 * byte_depth));

   *ptr = mem_p;
}

/*****************************************************************************/
/* STATIC                        deallocate_1d                               */
/*****************************************************************************/

static void deallocate_1d(void *ptr)
{
   if (ptr != NULL) {
      local_free(ptr);
   }
}

/*****************************************************************************/
/* STATIC                        deallocate_2d                               */
/*****************************************************************************/

static void deallocate_2d(void **ptr)
{
   if (ptr != NULL) {
      local_free(ptr[0]);
      local_free(ptr);
   }
}

/*****************************************************************************/
/* STATIC                           usage                                    */
/*****************************************************************************/

static void usage(char *fname)
{
   local_printf(0,70, "\nUsage: %s\n", fname);
   local_printf(0,70, "\n");
   
   local_printf(4,70, "-o <name of file for forward DCT result>");
   local_printf(4,70, "-s <DCT size>");
   local_printf(7,70, "DCT matrix will be `size' x `size'");

   local_exit(0);
}   

/*****************************************************************************/
/* STATIC                         calculate_dct                              */
/*****************************************************************************/

static void calculate_dct(int size, float **dct_mat)
{
   int i,j;
   double c_first, c_other, c_angle;

   c_first = sqrt(1.0 / (double)size);
   c_other = sqrt(2.0 / (double)size);
   c_angle = (M_PI / (2.0 * (double)size));
   for (i=0; i<size; i++) {
      float *coef = dct_mat[i];
      double c = (i == 0) ? c_first : c_other;
      for (j=0; j<size; j++) {
	 *coef++ = (float)(c * cos((2.0 * (double)j + 1.0) * (double)i * c_angle));
      }
   }
}

/* ========================================================================= */
/* -------------------------- External Functions --------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* EXTERN                             main                                   */
/*****************************************************************************/

int main(int argc, char *argv[])
{
   cmdl_ref cmdl;
   extern cmdl_ref create_std_cmdl(void);

   int p;
   char **params;
   char *out_fname = NULL;
   int size;
   float **dct_mat;
   FILE *fp;

   if (argc == 1) {
      usage(argv[0]);
   }

   /* Create `cmdl' object */

   cmdl = create_std_cmdl();
   cmdl->initialize(cmdl, argc, argv);

   if ((p = cmdl->extract(cmdl,"-o",-1,&params)) == 1) {
      out_fname = params[0];
   } else {
      local_error("The `-o' argument is invalid.");
   }
   if ((p = cmdl->extract(cmdl,"-s",-1,&params)) == 1) {
      size = atoi(params[0]);
   } else {
      local_error("The `-s' argument is invalid.");
   }

   if (out_fname == NULL) {
      local_error("Insufficient inputs on command-line.");
   } else {
      if ((fp = fopen(out_fname, "wb")) == NULL) {
	 local_error("Unable to open output file, %s.", out_fname);
      }
   }
   if (size <= 0) {
      local_error("DCT size must be a non-zero, positive value.");
   }
   
   allocate_2d((void ***)&dct_mat, size, size, sizeof(float));
   
   calculate_dct(size, dct_mat);
   
   if (fwrite((void *)dct_mat[0], sizeof(float), (size * size), fp) !=
       (unsigned int)(size * size)) {
      local_error("Error writing to output file, %s.", out_fname);
   }
   fclose(fp);

   deallocate_2d((void **)dct_mat);

   return 0;
}

⌨️ 快捷键说明

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