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

📄 std_bitsink.c

📁 JPEG2000 EBCOT算法源码
💻 C
字号:
/*****************************************************************************/
/* 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 sink -------------------- */
/* ========================================================================= */

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

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

static void
  __initialize(bitstream_sink_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_sink_ref base, FILE *dest)
{
  return; /* No special command-line options for this object. */
}

/*****************************************************************************/
/* STATIC                         __push_bytes                               */
/*****************************************************************************/

static void
  __push_bytes(bitstream_sink_ref base, unsigned char *buf, int num_bytes)
{
  bitstream_ref self = (bitstream_ref) base;
  int send_bytes;

  send_bytes = num_bytes;
  if (send_bytes > self->max_bytes)
    send_bytes = self->max_bytes;
  self->max_bytes -= num_bytes;
  if (send_bytes > 0)
    fwrite(buf,1,(size_t) send_bytes,self->fp);
}

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

static int
  __terminate(bitstream_sink_ref base)
{
  bitstream_ref self = (bitstream_ref) base;
  int result;

  if (self->fp != NULL)
    fflush(self->fp);
  result = -self->max_bytes;
  local_free(self);
  return(result);
}

/*****************************************************************************/
/* EXTERN                     create_bitstream_sink                          */
/*****************************************************************************/

bitstream_sink_ref
  create_bitstream_sink(void)
{
  bitstream_ref result;

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

⌨️ 快捷键说明

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