bio.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. namespace OpenSim.Framework.OpenJpeg
  31. {
  32. public static class bio
  33. {
  34. public static opj_bio bio_create()
  35. {
  36. opj_bio bio = new opj_bio();
  37. return bio;
  38. }
  39. public static void bio_destroy(opj_bio bio)
  40. {
  41. // not needed on C#
  42. }
  43. public static int bio_numbytes(opj_bio bio)
  44. {
  45. return (bio.bp - bio.start);
  46. }
  47. public static void bio_init_enc(opj_bio bio, sbyte bp, int len)
  48. {
  49. bio.start = (byte)bp;
  50. bio.end = (byte)(bp + (byte)len);
  51. bio.bp = (byte)bp;
  52. bio.buf = 0;
  53. bio.ct = 8;
  54. }
  55. public static void bio_init_dec(opj_bio bio, sbyte bp, int len)
  56. {
  57. bio.start = (byte)bp;
  58. bio.end = (byte)(bp + len);
  59. bio.bp = (byte)bp;
  60. bio.buf = 0;
  61. bio.ct = 0;
  62. }
  63. public static void bio_write(opj_bio bio, int v, int n)
  64. {
  65. for (int i = n - 1; i >= 0; i--)
  66. bio_putbit(bio, (v >> i) & 1);
  67. }
  68. public static int bio_read(opj_bio bio, int n)
  69. {
  70. int v = 0;
  71. for (int i = n - 1; i >= 0; i--)
  72. v += bio_getbit(bio) << i;
  73. return v;
  74. }
  75. public static int bio_flush(opj_bio bio)
  76. {
  77. bio.ct = 0;
  78. if (bio_byteout(bio) != 0)
  79. return 1;
  80. if (bio.ct == 7)
  81. {
  82. bio.ct = 0;
  83. if (bio_byteout(bio) != 0)
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. public static int bio_inalign(opj_bio bio)
  89. {
  90. bio.ct = 0;
  91. if ((bio.buf & 0xff) == 0xff)
  92. {
  93. if (bio_bytein(bio) != 0)
  94. return 1;
  95. bio.ct = 0;
  96. }
  97. return 0;
  98. }
  99. private static int bio_bytein(opj_bio bio)
  100. {
  101. bio.buf = (bio.buf << 8) & 0xffff;
  102. bio.ct = bio.buf == 0xff00 ? 7 : 8;
  103. if (bio.bp >= bio.end)
  104. return 1;
  105. bio.buf |= bio.bp++;
  106. return 0;
  107. }
  108. private static int bio_byteout(opj_bio bio)
  109. {
  110. bio.buf = (bio.buf << 8) & 0xffff;
  111. bio.ct = bio.buf == 0xff00 ? 7 : 8;
  112. if (bio.bp >= bio.end)
  113. return 1;
  114. bio.bp = (byte)(bio.buf >> 8);
  115. bio.bp++;
  116. return 0;
  117. }
  118. private static void bio_putbit(opj_bio bio, int b)
  119. {
  120. if (bio.ct == 0)
  121. bio_byteout(bio);
  122. bio.ct--;
  123. bio.buf |= (byte)(b << bio.ct);
  124. }
  125. private static int bio_getbit(opj_bio bio)
  126. {
  127. if (bio.ct == 0)
  128. bio_bytein(bio);
  129. bio.ct--;
  130. return (int)((bio.buf >> bio.ct) & 1);
  131. }
  132. }
  133. public struct opj_bio
  134. {
  135. public byte start;
  136. public byte end;
  137. public byte bp;
  138. public uint buf;
  139. public int ct;
  140. }
  141. }