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

  1. /* undither.c:
  2.  *
  3.  * this converts a bitmap image into a greyscale image.
  4.  *
  5.  * Eckhard R"uggeberg (erueg@cfgauss.uni-math.gwdg.de).
  6.  */
  7.  
  8. #include "image.h"
  9.  
  10. Image *undither(oimage, verbose)
  11.      Image *oimage;
  12.      unsigned int  verbose;
  13. {
  14.   Image *nimage;            /* new image to build        */
  15.   byte *optr, *nptr;        /* Ptr into old/new data    */
  16.   int i, j, k;            /* Loop counters        */ 
  17.   int hi, lo;            /* 2 new Pixels constructed    */
  18.   register int scanned;        /* byte scanned in old data    */
  19.   int newX, newY;            /* new sizes            */
  20.   int bitskip, byteskip;        /* skips for ptr in old data    */
  21.   int scantimes;            /* how often to scan a byte    */
  22.   int oddness;            /* If newX is odd        */
  23.  
  24.   if (!BITMAPP(oimage))
  25.     return(oimage);
  26.   if (verbose) {
  27.     printf ( "  Undithering image into grayscale..." );
  28.     fflush(stdout);
  29.   }
  30.   newX = (oimage->width  / 4);
  31.   oddness = newX & 1; 
  32.   newY = oimage->height / 4;
  33.   nimage = newRGBImage ( newX, newY, 8 );
  34.   if (oimage->title) {
  35.     nimage->title = (char *) lmalloc ( strlen (oimage->title) + 13 );
  36.     sprintf ( nimage->title, "%s (grayscaled)", oimage->title );
  37.   }
  38.   nimage->rgb.used = 17;
  39.   for ( i = 0; i < 17; i++ )
  40.     *(nimage->rgb.red + i) = *(nimage->rgb.green + i) = 
  41.       *(nimage->rgb.blue + i) = 65535.0 * (16 - i) / 16.0;
  42.   bitskip   = (oimage->width / 8) + (oimage->width % 8 ? 1 : 0);
  43.   byteskip  = (bitskip << 2);
  44.   scantimes = (newX >> 1) + oddness; 
  45.   optr = oimage->data;
  46.   nptr = nimage->data;
  47.   for ( i = 0; i < newY; i++ ) {        /* for each new row    */
  48.     for ( j = 0; j < scantimes; j++ ) {    /* for each new colunm    */
  49.       lo = hi = 0;            
  50.       for ( k = 0; k < 4; k++ ) {    /* for each of the 4    */
  51.     scanned = *(optr + j + k*bitskip);    /*     old rows */
  52.     if (scanned & 0x80) hi++;
  53.     if (scanned & 0x40) hi++;
  54.     if (scanned & 0x20) hi++;
  55.     if (scanned & 0x10) hi++;
  56.     if (scanned & 0x08) lo++;
  57.     if (scanned & 0x04) lo++;
  58.     if (scanned & 0x02) lo++;
  59.     if (scanned & 0x01) lo++;
  60.       }
  61.       *nptr++ = hi;            /* store new data    */
  62.       *nptr++ = lo;
  63.     }
  64.     if (oddness) nptr--;        /* correct at end of row*/
  65.     optr += byteskip;
  66.   }
  67.   if (verbose)
  68.     printf("done\n");
  69.   return(nimage);
  70. }
  71.