cio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. #include <assert.h>
  33. /* ----------------------------------------------------------------------- */
  34. opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
  35. opj_cp_t *cp = NULL;
  36. opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
  37. if(!cio) return NULL;
  38. cio->cinfo = cinfo;
  39. if(buffer && length) {
  40. /* wrap a user buffer containing the encoded image */
  41. cio->openmode = OPJ_STREAM_READ;
  42. cio->buffer = buffer;
  43. cio->length = length;
  44. }
  45. else if(!buffer && !length && cinfo) {
  46. /* allocate a buffer for the encoded image */
  47. cio->openmode = OPJ_STREAM_WRITE;
  48. switch(cinfo->codec_format) {
  49. case CODEC_J2K:
  50. cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
  51. break;
  52. case CODEC_JP2:
  53. cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
  54. break;
  55. default:
  56. opj_free(cio);
  57. return NULL;
  58. }
  59. cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
  60. cio->buffer = (unsigned char *)opj_malloc(cio->length);
  61. if(!cio->buffer) {
  62. opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
  63. opj_free(cio);
  64. return NULL;
  65. }
  66. }
  67. else {
  68. opj_free(cio);
  69. return NULL;
  70. }
  71. /* Initialize byte IO */
  72. cio->start = cio->buffer;
  73. cio->end = cio->buffer + cio->length;
  74. cio->bp = cio->buffer;
  75. return cio;
  76. }
  77. void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
  78. if(cio) {
  79. if(cio->openmode == OPJ_STREAM_WRITE) {
  80. /* destroy the allocated buffer */
  81. opj_free(cio->buffer);
  82. }
  83. /* destroy the cio */
  84. opj_free(cio);
  85. }
  86. }
  87. /* ----------------------------------------------------------------------- */
  88. /*
  89. * Get position in byte stream.
  90. */
  91. int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
  92. return cio->bp - cio->start;
  93. }
  94. /*
  95. * Set position in byte stream.
  96. *
  97. * pos : position, in number of bytes, from the beginning of the stream
  98. */
  99. void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
  100. assert((cio->start + pos) <= cio->end);
  101. cio->bp = cio->start + pos;
  102. }
  103. /*
  104. * Number of bytes left before the end of the stream.
  105. */
  106. int cio_numbytesleft(opj_cio_t *cio) {
  107. assert((cio->end - cio->bp) >= 0);
  108. return cio->end - cio->bp;
  109. }
  110. /*
  111. * Get pointer to the current position in the stream.
  112. */
  113. unsigned char *cio_getbp(opj_cio_t *cio) {
  114. return cio->bp;
  115. }
  116. /*
  117. * Write a byte.
  118. */
  119. opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
  120. if (cio->bp >= cio->end) {
  121. opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
  122. return OPJ_FALSE;
  123. }
  124. *cio->bp++ = v;
  125. return OPJ_TRUE;
  126. }
  127. /*
  128. * Read a byte.
  129. */
  130. unsigned char cio_bytein(opj_cio_t *cio) {
  131. assert(cio->bp >= cio->start);
  132. if (cio->bp >= cio->end) {
  133. opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
  134. return 0;
  135. }
  136. return *cio->bp++;
  137. }
  138. /*
  139. * Write some bytes.
  140. *
  141. * v : value to write
  142. * n : number of bytes to write
  143. */
  144. unsigned int cio_write(opj_cio_t *cio, unsigned int64 v, int n) {
  145. int i;
  146. for (i = n - 1; i >= 0; i--) {
  147. if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
  148. return 0;
  149. }
  150. return n;
  151. }
  152. /*
  153. * Read some bytes.
  154. *
  155. * n : number of bytes to read
  156. *
  157. * return : value of the n bytes read
  158. */
  159. unsigned int cio_read(opj_cio_t *cio, int n) {
  160. int i;
  161. unsigned int v;
  162. v = 0;
  163. for (i = n - 1; i >= 0; i--) {
  164. v += (unsigned int)cio_bytein(cio) << (i << 3);
  165. }
  166. return v;
  167. }
  168. /*
  169. * Skip some bytes.
  170. *
  171. * n : number of bytes to skip
  172. */
  173. void cio_skip(opj_cio_t *cio, int n) {
  174. assert((cio->bp + n) >= cio->bp);
  175. if (((cio->bp + n) < cio->start) || ((cio->bp + n) > cio->end)) {
  176. assert(0);
  177. }
  178. cio->bp += n;
  179. }