BitDecode.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for packed bitfields (converts to floating point)
  6. *
  7. * history:
  8. * 97-05-31 fl created (much more than originally intended)
  9. *
  10. * Copyright (c) Fredrik Lundh 1997.
  11. * Copyright (c) Secret Labs AB 1997.
  12. *
  13. * See the README file for information on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. #include "Bit.h"
  17. int
  18. ImagingBitDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
  19. BITSTATE *bitstate = state->context;
  20. UINT8 *ptr;
  21. if (state->state == 0) {
  22. /* Initialize context variables */
  23. /* this decoder only works for float32 image buffers */
  24. if (im->type != IMAGING_TYPE_FLOAT32) {
  25. state->errcode = IMAGING_CODEC_CONFIG;
  26. return -1;
  27. }
  28. /* sanity check */
  29. if (bitstate->bits < 1 || bitstate->bits >= 32) {
  30. state->errcode = IMAGING_CODEC_CONFIG;
  31. return -1;
  32. }
  33. bitstate->mask = (1 << bitstate->bits) - 1;
  34. if (bitstate->sign) {
  35. bitstate->signmask = (1 << (bitstate->bits - 1));
  36. }
  37. /* check image orientation */
  38. if (state->ystep < 0) {
  39. state->y = state->ysize - 1;
  40. state->ystep = -1;
  41. } else {
  42. state->ystep = 1;
  43. }
  44. state->state = 1;
  45. }
  46. ptr = buf;
  47. while (bytes > 0) {
  48. UINT8 byte = *ptr;
  49. ptr++;
  50. bytes--;
  51. /* get a byte from the input stream and insert in the bit buffer */
  52. if (bitstate->fill & 1) {
  53. /* fill MSB first */
  54. bitstate->bitbuffer |= (unsigned long)byte << bitstate->bitcount;
  55. } else {
  56. /* fill LSB first */
  57. bitstate->bitbuffer = (bitstate->bitbuffer << 8) | byte;
  58. }
  59. bitstate->bitcount += 8;
  60. while (bitstate->bitcount >= bitstate->bits) {
  61. /* get a pixel from the bit buffer */
  62. unsigned long data;
  63. FLOAT32 pixel;
  64. if (bitstate->fill & 2) {
  65. /* store LSB first */
  66. data = bitstate->bitbuffer & bitstate->mask;
  67. if (bitstate->bitcount > 32) {
  68. /* bitbuffer overflow; restore it from last input byte */
  69. bitstate->bitbuffer =
  70. byte >> (8 - (bitstate->bitcount - bitstate->bits));
  71. } else {
  72. bitstate->bitbuffer >>= bitstate->bits;
  73. }
  74. } else {
  75. /* store MSB first */
  76. data = (bitstate->bitbuffer >> (bitstate->bitcount - bitstate->bits)) &
  77. bitstate->mask;
  78. }
  79. bitstate->bitcount -= bitstate->bits;
  80. if (bitstate->lutsize > 0) {
  81. /* map through lookup table */
  82. if (data <= 0) {
  83. pixel = bitstate->lut[0];
  84. } else if (data >= bitstate->lutsize) {
  85. pixel = bitstate->lut[bitstate->lutsize - 1];
  86. } else {
  87. pixel = bitstate->lut[data];
  88. }
  89. } else {
  90. /* convert */
  91. if (data & bitstate->signmask) {
  92. /* image memory contains signed data */
  93. pixel = (FLOAT32)(INT32)(data | ~bitstate->mask);
  94. } else {
  95. pixel = (FLOAT32)data;
  96. }
  97. }
  98. *(FLOAT32 *)(&im->image32[state->y][state->x]) = pixel;
  99. /* step forward */
  100. if (++state->x >= state->xsize) {
  101. /* new line */
  102. state->y += state->ystep;
  103. if (state->y < 0 || state->y >= state->ysize) {
  104. /* end of file (errcode = 0) */
  105. return -1;
  106. }
  107. state->x = 0;
  108. /* reset bit buffer */
  109. if (bitstate->pad > 0) {
  110. bitstate->bitcount = 0;
  111. }
  112. }
  113. }
  114. }
  115. return ptr - buf;
  116. }