std_bitsource.c

来自「JPEG2000 EBCOT算法源码」· C语言 代码 · 共 97 行

C
97
字号
/*****************************************************************************/
/* 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 <assert.h>
#include <line_block_ifc.h>

/* ========================================================================= */
/* ------------------ Implementation of bitstream source ------------------- */
/* ========================================================================= */

typedef
  struct bitstream_obj {
    bitstream_source_obj base;
    FILE *fp;
    int max_bytes;
  } bitstream_obj, *bitstream_ref;

/*****************************************************************************/
/* STATIC                         __initialize                               */
/*****************************************************************************/

static void
  __initialize(bitstream_source_ref base, int max_bytes, FILE *fp,
               int argc, char *argv[])
{
  bitstream_ref self = (bitstream_ref) base;
  self->fp = fp;
  self->max_bytes = max_bytes;
}

/*****************************************************************************/
/* STATIC                         __print_usage                              */
/*****************************************************************************/

static void
  __print_usage(bitstream_source_ref base, FILE *dest)
{
  return; /* No special command-line options for this object. */
}

/*****************************************************************************/
/* STATIC                         __pull_bytes                               */
/*****************************************************************************/

static int
  __pull_bytes(bitstream_source_ref base, unsigned char *buf, int num_bytes)
{
  bitstream_ref self = (bitstream_ref) base;
  int can_read;

  can_read = num_bytes;
  if (can_read > self->max_bytes)
    can_read = self->max_bytes;
  self->max_bytes -= can_read;
  if (can_read > 0)
    can_read = fread(buf,1,(size_t) can_read,self->fp);
  return(can_read);
}

/*****************************************************************************/
/* STATIC                         __terminate                                */
/*****************************************************************************/

static void
  __terminate(bitstream_source_ref base)
{
  bitstream_ref self = (bitstream_ref) base;

  local_free(self);
}

/*****************************************************************************/
/* EXTERN                    create_bitstream_source                         */
/*****************************************************************************/

bitstream_source_ref
  create_bitstream_source(void)
{
  bitstream_ref result;

  result = (bitstream_ref)
    local_malloc(sizeof(bitstream_obj));
  result->base.initialize = __initialize;
  result->base.print_usage = __print_usage;
  result->base.pull_bytes = __pull_bytes;
  result->base.terminate = __terminate;
  result->fp = NULL;
  result->max_bytes = 0;
  return((bitstream_source_ref) result);
}

⌨️ 快捷键说明

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