Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2001 Artifex Software, Inc.
  5.  
  6.     This software is provided AS-IS with no warranty,
  7.     either express or implied.
  8.  
  9.     This software is distributed under license and may not
  10.     be copied, modified or distributed except as expressly
  11.     authorized under the terms of the license contained in
  12.     the file LICENSE in this distribution.
  13.  
  14.     For further licensing information refer to http://artifex.com/ or
  15.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  16.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  17. */
  18.  
  19. /* Annex A.3 */
  20.  
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "os_types.h"
  25.  
  26. #include <stddef.h>
  27. #include <string.h> /* memset() */
  28.  
  29. #ifdef VERBOSE
  30. #include <stdio.h> /* for debug printing only */
  31. #endif
  32.  
  33. #include "jbig2.h"
  34. #include "jbig2_priv.h"
  35. #include "jbig2_arith.h"
  36. #include "jbig2_arith_iaid.h"
  37.  
  38. struct _Jbig2ArithIaidCtx {
  39.   int SBSYMCODELEN;
  40.   Jbig2ArithCx *IAIDx;
  41. };
  42.  
  43. Jbig2ArithIaidCtx *
  44. jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN)
  45. {
  46.   Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
  47.   int ctx_size = 1 << SBSYMCODELEN;
  48.  
  49.   result->SBSYMCODELEN = SBSYMCODELEN;
  50.   result->IAIDx = jbig2_alloc(ctx->allocator, ctx_size);
  51.   memset(result->IAIDx, 0, ctx_size);
  52.  
  53.   return result;
  54. }
  55.  
  56. /* A.3 */
  57. /* Return value: -1 on error, 0 on normal value */
  58. int
  59. jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as,
  60.                        int32_t *p_result)
  61. {
  62.   Jbig2ArithCx *IAIDx = ctx->IAIDx;
  63.   int SBSYMCODELEN = ctx->SBSYMCODELEN;
  64.   int PREV = 1;
  65.   int D;
  66.   int i;
  67.  
  68.   /* A.3 (2) */
  69.   for (i = 0; i < SBSYMCODELEN; i++)
  70.     {
  71.       D = jbig2_arith_decode(as, &IAIDx[PREV]);
  72. #ifdef VERBOSE
  73.       fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
  74. #endif
  75.       PREV = (PREV << 1) | D;
  76.     }
  77.   /* A.3 (3) */
  78.   PREV -= 1 << SBSYMCODELEN;
  79. #ifdef VERBOSE
  80.   fprintf(stderr, "IAID result: %d\n", PREV);
  81. #endif
  82.   *p_result = PREV;
  83.   return 0;
  84. }
  85.  
  86. void
  87. jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
  88. {
  89.   jbig2_free(ctx->allocator, iax->IAIDx);
  90.   jbig2_free(ctx->allocator, iax);
  91. }
  92.