Basically I'm trying to figure out how I can use the same exact procedure in this Delphi loop, but in Java.
's' is a pointer.
[code] GetMem(s, RawHdr.Width * RawHdr.Height * 4);
try
src.Read(s^, RawHdr.Width * RawHdr.Height * 4);
GetMem(d, RawHdr.Width * RawHdr.Height * 4);
try
dp := d;
// Red
for y := RawHdr.Height -1 downto 0 do
begin
Cardinal(sp) := Cardinal(s) + y*RawHdr.Width*4 + 2;
for x := 0 to RawHdr.Width - 1 do
begin
dp^ := sp^;
Inc(dp);
Inc(sp, 4);
end;
end;
// Green
for y := RawHdr.Height -1 downto 0 do
begin
Cardinal(sp) := Cardinal(s) + y*RawHdr.Width*4 + 1;
for x := 0 to RawHdr.Width - 1 do
begin
dp^ := sp^;
Inc(dp);
Inc(sp, 4);
end;
end;
// Blue
for y := RawHdr.Height -1 downto 0 do
begin
Cardinal(sp) := Cardinal(s) + y*RawHdr.Width*4;
for x := 0 to RawHdr.Width - 1 do
begin
dp^ := sp^;
Inc(dp);
Inc(sp, 4);
end;
end;
// Alpha
for y := RawHdr.Height -1 downto 0 do
begin
Cardinal(sp) := Cardinal(s) + y*RawHdr.Width*4 + 3;
for x := 0 to RawHdr.Width - 1 do
begin
dp^ := sp^;
Inc(dp);
Inc(sp, 4);
end;
end;
dst.Write(d^, RawHdr.Width * RawHdr.Height * 4);[/code]
Basically what I need to do is be able to figure out this but in the language Java:
[code] // Green
for y := RawHdr.Height -1 downto 0 do
begin
Cardinal(sp) := Cardinal(s) + y*RawHdr.Width*4 + 1;
for x := 0 to RawHdr.Width - 1 do
begin
dp^ := sp^;
Inc(dp);
Inc(sp, 4);
end;
end;[/code]
Since I have no idea what these functions are, or the operations used.. I'm stumped.
[code]Cardinal(sp) = Cardinal(s) + y * RawHdr.Width * 4 + 1;[/code]
Cardinal(s) is a cast; they're casting the image pointer to an unsigned long, the purpose of which is to allow calculating the position of the scanline in memory.
[code]dp^ = sp^;[/code]
Both dp and sp are pointers. A caret behind a pointer is a dereference; this statement means that the value where sp points is copied to where dp points.
[code]Inc(dp);
Inc(sp, 4);[/code]
Increment
I'm not sure how you'd replicate this kind of code in Java since Java has no pointers. Probably with arrays.
[editline]16th August 2012[/editline]
This is the equivalent C code with single-dimensional arrays instead of pointers.
[cpp]// Red
for (y = RawHdr.Height - 1; y >= 0; y--)
{
i = y * RawHdr.Width * 4 + 2;
for (x = 0; x < RawHdr.Width; x++)
{
d[j] = s[i];
j++;
i += 4;
}
}[/cpp]
[QUOTE=ThePuska;37266852][code]Cardinal(sp) = Cardinal(s) + y * RawHdr.Width * 4 + 1;[/code]
Cardinal(s) is a cast; they're casting the image pointer to an unsigned long, the purpose of which is to allow calculating the position of the scanline in memory.
[code]dp^ = sp^;[/code]
Both dp and sp are pointers. A caret behind a pointer is a dereference; this statement means that the value where sp points is copied to where dp points.
[code]Inc(dp);
Inc(sp, 4);[/code]
Increment
I'm not sure how you'd replicate this kind of code in Java since Java has no pointers. Probably with arrays.
[editline]16th August 2012[/editline]
This is the equivalent C code with single-dimensional arrays instead of pointers.
[cpp]// Red
for (y = RawHdr.Height - 1; y >= 0; y--)
{
i = y * RawHdr.Width * 4 + 2;
for (x = 0; x < RawHdr.Width; x++)
{
d[j] = s[i];
j++;
i += 4;
}
}[/cpp][/QUOTE]
Thank you very much, your C code equivalent helped me figure it out.
Here's what I got that worked:
[code] int channels = 4;
int length = width * height;
int[] pixels = new int[length * channels];
for (int i = 0; i < pixels.length; i += channels) {
pixels[i + 2] = readByte();
pixels[i + 1] = readByte();
pixels[i] = readByte();
pixels[i + 3] = readByte();
}[/code]
The pixel format is BGRA.
Sorry, you need to Log In to post a reply to this thread.