home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / texmf / source / SpecHost / Dither.c < prev    next >
C/C++ Source or Header  |  1994-07-25  |  62KB  |  2,633 lines

  1. /*
  2. **    SpecialHost for PasTeX
  3. **
  4. **    Copyright © by Olaf Barthel & Georg Heßmann
  5. */
  6.  
  7. #include "Global.h"
  8.  
  9. STATIC LONG Seed = 42013;
  10.  
  11. LONG __regargs
  12. ScaleImage(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  13. {
  14.     struct BitMap    *BitMap    = NULL;
  15.     LONG         Error    = 0;
  16.     UBYTE        *TempLine;
  17.  
  18.     if(TempLine = CreateTempLine(NewWidth,1))
  19.     {
  20.         if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  21.         {
  22.             struct RastPort __aligned DummyRPort,*TempRPort;
  23.  
  24.             InitRastPort(&DummyRPort);
  25.  
  26.             DummyRPort . BitMap = BitMap;
  27.  
  28.             if(TempRPort = CreateTempRPort(&DummyRPort))
  29.             {
  30.                 LONG     i,j;
  31.                 UBYTE    *Line;
  32.  
  33.                 if(NewHeight == Image -> Height)
  34.                 {
  35.                     if(NewWidth == Image -> Width)
  36.                     {
  37.                         for(i = 0 ; i < NewHeight ; i++)
  38.                         {
  39.                             Line = Image -> Lines[i];
  40.  
  41.                             for(j = 0 ; j < NewWidth ; j++)
  42.                                 TempLine[j] = ((BYTE)Filter[*Line++] >= 0);
  43.  
  44.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  45.  
  46.                             ShowProgress(i);
  47.                         }
  48.                     }
  49.                     else
  50.                     {
  51.                         for(i = 0 ; i < NewHeight ; i++)
  52.                         {
  53.                             Line = Image -> Lines[i];
  54.  
  55.                             for(j = 0 ; j < NewWidth ; j++)
  56.                                 TempLine[j] = ((BYTE)Filter[Line[(j * Image -> Width) / NewWidth]] >= 0);
  57.  
  58.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  59.  
  60.                             ShowProgress(i);
  61.                         }
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     LONG        Y,LastY = -1;
  67.                     PLANEPTR    Offset = BitMap -> Planes[0];
  68.  
  69.                     if(NewWidth == Image -> Width)
  70.                     {
  71.                         for(i = 0 ; i < NewHeight ; i++)
  72.                         {
  73.                             if((Y = (i * Image -> Height) / NewHeight) == LastY)
  74.                                 CopyMem(Offset - BitMap -> BytesPerRow,Offset,BitMap -> BytesPerRow);
  75.                             else
  76.                             {
  77.                                 Line = Image -> Lines[LastY = Y];
  78.  
  79.                                 for(j = 0 ; j < NewWidth ; j++)
  80.                                     TempLine[j] = ((BYTE)Filter[Line[j]] >= 0);
  81.  
  82.                                 (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  83.                             }
  84.  
  85.                             Offset += BitMap -> BytesPerRow;
  86.  
  87.                             ShowProgress(i);
  88.                         }
  89.                     }
  90.                     else
  91.                     {
  92.                         for(i = 0 ; i < NewHeight ; i++)
  93.                         {
  94.                             if((Y = (i * Image -> Height) / NewHeight) == LastY)
  95.                                 CopyMem(Offset - BitMap -> BytesPerRow,Offset,BitMap -> BytesPerRow);
  96.                             else
  97.                             {
  98.                                 Line = Image -> Lines[LastY = Y];
  99.  
  100.                                 for(j = 0 ; j < NewWidth ; j++)
  101.                                     TempLine[j] = ((BYTE)Filter[Line[(j * Image -> Width) / NewWidth]] >= 0);
  102.  
  103.                                 (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  104.                             }
  105.  
  106.                             Offset += BitMap -> BytesPerRow;
  107.  
  108.                             ShowProgress(i);
  109.                         }
  110.                     }
  111.                 }
  112.  
  113.                 DeleteTempRPort(TempRPort);
  114.             }
  115.             else
  116.             {
  117.                 DeleteBitMap(BitMap);
  118.  
  119.                 BitMap = NULL;
  120.  
  121.                 Error = ERR_NO_MEM;
  122.             }
  123.         }
  124.         else
  125.             Error = ERR_NO_MEM;
  126.  
  127.         DeleteTempLine(TempLine);
  128.     }
  129.     else
  130.         Error = ERR_NO_MEM;
  131.  
  132.     *BitMapPtr = BitMap;
  133.  
  134.     return(Error);
  135. }
  136.  
  137. LONG __regargs
  138. DitherImage_Matrix(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight,UBYTE *Matrix)
  139. {
  140.     struct BitMap    *BitMap    = NULL;
  141.     LONG         Error    = 0;
  142.     UBYTE        *TempLine;
  143.  
  144.     if(TempLine = CreateTempLine(NewWidth,1))
  145.     {
  146.         if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  147.         {
  148.             struct RastPort __aligned DummyRPort,*TempRPort;
  149.  
  150.             InitRastPort(&DummyRPort);
  151.  
  152.             DummyRPort . BitMap = BitMap;
  153.  
  154.             if(TempRPort = CreateTempRPort(&DummyRPort))
  155.             {
  156.                 if(Matrix)
  157.                 {
  158.                     LONG     i,j;
  159.                     UBYTE    *Line,
  160.                         *Row = Matrix;
  161.  
  162.                     if(NewHeight == Image -> Height)
  163.                     {
  164.                         for(i = 0 ; i < NewHeight ; i++)
  165.                         {
  166.                             if(i & 15)
  167.                                 Row += 16;
  168.                             else
  169.                                 Row = Matrix;
  170.  
  171.                             Line = Image -> Lines[i];
  172.  
  173.                             for(j = 0 ; j < NewWidth ; j++)
  174.                                 TempLine[j] = (Filter[Line[(j * Image -> Width) / NewWidth]] <= Row[j & 15]);
  175.  
  176.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  177.  
  178.                             ShowProgress(i);
  179.                         }
  180.                     }
  181.                     else
  182.                     {
  183.                         for(i = 0 ; i < NewHeight ; i++)
  184.                         {
  185.                             if(i & 15)
  186.                                 Row += 16;
  187.                             else
  188.                                 Row = Matrix;
  189.  
  190.                             Line = (BYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  191.  
  192.                             for(j = 0 ; j < NewWidth ; j++)
  193.                                 TempLine[j] = (Filter[Line[(j * Image -> Width) / NewWidth]] <= Row[j & 15]);
  194.  
  195.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  196.  
  197.                             ShowProgress(i);
  198.                         }
  199.                     }
  200.                 }
  201.                 else
  202.                 {
  203.                     UWORD     Value;
  204.                     LONG     i,j;
  205.                     UBYTE    *Line;
  206.  
  207.                     Value = Seed + 16411;
  208.  
  209.                     if(NewHeight == Image -> Height)
  210.                     {
  211.                         for(i = 0 ; i < NewHeight ; i++)
  212.                         {
  213.                             Line = Image -> Lines[i];
  214.  
  215.                             for(j = 0 ; j < NewWidth ; j++)
  216.                             {
  217.                                 TempLine[j] = (Filter[Line[(j * Image -> Width) / NewWidth]] <= (Value & 0xFF));
  218.  
  219.                                 Value += 16411;
  220.                             }
  221.  
  222.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  223.  
  224.                             ShowProgress(i);
  225.                         }
  226.                     }
  227.                     else
  228.                     {
  229.                         for(i = 0 ; i < NewHeight ; i++)
  230.                         {
  231.                             Line = (BYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  232.  
  233.                             for(j = 0 ; j < NewWidth ; j++)
  234.                             {
  235.                                 TempLine[j] = (Filter[Line[(j * Image -> Width) / NewWidth]] <= (Value & 0xFF));
  236.  
  237.                                 Value += 16411;
  238.                             }
  239.  
  240.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  241.  
  242.                             ShowProgress(i);
  243.                         }
  244.                     }
  245.  
  246.                     Seed = Value;
  247.                 }
  248.  
  249.                 DeleteTempRPort(TempRPort);
  250.             }
  251.             else
  252.             {
  253.                 DeleteBitMap(BitMap);
  254.  
  255.                 BitMap = NULL;
  256.  
  257.                 Error = ERR_NO_MEM;
  258.             }
  259.         }
  260.         else
  261.             Error = ERR_NO_MEM;
  262.  
  263.         DeleteTempLine(TempLine);
  264.     }
  265.     else
  266.         Error = ERR_NO_MEM;
  267.  
  268.     *BitMapPtr = BitMap;
  269.  
  270.     return(Error);
  271. }
  272.  
  273. LONG __regargs
  274. DitherImage_FS(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  275. {
  276.     struct BitMap    *BitMap    = NULL;
  277.     LONG         Error    = 0;
  278.     APTR         Data;
  279.  
  280.     if(Data = AllocVecPooled(2 * (NewWidth + 2) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  281.     {
  282.         UBYTE *TempLine;
  283.  
  284.         if(TempLine = CreateTempLine(NewWidth,1))
  285.         {
  286.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  287.             {
  288.                 struct RastPort __aligned DummyRPort,*TempRPort;
  289.  
  290.                 InitRastPort(&DummyRPort);
  291.  
  292.                 DummyRPort . BitMap = BitMap;
  293.  
  294.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  295.                 {
  296.                     LONG     i,j;
  297.                     UBYTE    *Line;
  298.  
  299.                     WORD    *ThisError,
  300.                         *NextError,
  301.                          Value,
  302.                          Valu2;
  303.  
  304.                     ThisError = Data;
  305.                     NextError = ThisError + NewWidth + 2;
  306.  
  307.                     ThisError++;
  308.                     NextError++;
  309.  
  310.                     j = Seed + 16411;
  311.  
  312.                     for(i = 0 ; i < NewWidth ; i++)
  313.                     {
  314.                         ThisError[i] = ((j & 1023) - 512) / 4;
  315.  
  316.                         j += 16411;
  317.                     }
  318.  
  319.                     Seed = j;
  320.  
  321.                     if(NewHeight == Image -> Height)
  322.                     {
  323.                         for(i = 0 ; i < NewHeight ; i++)
  324.                         {
  325.                             Line = Image -> Lines[i];
  326.  
  327.                             if(i & 1)
  328.                             {
  329.                                 for(j = 0 ; j < NewWidth ; j++)
  330.                                 {
  331.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  332.                                         Value = 255;
  333.                                     else
  334.                                     {
  335.                                         if(Value < 0)
  336.                                             Value = 0;
  337.                                     }
  338.  
  339.                                     if(TempLine[j] = (Value < 128))
  340.                                     {
  341.                                         if(Valu2 = Value + Value)
  342.                                         {
  343.                                             NextError[j + 1]     = Value;Value += Valu2;
  344.                                             NextError[j - 1]    += Value;Value += Valu2;
  345.                                             NextError[j]        += Value;Value += Valu2;
  346.                                             ThisError[j + 1]    += Value;
  347.                                         }
  348.                                     }
  349.                                     else
  350.                                     {
  351.                                         if(Value = Value - 255)
  352.                                         {
  353.                                             Valu2 = Value + Value;
  354.  
  355.                                             NextError[j + 1]     = Value;Value += Valu2;
  356.                                             NextError[j - 1]    += Value;Value += Valu2;
  357.                                             NextError[j]        += Value;Value += Valu2;
  358.                                             ThisError[j + 1]    += Value;
  359.                                         }
  360.                                     }
  361.                                 }
  362.  
  363.                                 {
  364.                                     register WORD *Swap;
  365.  
  366.                                     Swap        = ThisError;
  367.                                     ThisError    = NextError;
  368.                                     NextError    = Swap;
  369.  
  370.                                     Swap[NewWidth - 1] = 0;
  371.  
  372.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  373.                                 }
  374.                             }
  375.                             else
  376.                             {
  377.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  378.                                 {
  379.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  380.                                         Value = 255;
  381.                                     else
  382.                                     {
  383.                                         if(Value < 0)
  384.                                             Value = 0;
  385.                                     }
  386.  
  387.                                     if(TempLine[j] = (Value < 128))
  388.                                     {
  389.                                         if(Valu2 = Value + Value)
  390.                                         {
  391.                                             NextError[j - 1]     = Value;Value += Valu2;
  392.                                             NextError[j + 1]    += Value;Value += Valu2;
  393.                                             NextError[j]        += Value;Value += Valu2;
  394.                                             ThisError[j - 1]    += Value;
  395.                                         }
  396.                                     }
  397.                                     else
  398.                                     {
  399.                                         if(Value = Value - 255)
  400.                                         {
  401.                                             Valu2 = Value + Value;
  402.  
  403.                                             NextError[j - 1]     = Value;Value += Valu2;
  404.                                             NextError[j + 1]    += Value;Value += Valu2;
  405.                                             NextError[j]        += Value;Value += Valu2;
  406.                                             ThisError[j - 1]    += Value;
  407.                                         }
  408.                                     }
  409.                                 }
  410.  
  411.                                 {
  412.                                     register WORD *Swap;
  413.  
  414.                                     Swap        = ThisError;
  415.                                     ThisError    = NextError;
  416.                                     NextError    = Swap;
  417.  
  418.                                     *Swap = 0;
  419.  
  420.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  421.                                 }
  422.                             }
  423.  
  424.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  425.  
  426.                             ShowProgress(i);
  427.                         }
  428.                     }
  429.                     else
  430.                     {
  431.                         for(i = 0 ; i < NewHeight ; i++)
  432.                         {
  433.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  434.  
  435.                             if(i & 1)
  436.                             {
  437.                                 for(j = 0 ; j < NewWidth ; j++)
  438.                                 {
  439.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  440.                                         Value = 255;
  441.                                     else
  442.                                     {
  443.                                         if(Value < 0)
  444.                                             Value = 0;
  445.                                     }
  446.  
  447.                                     if(TempLine[j] = (Value < 128))
  448.                                     {
  449.                                         if(Valu2 = Value + Value)
  450.                                         {
  451.                                             NextError[j + 1]     = Value;Value += Valu2;
  452.                                             NextError[j - 1]    += Value;Value += Valu2;
  453.                                             NextError[j]        += Value;Value += Valu2;
  454.                                             ThisError[j + 1]    += Value;
  455.                                         }
  456.                                     }
  457.                                     else
  458.                                     {
  459.                                         if(Value = Value - 255)
  460.                                         {
  461.                                             Valu2 = Value + Value;
  462.  
  463.                                             NextError[j + 1]     = Value;Value += Valu2;
  464.                                             NextError[j - 1]    += Value;Value += Valu2;
  465.                                             NextError[j]        += Value;Value += Valu2;
  466.                                             ThisError[j + 1]    += Value;
  467.                                         }
  468.                                     }
  469.                                 }
  470.  
  471.                                 {
  472.                                     register WORD *Swap;
  473.  
  474.                                     Swap        = ThisError;
  475.                                     ThisError    = NextError;
  476.                                     NextError    = Swap;
  477.  
  478.                                     Swap[NewWidth - 1] = 0;
  479.  
  480.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  481.                                 }
  482.                             }
  483.                             else
  484.                             {
  485.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  486.                                 {
  487.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  488.                                         Value = 255;
  489.                                     else
  490.                                     {
  491.                                         if(Value < 0)
  492.                                             Value = 0;
  493.                                     }
  494.  
  495.                                     if(TempLine[j] = (Value < 128))
  496.                                     {
  497.                                         if(Valu2 = Value + Value)
  498.                                         {
  499.                                             NextError[j - 1]     = Value;Value += Valu2;
  500.                                             NextError[j + 1]    += Value;Value += Valu2;
  501.                                             NextError[j]        += Value;Value += Valu2;
  502.                                             ThisError[j - 1]    += Value;
  503.                                         }
  504.                                     }
  505.                                     else
  506.                                     {
  507.                                         if(Value = Value - 255)
  508.                                         {
  509.                                             Valu2 = Value + Value;
  510.  
  511.                                             NextError[j - 1]     = Value;Value += Valu2;
  512.                                             NextError[j + 1]    += Value;Value += Valu2;
  513.                                             NextError[j]        += Value;Value += Valu2;
  514.                                             ThisError[j - 1]    += Value;
  515.                                         }
  516.                                     }
  517.                                 }
  518.  
  519.                                 {
  520.                                     register WORD *Swap;
  521.  
  522.                                     Swap        = ThisError;
  523.                                     ThisError    = NextError;
  524.                                     NextError    = Swap;
  525.  
  526.                                     *Swap = 0;
  527.  
  528.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  529.                                 }
  530.                             }
  531.  
  532.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  533.  
  534.                             ShowProgress(i);
  535.                         }
  536.                     }
  537.  
  538.                     DeleteTempRPort(TempRPort);
  539.                 }
  540.                 else
  541.                 {
  542.                     DeleteBitMap(BitMap);
  543.  
  544.                     BitMap = NULL;
  545.  
  546.                     Error = ERR_NO_MEM;
  547.                 }
  548.             }
  549.             else
  550.                 Error = ERR_NO_MEM;
  551.  
  552.             DeleteTempLine(TempLine);
  553.         }
  554.         else
  555.             Error = ERR_NO_MEM;
  556.  
  557.         FreeVecPooled(Data);
  558.     }
  559.     else
  560.         Error = ERR_NO_MEM;
  561.  
  562.     *BitMapPtr = BitMap;
  563.  
  564.     return(Error);
  565. }
  566.  
  567. LONG __regargs
  568. DitherImage_Burkes(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  569. {
  570.     struct BitMap    *BitMap    = NULL;
  571.     LONG         Error    = 0;
  572.     APTR         Data;
  573.  
  574.     if(Data = AllocVecPooled(2 * (NewWidth + 4) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  575.     {
  576.         UBYTE *TempLine;
  577.  
  578.         if(TempLine = CreateTempLine(NewWidth,1))
  579.         {
  580.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  581.             {
  582.                 struct RastPort __aligned DummyRPort,*TempRPort;
  583.  
  584.                 InitRastPort(&DummyRPort);
  585.  
  586.                 DummyRPort . BitMap = BitMap;
  587.  
  588.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  589.                 {
  590.                     LONG     i,j;
  591.                     UBYTE    *Line;
  592.  
  593.                     WORD    *ThisError,
  594.                         *NextError,
  595.                          Value;
  596.  
  597.                     ThisError = Data;
  598.                     NextError = ThisError + NewWidth + 4;
  599.  
  600.                     ThisError += 2;
  601.                     NextError += 2;
  602.  
  603.                     if(NewHeight == Image -> Height)
  604.                     {
  605.                         for(i = 0 ; i < NewHeight ; i++)
  606.                         {
  607.                             Line = Image -> Lines[i];
  608.  
  609.                             if(i & 1)
  610.                             {
  611.                                 for(j = 0 ; j < NewWidth ; j++)
  612.                                 {
  613.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  614.                                         Value = 255;
  615.                                     else
  616.                                     {
  617.                                         if(Value < 0)
  618.                                             Value = 0;
  619.                                     }
  620.  
  621.                                     if(TempLine[j] = (Value < 128))
  622.                                     {
  623.                                         if(Value)
  624.                                         {
  625.                                             Value += Value;
  626.  
  627.                                             NextError[j + 2]     = Value;
  628.                                             NextError[j - 2]    += Value;Value += Value;
  629.                                             NextError[j + 1]    += Value;
  630.                                             NextError[j - 1]    += Value;
  631.                                             ThisError[j + 2]    += Value;Value += Value;
  632.                                             NextError[j]        += Value;
  633.                                             ThisError[j + 1]    += Value;
  634.                                         }
  635.                                     }
  636.                                     else
  637.                                     {
  638.                                         if(Value = Value - 255)
  639.                                         {
  640.                                             Value += Value;
  641.  
  642.                                             NextError[j + 2]     = Value;
  643.                                             NextError[j - 2]    += Value;Value += Value;
  644.                                             NextError[j + 1]    += Value;
  645.                                             NextError[j - 1]    += Value;
  646.                                             ThisError[j + 2]    += Value;Value += Value;
  647.                                             NextError[j]        += Value;
  648.                                             ThisError[j + 1]    += Value;
  649.                                         }
  650.                                     }
  651.                                 }
  652.  
  653.                                 {
  654.                                     register WORD *Swap;
  655.  
  656.                                     Swap        = ThisError;
  657.                                     ThisError    = NextError;
  658.                                     NextError    = Swap;
  659.  
  660.                                     Swap[NewWidth - 1] = 0;
  661.                                     Swap[NewWidth - 2] = 0;
  662.  
  663.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  664.                                 }
  665.                             }
  666.                             else
  667.                             {
  668.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  669.                                 {
  670.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  671.                                         Value = 255;
  672.                                     else
  673.                                     {
  674.                                         if(Value < 0)
  675.                                             Value = 0;
  676.                                     }
  677.  
  678.                                     if(TempLine[j] = (Value < 128))
  679.                                     {
  680.                                         if(Value)
  681.                                         {
  682.                                             Value += Value;
  683.  
  684.                                             NextError[j - 2]     = Value;
  685.                                             NextError[j + 2]    += Value;Value += Value;
  686.                                             NextError[j - 1]    += Value;
  687.                                             NextError[j + 1]    += Value;
  688.                                             ThisError[j - 2]    += Value;Value += Value;
  689.                                             NextError[j]        += Value;
  690.                                             ThisError[j - 1]    += Value;
  691.                                         }
  692.                                     }
  693.                                     else
  694.                                     {
  695.                                         if(Value = Value - 255)
  696.                                         {
  697.                                             Value += Value;
  698.  
  699.                                             NextError[j - 2]     = Value;
  700.                                             NextError[j + 2]    += Value;Value += Value;
  701.                                             NextError[j - 1]    += Value;
  702.                                             NextError[j + 1]    += Value;
  703.                                             ThisError[j - 2]    += Value;Value += Value;
  704.                                             NextError[j]        += Value;
  705.                                             ThisError[j - 1]    += Value;
  706.                                         }
  707.                                     }
  708.                                 }
  709.  
  710.                                 {
  711.                                     register WORD *Swap;
  712.  
  713.                                     Swap        = ThisError;
  714.                                     ThisError    = NextError;
  715.                                     NextError    = Swap;
  716.  
  717.                                     Swap[0] = 0;
  718.                                     Swap[1] = 0;
  719.  
  720.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  721.                                 }
  722.                             }
  723.  
  724.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  725.  
  726.                             ShowProgress(i);
  727.                         }
  728.                     }
  729.                     else
  730.                     {
  731.                         for(i = 0 ; i < NewHeight ; i++)
  732.                         {
  733.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  734.  
  735.                             if(i & 1)
  736.                             {
  737.                                 for(j = 0 ; j < NewWidth ; j++)
  738.                                 {
  739.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  740.                                         Value = 255;
  741.                                     else
  742.                                     {
  743.                                         if(Value < 0)
  744.                                             Value = 0;
  745.                                     }
  746.  
  747.                                     if(TempLine[j] = (Value < 128))
  748.                                     {
  749.                                         if(Value)
  750.                                         {
  751.                                             Value += Value;
  752.  
  753.                                             NextError[j + 2]     = Value;
  754.                                             NextError[j - 2]    += Value;Value += Value;
  755.                                             NextError[j + 1]    += Value;
  756.                                             NextError[j - 1]    += Value;
  757.                                             ThisError[j + 2]    += Value;Value += Value;
  758.                                             NextError[j]        += Value;
  759.                                             ThisError[j + 1]    += Value;
  760.                                         }
  761.                                     }
  762.                                     else
  763.                                     {
  764.                                         if(Value = Value - 255)
  765.                                         {
  766.                                             Value += Value;
  767.  
  768.                                             NextError[j + 2]     = Value;
  769.                                             NextError[j - 2]    += Value;Value += Value;
  770.                                             NextError[j + 1]    += Value;
  771.                                             NextError[j - 1]    += Value;
  772.                                             ThisError[j + 2]    += Value;Value += Value;
  773.                                             NextError[j]        += Value;
  774.                                             ThisError[j + 1]    += Value;
  775.                                         }
  776.                                     }
  777.                                 }
  778.  
  779.                                 {
  780.                                     register WORD *Swap;
  781.  
  782.                                     Swap        = ThisError;
  783.                                     ThisError    = NextError;
  784.                                     NextError    = Swap;
  785.  
  786.                                     Swap[NewWidth - 1] = 0;
  787.                                     Swap[NewWidth - 2] = 0;
  788.  
  789.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  790.                                 }
  791.                             }
  792.                             else
  793.                             {
  794.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  795.                                 {
  796.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  797.                                         Value = 255;
  798.                                     else
  799.                                     {
  800.                                         if(Value < 0)
  801.                                             Value = 0;
  802.                                     }
  803.  
  804.                                     if(TempLine[j] = (Value < 128))
  805.                                     {
  806.                                         if(Value)
  807.                                         {
  808.                                             Value += Value;
  809.  
  810.                                             NextError[j - 2]     = Value;
  811.                                             NextError[j + 2]    += Value;Value += Value;
  812.                                             NextError[j - 1]    += Value;
  813.                                             NextError[j + 1]    += Value;
  814.                                             ThisError[j - 2]    += Value;Value += Value;
  815.                                             NextError[j]        += Value;
  816.                                             ThisError[j - 1]    += Value;
  817.                                         }
  818.                                     }
  819.                                     else
  820.                                     {
  821.                                         if(Value = Value - 255)
  822.                                         {
  823.                                             Value += Value;
  824.  
  825.                                             NextError[j - 2]     = Value;
  826.                                             NextError[j + 2]    += Value;Value += Value;
  827.                                             NextError[j - 1]    += Value;
  828.                                             NextError[j + 1]    += Value;
  829.                                             ThisError[j - 2]    += Value;Value += Value;
  830.                                             NextError[j]        += Value;
  831.                                             ThisError[j - 1]    += Value;
  832.                                         }
  833.                                     }
  834.                                 }
  835.  
  836.                                 {
  837.                                     register WORD *Swap;
  838.  
  839.                                     Swap        = ThisError;
  840.                                     ThisError    = NextError;
  841.                                     NextError    = Swap;
  842.  
  843.                                     Swap[0] = 0;
  844.                                     Swap[1] = 0;
  845.  
  846.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  847.                                 }
  848.                             }
  849.  
  850.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  851.  
  852.                             ShowProgress(i);
  853.                         }
  854.                     }
  855.  
  856.                     DeleteTempRPort(TempRPort);
  857.                 }
  858.                 else
  859.                 {
  860.                     DeleteBitMap(BitMap);
  861.  
  862.                     BitMap = NULL;
  863.  
  864.                     Error = ERR_NO_MEM;
  865.                 }
  866.             }
  867.             else
  868.                 Error = ERR_NO_MEM;
  869.  
  870.             DeleteTempLine(TempLine);
  871.         }
  872.         else
  873.             Error = ERR_NO_MEM;
  874.  
  875.         FreeVecPooled(Data);
  876.     }
  877.     else
  878.         Error = ERR_NO_MEM;
  879.  
  880.     *BitMapPtr = BitMap;
  881.  
  882.     return(Error);
  883. }
  884.  
  885. LONG __regargs
  886. DitherImage_Sierra(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  887. {
  888.     struct BitMap    *BitMap    = NULL;
  889.     LONG         Error    = 0;
  890.     APTR         Data;
  891.  
  892.     if(Data = AllocVecPooled(3 * (NewWidth + 4) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  893.     {
  894.         UBYTE *TempLine;
  895.  
  896.         if(TempLine = CreateTempLine(NewWidth,1))
  897.         {
  898.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  899.             {
  900.                 struct RastPort __aligned DummyRPort,*TempRPort;
  901.  
  902.                 InitRastPort(&DummyRPort);
  903.  
  904.                 DummyRPort . BitMap = BitMap;
  905.  
  906.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  907.                 {
  908.                     LONG     i,j;
  909.                     UBYTE    *Line;
  910.  
  911.                     WORD    *ThisError,
  912.                         *NextError,
  913.                         *LastError,
  914.                          Fill,
  915.                          Value;
  916.  
  917.                     ThisError = Data;
  918.                     NextError = ThisError + NewWidth + 4;
  919.                     LastError = NextError + NewWidth + 4;
  920.  
  921.                     ThisError += 2;
  922.                     NextError += 2;
  923.                     LastError += 2;
  924.  
  925.                     if(NewHeight == Image -> Height)
  926.                     {
  927.                         for(i = 0 ; i < NewHeight ; i++)
  928.                         {
  929.                             Line = Image -> Lines[i];
  930.  
  931.                             if(i & 1)
  932.                             {
  933.                                 for(j = 0 ; j < NewWidth ; j++)
  934.                                 {
  935.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  936.                                         Value = 255;
  937.                                     else
  938.                                     {
  939.                                         if(Value < 0)
  940.                                             Value = 0;
  941.                                     }
  942.  
  943.                                     if(TempLine[j] = (Value < 128))
  944.                                     {
  945.                                         if(Value)
  946.                                         {
  947.                                             Fill = Value + Value;
  948.  
  949.                                             LastError[j + 1]     = Fill;
  950.                                             LastError[j - 1]    += Fill;
  951.                                             NextError[j + 2]    += Fill;
  952.                                             NextError[j - 2]    += Fill; Fill += Value;
  953.                                             ThisError[j + 2]    += Fill;
  954.                                             LastError[j]        += Fill; Fill += Value;
  955.                                             NextError[j + 1]    += Fill;
  956.                                             NextError[j - 1]    += Fill; Fill += Value;
  957.                                             NextError[j]        += Fill;
  958.                                             ThisError[j + 1]    += Fill;
  959.                                         }
  960.                                     }
  961.                                     else
  962.                                     {
  963.                                         if(Value = Value - 255)
  964.                                         {
  965.                                             Fill = Value + Value;
  966.  
  967.                                             LastError[j + 1]     = Fill;
  968.                                             LastError[j - 1]    += Fill;
  969.                                             NextError[j + 2]    += Fill;
  970.                                             NextError[j - 2]    += Fill; Fill += Value;
  971.                                             ThisError[j + 2]    += Fill;
  972.                                             LastError[j]        += Fill; Fill += Value;
  973.                                             NextError[j + 1]    += Fill;
  974.                                             NextError[j - 1]    += Fill; Fill += Value;
  975.                                             NextError[j]        += Fill;
  976.                                             ThisError[j + 1]    += Fill;
  977.                                         }
  978.                                     }
  979.                                 }
  980.  
  981.                                 {
  982.                                     register WORD *Swap;
  983.  
  984.                                     Swap        = ThisError;
  985.                                     ThisError    = NextError;
  986.                                     NextError    = LastError;
  987.                                     LastError    = Swap;
  988.  
  989.                                     Swap[NewWidth - 1] = 0;
  990.                                     Swap[NewWidth - 2] = 0;
  991.  
  992.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  993.                                 }
  994.                             }
  995.                             else
  996.                             {
  997.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  998.                                 {
  999.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  1000.                                         Value = 255;
  1001.                                     else
  1002.                                     {
  1003.                                         if(Value < 0)
  1004.                                             Value = 0;
  1005.                                     }
  1006.  
  1007.                                     if(TempLine[j] = (Value < 128))
  1008.                                     {
  1009.                                         if(Value)
  1010.                                         {
  1011.                                             Fill = Value + Value;
  1012.  
  1013.                                             LastError[j - 1]     = Fill;
  1014.                                             LastError[j + 1]    += Fill;
  1015.                                             NextError[j - 2]    += Fill;
  1016.                                             NextError[j + 2]    += Fill; Fill += Value;
  1017.                                             ThisError[j - 2]    += Fill;
  1018.                                             LastError[j]        += Fill; Fill += Value;
  1019.                                             NextError[j - 1]    += Fill;
  1020.                                             NextError[j + 1]    += Fill; Fill += Value;
  1021.                                             NextError[j]        += Fill;
  1022.                                             ThisError[j - 1]    += Fill;
  1023.                                         }
  1024.                                     }
  1025.                                     else
  1026.                                     {
  1027.                                         if(Value = Value - 255)
  1028.                                         {
  1029.                                             Fill = Value + Value;
  1030.  
  1031.                                             LastError[j - 1]     = Fill;
  1032.                                             LastError[j + 1]    += Fill;
  1033.                                             NextError[j - 2]    += Fill;
  1034.                                             NextError[j + 2]    += Fill; Fill += Value;
  1035.                                             ThisError[j - 2]    += Fill;
  1036.                                             LastError[j]        += Fill; Fill += Value;
  1037.                                             NextError[j - 1]    += Fill;
  1038.                                             NextError[j + 1]    += Fill; Fill += Value;
  1039.                                             NextError[j]        += Fill;
  1040.                                             ThisError[j - 1]    += Fill;
  1041.                                         }
  1042.                                     }
  1043.                                 }
  1044.  
  1045.                                 {
  1046.                                     register WORD *Swap;
  1047.  
  1048.                                     Swap        = ThisError;
  1049.                                     ThisError    = NextError;
  1050.                                     NextError    = LastError;
  1051.                                     LastError    = Swap;
  1052.  
  1053.                                     Swap[0] = 0;
  1054.                                     Swap[1] = 0;
  1055.  
  1056.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1057.                                 }
  1058.                             }
  1059.  
  1060.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1061.  
  1062.                             ShowProgress(i);
  1063.                         }
  1064.                     }
  1065.                     else
  1066.                     {
  1067.                         for(i = 0 ; i < NewHeight ; i++)
  1068.                         {
  1069.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  1070.  
  1071.                             if(i & 1)
  1072.                             {
  1073.                                 for(j = 0 ; j < NewWidth ; j++)
  1074.                                 {
  1075.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  1076.                                         Value = 255;
  1077.                                     else
  1078.                                     {
  1079.                                         if(Value < 0)
  1080.                                             Value = 0;
  1081.                                     }
  1082.  
  1083.                                     if(TempLine[j] = (Value < 128))
  1084.                                     {
  1085.                                         if(Value)
  1086.                                         {
  1087.                                             Fill = Value + Value;
  1088.  
  1089.                                             LastError[j + 1]     = Fill;
  1090.                                             LastError[j - 1]    += Fill;
  1091.                                             NextError[j + 2]    += Fill;
  1092.                                             NextError[j - 2]    += Fill; Fill += Value;
  1093.                                             ThisError[j + 2]    += Fill;
  1094.                                             LastError[j]        += Fill; Fill += Value;
  1095.                                             NextError[j + 1]    += Fill;
  1096.                                             NextError[j - 1]    += Fill; Fill += Value;
  1097.                                             NextError[j]        += Fill;
  1098.                                             ThisError[j + 1]    += Fill;
  1099.                                         }
  1100.                                     }
  1101.                                     else
  1102.                                     {
  1103.                                         if(Value = Value - 255)
  1104.                                         {
  1105.                                             Fill = Value + Value;
  1106.  
  1107.                                             LastError[j + 1]     = Fill;
  1108.                                             LastError[j - 1]    += Fill;
  1109.                                             NextError[j + 2]    += Fill;
  1110.                                             NextError[j - 2]    += Fill; Fill += Value;
  1111.                                             ThisError[j + 2]    += Fill;
  1112.                                             LastError[j]        += Fill; Fill += Value;
  1113.                                             NextError[j + 1]    += Fill;
  1114.                                             NextError[j - 1]    += Fill; Fill += Value;
  1115.                                             NextError[j]        += Fill;
  1116.                                             ThisError[j + 1]    += Fill;
  1117.                                         }
  1118.                                     }
  1119.                                 }
  1120.  
  1121.                                 {
  1122.                                     register WORD *Swap;
  1123.  
  1124.                                     Swap        = ThisError;
  1125.                                     ThisError    = NextError;
  1126.                                     NextError    = LastError;
  1127.                                     LastError    = Swap;
  1128.  
  1129.                                     Swap[NewWidth - 1] = 0;
  1130.                                     Swap[NewWidth - 2] = 0;
  1131.  
  1132.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1133.                                 }
  1134.                             }
  1135.                             else
  1136.                             {
  1137.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  1138.                                 {
  1139.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 32) > 255)
  1140.                                         Value = 255;
  1141.                                     else
  1142.                                     {
  1143.                                         if(Value < 0)
  1144.                                             Value = 0;
  1145.                                     }
  1146.  
  1147.                                     if(TempLine[j] = (Value < 128))
  1148.                                     {
  1149.                                         if(Value)
  1150.                                         {
  1151.                                             Fill = Value + Value;
  1152.  
  1153.                                             LastError[j - 1]     = Fill;
  1154.                                             LastError[j + 1]    += Fill;
  1155.                                             NextError[j - 2]    += Fill;
  1156.                                             NextError[j + 2]    += Fill; Fill += Value;
  1157.                                             ThisError[j - 2]    += Fill;
  1158.                                             LastError[j]        += Fill; Fill += Value;
  1159.                                             NextError[j - 1]    += Fill;
  1160.                                             NextError[j + 1]    += Fill; Fill += Value;
  1161.                                             NextError[j]        += Fill;
  1162.                                             ThisError[j - 1]    += Fill;
  1163.                                         }
  1164.                                     }
  1165.                                     else
  1166.                                     {
  1167.                                         if(Value = Value - 255)
  1168.                                         {
  1169.                                             Fill = Value + Value;
  1170.  
  1171.                                             LastError[j - 1]     = Fill;
  1172.                                             LastError[j + 1]    += Fill;
  1173.                                             NextError[j - 2]    += Fill;
  1174.                                             NextError[j + 2]    += Fill; Fill += Value;
  1175.                                             ThisError[j - 2]    += Fill;
  1176.                                             LastError[j]        += Fill; Fill += Value;
  1177.                                             NextError[j - 1]    += Fill;
  1178.                                             NextError[j + 1]    += Fill; Fill += Value;
  1179.                                             NextError[j]        += Fill;
  1180.                                             ThisError[j - 1]    += Fill;
  1181.                                         }
  1182.                                     }
  1183.                                 }
  1184.  
  1185.                                 {
  1186.                                     register WORD *Swap;
  1187.  
  1188.                                     Swap        = ThisError;
  1189.                                     ThisError    = NextError;
  1190.                                     NextError    = LastError;
  1191.                                     LastError    = Swap;
  1192.  
  1193.                                     Swap[0] = 0;
  1194.                                     Swap[1] = 0;
  1195.  
  1196.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1197.                                 }
  1198.                             }
  1199.  
  1200.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1201.  
  1202.                             ShowProgress(i);
  1203.                         }
  1204.                     }
  1205.  
  1206.                     DeleteTempRPort(TempRPort);
  1207.                 }
  1208.                 else
  1209.                 {
  1210.                     DeleteBitMap(BitMap);
  1211.  
  1212.                     BitMap = NULL;
  1213.  
  1214.                     Error = ERR_NO_MEM;
  1215.                 }
  1216.             }
  1217.             else
  1218.                 Error = ERR_NO_MEM;
  1219.  
  1220.             DeleteTempLine(TempLine);
  1221.         }
  1222.         else
  1223.             Error = ERR_NO_MEM;
  1224.  
  1225.         FreeVecPooled(Data);
  1226.     }
  1227.     else
  1228.         Error = ERR_NO_MEM;
  1229.  
  1230.     *BitMapPtr = BitMap;
  1231.  
  1232.     return(Error);
  1233. }
  1234.  
  1235. LONG __regargs
  1236. DitherImage_JJN(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  1237. {
  1238.     struct BitMap    *BitMap    = NULL;
  1239.     LONG         Error    = 0;
  1240.     APTR         Data;
  1241.  
  1242.     if(Data = AllocVecPooled(3 * (NewWidth + 4) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  1243.     {
  1244.         UBYTE *TempLine;
  1245.  
  1246.         if(TempLine = CreateTempLine(NewWidth,1))
  1247.         {
  1248.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  1249.             {
  1250.                 struct RastPort __aligned DummyRPort,*TempRPort;
  1251.  
  1252.                 InitRastPort(&DummyRPort);
  1253.  
  1254.                 DummyRPort . BitMap = BitMap;
  1255.  
  1256.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  1257.                 {
  1258.                     LONG     i,j;
  1259.                     UBYTE    *Line;
  1260.  
  1261.                     WORD    *ThisError,
  1262.                         *NextError,
  1263.                         *LastError,
  1264.                          Fill,
  1265.                          Value;
  1266.  
  1267.                     ThisError = Data;
  1268.                     NextError = ThisError + NewWidth + 4;
  1269.                     LastError = NextError + NewWidth + 4;
  1270.  
  1271.                     ThisError += 2;
  1272.                     NextError += 2;
  1273.                     LastError += 2;
  1274.  
  1275.                     if(NewHeight == Image -> Height)
  1276.                     {
  1277.                         for(i = 0 ; i < NewHeight ; i++)
  1278.                         {
  1279.                             Line = Image -> Lines[i];
  1280.  
  1281.                             if(i & 1)
  1282.                             {
  1283.                                 for(j = 0 ; j < NewWidth ; j++)
  1284.                                 {
  1285.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 48) > 255)
  1286.                                         Value = 255;
  1287.                                     else
  1288.                                     {
  1289.                                         if(Value < 0)
  1290.                                             Value = 0;
  1291.                                     }
  1292.  
  1293.                                     if(TempLine[j] = (Value < 128))
  1294.                                     {
  1295.                                         if(Value)
  1296.                                         {
  1297.                                             Fill = Value + Value;
  1298.  
  1299.                                             LastError[j + 2]     = Value;
  1300.                                             LastError[j - 2]    += Value;Value += Fill;
  1301.                                             LastError[j + 1]    += Value;
  1302.                                             LastError[j - 1]    += Value;
  1303.                                             NextError[j + 2]    += Value;
  1304.                                             NextError[j - 2]    += Value;Value += Fill;
  1305.                                             LastError[j]        += Value;
  1306.                                             NextError[j + 1]    += Value;
  1307.                                             NextError[j - 1]    += Value;
  1308.                                             ThisError[j + 2]    += Value;Value += Fill;
  1309.                                             ThisError[j + 1]    += Value;
  1310.                                             NextError[j]        += Value;
  1311.                                         }
  1312.                                     }
  1313.                                     else
  1314.                                     {
  1315.                                         if(Value = Value - 255)
  1316.                                         {
  1317.                                             Fill = Value + Value;
  1318.  
  1319.                                             LastError[j + 2]     = Value;
  1320.                                             LastError[j - 2]    += Value;Value += Fill;
  1321.                                             LastError[j + 1]    += Value;
  1322.                                             LastError[j - 1]    += Value;
  1323.                                             NextError[j + 2]    += Value;
  1324.                                             NextError[j - 2]    += Value;Value += Fill;
  1325.                                             LastError[j]        += Value;
  1326.                                             NextError[j + 1]    += Value;
  1327.                                             NextError[j - 1]    += Value;
  1328.                                             ThisError[j + 2]    += Value;Value += Fill;
  1329.                                             ThisError[j + 1]    += Value;
  1330.                                             NextError[j]        += Value;
  1331.                                         }
  1332.                                     }
  1333.                                 }
  1334.  
  1335.                                 {
  1336.                                     register WORD *Swap;
  1337.  
  1338.                                     Swap        = ThisError;
  1339.                                     ThisError    = NextError;
  1340.                                     NextError    = LastError;
  1341.                                     LastError    = Swap;
  1342.  
  1343.                                     Swap[NewWidth - 1] = 0;
  1344.                                     Swap[NewWidth - 2] = 0;
  1345.  
  1346.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1347.                                 }
  1348.                             }
  1349.                             else
  1350.                             {
  1351.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  1352.                                 {
  1353.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 48) > 255)
  1354.                                         Value = 255;
  1355.                                     else
  1356.                                     {
  1357.                                         if(Value < 0)
  1358.                                             Value = 0;
  1359.                                     }
  1360.  
  1361.                                     if(TempLine[j] = (Value < 128))
  1362.                                     {
  1363.                                         if(Value)
  1364.                                         {
  1365.                                             Fill = Value + Value;
  1366.  
  1367.                                             LastError[j - 2]     = Value;
  1368.                                             LastError[j + 2]    += Value;Value += Fill;
  1369.                                             LastError[j - 1]    += Value;
  1370.                                             LastError[j + 1]    += Value;
  1371.                                             NextError[j - 2]    += Value;
  1372.                                             NextError[j + 2]    += Value;Value += Fill;
  1373.                                             LastError[j]        += Value;
  1374.                                             NextError[j - 1]    += Value;
  1375.                                             NextError[j + 1]    += Value;
  1376.                                             ThisError[j - 2]    += Value;Value += Fill;
  1377.                                             ThisError[j - 1]    += Value;
  1378.                                             NextError[j]        += Value;
  1379.                                         }
  1380.                                     }
  1381.                                     else
  1382.                                     {
  1383.                                         if(Value = Value - 255)
  1384.                                         {
  1385.                                             Fill = Value + Value;
  1386.  
  1387.                                             LastError[j - 2]     = Value;
  1388.                                             LastError[j + 2]    += Value;Value += Fill;
  1389.                                             LastError[j - 1]    += Value;
  1390.                                             LastError[j + 1]    += Value;
  1391.                                             NextError[j - 2]    += Value;
  1392.                                             NextError[j + 2]    += Value;Value += Fill;
  1393.                                             LastError[j]        += Value;
  1394.                                             NextError[j - 1]    += Value;
  1395.                                             NextError[j + 1]    += Value;
  1396.                                             ThisError[j - 2]    += Value;Value += Fill;
  1397.                                             ThisError[j - 1]    += Value;
  1398.                                             NextError[j]        += Value;
  1399.                                         }
  1400.                                     }
  1401.                                 }
  1402.  
  1403.                                 {
  1404.                                     register WORD *Swap;
  1405.  
  1406.                                     Swap        = ThisError;
  1407.                                     ThisError    = NextError;
  1408.                                     NextError    = LastError;
  1409.                                     LastError    = Swap;
  1410.  
  1411.                                     Swap[0] = 0;
  1412.                                     Swap[1] = 0;
  1413.  
  1414.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1415.                                 }
  1416.                             }
  1417.  
  1418.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1419.  
  1420.                             ShowProgress(i);
  1421.                         }
  1422.                     }
  1423.                     else
  1424.                     {
  1425.                         for(i = 0 ; i < NewHeight ; i++)
  1426.                         {
  1427.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  1428.  
  1429.                             if(i & 1)
  1430.                             {
  1431.                                 for(j = 0 ; j < NewWidth ; j++)
  1432.                                 {
  1433.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 48) > 255)
  1434.                                         Value = 255;
  1435.                                     else
  1436.                                     {
  1437.                                         if(Value < 0)
  1438.                                             Value = 0;
  1439.                                     }
  1440.  
  1441.                                     if(TempLine[j] = (Value < 128))
  1442.                                     {
  1443.                                         if(Value)
  1444.                                         {
  1445.                                             Fill = Value + Value;
  1446.  
  1447.                                             LastError[j + 2]     = Value;
  1448.                                             LastError[j - 2]    += Value;Value += Fill;
  1449.                                             LastError[j + 1]    += Value;
  1450.                                             LastError[j - 1]    += Value;
  1451.                                             NextError[j + 2]    += Value;
  1452.                                             NextError[j - 2]    += Value;Value += Fill;
  1453.                                             LastError[j]        += Value;
  1454.                                             NextError[j + 1]    += Value;
  1455.                                             NextError[j - 1]    += Value;
  1456.                                             ThisError[j + 2]    += Value;Value += Fill;
  1457.                                             ThisError[j + 1]    += Value;
  1458.                                             NextError[j]        += Value;
  1459.                                         }
  1460.                                     }
  1461.                                     else
  1462.                                     {
  1463.                                         if(Value = Value - 255)
  1464.                                         {
  1465.                                             Fill = Value + Value;
  1466.  
  1467.                                             LastError[j + 2]     = Value;
  1468.                                             LastError[j - 2]    += Value;Value += Fill;
  1469.                                             LastError[j + 1]    += Value;
  1470.                                             LastError[j - 1]    += Value;
  1471.                                             NextError[j + 2]    += Value;
  1472.                                             NextError[j - 2]    += Value;Value += Fill;
  1473.                                             LastError[j]        += Value;
  1474.                                             NextError[j + 1]    += Value;
  1475.                                             NextError[j - 1]    += Value;
  1476.                                             ThisError[j + 2]    += Value;Value += Fill;
  1477.                                             ThisError[j + 1]    += Value;
  1478.                                             NextError[j]        += Value;
  1479.                                         }
  1480.                                     }
  1481.                                 }
  1482.  
  1483.                                 {
  1484.                                     register WORD *Swap;
  1485.  
  1486.                                     Swap        = ThisError;
  1487.                                     ThisError    = NextError;
  1488.                                     NextError    = LastError;
  1489.                                     LastError    = Swap;
  1490.  
  1491.                                     Swap[NewWidth - 1] = 0;
  1492.                                     Swap[NewWidth - 2] = 0;
  1493.  
  1494.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1495.                                 }
  1496.                             }
  1497.                             else
  1498.                             {
  1499.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  1500.                                 {
  1501.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 48) > 255)
  1502.                                         Value = 255;
  1503.                                     else
  1504.                                     {
  1505.                                         if(Value < 0)
  1506.                                             Value = 0;
  1507.                                     }
  1508.  
  1509.                                     if(TempLine[j] = (Value < 128))
  1510.                                     {
  1511.                                         if(Value)
  1512.                                         {
  1513.                                             Fill = Value + Value;
  1514.  
  1515.                                             LastError[j - 2]     = Value;
  1516.                                             LastError[j + 2]    += Value;Value += Fill;
  1517.                                             LastError[j - 1]    += Value;
  1518.                                             LastError[j + 1]    += Value;
  1519.                                             NextError[j - 2]    += Value;
  1520.                                             NextError[j + 2]    += Value;Value += Fill;
  1521.                                             LastError[j]        += Value;
  1522.                                             NextError[j - 1]    += Value;
  1523.                                             NextError[j + 1]    += Value;
  1524.                                             ThisError[j - 2]    += Value;Value += Fill;
  1525.                                             ThisError[j - 1]    += Value;
  1526.                                             NextError[j]        += Value;
  1527.                                         }
  1528.                                     }
  1529.                                     else
  1530.                                     {
  1531.                                         if(Value = Value - 255)
  1532.                                         {
  1533.                                             Fill = Value + Value;
  1534.  
  1535.                                             LastError[j - 2]     = Value;
  1536.                                             LastError[j + 2]    += Value;Value += Fill;
  1537.                                             LastError[j - 1]    += Value;
  1538.                                             LastError[j + 1]    += Value;
  1539.                                             NextError[j - 2]    += Value;
  1540.                                             NextError[j + 2]    += Value;Value += Fill;
  1541.                                             LastError[j]        += Value;
  1542.                                             NextError[j - 1]    += Value;
  1543.                                             NextError[j + 1]    += Value;
  1544.                                             ThisError[j - 2]    += Value;Value += Fill;
  1545.                                             ThisError[j - 1]    += Value;
  1546.                                             NextError[j]        += Value;
  1547.                                         }
  1548.                                     }
  1549.                                 }
  1550.  
  1551.                                 {
  1552.                                     register WORD *Swap;
  1553.  
  1554.                                     Swap        = ThisError;
  1555.                                     ThisError    = NextError;
  1556.                                     NextError    = LastError;
  1557.                                     LastError    = Swap;
  1558.  
  1559.                                     Swap[0] = 0;
  1560.                                     Swap[1] = 0;
  1561.  
  1562.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  1563.                                 }
  1564.                             }
  1565.  
  1566.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1567.  
  1568.                             ShowProgress(i);
  1569.                         }
  1570.                     }
  1571.  
  1572.                     DeleteTempRPort(TempRPort);
  1573.                 }
  1574.                 else
  1575.                 {
  1576.                     DeleteBitMap(BitMap);
  1577.  
  1578.                     BitMap = NULL;
  1579.  
  1580.                     Error = ERR_NO_MEM;
  1581.                 }
  1582.             }
  1583.             else
  1584.                 Error = ERR_NO_MEM;
  1585.  
  1586.             DeleteTempLine(TempLine);
  1587.         }
  1588.         else
  1589.             Error = ERR_NO_MEM;
  1590.  
  1591.         FreeVecPooled(Data);
  1592.     }
  1593.     else
  1594.         Error = ERR_NO_MEM;
  1595.  
  1596.     *BitMapPtr = BitMap;
  1597.  
  1598.     return(Error);
  1599. }
  1600.  
  1601. LONG __regargs
  1602. DitherImage_Stevenson_Arce(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  1603. {
  1604.     struct BitMap    *BitMap    = NULL;
  1605.     LONG         Error    = 0;
  1606.     APTR         Data;
  1607.  
  1608.     if(Data = AllocVecPooled(4 * (NewWidth + 6) * sizeof(LONG),MEMF_ANY | MEMF_CLEAR))
  1609.     {
  1610.         UBYTE *TempLine;
  1611.  
  1612.         if(TempLine = CreateTempLine(NewWidth,1))
  1613.         {
  1614.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  1615.             {
  1616.                 struct RastPort __aligned DummyRPort,*TempRPort;
  1617.  
  1618.                 InitRastPort(&DummyRPort);
  1619.  
  1620.                 DummyRPort . BitMap = BitMap;
  1621.  
  1622.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  1623.                 {
  1624.                     LONG     i,j;
  1625.                     UBYTE    *Line;
  1626.  
  1627.                     LONG    *ThisError,
  1628.                         *NextError,
  1629.                         *TempError,
  1630.                         *LastError,
  1631.                          Value;
  1632.  
  1633.                     ThisError = Data;
  1634.                     NextError = ThisError + NewWidth + 6;
  1635.                     TempError = NextError + NewWidth + 6;
  1636.                     LastError = TempError + NewWidth + 6;
  1637.  
  1638.                     ThisError += 3;
  1639.                     NextError += 3;
  1640.                     TempError += 3;
  1641.                     LastError += 3;
  1642.  
  1643.                     if(NewHeight == Image -> Height)
  1644.                     {
  1645.                         for(i = 0 ; i < NewHeight ; i++)
  1646.                         {
  1647.                             Line = Image -> Lines[i];
  1648.  
  1649.                             if(i & 1)
  1650.                             {
  1651.                                 for(j = 0 ; j < NewWidth ; j++)
  1652.                                 {
  1653.                                     if((Value = (LONG)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 200) > 255)
  1654.                                         Value = 255;
  1655.                                     else
  1656.                                     {
  1657.                                         if(Value < 0)
  1658.                                             Value = 0;
  1659.                                     }
  1660.  
  1661.                                     if(TempLine[j] = (Value < 128))
  1662.                                     {
  1663.                                         if(Value)
  1664.                                         {
  1665.                                             ThisError[j + 2]    += 32 * Value;
  1666.  
  1667.                                             NextError[j - 3]    += 12 * Value;
  1668.                                             NextError[j - 1]    += 26 * Value;
  1669.                                             NextError[j + 1]    += 30 * Value;
  1670.                                             NextError[j + 3]    += 16 * Value;
  1671.  
  1672.                                             TempError[j - 2]    += 12 * Value;
  1673.                                             TempError[j    ]    += 26 * Value;
  1674.                                             TempError[j + 2]    += 12 * Value;
  1675.  
  1676.                                             LastError[j - 3]    +=  5 * Value;
  1677.                                             LastError[j - 1]    += 12 * Value;
  1678.                                             LastError[j + 1]    += 12 * Value;
  1679.                                             LastError[j + 3]    +=  5 * Value;
  1680.                                         }
  1681.                                     }
  1682.                                     else
  1683.                                     {
  1684.                                         if(Value = Value - 255)
  1685.                                         {
  1686.                                             ThisError[j + 2]    += 32 * Value;
  1687.  
  1688.                                             NextError[j - 3]    += 12 * Value;
  1689.                                             NextError[j - 1]    += 26 * Value;
  1690.                                             NextError[j + 1]    += 30 * Value;
  1691.                                             NextError[j + 3]    += 16 * Value;
  1692.  
  1693.                                             TempError[j - 2]    += 12 * Value;
  1694.                                             TempError[j    ]    += 26 * Value;
  1695.                                             TempError[j + 2]    += 12 * Value;
  1696.  
  1697.                                             LastError[j - 3]    +=  5 * Value;
  1698.                                             LastError[j - 1]    += 12 * Value;
  1699.                                             LastError[j + 1]    += 12 * Value;
  1700.                                             LastError[j + 3]    +=  5 * Value;
  1701.                                         }
  1702.                                     }
  1703.                                 }
  1704.  
  1705.                                 {
  1706.                                     register LONG *Swap;
  1707.  
  1708.                                     Swap        = ThisError;
  1709.                                     ThisError    = NextError;
  1710.                                     NextError    = TempError;
  1711.                                     TempError    = LastError;
  1712.                                     LastError    = Swap;
  1713.  
  1714.                                     memset(Swap,0,sizeof(LONG) * NewWidth);
  1715.                                 }
  1716.                             }
  1717.                             else
  1718.                             {
  1719.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  1720.                                 {
  1721.                                     if((Value = (LONG)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 200) > 255)
  1722.                                         Value = 255;
  1723.                                     else
  1724.                                     {
  1725.                                         if(Value < 0)
  1726.                                             Value = 0;
  1727.                                     }
  1728.  
  1729.                                     if(TempLine[j] = (Value < 128))
  1730.                                     {
  1731.                                         if(Value)
  1732.                                         {
  1733.                                             ThisError[j - 2]    += 32 * Value;
  1734.  
  1735.                                             NextError[j + 3]    += 12 * Value;
  1736.                                             NextError[j + 1]    += 26 * Value;
  1737.                                             NextError[j - 1]    += 30 * Value;
  1738.                                             NextError[j - 3]    += 16 * Value;
  1739.  
  1740.                                             TempError[j + 2]    += 12 * Value;
  1741.                                             TempError[j    ]    += 26 * Value;
  1742.                                             TempError[j - 2]    += 12 * Value;
  1743.  
  1744.                                             LastError[j + 3]    +=  5 * Value;
  1745.                                             LastError[j + 1]    += 12 * Value;
  1746.                                             LastError[j - 1]    += 12 * Value;
  1747.                                             LastError[j - 3]    +=  5 * Value;
  1748.                                         }
  1749.                                     }
  1750.                                     else
  1751.                                     {
  1752.                                         if(Value = Value - 255)
  1753.                                         {
  1754.                                             ThisError[j - 2]    += 32 * Value;
  1755.  
  1756.                                             NextError[j + 3]    += 12 * Value;
  1757.                                             NextError[j + 1]    += 26 * Value;
  1758.                                             NextError[j - 1]    += 30 * Value;
  1759.                                             NextError[j - 3]    += 16 * Value;
  1760.  
  1761.                                             TempError[j + 2]    += 12 * Value;
  1762.                                             TempError[j    ]    += 26 * Value;
  1763.                                             TempError[j - 2]    += 12 * Value;
  1764.  
  1765.                                             LastError[j + 3]    +=  5 * Value;
  1766.                                             LastError[j + 1]    += 12 * Value;
  1767.                                             LastError[j - 1]    += 12 * Value;
  1768.                                             LastError[j - 3]    +=  5 * Value;
  1769.                                         }
  1770.                                     }
  1771.                                 }
  1772.  
  1773.                                 {
  1774.                                     register LONG *Swap;
  1775.  
  1776.                                     Swap        = ThisError;
  1777.                                     ThisError    = NextError;
  1778.                                     NextError    = TempError;
  1779.                                     TempError    = LastError;
  1780.                                     LastError    = Swap;
  1781.  
  1782.                                     memset(Swap,0,sizeof(LONG) * NewWidth);
  1783.                                 }
  1784.                             }
  1785.  
  1786.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1787.  
  1788.                             ShowProgress(i);
  1789.                         }
  1790.                     }
  1791.                     else
  1792.                     {
  1793.                         for(i = 0 ; i < NewHeight ; i++)
  1794.                         {
  1795.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  1796.  
  1797.                             if(i & 1)
  1798.                             {
  1799.                                 for(j = 0 ; j < NewWidth ; j++)
  1800.                                 {
  1801.                                     if((Value = (LONG)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 200) > 255)
  1802.                                         Value = 255;
  1803.                                     else
  1804.                                     {
  1805.                                         if(Value < 0)
  1806.                                             Value = 0;
  1807.                                     }
  1808.  
  1809.                                     if(TempLine[j] = (Value < 128))
  1810.                                     {
  1811.                                         if(Value)
  1812.                                         {
  1813.                                             ThisError[j + 2]    += 32 * Value;
  1814.  
  1815.                                             NextError[j - 3]    += 12 * Value;
  1816.                                             NextError[j - 1]    += 26 * Value;
  1817.                                             NextError[j + 1]    += 30 * Value;
  1818.                                             NextError[j + 3]    += 16 * Value;
  1819.  
  1820.                                             TempError[j - 2]    += 12 * Value;
  1821.                                             TempError[j    ]    += 26 * Value;
  1822.                                             TempError[j + 2]    += 12 * Value;
  1823.  
  1824.                                             LastError[j - 3]    +=  5 * Value;
  1825.                                             LastError[j - 1]    += 12 * Value;
  1826.                                             LastError[j + 1]    += 12 * Value;
  1827.                                             LastError[j + 3]    +=  5 * Value;
  1828.                                         }
  1829.                                     }
  1830.                                     else
  1831.                                     {
  1832.                                         if(Value = Value - 255)
  1833.                                         {
  1834.                                             ThisError[j + 2]    += 32 * Value;
  1835.  
  1836.                                             NextError[j - 3]    += 12 * Value;
  1837.                                             NextError[j - 1]    += 26 * Value;
  1838.                                             NextError[j + 1]    += 30 * Value;
  1839.                                             NextError[j + 3]    += 16 * Value;
  1840.  
  1841.                                             TempError[j - 2]    += 12 * Value;
  1842.                                             TempError[j    ]    += 26 * Value;
  1843.                                             TempError[j + 2]    += 12 * Value;
  1844.  
  1845.                                             LastError[j - 3]    +=  5 * Value;
  1846.                                             LastError[j - 1]    += 12 * Value;
  1847.                                             LastError[j + 1]    += 12 * Value;
  1848.                                             LastError[j + 3]    +=  5 * Value;
  1849.                                         }
  1850.                                     }
  1851.                                 }
  1852.  
  1853.                                 {
  1854.                                     register LONG *Swap;
  1855.  
  1856.                                     Swap        = ThisError;
  1857.                                     ThisError    = NextError;
  1858.                                     NextError    = TempError;
  1859.                                     TempError    = LastError;
  1860.                                     LastError    = Swap;
  1861.  
  1862.                                     memset(Swap,0,sizeof(LONG) * NewWidth);
  1863.                                 }
  1864.                             }
  1865.                             else
  1866.                             {
  1867.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  1868.                                 {
  1869.                                     if((Value = (LONG)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 200) > 255)
  1870.                                         Value = 255;
  1871.                                     else
  1872.                                     {
  1873.                                         if(Value < 0)
  1874.                                             Value = 0;
  1875.                                     }
  1876.  
  1877.                                     if(TempLine[j] = (Value < 128))
  1878.                                     {
  1879.                                         if(Value)
  1880.                                         {
  1881.                                             ThisError[j - 2]    += 32 * Value;
  1882.  
  1883.                                             NextError[j + 3]    += 12 * Value;
  1884.                                             NextError[j + 1]    += 26 * Value;
  1885.                                             NextError[j - 1]    += 30 * Value;
  1886.                                             NextError[j - 3]    += 16 * Value;
  1887.  
  1888.                                             TempError[j + 2]    += 12 * Value;
  1889.                                             TempError[j    ]    += 26 * Value;
  1890.                                             TempError[j - 2]    += 12 * Value;
  1891.  
  1892.                                             LastError[j + 3]    +=  5 * Value;
  1893.                                             LastError[j + 1]    += 12 * Value;
  1894.                                             LastError[j - 1]    += 12 * Value;
  1895.                                             LastError[j - 3]    +=  5 * Value;
  1896.                                         }
  1897.                                     }
  1898.                                     else
  1899.                                     {
  1900.                                         if(Value = Value - 255)
  1901.                                         {
  1902.                                             ThisError[j - 2]    += 32 * Value;
  1903.  
  1904.                                             NextError[j + 3]    += 12 * Value;
  1905.                                             NextError[j + 1]    += 26 * Value;
  1906.                                             NextError[j - 1]    += 30 * Value;
  1907.                                             NextError[j - 3]    += 16 * Value;
  1908.  
  1909.                                             TempError[j + 2]    += 12 * Value;
  1910.                                             TempError[j    ]    += 26 * Value;
  1911.                                             TempError[j - 2]    += 12 * Value;
  1912.  
  1913.                                             LastError[j + 3]    +=  5 * Value;
  1914.                                             LastError[j + 1]    += 12 * Value;
  1915.                                             LastError[j - 1]    += 12 * Value;
  1916.                                             LastError[j - 3]    +=  5 * Value;
  1917.                                         }
  1918.                                     }
  1919.                                 }
  1920.  
  1921.                                 {
  1922.                                     register LONG *Swap;
  1923.  
  1924.                                     Swap        = ThisError;
  1925.                                     ThisError    = NextError;
  1926.                                     NextError    = TempError;
  1927.                                     TempError    = LastError;
  1928.                                     LastError    = Swap;
  1929.  
  1930.                                     memset(Swap,0,sizeof(LONG) * NewWidth);
  1931.                                 }
  1932.                             }
  1933.  
  1934.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  1935.  
  1936.                             ShowProgress(i);
  1937.                         }
  1938.                     }
  1939.  
  1940.                     DeleteTempRPort(TempRPort);
  1941.                 }
  1942.                 else
  1943.                 {
  1944.                     DeleteBitMap(BitMap);
  1945.  
  1946.                     BitMap = NULL;
  1947.  
  1948.                     Error = ERR_NO_MEM;
  1949.                 }
  1950.             }
  1951.             else
  1952.                 Error = ERR_NO_MEM;
  1953.  
  1954.             DeleteTempLine(TempLine);
  1955.         }
  1956.         else
  1957.             Error = ERR_NO_MEM;
  1958.  
  1959.         FreeVecPooled(Data);
  1960.     }
  1961.     else
  1962.         Error = ERR_NO_MEM;
  1963.  
  1964.     *BitMapPtr = BitMap;
  1965.  
  1966.     return(Error);
  1967. }
  1968.  
  1969. LONG __regargs
  1970. DitherImage_Stucki(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight)
  1971. {
  1972.     struct BitMap    *BitMap    = NULL;
  1973.     LONG         Error    = 0;
  1974.     APTR         Data;
  1975.  
  1976.     if(Data = AllocVecPooled(3 * (NewWidth + 4) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  1977.     {
  1978.         UBYTE *TempLine;
  1979.  
  1980.         if(TempLine = CreateTempLine(NewWidth,1))
  1981.         {
  1982.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  1983.             {
  1984.                 struct RastPort __aligned DummyRPort,*TempRPort;
  1985.  
  1986.                 InitRastPort(&DummyRPort);
  1987.  
  1988.                 DummyRPort . BitMap = BitMap;
  1989.  
  1990.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  1991.                 {
  1992.                     LONG     i,j;
  1993.                     UBYTE    *Line;
  1994.  
  1995.                     WORD    *ThisError,
  1996.                         *NextError,
  1997.                         *LastError,
  1998.                          Value;
  1999.  
  2000.                     ThisError = Data;
  2001.                     NextError = ThisError + NewWidth + 4;
  2002.                     LastError = NextError + NewWidth + 4;
  2003.  
  2004.                     ThisError += 2;
  2005.                     NextError += 2;
  2006.                     LastError += 2;
  2007.  
  2008.                     if(NewHeight == Image -> Height)
  2009.                     {
  2010.                         for(i = 0 ; i < NewHeight ; i++)
  2011.                         {
  2012.                             Line = Image -> Lines[i];
  2013.  
  2014.                             if(i & 1)
  2015.                             {
  2016.                                 for(j = 0 ; j < NewWidth ; j++)
  2017.                                 {
  2018.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 42) > 255)
  2019.                                         Value = 255;
  2020.                                     else
  2021.                                     {
  2022.                                         if(Value < 0)
  2023.                                             Value = 0;
  2024.                                     }
  2025.  
  2026.                                     if(TempLine[j] = (Value < 128))
  2027.                                     {
  2028.                                         if(Value)
  2029.                                         {
  2030.                                             LastError[j + 2]     = Value;
  2031.                                             LastError[j - 2]    += Value;Value += Value;
  2032.                                             LastError[j + 1]    += Value;
  2033.                                             LastError[j - 1]    += Value;
  2034.                                             NextError[j + 2]    += Value;
  2035.                                             NextError[j - 2]    += Value;Value += Value;
  2036.                                             NextError[j + 1]    += Value;
  2037.                                             NextError[j - 1]    += Value;
  2038.                                             LastError[j]        += Value;
  2039.                                             ThisError[j + 2]    += Value;Value += Value;
  2040.                                             NextError[j]        += Value;
  2041.                                             ThisError[j + 1]    += Value;
  2042.                                         }
  2043.                                     }
  2044.                                     else
  2045.                                     {
  2046.                                         if(Value = Value - 255)
  2047.                                         {
  2048.                                             LastError[j + 2]     = Value;
  2049.                                             LastError[j - 2]    += Value;Value += Value;
  2050.                                             LastError[j + 1]    += Value;
  2051.                                             LastError[j - 1]    += Value;
  2052.                                             NextError[j + 2]    += Value;
  2053.                                             NextError[j - 2]    += Value;Value += Value;
  2054.                                             NextError[j + 1]    += Value;
  2055.                                             NextError[j - 1]    += Value;
  2056.                                             LastError[j]        += Value;
  2057.                                             ThisError[j + 2]    += Value;Value += Value;
  2058.                                             NextError[j]        += Value;
  2059.                                             ThisError[j + 1]    += Value;
  2060.                                         }
  2061.                                     }
  2062.                                 }
  2063.  
  2064.                                 {
  2065.                                     register WORD *Swap;
  2066.  
  2067.                                     Swap        = ThisError;
  2068.                                     ThisError    = NextError;
  2069.                                     NextError    = LastError;
  2070.                                     LastError    = Swap;
  2071.  
  2072.                                     Swap[NewWidth - 1] = 0;
  2073.                                     Swap[NewWidth - 2] = 0;
  2074.  
  2075.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2076.                                 }
  2077.                             }
  2078.                             else
  2079.                             {
  2080.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  2081.                                 {
  2082.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 42) > 255)
  2083.                                         Value = 255;
  2084.                                     else
  2085.                                     {
  2086.                                         if(Value < 0)
  2087.                                             Value = 0;
  2088.                                     }
  2089.  
  2090.                                     if(TempLine[j] = (Value < 128))
  2091.                                     {
  2092.                                         if(Value)
  2093.                                         {
  2094.                                             LastError[j - 2]     = Value;
  2095.                                             LastError[j + 2]    += Value;Value += Value;
  2096.                                             LastError[j - 1]    += Value;
  2097.                                             LastError[j + 1]    += Value;
  2098.                                             NextError[j - 2]    += Value;
  2099.                                             NextError[j + 2]    += Value;Value += Value;
  2100.                                             NextError[j - 1]    += Value;
  2101.                                             NextError[j + 1]    += Value;
  2102.                                             LastError[j]        += Value;
  2103.                                             ThisError[j - 2]    += Value;Value += Value;
  2104.                                             NextError[j]        += Value;
  2105.                                             ThisError[j - 1]    += Value;
  2106.                                         }
  2107.                                     }
  2108.                                     else
  2109.                                     {
  2110.                                         if(Value = Value - 255)
  2111.                                         {
  2112.                                             LastError[j - 2]     = Value;
  2113.                                             LastError[j + 2]    += Value;Value += Value;
  2114.                                             LastError[j - 1]    += Value;
  2115.                                             LastError[j + 1]    += Value;
  2116.                                             NextError[j - 2]    += Value;
  2117.                                             NextError[j + 2]    += Value;Value += Value;
  2118.                                             NextError[j - 1]    += Value;
  2119.                                             NextError[j + 1]    += Value;
  2120.                                             LastError[j]        += Value;
  2121.                                             ThisError[j - 2]    += Value;Value += Value;
  2122.                                             NextError[j]        += Value;
  2123.                                             ThisError[j - 1]    += Value;
  2124.                                         }
  2125.                                     }
  2126.                                 }
  2127.  
  2128.                                 {
  2129.                                     register WORD *Swap;
  2130.  
  2131.                                     Swap        = ThisError;
  2132.                                     ThisError    = NextError;
  2133.                                     NextError    = LastError;
  2134.                                     LastError    = Swap;
  2135.  
  2136.                                     Swap[0] = 0;
  2137.                                     Swap[1] = 0;
  2138.  
  2139.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2140.                                 }
  2141.                             }
  2142.  
  2143.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  2144.  
  2145.                             ShowProgress(i);
  2146.                         }
  2147.                     }
  2148.                     else
  2149.                     {
  2150.                         for(i = 0 ; i < NewHeight ; i++)
  2151.                         {
  2152.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  2153.  
  2154.                             if(i & 1)
  2155.                             {
  2156.                                 for(j = 0 ; j < NewWidth ; j++)
  2157.                                 {
  2158.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 42) > 255)
  2159.                                         Value = 255;
  2160.                                     else
  2161.                                     {
  2162.                                         if(Value < 0)
  2163.                                             Value = 0;
  2164.                                     }
  2165.  
  2166.                                     if(TempLine[j] = (Value < 128))
  2167.                                     {
  2168.                                         if(Value)
  2169.                                         {
  2170.                                             LastError[j + 2]     = Value;
  2171.                                             LastError[j - 2]    += Value;Value += Value;
  2172.                                             LastError[j + 1]    += Value;
  2173.                                             LastError[j - 1]    += Value;
  2174.                                             NextError[j + 2]    += Value;
  2175.                                             NextError[j - 2]    += Value;Value += Value;
  2176.                                             NextError[j + 1]    += Value;
  2177.                                             NextError[j - 1]    += Value;
  2178.                                             LastError[j]        += Value;
  2179.                                             ThisError[j + 2]    += Value;Value += Value;
  2180.                                             NextError[j]        += Value;
  2181.                                             ThisError[j + 1]    += Value;
  2182.                                         }
  2183.                                     }
  2184.                                     else
  2185.                                     {
  2186.                                         if(Value = Value - 255)
  2187.                                         {
  2188.                                             LastError[j + 2]     = Value;
  2189.                                             LastError[j - 2]    += Value;Value += Value;
  2190.                                             LastError[j + 1]    += Value;
  2191.                                             LastError[j - 1]    += Value;
  2192.                                             NextError[j + 2]    += Value;
  2193.                                             NextError[j - 2]    += Value;Value += Value;
  2194.                                             NextError[j + 1]    += Value;
  2195.                                             NextError[j - 1]    += Value;
  2196.                                             LastError[j]        += Value;
  2197.                                             ThisError[j + 2]    += Value;Value += Value;
  2198.                                             NextError[j]        += Value;
  2199.                                             ThisError[j + 1]    += Value;
  2200.                                         }
  2201.                                     }
  2202.                                 }
  2203.  
  2204.                                 {
  2205.                                     register WORD *Swap;
  2206.  
  2207.                                     Swap        = ThisError;
  2208.                                     ThisError    = NextError;
  2209.                                     NextError    = LastError;
  2210.                                     LastError    = Swap;
  2211.  
  2212.                                     Swap[NewWidth - 1] = 0;
  2213.                                     Swap[NewWidth - 2] = 0;
  2214.  
  2215.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2216.                                 }
  2217.                             }
  2218.                             else
  2219.                             {
  2220.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  2221.                                 {
  2222.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 42) > 255)
  2223.                                         Value = 255;
  2224.                                     else
  2225.                                     {
  2226.                                         if(Value < 0)
  2227.                                             Value = 0;
  2228.                                     }
  2229.  
  2230.                                     if(TempLine[j] = (Value < 128))
  2231.                                     {
  2232.                                         if(Value)
  2233.                                         {
  2234.                                             LastError[j - 2]     = Value;
  2235.                                             LastError[j + 2]    += Value;Value += Value;
  2236.                                             LastError[j - 1]    += Value;
  2237.                                             LastError[j + 1]    += Value;
  2238.                                             NextError[j - 2]    += Value;
  2239.                                             NextError[j + 2]    += Value;Value += Value;
  2240.                                             NextError[j - 1]    += Value;
  2241.                                             NextError[j + 1]    += Value;
  2242.                                             LastError[j]        += Value;
  2243.                                             ThisError[j - 2]    += Value;Value += Value;
  2244.                                             NextError[j]        += Value;
  2245.                                             ThisError[j - 1]    += Value;
  2246.                                         }
  2247.                                     }
  2248.                                     else
  2249.                                     {
  2250.                                         if(Value = Value - 255)
  2251.                                         {
  2252.                                             LastError[j - 2]     = Value;
  2253.                                             LastError[j + 2]    += Value;Value += Value;
  2254.                                             LastError[j - 1]    += Value;
  2255.                                             LastError[j + 1]    += Value;
  2256.                                             NextError[j - 2]    += Value;
  2257.                                             NextError[j + 2]    += Value;Value += Value;
  2258.                                             NextError[j - 1]    += Value;
  2259.                                             NextError[j + 1]    += Value;
  2260.                                             LastError[j]        += Value;
  2261.                                             ThisError[j - 2]    += Value;Value += Value;
  2262.                                             NextError[j]        += Value;
  2263.                                             ThisError[j - 1]    += Value;
  2264.                                         }
  2265.                                     }
  2266.                                 }
  2267.  
  2268.                                 {
  2269.                                     register WORD *Swap;
  2270.  
  2271.                                     Swap        = ThisError;
  2272.                                     ThisError    = NextError;
  2273.                                     NextError    = LastError;
  2274.                                     LastError    = Swap;
  2275.  
  2276.                                     Swap[0] = 0;
  2277.                                     Swap[1] = 0;
  2278.  
  2279.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2280.                                 }
  2281.                             }
  2282.  
  2283.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  2284.  
  2285.                             ShowProgress(i);
  2286.                         }
  2287.                     }
  2288.  
  2289.                     DeleteTempRPort(TempRPort);
  2290.                 }
  2291.                 else
  2292.                 {
  2293.                     DeleteBitMap(BitMap);
  2294.  
  2295.                     BitMap = NULL;
  2296.  
  2297.                     Error = ERR_NO_MEM;
  2298.                 }
  2299.             }
  2300.             else
  2301.                 Error = ERR_NO_MEM;
  2302.  
  2303.             DeleteTempLine(TempLine);
  2304.         }
  2305.         else
  2306.             Error = ERR_NO_MEM;
  2307.  
  2308.         FreeVecPooled(Data);
  2309.     }
  2310.     else
  2311.         Error = ERR_NO_MEM;
  2312.  
  2313.     *BitMapPtr = BitMap;
  2314.  
  2315.     return(Error);
  2316. }
  2317.  
  2318. LONG __regargs
  2319. DitherImage_BlueNoise(struct BitMap **BitMapPtr,struct GreyImage *Image,LONG NewWidth,LONG NewHeight,WORD NoiseLevel)
  2320. {
  2321.     struct BitMap    *BitMap    = NULL;
  2322.     LONG         Error    = 0;
  2323.     APTR         Data;
  2324.  
  2325.     if(Data = AllocVecPooled(2 * (NewWidth + 2) * sizeof(WORD),MEMF_ANY | MEMF_CLEAR))
  2326.     {
  2327.         UBYTE *TempLine;
  2328.  
  2329.         if(TempLine = CreateTempLine(NewWidth,1))
  2330.         {
  2331.             if(BitMap = CreateBitMap(NewWidth,NewHeight,1,BMF_CLEAR,NULL))
  2332.             {
  2333.                 struct RastPort __aligned DummyRPort,*TempRPort;
  2334.  
  2335.                 InitRastPort(&DummyRPort);
  2336.  
  2337.                 DummyRPort . BitMap = BitMap;
  2338.  
  2339.                 if(TempRPort = CreateTempRPort(&DummyRPort))
  2340.                 {
  2341.                     LONG     i,j;
  2342.                     UBYTE    *Line;
  2343.  
  2344.                     WORD    *ThisError,
  2345.                         *NextError,
  2346.                          Value,
  2347.                          Valu2,
  2348.                          SmallRange,
  2349.                          LargeRange;
  2350.  
  2351.                     SmallRange    = (10 * NoiseLevel) / 100;
  2352.                     LargeRange    = (50 * NoiseLevel) / 100;
  2353.  
  2354.                     if(!SmallRange)
  2355.                         SmallRange = 1;
  2356.  
  2357.                     if(!LargeRange)
  2358.                         LargeRange = 1;
  2359.  
  2360.                     ThisError    = Data;
  2361.                     NextError    = ThisError + NewWidth + 2;
  2362.  
  2363.                     ThisError++;
  2364.                     NextError++;
  2365.  
  2366.                     j = Seed + 16411;
  2367.  
  2368.                     for(i = 0 ; i < NewWidth ; i++)
  2369.                     {
  2370.                         ThisError[i] = ((j & 1023) - 512) / 4;
  2371.  
  2372.                         j += 16411;
  2373.                     }
  2374.  
  2375.                     Seed = j;
  2376.  
  2377.                     if(NewHeight == Image -> Height)
  2378.                     {
  2379.                         for(i = 0 ; i < NewHeight ; i++)
  2380.                         {
  2381.                             Line = Image -> Lines[i];
  2382.  
  2383.                             if(i & 1)
  2384.                             {
  2385.                                 for(j = 0 ; j < NewWidth ; j++)
  2386.                                 {
  2387.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  2388.                                         Value = 255;
  2389.                                     else
  2390.                                     {
  2391.                                         if(Value < 0)
  2392.                                             Value = 0;
  2393.                                     }
  2394.  
  2395.                                     if(TempLine[j] = (Value < 128))
  2396.                                     {
  2397.                                         Value *= 10;
  2398.  
  2399.                                         if(Valu2 = Value + Value)
  2400.                                         {
  2401.                                             NextError[j + 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2402.                                             NextError[j - 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2403.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2404.                                             ThisError[j + 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2405.                                         }
  2406.                                     }
  2407.                                     else
  2408.                                     {
  2409.                                         if(Value = Value - 255)
  2410.                                         {
  2411.                                             Value *= 10;
  2412.                                             Valu2 = Value + Value;
  2413.  
  2414.                                             NextError[j + 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2415.                                             NextError[j - 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2416.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2417.                                             ThisError[j + 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2418.                                         }
  2419.                                     }
  2420.                                 }
  2421.  
  2422.                                 {
  2423.                                     register WORD *Swap;
  2424.  
  2425.                                     Swap        = ThisError;
  2426.                                     ThisError    = NextError;
  2427.                                     NextError    = Swap;
  2428.  
  2429.                                     Swap[NewWidth - 1] = 0;
  2430.  
  2431.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2432.                                 }
  2433.                             }
  2434.                             else
  2435.                             {
  2436.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  2437.                                 {
  2438.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  2439.                                         Value = 255;
  2440.                                     else
  2441.                                     {
  2442.                                         if(Value < 0)
  2443.                                             Value = 0;
  2444.                                     }
  2445.  
  2446.                                     if(TempLine[j] = (Value < 128))
  2447.                                     {
  2448.                                         Value *= 10;
  2449.  
  2450.                                         if(Valu2 = Value + Value)
  2451.                                         {
  2452.                                             NextError[j - 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2453.                                             NextError[j + 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2454.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2455.                                             ThisError[j - 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2456.                                         }
  2457.                                     }
  2458.                                     else
  2459.                                     {
  2460.                                         if(Value = Value - 255)
  2461.                                         {
  2462.                                             Value *= 10;
  2463.  
  2464.                                             NextError[j - 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2465.                                             NextError[j + 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2466.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2467.                                             ThisError[j - 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2468.                                         }
  2469.                                     }
  2470.                                 }
  2471.  
  2472.                                 {
  2473.                                     register WORD *Swap;
  2474.  
  2475.                                     Swap        = ThisError;
  2476.                                     ThisError    = NextError;
  2477.                                     NextError    = Swap;
  2478.  
  2479.                                     *Swap = 0;
  2480.  
  2481.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2482.                                 }
  2483.                             }
  2484.  
  2485.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  2486.  
  2487.                             ShowProgress(i);
  2488.                         }
  2489.                     }
  2490.                     else
  2491.                     {
  2492.                         for(i = 0 ; i < NewHeight ; i++)
  2493.                         {
  2494.                             Line = (UBYTE *)Image -> Lines[(i * Image -> Height) / NewHeight];
  2495.  
  2496.                             if(i & 1)
  2497.                             {
  2498.                                 for(j = 0 ; j < NewWidth ; j++)
  2499.                                 {
  2500.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  2501.                                         Value = 255;
  2502.                                     else
  2503.                                     {
  2504.                                         if(Value < 0)
  2505.                                             Value = 0;
  2506.                                     }
  2507.  
  2508.                                     if(TempLine[j] = (Value < 128))
  2509.                                     {
  2510.                                         Value *= 10;
  2511.  
  2512.                                         if(Valu2 = Value + Value)
  2513.                                         {
  2514.                                             NextError[j + 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2515.                                             NextError[j - 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2516.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2517.                                             ThisError[j + 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2518.                                         }
  2519.                                     }
  2520.                                     else
  2521.                                     {
  2522.                                         if(Value = Value - 255)
  2523.                                         {
  2524.                                             Value *= 10;
  2525.                                             Valu2 = Value + Value;
  2526.  
  2527.                                             NextError[j + 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2528.                                             NextError[j - 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2529.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2530.                                             ThisError[j + 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2531.                                         }
  2532.                                     }
  2533.                                 }
  2534.  
  2535.                                 {
  2536.                                     register WORD *Swap;
  2537.  
  2538.                                     Swap        = ThisError;
  2539.                                     ThisError    = NextError;
  2540.                                     NextError    = Swap;
  2541.  
  2542.                                     Swap[NewWidth - 1] = 0;
  2543.  
  2544.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2545.                                 }
  2546.                             }
  2547.                             else
  2548.                             {
  2549.                                 for(j = NewWidth - 1 ; j >= 0 ; j--)
  2550.                                 {
  2551.                                     if((Value = (WORD)Filter[Line[(j * Image -> Width) / NewWidth]] + ThisError[j] / 16) > 255)
  2552.                                         Value = 255;
  2553.                                     else
  2554.                                     {
  2555.                                         if(Value < 0)
  2556.                                             Value = 0;
  2557.                                     }
  2558.  
  2559.                                     if(TempLine[j] = (Value < 128))
  2560.                                     {
  2561.                                         Value *= 10;
  2562.  
  2563.                                         if(Valu2 = Value + Value)
  2564.                                         {
  2565.                                             NextError[j - 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2566.                                             NextError[j + 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2567.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2568.                                             ThisError[j - 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2569.                                         }
  2570.                                     }
  2571.                                     else
  2572.                                     {
  2573.                                         if(Value = Value - 255)
  2574.                                         {
  2575.                                             Value *= 10;
  2576.                                             Valu2 = Value + Value;
  2577.  
  2578.                                             NextError[j - 1]     = (Value + (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2579.                                             NextError[j + 1]    += (Value - (Seed % SmallRange) -  5) / 10;Seed += 16411;Value += Valu2;
  2580.                                             NextError[j]        += (Value + (Seed % LargeRange) - 25) / 10;Seed += 16411;Value += Valu2;
  2581.                                             ThisError[j - 1]    += (Value - (Seed % LargeRange) - 25) / 10;Seed += 16411;
  2582.                                         }
  2583.                                     }
  2584.                                 }
  2585.  
  2586.                                 {
  2587.                                     register WORD *Swap;
  2588.  
  2589.                                     Swap        = ThisError;
  2590.                                     ThisError    = NextError;
  2591.                                     NextError    = Swap;
  2592.  
  2593.                                     *Swap = 0;
  2594.  
  2595.                                     memset(Swap,0,sizeof(WORD) * NewWidth);
  2596.                                 }
  2597.                             }
  2598.  
  2599.                             (* WriteLine)(&DummyRPort,i,NewWidth,TempLine,TempRPort);
  2600.  
  2601.                             ShowProgress(i);
  2602.                         }
  2603.                     }
  2604.  
  2605.                     DeleteTempRPort(TempRPort);
  2606.                 }
  2607.                 else
  2608.                 {
  2609.                     DeleteBitMap(BitMap);
  2610.  
  2611.                     BitMap = NULL;
  2612.  
  2613.                     Error = ERR_NO_MEM;
  2614.                 }
  2615.             }
  2616.             else
  2617.                 Error = ERR_NO_MEM;
  2618.  
  2619.             DeleteTempLine(TempLine);
  2620.         }
  2621.         else
  2622.             Error = ERR_NO_MEM;
  2623.  
  2624.         FreeVecPooled(Data);
  2625.     }
  2626.     else
  2627.         Error = ERR_NO_MEM;
  2628.  
  2629.     *BitMapPtr = BitMap;
  2630.  
  2631.     return(Error);
  2632. }
  2633.