home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / graphics / gifbla20.zip / SOURCE / GB11CODE.C < prev    next >
C/C++ Source or Header  |  1992-12-15  |  4KB  |  157 lines

  1.  
  2. /* gb11code.c - Special purpose GIF compressor routines (version 1.1). */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "uffile.h"
  8. #include "arith.h"
  9. #include "arithmod.h"
  10. #include "gbcon.h"
  11. #include "gb11code.h"
  12.  
  13. extern void *(*basic_alloc) P((size_t size),());
  14. extern void (*basic_free) P((void *block),());
  15.  
  16. GB11_CODER *
  17. gb11_alloc_coder()
  18. {
  19.     GB11_CODER *gb11;
  20.  
  21.     if ((gb11=basic_alloc(sizeof(GB11_CODER))) == NULL)
  22.         return NULL;
  23.     gb11->ac_order_1_cons =
  24.         basic_alloc(ARCON_NCODES*sizeof(ARCON_SMALL_CONTEXT));
  25.     if (gb11->ac_order_1_cons == NULL) {
  26.         basic_free(gb11);
  27.         return NULL;
  28.     }
  29.     return gb11;
  30. }
  31.  
  32. void
  33. gb11_free_coder(gb11)
  34. GB11_CODER *gb11;
  35. {
  36.     if (gb11 != NULL) {
  37.         if (gb11->ac_order_1_cons != NULL)
  38.             basic_free(gb11->ac_order_1_cons);
  39.         basic_free(gb11);
  40.     }
  41. }
  42.  
  43. static void
  44. gb11_init(gb11)
  45. GB11_CODER *gb11;
  46. {
  47.     int i;
  48.  
  49.     arcon_big_init(&gb11->ac_order_0_con);
  50.     for (i=0; i<ARCON_NCODES; i++)
  51.         arcon_small_init(&(gb11->ac_order_1_cons[i]),ARCON_V11_SMALL_RBSIZE);
  52.     arcon_type_init(&gb11->ac_type_con);
  53.     gb11->prev = 0;
  54. }
  55.  
  56. int
  57. gb11_start_encoding(gb11,ff)
  58. GB11_CODER *gb11; FFILE *ff;
  59. {
  60.     gb11_init(gb11);
  61.     return arith_start_encoding(&gb11->ac_struct,ff);
  62. }
  63.  
  64. int
  65. gb11_encode_c(c,gb11)
  66. int c; GB11_CODER *gb11;
  67. {
  68.     int rstart,rend,rtot,t,trstart,trend,trtot;
  69.     ARCON_BIG_CONTEXT *big_con;
  70.     ARCON_SMALL_CONTEXT *small_con;
  71.     ARCON_TYPE_CONTEXT *type_con;
  72.  
  73.     big_con = &gb11->ac_order_0_con;
  74.     small_con = &(gb11->ac_order_1_cons[gb11->prev]);
  75.     type_con = &gb11->ac_type_con;
  76.     if (arcon_small_find_range(small_con,c,&rstart,&rend)) {
  77.         t = 1;
  78.         rtot = arcon_small_rtot(small_con);
  79.     } else if (arcon_big_find_range(big_con,c,&rstart,&rend)) {
  80.         t = 0;
  81.         rtot = arcon_big_rtot(big_con);
  82.     } else
  83.         return -1;
  84.     if (arcon_type_find_range(type_con,t,&trstart,&trend))
  85.         trtot = arcon_type_rtot(type_con);
  86.     else
  87.         return -1;
  88.     if (arith_encode(&gb11->ac_struct,trstart,trend,trtot)<0
  89.         || arith_encode(&gb11->ac_struct,rstart,rend,rtot)<0
  90.         || arcon_type_add(type_con,t)<0
  91.         || arcon_small_add(small_con,c)<0
  92.         || (t==0 && arcon_big_add(big_con,c)<0))
  93.         return -1;
  94.     gb11->prev = c;
  95.     return 0;
  96. }
  97.  
  98. int
  99. gb11_end_encoding(gb11)
  100. GB11_CODER *gb11;
  101. {
  102.     return arith_end_encoding(&gb11->ac_struct);
  103. }
  104.  
  105. int
  106. gb11_start_decoding(gb11,ff)
  107. GB11_CODER *gb11; FFILE *ff;
  108. {
  109.     gb11_init(gb11);
  110.     return arith_start_decoding(&gb11->ac_struct,ff);
  111. }
  112.  
  113. int
  114. gb11_decode_c(gb11)
  115. GB11_CODER *gb11;
  116. {
  117.     int trstart,trend,trtot,trpos,t,rstart,rend,rtot,rpos,c;
  118.     ARCON_BIG_CONTEXT *big_con;
  119.     ARCON_SMALL_CONTEXT *small_con;
  120.     ARCON_TYPE_CONTEXT *type_con;
  121.  
  122.     big_con = &gb11->ac_order_0_con;
  123.     small_con = &(gb11->ac_order_1_cons[gb11->prev]);
  124.     type_con = &gb11->ac_type_con;
  125.     trtot = arcon_type_rtot(type_con);
  126.     trpos = arith_decode_getrpos(&gb11->ac_struct,trtot);
  127.     if ((t=arcon_type_find_c(type_con,trpos,&trstart,&trend))<0
  128.         || arith_decode_advance(&gb11->ac_struct,trstart,trend,trtot)<0
  129.         || arcon_type_add(type_con,t)<0)
  130.         return -1;
  131.     if (t == 0) {
  132.         rtot = arcon_big_rtot(big_con);
  133.         rpos = arith_decode_getrpos(&gb11->ac_struct,rtot);
  134.         if ((c=arcon_big_find_c(big_con,rpos,&rstart,&rend))<0
  135.             || arith_decode_advance(&gb11->ac_struct,rstart,rend,rtot)<0
  136.             || arcon_big_add(big_con,c)<0
  137.             || arcon_small_add(small_con,c)<0)
  138.             return -1;
  139.     } else {
  140.         rtot = arcon_small_rtot(small_con);
  141.         rpos = arith_decode_getrpos(&gb11->ac_struct,rtot);
  142.         if ((c=arcon_small_find_c(small_con,rpos,&rstart,&rend))<0
  143.             || arith_decode_advance(&gb11->ac_struct,rstart,rend,rtot)<0
  144.             || arcon_small_add(small_con,c)<0)
  145.             return -1;
  146.     }
  147.     gb11->prev = c;
  148.     return c;
  149. }
  150.  
  151. int
  152. gb11_end_decoding(gb11)
  153. GB11_CODER *gb11;
  154. {
  155.     return arith_end_decoding(&gb11->ac_struct);
  156. }
  157.