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

📄 image_component.c

📁 spiht的压缩解压缩c编写的
💻 C
字号:
#include "spiht.h"
#include "spihtdecode.h"

static QccIMGImageArray QccIMGImageArrayAlloc(int num_rows, int num_cols)
{
  QccIMGImageArray new_array;
  int row;
  
  if ((num_rows <= 0) && (num_cols <= 0))
    return(NULL);
  
  if ((new_array = (double **)malloc(sizeof(double *)*num_rows)) == NULL)
    {
      QccErrorAddMessage("(QccIMGImageArrayAlloc): Error allocating memory");
      return(NULL);
    }
  
  for (row = 0; row < num_rows; row++)
    if ((new_array[row] = (double *)malloc(sizeof(double)*num_cols)) == NULL)
      {
        QccErrorAddMessage("(QccIMGImageArrayAlloc): Error allocating memory");
        return(NULL);
      }
  
  return(new_array);
}

static void QccIMGImageArrayFree(QccIMGImageArray image_array, int num_rows)
{
  int row;
  
  if (image_array != NULL)
    {
      for (row = 0; row < num_rows; row++)
        if (image_array[row] != NULL)
          free(image_array[row]);
      free(image_array);
    }
}

int QccIMGImageComponentInitialize(QccIMGImageComponent *image_component)
{
  if (image_component == NULL)
    return(0);

  QccStringMakeNull(image_component->filename);
  QccStringCopy(image_component->magic_num, QCCIMGIMAGECOMPONENT_MAGICNUM);
  QccGetQccPackVersion(&image_component->major_version,
                       &image_component->minor_version,
                       NULL);
  image_component->num_rows = 0;
  image_component->num_cols = 0;
  image_component->min_val = 0.0;
  image_component->max_val = 0.0;
  image_component->image = NULL;

  return(0);
}

int QccIMGImageComponentAlloc(QccIMGImageComponent *image_component)
{
  if (image_component == NULL)
    return(0);
  
  if (image_component->image == NULL)
    {
      if ((image_component->num_rows > 0) ||
          (image_component->num_cols > 0))
        {
          if ((image_component->image = 
               QccIMGImageArrayAlloc(image_component->num_rows, 
                                     image_component->num_cols)) == NULL)
            {
              QccErrorAddMessage("(QccIMGImageCompoenntAlloc): Error calling QccIMGImageArrayAlloc()");
              return(1);
            }
        }
      else
        image_component->image = NULL;
    }
  
  return(0);
}

void QccIMGImageComponentFree(QccIMGImageComponent *image_component)
{
  if (image_component == NULL)
    return;
  
  if (image_component->image != NULL)
    {
      QccIMGImageArrayFree(image_component->image, 
                           image_component->num_rows);
      image_component->image = NULL;
    }
}

int QccIMGImageComponentReadHeader(FILE *infile, 
                                   QccIMGImageComponent *image_component)
{
  if ((infile == NULL) || (image_component == NULL))
    return(0);
  
  if (QccFileReadMagicNumber(infile,
                             image_component->magic_num,
                             &image_component->major_version,
                             &image_component->minor_version))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading magic number in %s",
                         image_component->filename);
      return(1);
    }
  
  if (strcmp(image_component->magic_num, QCCIMGIMAGECOMPONENT_MAGICNUM))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): %s is not of image_component (%s) type",
                         image_component->filename, QCCIMGIMAGECOMPONENT_MAGICNUM);
      return(1);
    }
  
  fscanf(infile, "%d", &(image_component->num_cols));
  if (ferror(infile) || feof(infile))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of columns in %s",
                         image_component->filename);
      return(1);
    }
  
  if (QccFileSkipWhiteSpace(infile, 0))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of columns in %s",
                         image_component->filename);
      return(1);
    }
  
  fscanf(infile, "%d", &(image_component->num_rows));
  if (ferror(infile) || feof(infile))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s",
                         image_component->filename);
      return(1);
    }
  
  if (QccFileSkipWhiteSpace(infile, 0))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s",
                         image_component->filename);
      return(1);
    }
  
  fscanf(infile, "%lf", &(image_component->min_val));
  if (ferror(infile) || feof(infile))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s",
                         image_component->filename);
      return(1);
    }
  
  if (QccFileSkipWhiteSpace(infile, 0))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s",
                         image_component->filename);
      return(1);
    }
  
  fscanf(infile, "%lf%*1[\n]", &(image_component->max_val));
  if (ferror(infile) || feof(infile))
    {
      QccErrorAddMessage("(QccIMGImageComponentReadHeader): Error reading number of rows in %s",
                         image_component->filename);
      return(1);
    }
  
  return(0);
}

int QccIMGImageComponentReadData(FILE *infile, 
                                 QccIMGImageComponent *image_component)
{
  int row, col;
  
  if ((infile == NULL) ||
      (image_component == NULL))
    return(0);
  
  for (row = 0; row < image_component->num_rows; row++)
    for (col = 0; col < image_component->num_cols; col++)
      if (QccFileReadDouble(infile,
                            &(image_component->image[row][col])))
        {
          QccErrorAddMessage("(QccIMGImageComponentReadData): Error calling QccFileReadDouble()",
                             image_component->filename);
          return(1);
        }
  
  return(0);
}

int QccIMGImageComponentRead(QccIMGImageComponent *image_component)
{
  FILE *infile = NULL;
  
  if (image_component == NULL)
    return(0);
  
  if ((infile = QccFileOpen(image_component->filename, "r")) == NULL)
    {
      QccErrorAddMessage("(QccIMGImageComponentRead): Error opening %s for reading",
                         image_component->filename);
      return(1);
    }
  
  if (QccIMGImageComponentReadHeader(infile, image_component))
    {
      QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentReadHeader()");
      return(1);
    }
  
  if (QccIMGImageComponentAlloc(image_component))
    {
      QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentAlloc()");
      return(1);
    }
  
  if (QccIMGImageComponentReadData(infile, image_component))
    {
      QccErrorAddMessage("(QccIMGImageComponentRead): Error calling QccIMGImageComponentReadData()");
      return(1);
    }
  
  QccFileClose(infile);
  return(0);
}

int QccIMGImageComponentWriteHeader(FILE *outfile, 
                                    const QccIMGImageComponent 
                                    *image_component)
{
  if ((outfile == NULL) || (image_component == NULL))
    return(0);
  
  if (QccFileWriteMagicNumber(outfile, QCCIMGIMAGECOMPONENT_MAGICNUM))
    goto QccErr;
  
  fprintf(outfile, "%d %d\n",
          image_component->num_cols,
          image_component->num_rows);
  if (ferror(outfile))
    goto QccErr;
  
  fprintf(outfile, "% 16.9e % 16.9e\n",
          image_component->min_val,
          image_component->max_val);
  if (ferror(outfile))
    goto QccErr;
  
  return(0);
  
 QccErr:
  QccErrorAddMessage("(QccIMGImageComponentWriteHeader): Error writing header to %s",
                     image_component->filename);
  return(1);
}

int QccIMGImageComponentWriteData(FILE *outfile,
                                  const QccIMGImageComponent *image_component)
{
  int row, col;
  
  if ((image_component == NULL) ||
      (outfile == NULL))
    return(0);
  
  for (row = 0; row < image_component->num_rows; row++)
    for (col = 0; col < image_component->num_cols; col++)
      if (QccFileWriteDouble(outfile,
                             image_component->image[row][col]))
        {
          QccErrorAddMessage("(QccIMGImageComponentWriteData): Error calling QccFileWriteDouble()");
          return(1);
        }
  
  return(0);
}

int QccIMGImageComponentWrite(const QccIMGImageComponent *image_component)
{
  FILE *outfile;
  
  if (image_component == NULL)
    return(0);
  
  if ((outfile = QccFileOpen(image_component->filename, "w")) == NULL)
    {
      QccErrorAddMessage("(QccIMGImageComponentWrite): Error opening %s for writing",
                         image_component->filename);
      return(1);
    }
  
  if (QccIMGImageComponentWriteHeader(outfile, image_component))
    {
      QccErrorAddMessage("(QccIMGImageComponentWrite): Error calling QccIMGImageComponentWriteHeader()");
      return(1);
    }
  if (QccIMGImageComponentWriteData(outfile, image_component))
    {
      QccErrorAddMessage("(QccIMGImageComponentWrite): Error calling QccIMGImageComponentWriteData()");
      return(1);
    }
  
  QccFileClose(outfile);
  return(0);
}

int QccIMGImageComponentSetMin(QccIMGImageComponent *image_component)
{
  double min = MAXDOUBLE;
  int row, col;
  
  if (image_component == NULL)
    return(0);
  if (image_component->image == NULL)
    return(0);
  
  for (row = 0; row < image_component->num_rows; row++)
    for (col = 0; col < image_component->num_cols; col++)
      if (image_component->image[row][col] < min)
        min = image_component->image[row][col];
  
  image_component->min_val = min;
  
  return(0);
}

int QccIMGImageComponentSetMax(QccIMGImageComponent *image_component)
{
  double max = -MAXDOUBLE;
  int row, col;
  
  if (image_component == NULL)
    return(0);
  if (image_component->image == NULL)
    return(0);
  
  for (row = 0; row < image_component->num_rows; row++)
    for (col = 0; col < image_component->num_cols; col++)
      if (image_component->image[row][col] > max)
        max = image_component->image[row][col];
  
  image_component->max_val = max;
  
  return(0);
}

⌨️ 快捷键说明

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