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

📄 j2g_fiddle.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
            }
          packet->head_bytes = 0;
        }
      if (buf_bytes > 0)
        add_ppt_marker(tp,buf,buf_bytes,Zppt++);
      update_tilepart_length(tp);
    }
  local_free(buf);
}

/*****************************************************************************/
/* STATIC                         make_ppm_markers                           */
/*****************************************************************************/

/* Begin DST PPM fix */
static void
  make_ppm_markers(j2g_codestream_ptr codestream, int max_bytes)
{
  j2g_tilepart_ptr tp;
  j2g_packet_ptr packet;
  std_byte *buf, *bp, Zppm;
  int buf_bytes, tp_bytes, head_bytes, length_bytes;

  if (max_bytes > (1<<16))
    max_bytes = (1<<16);
  if (max_bytes < 9)
    local_error("Non-empty PPM marker segments must have at least 7 bytes "
                "each!  You must supply a larger parameter to the "
                "`-make_ppm' argument!");
  max_bytes -= 5;
  buf = (std_byte *) local_malloc("FIDDLE",max_bytes);
  buf_bytes = 0;
  Zppm = 0;
  bp = buf;
  for (tp=codestream->tileparts; tp != NULL; tp = tp->next)
    {
      tp_bytes = 0;
      for (packet=tp->packets; packet != NULL; packet = packet->next)
        {
          head_bytes = packet->head_bytes;
          if (head_bytes <= 0)
            local_error("Cannot generate PPM markers for the current "
                        "codestream!  You need to include EPH markers when "
                        "generating the original codestream, using the "
                        "`-Beph' option with the VM's compressor!");
          if (tp->uses_resync)
            head_bytes -= 6;
          assert(head_bytes > 0);
          tp_bytes += head_bytes;
        }
      if ((buf_bytes+4) > max_bytes)
        {
          add_ppm_marker(codestream,buf,buf_bytes,Zppm++);
          buf_bytes = 0;
          bp = buf;
        }

      for (length_bytes=4, packet=tp->packets;
           packet != NULL;
           packet = packet->next, length_bytes=0)
        {
          head_bytes = packet->head_bytes;
          if (tp->uses_resync)
            head_bytes -= 6;
          tp->tile_length -= head_bytes;
          packet->packet_bytes -= head_bytes;
          if (tp->uses_resync)
            packet->buf += 6;
          if ((head_bytes+length_bytes+buf_bytes) > max_bytes)
            {
              add_ppm_marker(codestream,buf,buf_bytes,Zppm++);
              buf_bytes = 0;
              bp = buf;
              if ((head_bytes+length_bytes) > max_bytes)
                local_error("You have restricted PPM markers to be so small "
                            "that it is not possible to avoid fragmenting "
                            "one of the packet headers across multiple PPM "
                            "markers!  You should specify a larger parameter "
                            "with the `-make_ppm' argument!");
            }
          if (length_bytes)
            {
              *(bp++) = (std_byte)((tp_bytes >> 24) & 0x00FF); buf_bytes++;
              *(bp++) = (std_byte)((tp_bytes >> 16) & 0x00FF); buf_bytes++;
              *(bp++) = (std_byte)((tp_bytes >> 8) & 0x00FF); buf_bytes++;
              *(bp++) = (std_byte)((tp_bytes >> 0) & 0x00FF); buf_bytes++;
            }
          for (; head_bytes > 0; head_bytes--, buf_bytes++)
            *(bp++) = *(packet->buf++);
          if (tp->uses_resync)
            {
              head_bytes = packet->head_bytes;
              packet->buf[-1] = packet->buf[-head_bytes+5];
              packet->buf[-2] = packet->buf[-head_bytes+4];
              packet->buf[-3] = packet->buf[-head_bytes+3];
              packet->buf[-4] = packet->buf[-head_bytes+2];
              packet->buf[-5] = packet->buf[-head_bytes+1];
              packet->buf[-6] = packet->buf[-head_bytes+0];
              packet->buf -= 6;
              assert((packet->buf[0] == 0xFF) &&
                     (packet->buf[1] ==
                      (std_byte)(MARKER_SOP & 0x00FF)));
            }
          packet->head_bytes = 0;
        }
      update_tilepart_length(tp); /* DST fix */
    }
  if (buf_bytes > 0)
    add_ppm_marker(codestream,buf,buf_bytes,Zppm++);
  local_free(buf);
}
/* End DST PPM fix */

/*****************************************************************************/
/* STATIC                          add_plt_marker                            */
/*****************************************************************************/

static void
  add_plt_marker(j2g_tilepart_ptr tp, std_byte *buf, int buf_bytes,
                 std_byte Zplt)

 /* The `buf' array contains `buf_bytes' bytes of packet head bytes to
    pack into a new PPT marker. */

{
  j2g_marker_ptr marker;
  int marker_bytes, Lplt;
  std_byte *bp;

  marker_bytes = buf_bytes + 5;
  tp->tile_length += marker_bytes;
  Lplt = marker_bytes - 2;
  marker = (j2g_marker_ptr)
    local_malloc("FIDDLE",sizeof(j2g_marker));
  marker->marker_code = MARKER_PLT;
  marker->total_bytes = marker_bytes;
  marker->buf = marker->buf_handle = bp = (std_byte *)
    local_malloc("FIDDLE",marker_bytes);
  *(bp++) = 0xFF;
  *(bp++) = (std_byte)(MARKER_PLT & 0x00FF);
  *(bp++) = (std_byte)((Lplt >> 8) & 0x00FF);
  *(bp++) = (std_byte)((Lplt >> 0) & 0x00FF);
  *(bp++) = Zplt;

  memcpy(bp,buf,(size_t) buf_bytes);

  /* Now insert immediately after the SOT marker. */
  assert(tp->markers != NULL);
  marker->next = tp->markers->next;
  tp->markers->next = marker;
}

/*****************************************************************************/
/* STATIC                         make_plt_markers                           */
/*****************************************************************************/

static void
  make_plt_markers(j2g_codestream_ptr codestream, int max_bytes)
{
  j2g_tilepart_ptr tp;
  j2g_packet_ptr packet;
  std_byte *buf, *bp, Zplt;
  int buf_bytes, head_bytes, bytes, pack_num=0;

  buf = (std_byte *) local_malloc("FIDDLE",max_bytes);
  for (tp=codestream->tileparts; tp != NULL; tp = tp->next)
    {
      if (tp->packets == NULL)
        continue;
      buf_bytes = 0;
      bytes = 0;
      Zplt = 0;
      bp = buf;

      for (packet=tp->packets; packet != NULL; packet = packet->next)
        {
          head_bytes = packet->head_bytes;
          if (tp->uses_resync)
            head_bytes -= 4;
          assert(head_bytes > 0);

          if (tp->uses_resync)
            packet->buf += 4;

          if(packet->packet_bytes < (1<<7) ){
                   *(bp++) = (packet->packet_bytes & 0xff);
                   bytes++;
               } else {
                 if(packet->packet_bytes < (1<<14) ){
                     *(bp++) = ( 128 + (packet->packet_bytes >> 7));
                     *(bp++) = (packet->packet_bytes & 127) ;
                     bytes += 2;
                  } else {
                    if(packet->packet_bytes < (1<<21) ){
                      *(bp++) = ( 128 + (packet->packet_bytes>>14));
                      *(bp++) = ( 128 + ((packet->packet_bytes>>7) & 127));
                      *(bp++) = (packet->packet_bytes & 127) ;
                      bytes += 3;
                    } else {
                      *(bp++) = ( 128 + (packet->packet_bytes>>21));
                      *(bp++) = ( 128 + ((packet->packet_bytes>>14) & 127));
                      *(bp++) = ( 128 + ((packet->packet_bytes>>7) & 127));
                      *(bp++) = (packet->packet_bytes & 127) ;
                      bytes += 4;
                    }
                  }
                }

          if (tp->uses_resync)
            {
              head_bytes = packet->head_bytes;
              packet->buf -= 4;

              assert((packet->buf[0] == 0xFF) &&
                     (packet->buf[1] == (std_byte)(MARKER_SOP & 0x00FF)));
            }
          packet->head_bytes = 0;
        }
       buf_bytes = bytes;
      if (buf_bytes > 0)
        add_plt_marker(tp,buf,buf_bytes,Zplt++);

      update_tilepart_length(tp);
    }
  local_free(buf);
}

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

static void
  print_usage(void)
{
  local_printf(0,76,"The following arguments are accepted:");
  local_printf(4,76,
       "-i <input file>\n"
       "-o <output file>\n"
       "-leave_eph -- default is to discard EPH markers\n"
       "-leave_sop -- default is to discard RESYNC markers\n"
       "-partition <packets per part> -- partition into tile-parts\n"
       "-make_ppt <max marker size> -- pack packet headers into PPT markers\n"
       "-make_ppm <max marker size>-- pack packet headers into PPM markers\n"
       "-make_plt  -- write packet length into PLT markers\n"
       "-u -- print this usage statement\n");
  local_exit(-1);
}

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

int
  main(int argc, char *argv[])
{
  char *in_filename, *out_filename;
  FILE *in_file, *out_file;
  int discard_eph, discard_sop;
  int partition_packets;
  int max_ppt_bytes, max_ppm_bytes, max_plt_bytes;
  j2g_codestream_ptr codestream;

  in_filename = out_filename = NULL;
  discard_eph = discard_sop = 1;
  max_ppt_bytes = max_ppm_bytes = max_plt_bytes = 0;
  partition_packets = 0;
  argc--; argv++;
  if (argc == 0)
    print_usage();
  for (; argc > 0; argc--, argv++)
    if (strcmp(*argv,"-i") == 0)
      {
        argc--; argv++;
        if (argc > 0)
          in_filename = *argv;
      }
    else if (strcmp(*argv,"-o") == 0)
      {
        argc--; argv++;
        if (argc > 0)
          out_filename = *argv;
      }
    else if (strcmp(*argv,"-leave_eph") == 0)
      discard_eph = 0;
    else if (strcmp(*argv,"-leave_sop") == 0)
      discard_sop = 0;
    else if (strcmp(*argv,"-partition") == 0)
      {
        argc--; argv++;
        if ((argc == 0) || (sscanf(*argv,"%d",&partition_packets) == 0) ||
            (partition_packets <= 0))
          local_error("The `-partition' argument requires a positive integer "
                      "parameter!");
      }
    else if (strcmp(*argv,"-make_ppt") == 0)
      {
        argc--; argv++;
        if ((argc == 0) || (sscanf(*argv,"%d",&max_ppt_bytes) == 0) ||
            (max_ppt_bytes <= 0))
          local_error("The `-make_ppt' argument requires a positive integer "
                      "parameter!");
      }
    else if (strcmp(*argv,"-make_ppm") == 0)
      {
        argc--; argv++;
        if ((argc == 0) || (sscanf(*argv,"%d",&max_ppm_bytes) == 0) ||
            (max_ppm_bytes <= 0))
          local_error("The `-make_ppm' argument requires a positive integer "
                      "parameter!");
      }
    else if (strcmp(*argv,"-make_plt") == 0)
      {
        max_plt_bytes = 50;
      }
    else if (strcmp(*argv,"-u") == 0)
      print_usage();
    else
      local_error("Unrecognized command-line argument, \"%s\"!",*argv);
  if ((in_filename == NULL) || (out_filename == NULL))
    local_error("Must supply an input and an output file name!");
  if (max_ppt_bytes && max_ppm_bytes)
    local_error("PPT and PPM markers are currently mutually exclusive!");

  if ((in_file = fopen(in_filename,"rb")) == NULL)
    local_error("Cannot open input file, \"%s\"!",in_filename);
  codestream = read_codestream(in_file);
  fclose(in_file);
  local_printf(4,70,"Input stream contains %d tile-parts and %d packets!",
               codestream->total_tileparts,codestream->total_packets);

  process_in_data_markers(codestream,discard_eph,discard_sop);
  if (partition_packets)
    partition_into_tileparts(codestream,partition_packets);
  if (max_ppt_bytes)
    make_ppt_markers(codestream,max_ppt_bytes);
  if (max_ppm_bytes)
    make_ppm_markers(codestream,max_ppm_bytes);
  if (max_plt_bytes)
    make_plt_markers(codestream,max_plt_bytes);

  local_printf(4,70,"Output stream contains %d tile-parts and %d packets!",
               codestream->total_tileparts,codestream->total_packets);
  if ((out_file = fopen(out_filename,"wb")) == NULL)
    local_error("Cannot open output file, \"%s\"!",out_filename);
  write_codestream(codestream,out_file);  
  fclose(out_file);
  return(0);
}

⌨️ 快捷键说明

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