Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Translate a C code to Delphi

hadipardishadipardis Member Posts: 134
Hi, I want to translate the following C code into Delphi:

[code]
unsigned int Interpolate(unsigned int *pixels)
{
int x;
unsigned int ret;
float temp;

temp = (pixels[x] >> 16) & 0xff;
ret = temp;
}
[/code]

Delphi Code:
[code]
function Interpolate(pixels:PByte):byte;
var x:integer;
ret:byte;
temp:double;
p:PByte;
begin
p:=pointer(integer(pixels)+x);
temp:=(p^ shr 16) AND $ff;
ret:=floor(temp);
end;
[/code]

But is it true? Thanks

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi, I want to translate the following C code into Delphi:
    :
    : [code]
    : unsigned int Interpolate(unsigned int *pixels)
    : {
    : int x;
    : unsigned int ret;
    : float temp;
    :
    : temp = (pixels[x] >> 16) & 0xff;
    : ret = temp;
    : }
    : [/code]
    :
    : Delphi Code:
    : [code]
    : function Interpolate(pixels:PByte):byte;
    : var x:integer;
    : ret:byte;
    : temp:double;
    : p:PByte;
    : begin
    : p:=pointer(integer(pixels)+x);
    : temp:=(p^ shr 16) AND $ff;
    : ret:=floor(temp);
    : end;
    : [/code]
    :
    : But is it true? Thanks
    :
    :
    Almost, the return value of the function and the variable ret are Cardinals.
  • hadipardishadipardis Member Posts: 134
    : : Hi, I want to translate the following C code into Delphi:
    : :
    : : [code]
    : : unsigned int Interpolate(unsigned int *pixels)
    : : {
    : : int x;
    : : unsigned int ret;
    : : float temp;
    : :
    : : temp = (pixels[x] >> 16) & 0xff;
    : : ret = temp;
    : : }
    : : [/code]
    : :
    : : Delphi Code:
    : : [code]
    : : function Interpolate(pixels:PByte):byte;
    : : var x:integer;
    : : ret:byte;
    : : temp:double;
    : : p:PByte;
    : : begin
    : : p:=pointer(integer(pixels)+x);
    : : temp:=(p^ shr 16) AND $ff;
    : : ret:=floor(temp);
    : : end;
    : : [/code]
    : :
    : : But is it true? Thanks
    : :
    : :
    : Almost, the return value of the function and the variable ret are Cardinals.
    :
    This code must read a pixel in a 32bit pixel format image. For example to obtain the B value of a pixel we use this:
    [code]
    BValue=pixels[x] >> 16) & 0xff;
    GValue=pixels[x] >> 8) & 0xff;
    [/code]
    But can we still use PByte ?
  • zibadianzibadian Member Posts: 6,349
    : : : Hi, I want to translate the following C code into Delphi:
    : : :
    : : : [code]
    : : : unsigned int Interpolate(unsigned int *pixels)
    : : : {
    : : : int x;
    : : : unsigned int ret;
    : : : float temp;
    : : :
    : : : temp = (pixels[x] >> 16) & 0xff;
    : : : ret = temp;
    : : : }
    : : : [/code]
    : : :
    : : : Delphi Code:
    : : : [code]
    : : : function Interpolate(pixels:PByte):byte;
    : : : var x:integer;
    : : : ret:byte;
    : : : temp:double;
    : : : p:PByte;
    : : : begin
    : : : p:=pointer(integer(pixels)+x);
    : : : temp:=(p^ shr 16) AND $ff;
    : : : ret:=floor(temp);
    : : : end;
    : : : [/code]
    : : :
    : : : But is it true? Thanks
    : : :
    : : :
    : : Almost, the return value of the function and the variable ret are Cardinals.
    : :
    : This code must read a pixel in a 32bit pixel format image. For example to obtain the B value of a pixel we use this:
    : [code]
    : BValue=pixels[x] >> 16) & 0xff;
    : GValue=pixels[x] >> 8) & 0xff;
    : [/code]
    : But can we still use PByte ?
    :
    The argument is a PCardinal (sorry I missed that the first time).
    I would use a different method, which is much saver and easier to understand:
    [code]
    type
    PRGB32 = ^TRGB32;
    TRGB32 = packed record
    Alpha, Red, Blue, Green: byte;
    end;

    function Interpolate(pixels: PCardinal):byte;
    var
    P: PRGB32;
    x: integer;
    ret:byte;
    begin
    p := PRGB32(integer(pixels)+x);
    ret := P^.Red; // Change Red into the color you want.
    end;
    [/code]
    This type-casts the 4-byte integer into a 4-byte record, and then you can use the fields of the record to access the colors. It is much faster and easier to code. The only problem is the order of the colors. I'm not sure if that's correct. You can easily check that out by sending pure red/green/blue into the function and watch the P in the debugger.
  • hadipardishadipardis Member Posts: 134
    : : : : Hi, I want to translate the following C code into Delphi:
    : : : :
    : : : : [code]
    : : : : unsigned int Interpolate(unsigned int *pixels)
    : : : : {
    : : : : int x;
    : : : : unsigned int ret;
    : : : : float temp;
    : : : :
    : : : : temp = (pixels[x] >> 16) & 0xff;
    : : : : ret = temp;
    : : : : }
    : : : : [/code]
    : : : :
    : : : : Delphi Code:
    : : : : [code]
    : : : : function Interpolate(pixels:PByte):byte;
    : : : : var x:integer;
    : : : : ret:byte;
    : : : : temp:double;
    : : : : p:PByte;
    : : : : begin
    : : : : p:=pointer(integer(pixels)+x);
    : : : : temp:=(p^ shr 16) AND $ff;
    : : : : ret:=floor(temp);
    : : : : end;
    : : : : [/code]
    : : : :
    : : : : But is it true? Thanks
    : : : :
    : : : :
    : : : Almost, the return value of the function and the variable ret are Cardinals.
    : : :
    : : This code must read a pixel in a 32bit pixel format image. For example to obtain the B value of a pixel we use this:
    : : [code]
    : : BValue=pixels[x] >> 16) & 0xff;
    : : GValue=pixels[x] >> 8) & 0xff;
    : : [/code]
    : : But can we still use PByte ?
    : :
    : The argument is a PCardinal (sorry I missed that the first time).
    : I would use a different method, which is much saver and easier to understand:
    : [code]
    : type
    : PRGB32 = ^TRGB32;
    : TRGB32 = packed record
    : Alpha, Red, Blue, Green: byte;
    : end;
    :
    : function Interpolate(pixels: PCardinal):byte;
    : var
    : P: PRGB32;
    : x: integer;
    : ret:byte;
    : begin
    : p := PRGB32(integer(pixels)+x);
    : ret := P^.Red; // Change Red into the color you want.
    : end;
    : [/code]
    : This type-casts the 4-byte integer into a 4-byte record, and then you can use the fields of the record to access the colors. It is much faster and easier to code. The only problem is the order of the colors. I'm not sure if that's correct. You can easily check that out by sending pure red/green/blue into the function and watch the P in the debugger.
    :
    Thanks. But my code doesn't work. To be sure, could you please it?

    [code]
    unsigned int Interpolate(unsigned int *pixels,int sizex,int sizey,float x, float y)
    {
    int xi,yi;
    unsigned int ret;
    float xf,yf,xf1,yf1,temp;


    if(x < 0) x = 0; else if(x > sizex - 1) x = sizex - 1;
    if(y < 0) y = 0; else if(y > sizey - 1) y = sizey - 1;
    xf1 = 1.0 - (xf = x - (xi = floor(x)));
    yf1 = 1.0 - (yf = y - (yi = floor(y)));


    temp = (((pixels[yi * sizex + xi] >> 16) & 0xff) * xf1 + ((pixels[yi * sizex + xi + 1] >> 16) & 0xff) * xf) * yf1 +
    (((pixels[(yi + 1) * sizex + xi] >> 16) & 0xff) * xf1 + ((pixels[(yi + 1) * sizex + xi + 1] >> 16) & 0xff) * xf) * yf + 0.5;
    ret = temp;

    temp = (((pixels[yi * sizex + xi] >> 8) & 0xff) * xf1 + ((pixels[yi * sizex + xi + 1] >> 8) & 0xff) * xf) * yf1 +
    (((pixels[(yi + 1) * sizex + xi] >> 8) & 0xff) * xf1 + ((pixels[(yi + 1) * sizex + xi + 1] >> 8) & 0xff) * xf) * yf;
    ret = (ret << 8) + temp;

    temp = ((pixels[yi * sizex + xi] & 0xff) * xf1 + (pixels[yi * sizex + xi + 1] & 0xff) * xf) * yf1 +
    ((pixels[(yi + 1) * sizex + xi] & 0xff) * xf1 + (pixels[(yi + 1) * sizex + xi + 1] & 0xff) * xf) * yf + 0.5;
    ret = (ret << 8) + temp;

    return ret;
    }
    [/code]

    And my Delphi version:
    [code]
    function Interpolate(pixels:Pcardinal;sizex,sizey:integer;x,y:double):byte;
    var
    xi,yi:integer;
    ret:byte;
    xf,yf,xf1,yf1,temp:double;
    P0,P1,P2,P3:PRGB32;
    begin

    if(x < 0) then x := 0
    else if(x > sizex - 1) then x := sizex - 1;
    if(y < 0) then y := 0
    else if(y > sizey - 1) then y := sizey - 1;

    xi:=floor(x); xf:= x - xi;
    xf1 := 1.0 - xf;
    y:=floor(y); yf:= y - yi;
    yf1 := 1.0 - yf;


    P0 := PRGB32(integer(pixels)+ yi * sizex + xi);
    P1 := PRGB32(integer(pixels)+ yi * sizex + xi +1);
    P2 := PRGB32(integer(pixels)+ (yi + 1) * sizex + xi);
    P3 := PRGB32(integer(pixels)+ (yi + 1) * sizex + xi + 1);

    temp := ((P0^.red * xf1 + P1^.red * xf) * yf1 +
    (P2^.red * xf1 + P3^.red * xf) * yf + 0.5;
    ret = temp;

    // like above ....

    result:=ret;
    end;
    [/code]

    But what is the wrong?
Sign In or Register to comment.