/*
 * Copyright (c) 1997-2005 Alexandros Eleftheriadis and Danny Hong.
 * 
 * This file is part of Flavor, developed at Columbia University
 * (www.ee.columbia.edu/flavor).
 *
 * Flavor is free software; you can redistribute it and/or modify
 * it under the terms of the Flavor Artistic License as described in
 * the file COPYING.txt. 
 *
 */

/*
 * Authors:
 * Danny Hong <danny@ee.columbia.edu>
 *
 */


/* 
 * The binary arithmetic coder described in 
 * @MANUAL{
 * TITLE        = {Information technology -- Lossy/lossless coding of bi-level images},
 * ORGANIZATION = {ISO/IEC 14492 International Standard (JBIG2)},
 * YEAR         = {2001},
 *
 */


const int rTblMQ[47] = {
    0x5601, 0x3401, 0x1801, 0x0ac1, 0x0521, 0x0221, 0x5601, 0x5401, 0x4801, 0x3801, 
    0x3001, 0x2401, 0x1c01, 0x1601, 0x5601, 0x5401, 0x5101, 0x4801, 0x3801, 0x3401, 
    0x3001, 0x2801, 0x2401, 0x2201, 0x1c01, 0x1801, 0x1601, 0x1401, 0x1201, 0x1101, 
    0x0ac1, 0x09c1, 0x08a1, 0x0521, 0x0441, 0x02a1, 0x0221, 0x0141, 0x0111, 0x0085, 
    0x0049, 0x0025, 0x0015, 0x0009, 0x0005, 0x0001, 0x5601
};
const int iTblMQ[47][2] = {
    {1, 1},  {6, 2},  {9, 3}, {12, 4}, {29, 5}, {33,38},  {6, 7}, {14, 8}, {14, 9}, {14,10},
   {17,11}, {18,12}, {20,13}, {21,29}, {14,15}, {14,16}, {15,17}, {16,18}, {17,19}, {18,20},
   {19,21}, {19,22}, {20,23}, {21,24}, {22,25}, {23,26}, {24,27}, {25,28}, {26,29}, {27,30},
   {28,31}, {29,32}, {30,33}, {31,34}, {32,35}, {33,36}, {34,37}, {35,38}, {36,39}, {37,40},
   {38,41}, {39,42}, {40,43}, {41,44}, {42,45}, {43,45}, {46,46}
};

bac MQCoder { 
    "ooc",      2,                  // No multiplicative operation --> fast BAC
    "soc",      1,                  // MPS over LPS
    "norm",     0x8000,             // Renormalize whenever R < 0x8000 
    "bs",       {8,1,3},            // Stuff a 0 bit after each 0xFF; use 3 spacer bits
    "init",     {0x8000,15},        // Init: R=0x8000; read 15 code stream bits for decoding
    "end",      {0,2},              // End: Output minimal bits; byte align with 1 bits
    "rtable",   {47,1,"rTblMQ"},    // The rTbl table contains possible R values for the LPS 
    "nexti",    "iTblMQ",           // The iTbl table contains transition rules for the LPS
    "trans",    1,                  // Apply transition after renormalization
    "exch",     1                   // Apply conditional exchange 
}