home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / amiga / mpeg / mpgplyr1.lha / src / hybriderr.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  11KB  |  353 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 "dither.h"
  27.  
  28. #define DITH_SIZE 16
  29.  
  30. /* Structures used for hybrid dither with errors propogated. */
  31.  
  32. static unsigned char *l_darrays[DITH_SIZE];
  33. static unsigned char *l_darrays0, *l_darrays1, *l_darrays2, *l_darrays3;
  34. static unsigned char *l_darrays4, *l_darrays5, *l_darrays6, *l_darrays7;
  35. static unsigned char *l_darrays8, *l_darrays9, *l_darrays10, *l_darrays11;
  36. static unsigned char *l_darrays12, *l_darrays13, *l_darrays14, *l_darrays15;
  37. static unsigned char cr_fsarray[256*256][4];
  38. static unsigned char cb_fsarray[256*256][4];
  39. static unsigned short  c_fserr[256*256][2];
  40.  
  41.  
  42. /*
  43.  *--------------------------------------------------------------
  44.  *
  45.  *  InitHybridErrorDither--
  46.  *
  47.  *    Initializes structures used for hybrid dither algorithm
  48.  *      with errors propogated on Cr and Cb. 
  49.  *
  50.  * Results:
  51.  *      None.
  52.  *
  53.  * Side effects:
  54.  *      None.
  55.  *
  56.  *--------------------------------------------------------------
  57.  */
  58.  
  59. void
  60. InitHybridErrorDither()
  61. {
  62.   int i, j, k, err_range, threshval;
  63.   unsigned char *lmark;
  64.  
  65.  
  66.   for (i=0; i<DITH_SIZE; i++) {
  67.     lmark = l_darrays[i] = (unsigned char *) malloc(256);
  68.  
  69.     for (j=0; j<lum_values[0]; j++) {
  70.       *lmark++ = 0;
  71.     }
  72.  
  73.     for (j=0; j<(LUM_RANGE-1); j++) {
  74.       err_range = lum_values[j+1] - lum_values[j];
  75.       threshval = ((i * err_range) / DITH_SIZE)+lum_values[j];
  76.  
  77.       for (k=lum_values[j]; k<lum_values[j+1]; k++) {
  78.     if (k > threshval) *lmark++ = ((j+1) * (CR_RANGE * CB_RANGE));
  79.     else *lmark++ = (j * (CR_RANGE * CB_RANGE));
  80.       }
  81.     }
  82.  
  83.     for (j=lum_values[LUM_RANGE-1]; j <256; j++) {
  84.       *lmark++ = (LUM_RANGE-1)*(CR_RANGE * CB_RANGE);
  85.     }
  86.   }
  87.   l_darrays0 = l_darrays[0]; l_darrays8 = l_darrays[8];
  88.   l_darrays1 = l_darrays[1]; l_darrays9 = l_darrays[9];
  89.   l_darrays2 = l_darrays[2]; l_darrays10 = l_darrays[10];
  90.   l_darrays3 = l_darrays[3]; l_darrays11 = l_darrays[11];
  91.   l_darrays4 = l_darrays[4]; l_darrays12 = l_darrays[12];
  92.   l_darrays5 = l_darrays[5]; l_darrays13 = l_darrays[13];
  93.   l_darrays6 = l_darrays[6]; l_darrays14 = l_darrays[14];
  94.   l_darrays7 = l_darrays[7]; l_darrays15 = l_darrays[15];
  95.   {
  96.     int cr1, cr2, cr3, cr4, err1, err2;
  97.     int cb1, cb2, cb3, cb4, val, nval;
  98.     int outerr1, outerr2, outerr3, outerr4;
  99.     int inerr1, inerr2, inerr3, inerr4;
  100.     unsigned short oe1, oe2, oe3, oe4;
  101.  
  102.     for (j=0; j<65536; j+= 256) {
  103.  
  104.       inerr1 = (((j & 0xc000) >> 14)*8) - 12;
  105.       inerr2 = (((j & 0x3000) >> 12)*8) - 12;
  106.       inerr3 = (((j & 0x0c00) >> 10)*8) - 12;
  107.       inerr4 = (((j & 0x0300) >> 8) *8) - 12;
  108.  
  109.       for (i=0; i<256; i++) {
  110.     val = i;
  111.  
  112.     nval = val+inerr1+inerr3;
  113.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  114.     cr1 = ((nval) * CR_RANGE) / 256;
  115.     err1 = ((nval) - cr_values[cr1])/2;
  116.     err2 = ((nval) - cr_values[cr1]) - err1;
  117.  
  118.     nval = val+err1+inerr2;
  119.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  120.     cr2 = ((nval) * CR_RANGE) / 256;
  121.     err1 = ((nval) - cr_values[cr2])/2;
  122.     outerr3 = ((nval) - cr_values[cr2])-err1;
  123.  
  124.     nval = val+err2+inerr4;
  125.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  126.     cr3 = ((nval) * CR_RANGE) / 256;
  127.     err2 = ((nval) - cr_values[cr3])/2;
  128.     outerr1 = ((nval) - cr_values[cr3]) - err2;
  129.  
  130.     nval = val+err1+err2;
  131.     if (nval < 0) nval = 0; else if (nval > 255) nval = 255;
  132.     cr4 = ((nval) * CR_RANGE) / 256;
  133.     outerr2 = ((nval) - cr_values[cr4])/2;
  134.     outerr4 = ((nval) - cr_values[cr4])-outerr2;
  135.  
  136.     cr_fsarray[i+j][0] = cr1*CB_RANGE;
  137.     cr_fsarray[i+j][1] = cr2*CB_RANGE;
  138.     cr_fsarray[i+j][2] = cr3*CB_RANGE;
  139.     cr_fsarray[i+j][3] = cr4*CB_RANGE;
  140.  
  141.     if (outerr1 < -16) outerr1++;
  142.     else if (outerr1 > 15) outerr1--;
  143.     if (outerr2 < -16) outerr2++;
  144.     else if (outerr2 > 15) outerr2--;
  145.     if (outerr3 < -16) outerr3++;
  146.     else if (outerr3 > 15) outerr3--;
  147.     if (outerr4 < -16) outerr4++;
  148.     else if (outerr4 > 15) outerr4--;
  149.  
  150.     oe1 = (outerr1 + 16) / 8;
  151.     oe2 = (outerr2 + 16) / 8;
  152.     oe3 = (outerr3 + 16) / 8;
  153.     oe4 = (outerr4 + 16) / 8;
  154.  
  155.     if ((oe1 > 3) || (oe2 > 3) || (oe3 > 3) || (oe4 > 3))
  156.       fprintf(stderr, "OE error!!!!\n");
  157.  
  158.     c_fserr[i+j][0] = ((oe1 << 14) | (oe2 << 12));
  159.  
  160.     c_fserr[i+j][1] = ((oe3 << 10) | (oe4 << 8));
  161.       }
  162.  
  163.       for (i=0; i<256; i++) {
  164.     val = i;
  165.     nval = val+inerr1+inerr3;
  166.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  167.     cb1 = ((nval) * CB_RANGE) / 256;
  168.     err1 = ((nval) - cb_values[cb1])/2;
  169.     err2 = ((nval) - cb_values[cb1]) - err1;
  170.  
  171.     nval = val+err1+inerr2;
  172.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  173.     cb2 = ((nval) * CB_RANGE) / 256;
  174.     err1 = ((nval) - cb_values[cb2])/2;
  175.  
  176.     nval = val+err2+inerr4;
  177.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  178.     cb3 = ((nval) * CB_RANGE) / 256;
  179.     err2 = ((nval) - cb_values[cb3])/2;
  180.  
  181.     nval = val+err1+err2;
  182.       if (nval < 0) nval = 0; else if (nval > 255) nval = 255;     
  183.     cb4 = ((nval) * CB_RANGE) / 256;
  184.  
  185.     cb_fsarray[i+j][0] = cb1;
  186.     cb_fsarray[i+j][1] = cb2;
  187.     cb_fsarray[i+j][2] = cb3;
  188.     cb_fsarray[i+j][3] = cb4;
  189.       }
  190.     }
  191.   }
  192. }
  193.  
  194. /*
  195.  *--------------------------------------------------------------
  196.  *
  197.  * HybridErrorDitherImage --
  198.  *
  199.  *    Dithers an image using a hybrid ordered/floyd-steinberg dither.
  200.  *    Assumptions made:
  201.  *      1) The color space is allocated y:cr:cb = 8:4:4
  202.  *      2) The spatial resolution of y:cr:cb is 4:1:1
  203.  *      This dither is almost exactly like the dither implemented in the
  204.  *      file odith.c (i.e. hybrid dithering) except a quantized amount of
  205.  *      error is propogated between 2x2 pixel areas in Cr and Cb.
  206.  *
  207.  * Results:
  208.  *    None.
  209.  *
  210.  * Side effects:
  211.  *    None.
  212.  *
  213.  *--------------------------------------------------------------
  214.  */
  215. void
  216. HybridErrorDitherImage (lum, cr, cb, out, h, w)
  217.     unsigned char *lum;
  218.     unsigned char *cr;
  219.     unsigned char *cb;
  220.     unsigned char *out;
  221.     int w, h;
  222. {
  223.   unsigned char *l, *r, *b, *o1, *o2;
  224.   unsigned char *l2;
  225.   static int *cr_row_errs;
  226.   static int *cb_row_errs;
  227.   int *cr_r_err;
  228.   int *cb_r_err;
  229.   int cr_c_err;
  230.   int cb_c_err;
  231.   unsigned char *cr_fsptr;
  232.   unsigned char *cb_fsptr;
  233.   static int first = 1;
  234.   int cr_code, cb_code;
  235.  
  236.   int i, j;
  237.   int row_advance, row_advance2;
  238.   int half_row_advance, half_row_advance2;
  239.  
  240.   /* If first time called, allocate error arrays. */
  241.  
  242.   if (first) {
  243.     cr_row_errs = (int *) malloc((w+5)*sizeof(int));
  244.     cb_row_errs = (int *) malloc((w+5)*sizeof(int));
  245.     first = 0;
  246.   }
  247.  
  248.   row_advance = (w << 1) - 1;
  249.   row_advance2 = row_advance+2;
  250.   half_row_advance = (w>>1)-1;
  251.   half_row_advance2 = half_row_advance+2;
  252.  
  253.   l = lum;
  254.   l2 = lum+w;
  255.   r = cr;
  256.   b = cb;
  257.   o1 = out;
  258.   o2 = out+w;
  259.  
  260.   memset( (char *) cr_row_errs, 0, (w+5)*sizeof(int));
  261.   cr_r_err = cr_row_errs;
  262.   cr_c_err = 0;
  263.   memset( (char *) cb_row_errs, 0, (w+5)*sizeof(int));
  264.   cb_r_err = cb_row_errs;
  265.   cb_c_err = 0;
  266.  
  267.   for (i=0; i<h; i+=4) {
  268.  
  269.     for (j=w; j>0; j-=4) {
  270.  
  271.       cr_code = (*cr_r_err | cr_c_err | *r++);
  272.       cb_code = (*cb_r_err | cb_c_err | *b++);
  273.  
  274.       cr_fsptr = cr_fsarray[cr_code];
  275.       cb_fsptr = cb_fsarray[cb_code];
  276.  
  277.       *o1++ = pixel[(l_darrays0[*l++] | *cr_fsptr++ | *cb_fsptr++)];
  278.       *o1++ = pixel[(l_darrays8[*l++] | *cr_fsptr++ | *cb_fsptr++)];
  279.       *o2++ = pixel[(l_darrays12[*l2++] | *cr_fsptr++ | *cb_fsptr++)];
  280.       *o2++ = pixel[(l_darrays4[*l2++] | *cr_fsptr++ | *cb_fsptr++)];
  281.  
  282.       cr_c_err = c_fserr[cr_code][1];
  283.       cb_c_err = c_fserr[cb_code][1];
  284.       *cr_r_err++ = c_fserr[cr_code][0];
  285.       *cb_r_err++ = c_fserr[cb_code][0];
  286.       cr_code = (*cr_r_err | cr_c_err | *r++);
  287.       cb_code = (*cb_r_err | cb_c_err | *b++);
  288.  
  289.       cr_fsptr = cr_fsarray[cr_code];
  290.       cb_fsptr = cb_fsarray[cb_code];
  291.  
  292.       *o1++ = pixel[(l_darrays2[*l++] | *cr_fsptr++ | *cb_fsptr++)];
  293.       *o1++ = pixel[(l_darrays10[*l++] | *cr_fsptr++ | *cb_fsptr++)];
  294.       *o2++ = pixel[(l_darrays14[*l2++] | *cr_fsptr++ | *cb_fsptr++)];
  295.       *o2++ = pixel[(l_darrays6[*l2++] | *cr_fsptr++ | *cb_fsptr++)];
  296.  
  297.       cr_c_err = c_fserr[cr_code][1];
  298.       cb_c_err = c_fserr[cb_code][1];
  299.       *cr_r_err++ = c_fserr[cr_code][0];
  300.       *cb_r_err++ = c_fserr[cb_code][0];
  301.     }
  302.  
  303.     l += row_advance; l2 += row_advance;
  304.     o1 += row_advance; o2 += row_advance;
  305.     cr_c_err = 0;
  306.     cb_c_err = 0;
  307.     cr_r_err--; cb_r_err--;
  308.     r += half_row_advance; b += half_row_advance;
  309.  
  310.     for (j=w; j>0; j-=4) {
  311.  
  312.       cr_code = (*cr_r_err | cr_c_err | *r--);
  313.       cb_code = (*cb_r_err | cb_c_err | *b--);
  314.       cr_fsptr = cr_fsarray[cr_code];
  315.       cb_fsptr = cb_fsarray[cb_code];
  316.  
  317.       *o1-- = pixel[(l_darrays9[*l--] | *cr_fsptr++ | *cb_fsptr++)];
  318.       *o1-- = pixel[(l_darrays1[*l--] | *cr_fsptr++ | *cb_fsptr++)];
  319.       *o2-- = pixel[(l_darrays5[*l2--] | *cr_fsptr++ | *cb_fsptr++)];
  320.       *o2-- = pixel[(l_darrays13[*l2--] | *cr_fsptr++ | *cb_fsptr++)];
  321.  
  322.       cr_c_err = c_fserr[cr_code][1];
  323.       cb_c_err = c_fserr[cb_code][1];
  324.       *cr_r_err-- = c_fserr[cr_code][0];
  325.       *cb_r_err-- = c_fserr[cb_code][0];
  326.       cr_code = (*cr_r_err | cr_c_err | *r--);
  327.       cb_code = (*cb_r_err | cb_c_err | *b--);
  328.       cr_fsptr = cr_fsarray[cr_code];
  329.       cb_fsptr = cb_fsarray[cb_code];
  330.  
  331.       *o1-- = pixel[(l_darrays11[*l--] | *cr_fsptr++ | *cb_fsptr++)];
  332.       *o1-- = pixel[(l_darrays3[*l--] | *cr_fsptr++ | *cb_fsptr++)];
  333.       *o2-- = pixel[(l_darrays7[*l2--] | *cr_fsptr++ | *cb_fsptr++)];
  334.       *o2-- = pixel[(l_darrays15[*l2--] | *cr_fsptr++ | *cb_fsptr++)];
  335.  
  336.       cr_c_err = c_fserr[cr_code][1];
  337.       cb_c_err = c_fserr[cb_code][1];
  338.       *cr_r_err-- = c_fserr[cr_code][0];
  339.       *cb_r_err-- = c_fserr[cb_code][0];
  340.  
  341.     }
  342.  
  343.     l += row_advance2; l2 += row_advance2;
  344.     o1 += row_advance2; o2 += row_advance2;
  345.     cr_c_err = 0; cb_c_err = 0;
  346.     cr_r_err++; cb_r_err++;
  347.     r += half_row_advance2; b += half_row_advance2;
  348.   }
  349. }
  350.  
  351.  
  352.   
  353.