Annotation of 43BSDTahoe/new/dipress/src/bin/ipmetrics/stack.c, revision 1.1.1.1

1.1       root        1: /* stack.c
                      2:  *
                      3:  * Copyright (c) 1984, 1985 Xerox Corp.
                      4:  *
                      5:  * This module manipulates the RES stack.
                      6:  *
                      7:  */
                      8: 
                      9: #include "stack.h"
                     10: 
                     11: extern long filepos;
                     12: extern unsigned char **malloc();
                     13: 
                     14: /* Private procedures defined in this module. */
                     15: extern unsigned char *makeitem();
                     16: extern double readdouble();
                     17: 
                     18: struct item
                     19:   {
                     20:   int length;                 /* total byte length of this stack item. */
                     21:   struct item *next;          /* next element on the stack. */
                     22:   int type;                   /* stack element type. */
                     23:   int subtype;                /* subtype within this type. */
                     24:   };
                     25: 
                     26: struct number
                     27:   {
                     28:   int length;
                     29:   struct item *next;
                     30:   int type;
                     31:   int subtype;
                     32:   unsigned char nbytes;
                     33:   unsigned char num[1];
                     34:   };
                     35: 
                     36: struct string
                     37:   {
                     38:   int length;
                     39:   struct item *next;
                     40:   int type;
                     41:   int subtype;
                     42:   char s[1];
                     43:   /* the string follows here, above char gives room for terminating null */
                     44:   };
                     45: 
                     46: struct vector
                     47:   {
                     48:   int length;
                     49:   struct item *next;
                     50:   int type;
                     51:   int subtype;
                     52:   int depth;
                     53:   };
                     54: 
                     55: struct integers
                     56:   {
                     57:   int length;
                     58:   struct item *next;
                     59:   int type;
                     60:   int subtype;
                     61:   int bytesPerInteger;
                     62:   long bytepos;
                     63:   long bytelength;
                     64:   };
                     65: 
                     66: 
                     67: struct transformation
                     68:   {
                     69:   int length;
                     70:   struct item *next;
                     71:   int type;
                     72:   int subtype;
                     73:   double a;
                     74:   double b;
                     75:   double c;
                     76:   double d;
                     77:   double e;
                     78:   double f;
                     79:   };
                     80: 
                     81: static struct item *stack = (struct item *) 0;
                     82: 
                     83: #define err0 "(%d) stack: pop called on an empty stack!\n"
                     84: #define err1 "(%d) stack: expecting type (%s, %s), got (%d, %d)!\n"
                     85: #define err2 "(%d) stack: should be 8 elements in pixelarray, got %d!\n"
                     86: 
                     87: 
                     88: /*
                     89:  *  Items on the stack.
                     90:  *
                     91:  */
                     92: 
                     93: stackempty()
                     94:   {
                     95:   return(stack == (struct item *) 0);
                     96:   }
                     97: 
                     98: unsigned char *pop(type, subtype)
                     99:   int type, subtype;
                    100:   {
                    101:   unsigned char *ptr;
                    102:   struct item *pitem;
                    103: 
                    104:   pitem = stack;
                    105: 
                    106:   if (pitem == (struct item *) 0)
                    107:        error(err0, filepos);
                    108: 
                    109:   stack = pitem->next;
                    110:   ptr = (unsigned char *) pitem;
                    111:   checktype(ptr, type, subtype);
                    112:   return (ptr);
                    113:   }
                    114: 
                    115: push(pitem)
                    116:   struct item *pitem;
                    117:   {
                    118:   pitem->next = stack;
                    119:   stack = pitem;
                    120:   }
                    121: 
                    122: unsigned char *duplicate(from)
                    123:   unsigned char *from;
                    124:   {
                    125:   int n;
                    126:   unsigned char *to;
                    127:   to = makeitem(getlength(from), gettype(from), getsubtype(from));
                    128:   for (n=0; n < getlength(from); n++) to[n] = from[n];
                    129:   return (to);
                    130:   }
                    131: 
                    132: gettype(pitem)
                    133:   struct item *pitem;
                    134:   {
                    135:   return(pitem->type);
                    136:   }
                    137: 
                    138: getsubtype(pitem)
                    139:   struct item *pitem;
                    140:   {
                    141:   return(pitem->subtype);
                    142:   }
                    143: 
                    144: getlength(pitem)
                    145:   struct item *pitem;
                    146:   {
                    147:   return(pitem->length);
                    148:   }
                    149: 
                    150: checktype(ptr, type, subtype)
                    151:   unsigned char *ptr;
                    152:   int type, subtype;
                    153:   {
                    154:   int intype, insubtype, problem;
                    155:   char *typename, *subtypename;
                    156:   intype = gettype(ptr);
                    157:   insubtype = getsubtype(ptr);
                    158:   problem = 0;
                    159:   if (type)
                    160:     {
                    161:     if ((type & intype) == 0) problem = 1;
                    162:     if ((subtype != 0) && ((subtype & insubtype) == 0)) problem = 1;
                    163:     }
                    164:   if (problem)
                    165:     {
                    166:     typename = gettypename(type);
                    167:     subtypename = getsubtypename(subtype);
                    168:     error(err1, filepos, typename, subtypename, intype, insubtype);
                    169:     }
                    170:   }
                    171: 
                    172: char *gettypename(type)
                    173:   int type;
                    174:   {
                    175:   switch (type)
                    176:     {
                    177:     case 0: return("undefined");
                    178:     case 1: return("number");
                    179:     case 2: return("string");
                    180:     case 4: return("vector");
                    181:     case 8: return("operator");
                    182:     case 16: return("color");
                    183:     case 32: return("pixelarray");
                    184:     case 64: return("transformation");
                    185:     case 128: return("integers");
                    186:     default: return("unknown");
                    187:     };
                    188:   }
                    189: 
                    190: char *getsubtypename(subtype)
                    191:   int subtype;
                    192:   {
                    193:   switch (subtype)
                    194:     {
                    195:     case 0: return("undefined");
                    196:     case 1: return("integer");
                    197:     case 2: return("rational");
                    198:     case 4: return("identifier");
                    199:     case 8: return("string");
                    200:     case 16: return("general");
                    201:     case 32: return("integers");
                    202:     case 64: return("samples");
                    203:     case 128: return("decompressop");
                    204:     case 256: return("colorop");
                    205:     case 512: return("colormodelop");
                    206:     case 1024: return("value");
                    207:     case 2048: return("name");
                    208:     case 4096: return("operator");
                    209:     default: return("unknown");
                    210:     };
                    211:   }
                    212: 
                    213: 
                    214: /*
                    215:  *  Numbers
                    216:  *
                    217:  */
                    218: 
                    219: unsigned char *makenumber(nbytes, array, subtype)
                    220:   int nbytes;
                    221:   unsigned char *array;
                    222:   {
                    223:   int n;
                    224:   unsigned char *ptr;
                    225:   struct number *pnumber;
                    226:   ptr = makeitem(sizeof(struct number)+nbytes, type_number, subtype);
                    227:   pnumber = (struct number *) ptr;
                    228:   pnumber->nbytes = nbytes;
                    229:   for (n=0; n < nbytes; n++) pnumber->num[n] = array[n];
                    230:   return(ptr);
                    231:   }
                    232: 
                    233: getnumlen(pnumber)
                    234:   struct number *pnumber;
                    235:   {
                    236:   checktype(pnumber, type_number, 0);
                    237:   return(pnumber->nbytes);
                    238:   }
                    239: 
                    240: unsigned char *getnumber(pnumber)
                    241:   struct number *pnumber;
                    242:   {
                    243:   checktype(pnumber, type_number, 0);
                    244:   return(pnumber->num);
                    245:   }
                    246: 
                    247: getint(ptr)
                    248:   unsigned char *ptr;
                    249:   {
                    250:   int result;
                    251:   result = getdouble(ptr);
                    252:   return (result);
                    253:   }
                    254:  
                    255: double getdouble(pnumber)
                    256:   struct number *pnumber;
                    257:   {
                    258:   int nbytes;
                    259:   double result, numerator, denominator;
                    260:   checktype(pnumber, type_number, subtype_integer | subtype_rational);
                    261:   switch (getsubtype(pnumber))
                    262:     {
                    263:     case subtype_integer:
                    264:       result = readdouble(pnumber->nbytes, pnumber->num);
                    265:       break;
                    266:     case subtype_rational:
                    267:       nbytes = pnumber->nbytes/2;
                    268:       numerator = readdouble(nbytes, pnumber->num);
                    269:       denominator = readdouble(nbytes, pnumber->num+nbytes);
                    270:       result = numerator/denominator;
                    271:       break;
                    272:     }
                    273:   return (result);
                    274:   }
                    275: 
                    276: double getnumerator(pnumber)
                    277:   unsigned char *pnumber;
                    278:   {
                    279:   int nbytes;
                    280:   double numerator;
                    281:   checktype(pnumber, type_number, subtype_rational);
                    282:   nbytes = getnumlen(pnumber)/2;
                    283:   numerator = readdouble(nbytes, getnumber(pnumber));
                    284:   return (numerator);
                    285:   }
                    286: 
                    287: double getdenominator(pnumber)
                    288:   unsigned char *pnumber;
                    289:   {
                    290:   int nbytes;
                    291:   double denominator;
                    292:   checktype(pnumber, type_number, subtype_rational);
                    293:   nbytes = getnumlen(pnumber)/2;
                    294:   denominator = readdouble(nbytes, getnumber(pnumber)+nbytes);
                    295:   return (denominator);
                    296:   }
                    297: 
                    298: 
                    299: /*
                    300:  *  Strings
                    301:  *
                    302:  */
                    303: 
                    304: unsigned char *makestring(string, subtype)
                    305:   char *string;
                    306:   int subtype;
                    307:   {
                    308:   unsigned char *ptr;
                    309:   struct string *pstring;
                    310:   ptr = makeitem(sizeof(struct string)+strlen(string), type_string, subtype);
                    311:   pstring = (struct string *) ptr;
                    312:   strcpy(pstring->s, string);
                    313:   return(ptr);
                    314:   }
                    315: 
                    316: unsigned char *makeidentifier(ptr, prefix)
                    317:   unsigned char *ptr;
                    318:   char *prefix;
                    319:   {
                    320:   char *string;
                    321:   int n, len, depth;
                    322:   unsigned char *composite, **array;
                    323:   depth = getdepth(ptr);
                    324:   array = getvector(ptr);
                    325:   len = strlen(prefix);
                    326:   for (n=0; n < depth; n++)
                    327:     {
                    328:     string = getstring(array[n], subtype_identifier);
                    329:     len += strlen(string)+1;    /* add one for '/' character */
                    330:     }                           /* added one too many, gives extra space */
                    331:   string = (char *) malloc(len+1);   /* add one for null character */
                    332:   strcpy(string, prefix);
                    333:   for (n=0; n < depth; n++)
                    334:     {
                    335:     if (n) strcat(string, "/");
                    336:     strcat(string, getstring(array[n], subtype_identifier));
                    337:     }
                    338:   free(array);
                    339:   composite = makestring(string, subtype_identifier);
                    340:   free(string);
                    341:   return(composite);
                    342:   }
                    343: 
                    344: char *getstring(pstring, subtype)
                    345:   struct string *pstring;
                    346:   {
                    347:   checktype(pstring, type_string, subtype);
                    348:   return(pstring->s);
                    349:   }
                    350: 
                    351: 
                    352: /*
                    353:  *  Vectors
                    354:  *
                    355:  */
                    356: 
                    357: unsigned char *makevector(array, type, subtype)
                    358:   unsigned char **array;
                    359:   int type, subtype;
                    360:   {
                    361:   int n, m, len, depth;
                    362:   unsigned char *ptr, *to, *from;
                    363:   struct vector *pvector;
                    364:   len = sizeof(struct vector);
                    365:   for (depth=0; array[depth] != (unsigned char *) 0; depth++)
                    366:     len += getlength(array[depth]);
                    367:   ptr = makeitem(len, type, subtype);
                    368:   pvector = (struct vector *) ptr;
                    369:   pvector->depth = depth;
                    370:   to = ptr;
                    371:   to += sizeof(struct vector);
                    372:   for (n=0; n < depth; n++)
                    373:     {
                    374:     from = array[n];
                    375:     len = getlength(from);
                    376:     for (m=0; m < len; m++) to[m] = from[m];
                    377:     to += len;
                    378:     }
                    379:   return(ptr);
                    380:   }
                    381: 
                    382: unsigned char **getvector(from)
                    383:   unsigned char *from;
                    384:   {
                    385:   int n, m, depth, len;
                    386:   unsigned char *to, **array;
                    387: 
                    388:   depth = getdepth(from);
                    389:   array = malloc((depth+1)*sizeof(unsigned char *));
                    390:   from += sizeof(struct vector);
                    391: 
                    392:   for (n=0; n < depth; n++) {
                    393:     array[n] = from;
                    394:     len = getlength(from);
                    395:     from += len;
                    396:   }
                    397: 
                    398:   array[depth] = (unsigned char *) 0;    /* null terminated list */
                    399:   return(array);
                    400:   }
                    401:  
                    402: getdepth(pvector)
                    403:   struct vector *pvector;
                    404:   {
                    405:   return(pvector->depth);
                    406:   }
                    407: 
                    408: 
                    409: 
                    410: /*
                    411:  *  Operators
                    412:  *
                    413:  */
                    414: 
                    415: unsigned char *makeoperator(array, subtype)
                    416:   unsigned char **array;
                    417:   int subtype;
                    418:   {
                    419:   unsigned char *ptr;
                    420:   ptr = makevector(array, type_operator, subtype);
                    421:   return(ptr);
                    422:   }
                    423: 
                    424: unsigned char **getoperator(ptr)
                    425:   unsigned char *ptr;
                    426:   {
                    427:   unsigned char **array;
                    428:   array = getvector(ptr);
                    429:   return(array);
                    430:   }
                    431: 
                    432: 
                    433: 
                    434: 
                    435: /*
                    436:  *  Pixel array
                    437:  *
                    438:  */
                    439: 
                    440: 
                    441: unsigned char *makepixelarray(array)
                    442:   unsigned char **array;
                    443:   {
                    444:   int depth;
                    445:   unsigned char *ptr;
                    446:   for (depth=0; array[depth] != (unsigned char *) 0; depth++) ;
                    447:   if (depth != 8) error(err2, filepos, depth);
                    448:   ptr = makevector(array, type_pixelarray, 0);
                    449:   return(ptr);
                    450:   }
                    451: 
                    452: unsigned char *makeselect(max, samples)
                    453:   int max, samples;
                    454:   {
                    455:   int n, mask, depth;
                    456:   unsigned char *ptr, **array, value;
                    457:   array = malloc((max+1)*sizeof(unsigned char *));
                    458:   depth = 0;
                    459:   for (n=0; n < max; n++)
                    460:     {
                    461:     value = n;
                    462:     mask = 1 << n;
                    463:     if (samples & mask)
                    464:       {
                    465:       array[depth] = makenumber(1, &value, subtype_integer);
                    466:       depth++;
                    467:       }
                    468:     }
                    469:   array[depth] = (unsigned char *) 0;    /* null terminated list */
                    470:   ptr = makevector(array, type_vector, subtype_general);
                    471:   for (n=0; n < depth; n++) free(array[n]);
                    472:   free(array);
                    473:   return(ptr);
                    474:   }
                    475: 
                    476: unsigned char **getpixelarray(from)
                    477:   unsigned char *from;
                    478:   {
                    479:   if (getdepth(from) != 8) error(err2, filepos, getdepth(from));
                    480:   return(getvector(from));
                    481:   }
                    482: 
                    483: 
                    484: /*
                    485:  *  Transformations
                    486:  *
                    487:  */
                    488: 
                    489: unsigned char *maketransformation(a, b, c, d, e, f)
                    490:   double a, b, c, d, e, f;
                    491:   {
                    492:   unsigned char *ptr;
                    493:   struct transformation *ptransformation;
                    494:   ptr = makeitem(sizeof(struct transformation), type_transformation, 0);
                    495:   ptransformation = (struct transformation *) ptr;
                    496:   ptransformation->a = a;
                    497:   ptransformation->b = b;
                    498:   ptransformation->c = c;
                    499:   ptransformation->d = d;
                    500:   ptransformation->e = e;
                    501:   ptransformation->f = f;
                    502:   return(ptr);
                    503:   }
                    504: 
                    505: double *gettransformation(ptransformation)
                    506:   struct transformation *ptransformation;
                    507:   {
                    508:   double *array;
                    509:   checktype(ptransformation, type_transformation, 0);
                    510:   array = (double *) malloc(6*sizeof(double));
                    511:   array[0] = ptransformation->a;
                    512:   array[1] = ptransformation->b;
                    513:   array[2] = ptransformation->c;
                    514:   array[3] = ptransformation->d;
                    515:   array[4] = ptransformation->e;
                    516:   array[5] = ptransformation->f;
                    517:   return(array);
                    518:   }
                    519: 
                    520: 
                    521: /*
                    522:  *  Integers
                    523:  *
                    524:  */
                    525: 
                    526: unsigned char *makeintegers(bytesPerInteger, bytepos, bytelength)
                    527:   int bytesPerInteger;
                    528:   long bytepos, bytelength;
                    529:   {
                    530:   unsigned char *ptr;
                    531:   struct integers *pintegers;
                    532:   ptr = makeitem(sizeof(struct integers), type_integers, 0);
                    533:   pintegers = (struct integers *) ptr;
                    534:   pintegers->bytesPerInteger = bytesPerInteger;
                    535:   pintegers->bytepos = bytepos;
                    536:   pintegers->bytelength = bytelength;
                    537:   return(ptr);
                    538:   }
                    539: 
                    540: getbytesPerInteger(pintegers)
                    541:   struct integers *pintegers;
                    542:   {
                    543:   checktype(pintegers, type_integers, 0);
                    544:   return(pintegers->bytesPerInteger);
                    545:   }
                    546: 
                    547: long getbytepos(pintegers)
                    548:   struct integers *pintegers;
                    549:   {
                    550:   checktype(pintegers, type_integers, 0);
                    551:   return(pintegers->bytepos);
                    552:   }
                    553: 
                    554: long getbytelength(pintegers)
                    555:   struct integers *pintegers;
                    556:   {
                    557:   checktype(pintegers, type_integers, 0);
                    558:   return(pintegers->bytelength);
                    559:   }
                    560: 
                    561: 
                    562: 
                    563: /*
                    564:  *  Private procedures to this module.
                    565:  *
                    566:  */
                    567: 
                    568: static unsigned char *makeitem(length, type, subtype)
                    569:   int length, type, subtype;
                    570:   {
                    571:   unsigned char *ptr;
                    572:   struct item *pitem;
                    573:   length = (length+sizeof(int)-1)/sizeof(int);
                    574:   length *= sizeof(int);
                    575:   ptr = (unsigned char *) malloc(length);
                    576:   pitem = (struct item *) ptr;
                    577:   pitem->length = length;
                    578:   pitem->type = type;
                    579:   pitem->subtype = subtype;
                    580:   return(ptr);
                    581:   }
                    582: 
                    583: static double readdouble(nbytes, array)
                    584:   int nbytes;
                    585:   unsigned char *array;
                    586:   {
                    587:   int n, negative, temp;
                    588:   double result;
                    589:   negative = 0;
                    590:   if (array[0] > 127)
                    591:     {
                    592:     negative = 1;
                    593:     for (n=0; n < nbytes; n++) array[n] = ~array[n];
                    594:     }
                    595:   result = 0;
                    596:   for (n=0; n < nbytes; n++)
                    597:     {
                    598:     temp = array[n];
                    599:     result = 256.*result+(double)temp;
                    600:     }
                    601:   if (negative) result = -result-1;
                    602:   return(result);
                    603:   }
                    604: 
                    605: 
                    606: 
                    607: /* Change Log
                    608:  *
                    609:  * K. Knox, 29-Mar-85 18:20:18, Created first version.
                    610:  * K. Knox, 13-May-85  9:50:52, Fixed negative number bug in readdouble().
                    611:  *
                    612:  *
                    613:  *
                    614:  */
                    615:  
                    616:  
                    617:  
                    618:  

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.