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

📄 jp2.cpp

📁 该源码是JPEG2000的c++源代码,希望对研究JPEG2000标准以及编解码的朋友们有用.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************/void  j2_palette::init(int num_components, int num_entries){  if (this->num_components != 0)    { kdu_error e;      e << "JP2 palette information may be initialized only once!"; }  assert((num_components > 0) && (num_components < 256));  this->num_components = num_components;  this->num_entries = num_entries;  bit_depths = new int[num_components];  luts = new kdu_int32 *[num_components];  for (int c=0; c < num_components; c++)    {      bit_depths[c] = 0;      luts[c] = new kdu_int32[num_entries];    }}/*****************************************************************************//*                          j2_palette::init (input)                         *//*****************************************************************************/void  j2_palette::init(j2_input_box *pclr){  if (this->num_components != 0)    { kdu_error e;      e << "JP2 file contains multiple palette (pclr) boxes!"; }  assert(pclr->get_box_type() == j2_palette_box);  kdu_uint16 ne;  kdu_byte npc;  if (!(pclr->read(ne) && pclr->read(npc) &&        (ne >= 1) && (ne <= 1024) && (npc >= 1)))    { kdu_error e; e << "Malformed palette (pclr) box found in JP2 file.  "      "Insufficient or illegal fields encountered."; }  num_components = npc;  num_entries = ne;  int c;  bit_depths = new int[num_components];  for (c=0; c < num_components; c++)    {      kdu_byte bpc;      if (!pclr->read(bpc))        { kdu_error e; e << "Malformed palette (pclr) box found in "          "JP2 file.  The box contains insufficient bit-depth specifiers."; }      else if ((bpc & 0x7F) > 37)        { kdu_error e; e << "Malformed palette (pclr) box found in "          "JP2 file.  The box contains an illegal bit-depth specifier.  Bit "          "depths may not exceed 38 bits per sample."; }      else        bit_depths[c] = (bpc & 0x80)?(-((bpc & 0x7F)+1)):(bpc+1);    }  luts = new kdu_int32 *[num_components];  for (c=0; c < num_components; c++)    luts[c] = new kdu_int32[num_entries];  for (c=0; c < num_components; c++)    {      int bits = (bit_depths[c] < 0)?(-bit_depths[c]):bit_depths[c];      int entry_bytes = (bits+7)>>3;      int downshift = bits-32;      downshift = (downshift < 0)?0:downshift;      int upshift = 32+downshift-bits;      kdu_int32 val, offset = (bit_depths[c]<0)?0:KDU_INT32_MIN;      kdu_byte val_buf[5]; assert((entry_bytes <= 5) && (entry_bytes > 0));      for (int n=0; n < num_entries; n++)        {          if (pclr->read(val_buf,entry_bytes) != entry_bytes)            { kdu_error e; e << "Malformed palette (pclr) box found in JP2 "              "file.  The box contains insufficient palette entries."; }          val = val_buf[0];          if (entry_bytes > 1)            {              val = (val<<8) + val_buf[1];              if (entry_bytes > 2)                {                  val = (val<<8) + val_buf[2];                  if (entry_bytes > 3)                    {                      val = (val<<8) + val_buf[3];                      if (entry_bytes > 4)                        {                          val <<= (8-downshift);                          val += (val_buf[4] >> downshift);                        }                    }                }            }          val <<= upshift;          val += offset;          (luts[c])[n] = val;        }      if (downshift)        bit_depths[c] = (bit_depths[c]<0)?-32:32;    }  if (!pclr->close())    { kdu_error e; e << "Malformed palette (pclr) box found in JP2 file.  "      "Box appears to be too long."; }}/*****************************************************************************//*                             j2_palette::finalize                          *//*****************************************************************************/void  j2_palette::finalize(){  if (num_components == 0)    return;  int c;  for (c=0; c < num_components; c++)    if ((bit_depths[c] == 0) || (bit_depths[c] > 32) || (bit_depths[c] < -32))      break;  if ((num_components < 1) || (c < num_components) ||      (num_components > 255) || (num_entries < 1) || (num_entries > 1024))    { kdu_error e; e << "Incomplete or invalid information "      "provided for the JP2 file palette box."; }}/*****************************************************************************//*                            j2_palette::save_box                           *//*****************************************************************************/void  j2_palette::save_box(j2_output_box *super_box){  if (num_components == 0)    return;  finalize();  j2_output_box pclr;  pclr.open(super_box,j2_palette_box);  pclr.write((kdu_uint16) num_entries);  pclr.write((kdu_byte) num_components);  int c;  kdu_byte bpc;  for (c=0; c < num_components; c++)    {      bpc = (kdu_byte)        ((bit_depths[c]>0)?(bit_depths[c]-1):(0x80 | (-bit_depths[c]-1)));      pclr.write(bpc);    }  for (c=0; c < num_components; c++)    {      int bits = (bit_depths[c] < 0)?(-bit_depths[c]):bit_depths[c];      int entry_bytes = (bits+7)>>3;      int downshift = 32 - bits; assert(downshift >= 0);      kdu_int32 offset = (bit_depths[c]<0)?0:KDU_INT32_MIN;      kdu_uint32 val;      kdu_byte val_buf[4]; assert((entry_bytes > 0) && (entry_bytes <= 4));      for (int n=0; n < num_entries; n++)        {          val = (kdu_uint32)((luts[c])[n] - offset);          val >>= downshift; // Most significant bits hold 0's          val_buf[entry_bytes-1] = (kdu_byte) val;          if (entry_bytes > 1)            {              val >>= 8; val_buf[entry_bytes-2] = (kdu_byte) val;              if (entry_bytes > 2)                {                  val >>= 8; val_buf[entry_bytes-3] = (kdu_byte) val;                  if (entry_bytes > 3)                    {                      val >>= 8; val_buf[entry_bytes-4] = (kdu_byte) val;                    }                }            }          pclr.write(val_buf,entry_bytes);        }    }  pclr.close();}/* ========================================================================= *//*                                  jp2_palette                              *//* ========================================================================= *//*****************************************************************************//*                               jp2_palette::init                           *//*****************************************************************************/void  jp2_palette::init(int num_components, int num_entries){  assert(state != NULL);  state->init(num_components,num_entries);}/*****************************************************************************//*                              jp2_palette::set_lut                         *//*****************************************************************************/void  jp2_palette::set_lut(int comp_idx, kdu_int32 *lut, int bit_depth,                       bool is_signed){  assert((state != NULL) &&         (comp_idx >= 0) && (comp_idx < state->num_components) &&         (bit_depth <= 32) && (bit_depth >= 1));  state->bit_depths[comp_idx] = (is_signed)?(-bit_depth):bit_depth;  int upshift = 32-bit_depth;  kdu_int32 offset = (is_signed)?0:KDU_INT32_MIN;  kdu_int32 *dst = state->luts[comp_idx];  for (int n=0; n < state->num_entries; n++)    dst[n] = (lut[n] << upshift) + offset;}/*****************************************************************************//*                         jp2_palette::get_num_entries                      *//*****************************************************************************/int  jp2_palette::get_num_entries(){  assert(state != NULL);  return state->num_entries;}/*****************************************************************************//*                        jp2_palette::get_num_components                    *//*****************************************************************************/int  jp2_palette::get_num_components(){  assert(state != NULL);  return state->num_components;}/*****************************************************************************//*                          jp2_palette::get_bit_depth                       *//*****************************************************************************/int  jp2_palette::get_bit_depth(int comp_idx){  assert((state != NULL) &&         (comp_idx >= 0) && (comp_idx < state->num_components));  int depth = state->bit_depths[comp_idx];  return (depth<0)?-depth:depth;}/*****************************************************************************//*                           jp2_palette::get_signed                         *//*****************************************************************************/bool  jp2_palette::get_signed(int comp_idx){  assert((state != NULL) &&         (comp_idx >= 0) && (comp_idx < state->num_components));  return (state->bit_depths[comp_idx] < 0);}/*****************************************************************************//*                        jp2_palette::get_lut (float)                       *//*****************************************************************************/void  jp2_palette::get_lut(int comp_idx, float lut[]){  assert((state != NULL) &&         (comp_idx >= 0) && (comp_idx < state->num_components));  kdu_int32 *src = state->luts[comp_idx];  float scale = 1.0F / (((float)(1<<16)) * ((float)(1<<16)));  for (int n=0; n < state->num_entries; n++)    lut[n] = ((float) src[n]) * scale;}/*****************************************************************************//*                      jp2_palette::get_lut (fixed point)                   *//*****************************************************************************/void  jp2_palette::get_lut(int comp_idx, kdu_sample16 lut[]){  assert((state != NULL) &&         (comp_idx >= 0) && (comp_idx < state->num_components));  kdu_int32 *src = state->luts[comp_idx];  kdu_int32 downshift = 32 - KDU_FIX_POINT;  kdu_int32 offset = (1<<downshift)>>1;  for (int n=0; n < state->num_entries; n++)    lut[n].ival = (kdu_int16)((src[n]+offset)>>downshift);}/* ========================================================================= *//*                                j2_channels                                *//* ========================================================================= *//*****************************************************************************//*                          j2_channels::j2_channels                         *//*****************************************************************************/j2_channels::j2_channels(){  int c;  num_colours = num_components = num_palette_components = 0;  num_cmap_channels = 0;  cmap_channels = NULL;  for (c=0; c < 3; c++)    { // Set up default mapping in case no colour definition box.      channel_functions[c].source_component = c;      channel_functions[c].palette_component = -1;    }  for (; c < 9; c++)    {      channel_functions[c].source_component = -1;      channel_functions[c].palette_component = -1;    }}/*****************************************************************************//*                       j2_channels::process_cdef_box                       *//*****************************************************************************/void  j2_channels::process_cdef_box(j2_input_box *cdef){  assert(cdef->get_box_type() == j2_channel_definition_box);  kdu_uint16 num_descriptions, n;  if (!(cdef->read(num_descriptions) && (num_descriptions > 0)))    { kdu_error e; e << "Malformed channel definition (cdef) box found in JP2 "      "file.  Missing or invalid fields."; }  for (n=0; n < num_descriptions; n++)    {      kdu_uint16 channel_idx, typ, assoc;      if (!(cdef->read(channel_idx) && cdef->read(typ) && cdef->read(assoc) &&            ((typ < 3) || (typ == (kdu_uint16) 0xFFFF))))        { kdu_error e; e << "Malformed channel definition (cdef) box found "          "in JP2 file.  Missing or invalid channel association information.";}      if ((assoc > 3) || (typ > 2))        continue; // Channel has no defined function in JP2      if (assoc > 0)        channel_functions[assoc-1+3*typ].source_component = channel_idx;      else

⌨️ 快捷键说明

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