BitDecode.c 3.8 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. {
  20. BITSTATE* bitstate = state->context;
  21. UINT8* ptr;
  22. if (state->state == 0) {
  23. /* Initialize context variables */
  24. /* this decoder only works for float32 image buffers */
  25. if (im->type != IMAGING_TYPE_FLOAT32) {
  26. state->errcode = IMAGING_CODEC_CONFIG;
  27. return -1;
  28. }
  29. /* sanity check */
  30. if (bitstate->bits < 1 || bitstate->bits >= 32) {
  31. state->errcode = IMAGING_CODEC_CONFIG;
  32. return -1;
  33. }
  34. bitstate->mask = (1<<bitstate->bits)-1;
  35. if (bitstate->sign)
  36. bitstate->signmask = (1<<(bitstate->bits-1));
  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. state->state = 1;
  44. }
  45. ptr = buf;
  46. while (bytes > 0) {
  47. UINT8 byte = *ptr;
  48. ptr++;
  49. bytes--;
  50. /* get a byte from the input stream and insert in the bit buffer */
  51. if (bitstate->fill&1)
  52. /* fill MSB first */
  53. bitstate->bitbuffer |= (unsigned long) byte << bitstate->bitcount;
  54. else
  55. /* fill LSB first */
  56. bitstate->bitbuffer = (bitstate->bitbuffer << 8) | byte;
  57. bitstate->bitcount += 8;
  58. while (bitstate->bitcount >= bitstate->bits) {
  59. /* get a pixel from the bit buffer */
  60. unsigned long data;
  61. FLOAT32 pixel;
  62. if (bitstate->fill&2) {
  63. /* store LSB first */
  64. data = bitstate->bitbuffer & bitstate->mask;
  65. if (bitstate->bitcount > 32)
  66. /* bitbuffer overflow; restore it from last input byte */
  67. bitstate->bitbuffer = byte >> (8 - (bitstate->bitcount -
  68. bitstate->bits));
  69. else
  70. bitstate->bitbuffer >>= bitstate->bits;
  71. } else
  72. /* store MSB first */
  73. data = (bitstate->bitbuffer >> (bitstate->bitcount -
  74. bitstate->bits))
  75. & bitstate->mask;
  76. bitstate->bitcount -= bitstate->bits;
  77. if (bitstate->lutsize > 0) {
  78. /* map through lookup table */
  79. if (data <= 0)
  80. pixel = bitstate->lut[0];
  81. else if (data >= bitstate->lutsize)
  82. pixel = bitstate->lut[bitstate->lutsize-1];
  83. else
  84. pixel = bitstate->lut[data];
  85. } else {
  86. /* convert */
  87. if (data & bitstate->signmask)
  88. /* image memory contains signed data */
  89. pixel = (FLOAT32) (INT32) (data | ~bitstate->mask);
  90. else
  91. pixel = (FLOAT32) data;
  92. }
  93. *(FLOAT32*)(&im->image32[state->y][state->x]) = pixel;
  94. /* step forward */
  95. if (++state->x >= state->xsize) {
  96. /* new line */
  97. state->y += state->ystep;
  98. if (state->y < 0 || state->y >= state->ysize) {
  99. /* end of file (errcode = 0) */
  100. return -1;
  101. }
  102. state->x = 0;
  103. /* reset bit buffer */
  104. if (bitstate->pad > 0)
  105. bitstate->bitcount = 0;
  106. }
  107. }
  108. }
  109. return ptr - buf;
  110. }