Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
214 Beiträge
 
#22

AW: Floyd-Steinberg Dithering

  Alt 20. Okt 2023, 17:27
[QUOTE=himitsu;1528459]
Please be aware of that the values in Delta (R,G,B) may be negative.
Assume AP.Blue=200 and Delta.B=-10 and Factor=7.
Then V will evaluated as
V := Blue + ((B * Factor) shr 4);
V := 200 + ((-10 * 7) shr 4);
V := 200 + (-70 shr 4);
V := 200 + 268435451;
V := 268435651;
Korrekt is
V := Blue + ((B * Factor) div 16);
V := 200 + ((-10 * 7) div 16);
V := 200 + (-70 div 16);
V := 200 + -4;
V := 196;
Thank you, and i was wrong, it is a mistake, as i was after the speed not the functionality per se, and that is wrong, i was aware of of negative values, but was after proving a different thing.

So here fixed a function that will generate the right image, (well i hope so this time )
Code:
  procedure SetPixel(XOffset, YOffset: NativeInt; Factor: NativeUInt);
  var
    AP: TPBGR;
    v: Int16;
  begin
    AP := P;
    Inc(AP, XOffset);
    Inc(NativeInt(AP), YOffset);
    with AP^ do
    begin
      v := Int16(Blue) + Int16(Delta.B) * Uint16(Factor) shr 4;
      if v < 0 then
        Blue := 0
      else if v > 255 then
        Blue := 255
      else
        Blue := v;

      v := Int16(Green) + Int16(Delta.G) * Uint16(Factor) shr 4;
      if v < 0 then
        Green := 0
      else if v > 255 then
        Green := 255
      else
        Green := v;

      v := Int16(Red) + Int16(Delta.R) * Uint16(Factor) shr 4;
      if v < 0 then
        Red := 0
      else if v > 255 then
        Red := 255
      else
        Red := v;
    end;
  end;
and that shows another inefficiency in the compiler as we can't control the size of the operation and it will insist on using what ever get in its little brain, so i had to use (U)Int16 to force the compiler to use iMul, losing speed in the process.
This can be fixed but will need change the structure of the data in overall, but this still faster though we went from Native(U)Int instructions to much slower 16 bit instructions.
  Mit Zitat antworten Zitat