home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / value.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  2KB  |  93 lines

  1. /* value.c:
  2.  *
  3.  * routines for converting byte values to long values.  these are pretty
  4.  * portable although they are not necessarily the fastest things in the
  5.  * world.
  6.  *
  7.  * jim frost 10.02.89
  8.  *
  9.  * Copyright 1989 Jim Frost.
  10.  * See included file "copyright.h" for complete copyright information.
  11.  */
  12.  
  13. #include "copyright.h"
  14. #include "image.h"
  15.  
  16. unsigned long doMemToVal(p, len)
  17.      byte         *p;
  18.      unsigned int  len;
  19. { unsigned int  a;
  20.   unsigned long i;
  21.  
  22.   i= 0;
  23.   for (a= 0; a < len; a++)
  24.     i= (i << 8) + *(p++);
  25.   return(i);
  26. }
  27.  
  28. unsigned long doValToMem(val, p, len)
  29.      unsigned long  val;
  30.      byte          *p;
  31.      unsigned int   len;
  32. { int a;
  33.  
  34.   for (a= len - 1; a >= 0; a--) {
  35.     *(p + a)= val & 0xff;
  36.     val >>= 8;
  37.   }
  38.   return(val);
  39. }
  40.  
  41. unsigned long doMemToValLSB(p, len)
  42.      byte         *p;
  43.      unsigned int  len;
  44. { int val, a;
  45.  
  46.   val= 0;
  47.   for (a= len - 1; a >= 0; a--)
  48.     val= (val << 8) + *(p + a);
  49.   return(val);
  50. }
  51.  
  52. /* this is provided for orthagonality
  53.  */
  54.  
  55. unsigned long doValToMemLSB(val, p, len)
  56.      byte          *p;
  57.      unsigned long  val;
  58.      unsigned int   len;
  59. {
  60.   while (len--) {
  61.     *(p++)= val & 0xff;
  62.     val >>= 8;
  63.   }
  64.   return(val);
  65. }
  66.  
  67. /* this flips all the bits in a byte array at byte intervals
  68.  */
  69.  
  70. void flipBits(p, len)
  71.      byte *p;
  72.      unsigned int len;
  73. { static int init= 0;
  74.   static byte flipped[256];
  75.  
  76.   if (!init) {
  77.     int a, b;
  78.     byte norm;
  79.  
  80.     for (a= 0; a < 256; a++) {
  81.       flipped[a]= 0;
  82.       norm= a;
  83.       for (b= 0; b < 8; b++) {
  84.     flipped[a]= (flipped[a] << 1) | (norm & 1);
  85.     norm >>= 1;
  86.       }
  87.     }
  88.   }
  89.  
  90.   while (len--)
  91.     p[len]= flipped[p[len]];
  92. }
  93.