ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Convert Python to Pascal
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

yogiyang

India
727 Posts

Posted - Jan 26 2019 :  07:14:26  Show Profile  Reply
Hello,

I have this code snippet in python which I have picked up from a script that is designed to run in GIMP and this script is working as documented in GIMP:
MyVal = (v-min) * 1.0/(max-min)

I have coded the same as follows in Delphi:
MyVal := Trunc((PixVal - Mini) * (1.0/(Maxi - Mini)));

But the code of Pascal always return zero (0)!

Here the values of PixVal = 122, Mini = 122, Max = 16712448

Any idea as to how to code this properly?

TIA


Yogi Yang

yogiyang

India
727 Posts

Posted - Jan 26 2019 :  07:16:40  Show Profile  Reply
Hello,

The whole Python script is:

function get_min_max()
    min, max = 1000, -1000          
    for y=0, height-1 do
      for x=0, width-1 do
        value = get_value(x,y)
        if value<min then
          min = value
        end
        if value>max then
          max = value
        end
      end  
    end
    return min,max
end

function remap(v, min, max)
    return (v-min) * 1.0/(max-min)
end

function cs_get_rgb(x,y,min,max)
    r,g,b = get_rgb(x,y)
    r = remap(r, min, max)
    g = remap(g, min, max)
    b = remap(b, min, max)
    return r,g,b
end

function contrast_stretch()
    min, max = get_min_max()

    for y=0, height do
      for x=0, width do
        set_rgb(x,y, cs_get_rgb(x,y,min,max))
      end
    end
    flush ()
end

contrast_stretch()



Yogi Yang
Go to Top of Page

yogiyang

India
727 Posts

Posted - Jan 26 2019 :  07:51:07  Show Profile  Reply
Hello,

Here is the Pascal code that I have managed to convert from Python:

procedure GetMinMax(out PixMin, PixMax: Integer);
var
  X, Y, PixClr: integer;
  Clr: TRGB;
begin
  PixMin := 1000;
  PixMax := -1000;

  for Y := 0 to ImageEnViewMain.IEBitmap.Height - 1 do
  begin
    for X := 0 to ImageEnViewMain.IEBitmap.Width - 1 do
    begin
      try
        Clr := ImageEnViewMain.IEBitmap.Pixels[X, Y];
        PixClr := TRGB2TColor(Clr);

        if PixClr < PixMin then
          PixMin := PixClr;

        if PixClr > PixMax then
          PixMax := PixClr;
      except on E: Exception do
        ShowMessage(e.Message);
      end;

    end;
  end;
end;

function RemapPix(PixVal: Byte; Mini, Maxi: Integer): Integer;
var
  FinalVal: Real;
begin
  Result := Trunc((PixVal - Mini) * (1.0/(Maxi - Mini)));
end;

procedure GetRGB(const X, Y, Mini, Maxi: Integer; out R, G, B: Byte);
begin
  R := ImageEnViewMain.IEBitmap.Pixels[X, Y].r;
  G := ImageEnViewMain.IEBitmap.Pixels[X, Y].g;
  B := ImageEnViewMain.IEBitmap.Pixels[X, Y].b;

  R := RemapPix(R, Mini, Maxi);
  G := RemapPix(G, Mini, Maxi);
  B := RemapPix(B, Mini, Maxi);

end;

procedure ContrastStretch;
var
  PixMin, PixMax: Integer;
  X, Y, PixClr: integer;
  R, G, B: Byte;
begin
  GetMinMax(PixMin, PixMax);

  for Y := 0 to ImageEnViewMain.IEBitmap.Height - 1 do
  begin
    for X := 0 to ImageEnViewMain.IEBitmap.Width - 1 do
    begin
      try
        GetRGB(X, Y, PixMin, PixMax, R, G, B);
        ImageEnViewMain.IEBitmap.Pixels[X, Y] := CreateRGB(R, G, B);
      except on E: Exception do
        ShowMessage(e.Message);
      end;
    end;
  end;
  ImageEnViewMain.Update;
end;



Yogi Yang
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: