home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / mpegplay / hybrider.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  11KB  |  356 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. /* This file contains C code to implement an ordered dither in the 
  22.    luminance channel and F-S error diffusion on chrominance.
  23. */
  24.  
  25. #include "video.h"
  26. #include "proto.h"
  27. #include "dither.h"
  28.  
  29. #define DITH_SIZE 16
  30.  
  31. /* Structures used for hybrid dither with errors propogated. */
  32.  
  33. static unsigned char *l_darrays[DITH_SIZE];
  34. static unsigned char *l_darrays0, *l_darrays1, *l_darrays2, *l_darrays3;
  35. static unsigned char *l_darrays4, *l_darrays5, *l_darrays6, *l_darrays7;
  36. static unsigned char *l_darrays8, *l_darrays9, *l_darrays10, *l_darrays11;
  37. static unsigned char *l_darrays12, *l_darrays13, *l_darrays14, *l_darrays15;
  38. static unsigned char cr_fsarray[256*256][4];
  39. static unsigned char cb_fsarray[256*256][4];
  40. static unsigned short  c_fserr[256*256][2];
  41.  
  42.  
  43. /*
  44.  *--------------------------------------------------------------
  45.  *
  46.  *  InitHybridErrorDither--
  47.  *
  48.  *    Initializes structures used for hybrid dither algorithm
  49.  *      with errors propogated on Cr and Cb. 
  50.  *
  51.  * Results:
  52.  *      None.
  53.  *
  54.  * Side effects:
  55.  *      None.
  56.  *
  57.  *--------------------------------------------------------------
  58.  */
  59.  
  60. void
  61. InitHybridErrorDither()
  62. {
  63.   int i, j, k, err_range, threshval;
  64.   unsigned char *lmark;
  65.  
  66.  
  67.   for (i=0; i<DITH_SIZE; i++) {
  68.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  69.  
  70.     for (j=0; j<lum_values[0]; j++) {
  71.       *lmark++ = 0;
  72.     }
  73.  
  74.     for (j=0; j<(LUM_RANGE-1); j++) {
  75.       err_range = lum_values[j+1] - lum_values[j];
  76.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  77.  
  78.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  79.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  80.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  81.       }
  82.     }
  83.  
  84.     for (j=lum_values[LUM_RANGE-1]; j <256; j++) {
  85.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  86.     }
  87.   }
  88.   l_darrays0 = l_darrays[0]; l_darrays8 = l_darrays[8];
  89.   l_darrays1 = l_darrays[1]; l_darrays9 = l_darrays[9];
  90.   l_darrays2 = l_darrays[2]; l_darrays10 = l_darrays[10];
  91.   l_darrays3 = l_darrays[3]; l_darrays11 = l_darrays[11];
  92.   l_darrays4 = l_darrays[4]; l_darrays12 = l_darrays[12];
  93.   l_darrays5 = l_darrays[5]; l_darrays13 = l_darrays[13];
  94.   l_darrays6 = l_darrays[6]; l_darrays14 = l_darrays[14];
  95.   l_darrays7 = l_darrays[7]; l_darrays15 = l_darrays[15];
  96.   {
  97.     int cr1, cr2, cr3, cr4, err1, err2;
  98.     int cb1, cb2, cb3, cb4, val, nval;
  99.     int outerr1, outerr2, outerr3, outerr4;
  100.     int inerr1, inerr2, inerr3, inerr4;
  101.     unsigned short oe1, oe2, oe3, oe4;
  102.  
  103.     for (j=0; j<65536; j+= 256) {
  104.  
  105.       inerr1 = (((j & 0xc000) >> 14)*8) - 12;
  106.       inerr2 = (((j & 0x3000) >> 12)*8) - 12;
  107.       inerr3 = (((j & 0x0c00) >> 10)*8) - 12;
  108.       inerr4 = (((j & 0x0300) >> 8) *8) - 12;
  109.  
  110.       for (i=0; i<256; i++) {
  111.     val = i;
  112.  
  113.     nval = val+inerr1+inerr3;
  114.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  115.     cr1 = ((nval) * CR_RANGE) / 256;
  116.     err1 = ((nval) - cr_values[cr1])/2;
  117.     err2 = ((nval) - cr_values[cr1]) - err1;
  118.  
  119.     nval = val+err1+inerr2;
  120.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  121.     cr2 = ((nval) * CR_RANGE) / 256;
  122.     err1 = ((nval) - cr_values[cr2])/2;
  123.     outerr3 = ((nval) - cr_values[cr2])-err1;
  124.  
  125.     nval = val+err2+inerr4;
  126.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  127.     cr3 = ((nval) * CR_RANGE) / 256;
  128.     err2 = ((nval) - cr_values[cr3])/2;
  129.     outerr1 = ((nval) - cr_values[cr3]) - err2;
  130.  
  131.     nval = val+err1+err2;
  132.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  133.     cr4 = ((nval) * CR_RANGE) / 256;
  134.     outerr2 = ((nval) - cr_values[cr4])/2;
  135.     outerr4 = ((nval) - cr_values[cr4])-outerr2;
  136.  
  137.     cr_fsarray[i+j][0] = cr1*CB_RANGE;
  138.     cr_fsarray[i+j][1] = cr2*CB_RANGE;
  139.     cr_fsarray[i+j][2] = cr3*CB_RANGE;
  140.     cr_fsarray[i+j][3] = cr4*CB_RANGE;
  141.  
  142.     if (outerr1 < -16) outerr1++;
  143.     else if (outerr1 > 15) outerr1--;
  144.     if (outerr2 < -16) outerr2++;
  145.     else if (outerr2 > 15) outerr2--;
  146.     if (outerr3 < -16) outerr3++;
  147.     else if (outerr3 > 15) outerr3--;
  148.     if (outerr4 < -16) outerr4++;
  149.     else if (outerr4 > 15) outerr4--;
  150.  
  151.     oe1 = (outerr1 + 16) / 8;
  152.     oe2 = (outerr2 + 16) / 8;
  153.     oe3 = (outerr3 + 16) / 8;
  154.     oe4 = (outerr4 + 16) / 8;
  155.  
  156. /* This is a debugging check and should be removed if not needed. */
  157.     if ((oe1 > 3) || (oe2 > 3) || (oe3 > 3) || (oe4 > 3))
  158.       fprintf(stderr, "OE error!!!!\n");
  159.  
  160.  
  161.     c_fserr[i+j][0] = ((oe1 << 14) | (oe2 << 12));
  162.  
  163.     c_fserr[i+j][1] = ((oe3 << 10) | (oe4 << 8));
  164.       }
  165.  
  166.       for (i=0; i<256; i++) {
  167.     val = i;
  168.     nval = val+inerr1+inerr3;
  169.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  170.     cb1 = ((nval) * CB_RANGE) / 256;
  171.     err1 = ((nval) - cb_values[cb1])/2;
  172.     err2 = ((nval) - cb_values[cb1]) - err1;
  173.  
  174.     nval = val+err1+inerr2;
  175.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  176.     cb2 = ((nval) * CB_RANGE) / 256;
  177.     err1 = ((nval) - cb_values[cb2])/2;
  178.  
  179.     nval = val+err2+inerr4;
  180.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  181.     cb3 = ((nval) * CB_RANGE) / 256;
  182.     err2 = ((nval) - cb_values[cb3])/2;
  183.  
  184.     nval = val+err1+err2;
  185.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  186.     cb4 = ((nval) * CB_RANGE) / 256;
  187.  
  188.     cb_fsarray[i+j][0] = cb1;
  189.     cb_fsarray[i+j][1] = cb2;
  190.     cb_fsarray[i+j][2] = cb3;
  191.     cb_fsarray[i+j][3] = cb4;
  192.       }
  193.     }
  194.   }
  195. }
  196.  
  197. /*
  198.  *--------------------------------------------------------------
  199.  *
  200.  * HybridErrorDitherImage --
  201.  *
  202.  *    Dithers an image using a hybrid ordered/floyd-steinberg dither.
  203.  *    Assumptions made:
  204.  *      1) The color space is allocated y:cr:cb = 8:4:4
  205.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  206.  *      This dither is almost exactly like the dither implemented in the
  207.  *      file odith.c (i.e. hybrid dithering) except a quantized amount of
  208.  *      error is propogated between 2x2 pixel areas in Cr and Cb.
  209.  *
  210.  * Results:
  211.  *    None.
  212.  *
  213.  * Side effects:
  214.  *    None.
  215.  *
  216.  *--------------------------------------------------------------
  217.  */
  218. void
  219. HybridErrorDitherImage (lum, cr, cb, out, h, w)
  220.     unsigned char *lum;
  221.     unsigned char *cr;
  222.     unsigned char *cb;
  223.     unsigned char *out;
  224.     int w, h;
  225. {
  226.   unsigned char *l, *r, *b, *o1, *o2;
  227.   unsigned char *l2;
  228.   static int *cr_row_errs;
  229.   static int *cb_row_errs;
  230.   int *cr_r_err;
  231.   int *cb_r_err;
  232.   int cr_c_err;
  233.   int cb_c_err;
  234.   unsigned char *cr_fsptr;
  235.   unsigned char *cb_fsptr;
  236.   static int first = 1;
  237.   int cr_code, cb_code;
  238.  
  239.   int i, j;
  240.   int row_advance, row_advance2;
  241.   int half_row_advance, half_row_advance2;
  242.  
  243.   /* If first time called, allocate error arrays. */
  244.  
  245.   if (first) {
  246.     cr_row_errs = (int *) malloc((w+5)*sizeof(int));
  247.     cb_row_errs = (int *) malloc((w+5)*sizeof(int));
  248.     first = 0;
  249.   }
  250.  
  251.   row_advance = (w << 1) - 1;
  252.   row_advance2 = row_advance+2;
  253.   half_row_advance = (w>>1)-1;
  254.   half_row_advance2 = half_row_advance+2;
  255.  
  256.   l = lum;
  257.   l2 = lum+w;
  258.   r = cr;
  259.   b = cb;
  260.   o1 = out;
  261.   o2 = out+w;
  262.  
  263.   memset( (char *) cr_row_errs, 0, (w+5)*sizeof(int));
  264.   cr_r_err = cr_row_errs;
  265.   cr_c_err = 0;
  266.   memset( (char *) cb_row_errs, 0, (w+5)*sizeof(i