home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / lib / table_if.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-13  |  8.2 KB  |  317 lines

  1. /*    TABLE_IF . C
  2. #
  3. %    Copyright (c)    Jin Guojun
  4. %
  5. % AUTHOR:    Jin Guojun - LBL    04/01/93
  6. */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "header.def"
  11. #include "imagedef.h"
  12.  
  13. typedef    struct    {
  14.     MType    key,    /* table index, or magic #    */
  15.         dtype;    /* input format, or type    */
  16.     int    dlen,
  17.         val;    /* data value, or marks    shorter than 3 bytes    */
  18.     char*    symbol;    /* key string, or longer marks    */
  19.     } *TC;    /* table content    */
  20.  
  21. typedef    struct    {    /* need to match QSCell    */
  22.     MType    pos;
  23.     TC    tc;
  24.     } ccstable;
  25.  
  26. static char*    table_key[] = {    "header", "width", "height", "frames", "color",
  27.     "depth", "encode", "format", "magic", "mark", "separator"};
  28. static const int t_keys = sizeof(table_key) / sizeof(table_key[0]);
  29. ccstable    ct[12];    /* first entry is for header/magic only    */
  30.  
  31. #define    DEFAULT_CCSTABLES    t_keys + 1
  32. #define    TABLE_STOP    "#STOP"
  33. #define    iscstr(s)    !strncmp(cp, s, strlen(s))
  34. #define    isccstr(s)    !strncmp(cp, s, sizeof(s)-1)
  35. #define    isbcstr(s)    !strncmp(fetchbuf, s, strlen(s))
  36.  
  37. #define    Key_Header    0
  38. #define    Key_Width    1
  39. #define    Key_Height    2
  40. #define    Key_Frames    3
  41. #define    Key_Colormap    4
  42. #define    Key_Depth    5
  43. #define    Key_Encode    6
  44. #define    Key_Format    7
  45. #define    Key_Magic    8
  46. #define    Key_Mark    9
  47. #define    Key_Separator    10
  48. #define    Mark_EOH    Key_Mark
  49. #define    Mark_EOI    11    /* enf of item    */
  50.  
  51. /*    TABLE file formats    */
  52. #define    ADDR_HEADER    0    /* default    */
  53. #define    LINE_HEADER    1    /* item is terminated by LF    */
  54. #define    SKIP_HEADER    2    /* skip to end of header    */
  55.  
  56. vff_def_cmap(U_IMAGE *img)
  57. {
  58. }
  59.  
  60. build_ccs_table(register ccstable *ct, register int nt)
  61. {
  62. if (nt < 4)    nt = DEFAULT_CCSTABLES;
  63. if (verify_buffer_size(&ct->tc, sizeof(*ct->tc), nt, No))    {
  64. register int    i = nt;
  65.     while (--i)    ct[i].tc = ct->tc + i;
  66. }
  67. if (!ct->tc)    return    prgmerr(-1, "No core for TC");
  68. return    nt;
  69. }
  70.  
  71. void
  72. free_ccs_table(register ccstable *ct)
  73. {
  74. register int    i = pointer_buffer_size(ct->tc) / sizeof(*ct->tc);
  75.     while (i--)    if (ct[i].tc->symbol)    free(ct[i].tc->symbol);
  76.     free(ct->tc);    ct->tc = NULL;
  77. }
  78.  
  79. static    void
  80. setup_colormap(TC tc, U_IMAGE *img)
  81. {
  82. register char*    cp = (char*)&tc->dtype;
  83.  
  84.     if (isccstr("GIF"))
  85.         ReadRGBMap(img, GifScreen.ColorMap, tc->dlen, No);
  86.     else if (isccstr("RAS"))
  87.         read_ras_cmap(img, tc->dlen, 3);
  88.     else if (isccstr("HEX"))
  89.         read_hex_rgbmap(img, GifScreen.ColorMap, tc->dlen);
  90.     else if (isccstr("DEF"))
  91.         vff_def_cmap(img);
  92.     else prgmerr(0, "UNKNOWN color map format: %s", cp);
  93.     img->cmaplen = tc->dlen;
  94. }
  95.  
  96. static
  97. sscan_format(register char* cp, register int i)
  98. {
  99.     if (i == Key_Format)    {
  100.     i = IFMT_SGF;
  101.     if (isccstr("SCF"))    i = IFMT_SCF;
  102.     else if (isccstr("ILC"))    i = IFMT_ILC;
  103.     else if (isccstr("ILL"))    i = IFMT_ILL;
  104.     else if (isccstr("ALPHA"))    i = IFMT_ALPHA;
  105.     else if (isccstr("SEPLANE"))    i = IFMT_SEPLANE;
  106.     else if (isccstr("BITMAP"))    i = IFMT_BITMAP;
  107.     return    i;
  108.     }    else    {    /* type / encoding    */
  109.     i = strlen(cp) - 1;
  110.     if (cp[i] == LF)    cp[i] = 0;
  111.     return    i = available_type(cp);
  112.     }
  113. }
  114.  
  115. static
  116. ctcompare(register ccstable *ct1, register ccstable *ct2)
  117. {
  118.     return    ct1->pos > ct2->pos;
  119. }
  120.  
  121. static
  122. findmark(register ccstable* ct, register int n, register int key)
  123. {
  124.     while (--n)    if (ct->tc->key == key)
  125.         return    ct->tc->val;
  126. return    n;
  127. }
  128.  
  129. static
  130. setup_img(U_IMAGE *img, int    n)
  131. {
  132. register int    i = n;
  133. int    fw=1, fh=1, fd=1;
  134.     img->frames = img->pxl_in = 1;
  135.     img->in_type = RAS;
  136.     while (--i)    {
  137.     register int    val = ct[i].tc->val;
  138.         switch (ct[i].tc->key)    {
  139.         case Key_Width:
  140.             img->width = val;    fw = False;    break;
  141.         case Key_Height:
  142.             img->height = val;    fh = False;    break;
  143.         case Key_Frames:
  144.             img->frames = val;    break;
  145.         case Key_Depth:
  146.             img->channels = val >> 3;    fd = False;    break;
  147. /*        case Key_Colormap:    */
  148.         case Key_Encode:
  149.             img->in_type = val;
  150.             val ^= img->o_type;
  151.             img->o_type ^= val;
  152.             (*img->header_handle)(HEADER_TO, img, 0, 0);
  153.             img->o_type ^= val;    break;
  154.         case Key_Format:
  155.             img->in_form = val;
  156.             img->in_color = imageform_to_colorform(val);
  157.         default:    break;
  158.         }
  159.     }
  160. free_ccs_table(ct);
  161. return    -(fw | fh | fd);
  162. }
  163.  
  164. ccs_table_if(int job, U_IMAGE*    img /* ... */)
  165. {
  166. char    fetchbuf[256], *table_list;
  167. FILE    *name_f, *table_f;
  168. int    magic, n, i;
  169. MType    mask=check_host()==5 ? 0xFFFFFF00 : 0xFFFFFF;
  170. #define    ep    table_list
  171.  
  172.     if (!(table_list=getenv("CCSTABLE_LIST")) ||
  173.     !(name_f=fopen(table_list, "r")))    return    EOF;
  174.     do    {
  175.     register TC    tc;
  176. ntry:    if (!fgets(fetchbuf, sizeof(fetchbuf), name_f))    continue;
  177.     if (isbcstr(TABLE_STOP))    break;
  178.     if (fetchbuf[i=strlen(fetchbuf)-1] == LF)
  179.         fetchbuf[i] = 0;
  180.     if (!(table_f=fopen(fetchbuf, "r")))    continue; /* ignore erroe */
  181.     if (!build_ccs_table(ct, 0))    return    EOF;
  182.     i = 1;    /* point after header type - magic field    */
  183.     do {
  184.         if (!fgets(fetchbuf, sizeof(fetchbuf), table_f))    continue;
  185.         for (n=t_keys; n--;)
  186.         if (isbcstr(table_key[n]))    {
  187.         register char*    cp = index(fetchbuf, Space);
  188.             if (!cp)    {
  189.         wf:    prgmerr(0, "wrong format %s", fetchbuf);
  190.             goto    ntry;
  191.             }
  192.             if (!n || n == Key_Magic)
  193.             tc = ct->tc;
  194.             else    tc = ct[i].tc;
  195.             if (n < Key_Mark)    {    /* get 2nd param, data len */
  196.             sscanf(++cp, "%d", &tc->dlen);
  197.             cp = index(cp, Space);
  198.             }
  199.             tc->key = n;
  200.             if(n && n < Key_Magic) {    /* common formats    */
  201.             register int    dtype;
  202.             if (!sscanf(++cp, "%s", &tc->dtype) ||
  203.                 !(cp = index(cp, Space)))    goto    wf;
  204.             tc->dtype &= mask;    ep = (char*)&tc->dtype;
  205.             dtype = *ep == '#';    /* IM value    */
  206.             if (n > Key_Depth && dtype) {    /* check format    */
  207.                 tc->val = sscan_format(++cp, n);
  208.                 if (n == Key_Encode)
  209.                     ct[i].pos = -1;
  210.             }
  211.             else sscanf(++cp, "%d", dtype ? &tc->val : &ct[i].pos);
  212.             if ((cp=index(cp, Space)) && *++cp != '#')    {
  213.                 if (ep=index(cp, Space))
  214.                 n = ep - cp;
  215.                 else n = strlen(cp);
  216.                 if (tc->symbol=NZALLOC(n, 1, No))
  217.                 strncpy(tc->symbol, cp, n);
  218.             }
  219.             } else {    /* key & marks    */
  220.             switch (n)    {
  221.             case Key_Header:
  222.             case Key_Magic:
  223.                 if (!sscanf(++cp, "%d", &tc->dtype) ||
  224.                 tc->dlen && (cp=index(cp, Space)) &&
  225.                 !sscanf(++cp, "%4c", &tc->key))    goto    wf;
  226.                 if (cp = index(cp, Space))    {
  227.                 if (iscstr("line"))
  228.                     tc->val = LINE_HEADER;
  229.                 else if (iscstr("skip") || !tc->key)
  230.                     tc->val = SKIP_HEADER;
  231.                 }    i--;    /* header is reserved    */
  232.                 break;
  233.             case Key_Mark:
  234.                 if (iscstr("end of"))    cp += 7;
  235.                 if (iscstr("item") || iscstr("line"))
  236.                 cp += 5,    tc->key = Mark_EOI;
  237.                 else    {
  238.                 if (iscstr("eoh"))    cp += 4;
  239.                 else if (iscstr("header"))    cp += 7;
  240.                 else    goto    wf;
  241.                 }
  242.                 if (!sscanf(cp, "%s", &tc->val))    goto    wf;
  243.                 tc->val &= mask;
  244.                 if (index((char*)&tc->val, LF))
  245.                 ct[0].tc->val = LINE_HEADER;
  246.                 break;
  247.             case Key_Separator:
  248.             default:    goto    wf;
  249.             }
  250.             }
  251.         i++;    break;
  252.         }
  253.         if (n < 0)    prgmerr(0, "non table key %s", fetchbuf);
  254.     } while (!feof(table_f));
  255.     fclose(table_f);
  256.  
  257.     tc = ct->tc;
  258.     if (tc->key)    {    /* magic present (not skip)    */
  259.     (*img->seek)(img->IN_FP, 0, 0);
  260.     fread(&magic, tc->dlen, 1, img->IN_FP);
  261.         if (tc->key != magic)    continue;
  262.         else if (!tc->val)    {    /* file macth in address mode    */
  263. #ifdef    FAST_REVERSE_SORT
  264.         QuickSort(1, i-1, i-1, ct);
  265. #else
  266.         qsort(ct+1, i-1, sizeof(ct[1]), ctcompare);
  267. #endif
  268.         for (n=0; ++n < i;)    {
  269.             tc = ct[n].tc;
  270.             if (tc->key == Key_Colormap)    {
  271.             if (ftell(img->IN_FP) != ct[n].pos)
  272.                 (*img->seek)(img->IN_FP, ct[n].pos, 0);
  273.             setup_colormap(tc, img);
  274.             } else if (ct[n].pos && tc->dtype)    {
  275.             (*img->seek)(img->IN_FP, ct[n].pos, 0);
  276.             fscanf(img->IN_FP, (char*)&tc->dtype, &tc->val);
  277.             }
  278.         }
  279.             goto    tif_skip;
  280.         } else {    /* in line mode    */
  281.         register char*    mark = (char*)findmark(ct, i, Mark_EOI);
  282.             if (n=(int)index(mark, LF))
  283.             fgets(fetchbuf, sizeof(fetchbuf), img->IN_FP);
  284.             else for (n=0; fetchbuf[n] != *mark; n++)
  285.             fetchbuf[n] = fgetc(img->IN_FP);
  286.  
  287.         goto    tif_skip;
  288.         }
  289.     } else    {    /* skipping mode    */
  290. tif_skip:    tc = ct->tc;
  291.         if (tc->dtype)    /* skip to eoh    */
  292.             (*img->seek)(img->IN_FP, tc->dtype, 0);
  293.             else if (n=findmark(ct, i, Mark_EOH))    {
  294.             register char*    mark = (char*)n;
  295.     fag:        while (fgetc(img->IN_FP) != *mark && !feof(img->IN_FP));
  296.             if (mark[1] && fgetc(img->IN_FP) != mark[1])
  297.                 if (!feof(img->IN_FP))
  298.                 goto    fag;
  299.                 else return    prgmerr(0, "? eoh mark %s", mark);
  300.             } else    message("warning: not search for eoh\n");
  301.         fclose(name_f);
  302.         return    setup_img(img, i);
  303.     }
  304.     } while (!feof(name_f));
  305.     fclose(name_f);
  306. return    EOF;
  307. }
  308.  
  309. #ifdef    PROGRAM_TEST
  310. char    *Progname;
  311. main()
  312. {
  313. U_IMAGE    img;
  314.     ccs_table_if(&img);
  315. }
  316. #endif
  317.