home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / amiga / mpeg / mpgplyr1.lha / src / mono.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  4KB  |  194 lines

  1. /*
  2.  * Author:    Yoichiro Ueno (ueno@cs.titech.ac.jp)
  3.  *
  4.  * Copyright (C) 1991, 1992, Yoichiro Ueno.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and
  7.  * its documentation for any purpose is hereby granted by the Author without
  8.  * fee, provided that the above copyright notice appear in all copies and
  9.  * that both the copyright notice and this permission notice appear in
  10.  * supporting documentation, and that the name of the Author not be used
  11.  * in advertising or publicity pertaining to distribution of the software
  12.  * without specific, written prior permission.  The Author makes no
  13.  * representations about the suitability of this software for any purpose.
  14.  * It is provided "as is" without express or implied warranty.
  15.  *
  16.  * THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  18.  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  20.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22.  * PERFORMANCE OF THIS SOFTWARE.
  23.  *
  24.  */
  25.  
  26. #include "video.h"
  27. #include "dither.h"
  28.  
  29.  
  30. /*
  31.  *--------------------------------------------------------------
  32.  *
  33.  * MonoDitherImage --
  34.  *
  35.  *    Dithers image into monochrome.
  36.  *    Dither algorithm is based on dither.c in xli.1.11.
  37.  *
  38.  * Results:
  39.  *    None.
  40.  *
  41.  * Side effects:
  42.  *    None.
  43.  *
  44.  *--------------------------------------------------------------
  45.  */
  46. #define MaxGrey        65280
  47. #define Threshold    (MaxGrey/2)
  48. #define MinGrey        0
  49.  
  50. static int    *curr = NULL;
  51. static int    *next = NULL;
  52.  
  53. void
  54. MonoDitherImage(lum, cr, cb, out, h, w)
  55.     register unsigned char *lum;
  56.     unsigned char *cr;
  57.     unsigned char *cb;
  58.     register unsigned char *out;
  59.     int w, h;
  60. {
  61.   int bit_r2l;
  62.   register unsigned int bit;
  63.   register unsigned int data;
  64.   int i;
  65.   register int j;
  66.   int *swap;
  67.   register int out_err;
  68.   register int next1;
  69.   register int next2;
  70.  
  71.   if(curr == NULL) {
  72.     curr = (int *)malloc(sizeof(int) * (w + 2));
  73.     curr += 1;
  74.   }
  75.   if(next == NULL) {
  76.     next = (int *)malloc(sizeof(int) * (w + 2));
  77.     next += 1;
  78.   }
  79.  
  80.   bzero ((char *)curr, w * sizeof(*curr));
  81.  
  82.   bit_r2l = 0x80 >> (w - 1 & 7);
  83.   for(i = 0; i < h; i ++) {
  84.     if(i & 0x01) {                /* Right to Left */
  85.       bit = bit_r2l;
  86.       data = 0;
  87.       out_err = curr[w-1];
  88.       next1 = 0;
  89.       next2 = 0;
  90.       for (j=(w-1); j>=0; j--)
  91.       {
  92.     out_err = (out_err >> 4) + (lum[j] << 8);
  93.     if(out_err > Threshold) {
  94.       data |= bit;
  95.       out_err -= MaxGrey;
  96.     }
  97.     else
  98.       out_err -= MinGrey;
  99.  
  100.     next[j+1] = next1 +     (out_err * 3);
  101.     next1     = next2 +     (out_err * 5);
  102.     next2     =             (out_err * 1);
  103.     out_err   = curr[j-1] + (out_err * 7);
  104.  
  105.     bit <<= 1;
  106.     if(bit > 0x80) {
  107.       out[j >> 3] = data;
  108.       bit = 0x01;
  109.       data = 0;
  110.     }
  111.       }
  112.       next[0] = next1;
  113.     }
  114.     else {                    /* Left to Right */
  115.       bit = 0x80;
  116.       data = 0;
  117.       out_err = curr[0];
  118.       next1 = 0;
  119.       next2 = 0;
  120.       for (j=0; j<w; j++)
  121.       {
  122.     out_err = (out_err >> 4) + (lum[j] << 8);
  123.     if(out_err > Threshold) {
  124.       data |= bit;
  125.       out_err = out_err - MaxGrey;
  126.     }
  127.     else
  128.       out_err = out_err - MinGrey;
  129.  
  130.     next[j-1] = next1 +     (out_err * 3);
  131.     next1     = next2 +     (out_err * 5);
  132.     next2     =             (out_err * 1);
  133.     out_err   = curr[j+1] + (out_err * 7);
  134.  
  135.     bit >>= 1;
  136.     if(bit == 0) {
  137.       out[j >> 3] = data;
  138.       bit = 0x80;
  139.       data = 0;
  140.     }
  141.       }
  142.       next[w-1] = next1;
  143.     }
  144.     
  145.     lum += w;
  146.     out += w >> 3;
  147.     swap = curr;
  148.     curr = next;
  149.     next = swap;
  150.   }
  151. }
  152.  
  153.  
  154. /*
  155.  *--------------------------------------------------------------
  156.  *
  157.  * MonoThresholdImage --
  158.  *
  159.  *    convert image into monochrome with threshold.
  160.  *
  161.  * Results:
  162.  *    None.
  163.  *
  164.  * Side effects:
  165.  *    None.
  166.  *
  167.  *--------------------------------------------------------------
  168.  */
  169. void
  170. MonoThresholdImage(lum, cr, cb, out, h, w)
  171.     unsigned char *lum;
  172.     unsigned char *cr;
  173.     unsigned char *cb;
  174.     unsigned char *out;
  175.     int w, h;
  176. {
  177.   unsigned char    bit;
  178.   unsigned char    data;
  179.  
  180.   bit = 0x80;
  181.   data = 0;
  182.   for (w*=h; w>0; w--) {
  183.     if(*lum++>128)
  184.       data |= bit;
  185.  
  186.     bit >>= 1;
  187.     if(bit == 0) {
  188.       *out ++ = data;
  189.       bit = 0x80;
  190.       data = 0;
  191.     }
  192.   }
  193. }
  194.