cio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /* ----------------------------------------------------------------------- */
  33. opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
  34. opj_cp_t *cp = NULL;
  35. opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
  36. if(!cio) return NULL;
  37. cio->cinfo = cinfo;
  38. if(buffer && length) {
  39. /* wrap a user buffer containing the encoded image */
  40. cio->openmode = OPJ_STREAM_READ;
  41. cio->buffer = buffer;
  42. cio->length = length;
  43. }
  44. else if(!buffer && !length && cinfo) {
  45. /* allocate a buffer for the encoded image */
  46. cio->openmode = OPJ_STREAM_WRITE;
  47. switch(cinfo->codec_format) {
  48. case CODEC_J2K:
  49. cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
  50. break;
  51. case CODEC_JP2:
  52. cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
  53. break;
  54. default:
  55. opj_free(cio);
  56. return NULL;
  57. }
  58. cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
  59. cio->buffer = (unsigned char *)opj_malloc(cio->length);
  60. if(!cio->buffer) {
  61. opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
  62. opj_free(cio);
  63. return NULL;
  64. }
  65. }
  66. else {
  67. opj_free(cio);
  68. return NULL;
  69. }
  70. /* Initialize byte IO */
  71. cio->start = cio->buffer;
  72. cio->end = cio->buffer + cio->length;
  73. cio->bp = cio->buffer;
  74. return cio;
  75. }
  76. void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
  77. if(cio) {
  78. if(cio->openmode == OPJ_STREAM_WRITE) {
  79. /* destroy the allocated buffer */
  80. opj_free(cio->buffer);
  81. }
  82. /* destroy the cio */
  83. opj_free(cio);
  84. }
  85. }
  86. /* ----------------------------------------------------------------------- */
  87. /*
  88. * Get position in byte stream.
  89. */
  90. int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
  91. return (int)(cio->bp - cio->start);
  92. }
  93. /*
  94. * Set position in byte stream.
  95. *
  96. * pos : position, in number of bytes, from the beginning of the stream
  97. */
  98. void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
  99. unsigned char* new_pos = cio->start + pos;
  100. if (new_pos > cio->end) {
  101. opj_event_msg(cio->cinfo, EVT_ERROR,
  102. "error: trying to seek past the end of the codestream (start = %d, change = %d, end = %d\n",
  103. cio->start, pos, cio->end);
  104. return;
  105. }
  106. cio->bp = new_pos;
  107. }
  108. /*
  109. * Number of bytes left before the end of the stream.
  110. */
  111. int cio_numbytesleft(opj_cio_t *cio) {
  112. int delta = (int)(cio->end - cio->bp);
  113. if (delta < 0) {
  114. opj_event_msg(cio->cinfo, EVT_ERROR,
  115. "error: current pointer = %d is already past end = %d\n",
  116. cio->bp, cio->end);
  117. }
  118. return delta;
  119. }
  120. /*
  121. * Get pointer to the current position in the stream.
  122. */
  123. unsigned char *cio_getbp(opj_cio_t *cio) {
  124. return cio->bp;
  125. }
  126. /*
  127. * Write a byte.
  128. */
  129. bool cio_byteout(opj_cio_t *cio, unsigned char v) {
  130. if (cio->bp >= cio->end) {
  131. opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
  132. return false;
  133. }
  134. *cio->bp++ = v;
  135. return true;
  136. }
  137. /*
  138. * Read a byte.
  139. */
  140. unsigned char cio_bytein(opj_cio_t *cio) {
  141. if (cio->bp < cio->start) {
  142. opj_event_msg(cio->cinfo, EVT_ERROR, "read error: trying to read from before the start of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
  143. return 0;
  144. }
  145. if (cio->bp >= cio->end) {
  146. opj_event_msg(cio->cinfo, EVT_INFO, "passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
  147. return 0;
  148. }
  149. return *cio->bp++;
  150. }
  151. /*
  152. * Write some bytes.
  153. *
  154. * v : value to write
  155. * n : number of bytes to write
  156. */
  157. unsigned int cio_write(opj_cio_t *cio, unsigned int v, int n) {
  158. int i;
  159. for (i = n - 1; i >= 0; --i) {
  160. if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
  161. return 0;
  162. }
  163. return n;
  164. }
  165. /*
  166. * Read some bytes.
  167. *
  168. * n : number of bytes to read
  169. *
  170. * return : value of the n bytes read
  171. */
  172. unsigned int cio_read(opj_cio_t *cio, int n) {
  173. int i;
  174. unsigned int v;
  175. v = 0;
  176. for (i = n - 1; i >= 0; --i) {
  177. v += (unsigned int)cio_bytein(cio) << (i << 3);
  178. }
  179. return v;
  180. }
  181. /*
  182. * Skip some bytes.
  183. *
  184. * n : number of bytes to skip
  185. */
  186. void cio_skip(opj_cio_t *cio, int n) {
  187. if (cio->bp + n < cio->start || cio->bp + n > cio->end) {
  188. opj_event_msg(cio->cinfo, EVT_WARNING,
  189. "skipping past the end of the codestream (current = %d, change = %d, end = %d\n",
  190. cio->bp, n, cio->end);
  191. return;
  192. }
  193. cio->bp += n;
  194. }