After further investigation and testing I seem to have isolated the source of the formatting/alignment problem.
It seems to reside with TBitmap.
The function was originally constructed to return a TBitmap.
I removed this and after the glReadPixels loaded the RGBA data from the TBitmap onto a dynamic array.
All seems to work now!
Have listed the revised code below. Thus far tested on intel mac and linux deb 64
type
TMapSizeR=record
Width,
Height :Integer;
end;
TPixelIndicesR=record
R,G,B,A :Byte;
end;
TByteMapR=record
Size:TMapSizeR
ByteMap :array of TPixelIndicesR;
end;
var
ImageMap :TByteMapR;
function OpenGLContextToImageMap(const Left,Top,BMW,BMH:Integer):Boolean;
type
PRGBA = ^TRGBA;
TRGBA = packed record
R,G,B,A:byte;
end;
var
Data,
rData :PByte;
n,
Width,
Height,
ByteWidth,
Size :Integer;
BMP1 :TBitmap;
RGBA :PRGBA;
begin
Result:=False;
Width:=BMW;
Height := BMH;
ByteWidth := Width * 4; // 32 bit pixel format so 4 bytes per pixel
Size := ByteWidth*Height;
try
BMP1 := TBitmap.Create;
BMP1.Width := Width;
BMP1.Height := Height;
BMP1.PixelFormat:=pf32bit;
try
Data := getMem(Size);
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(Left,Top,Width,Height, GL_RGBA, GL_UNSIGNED_BYTE, Data);
BMP1.BeginUpdate();
rData :=BMP1.RawImage.Data;
Move(Data^,rData^,BMP1.RawImage.DataSize);
//load RGBA values to dynamic array
if (Length(ImageMap.ByteMap)>0) then begin Finalize(ImageMap.ByteMap); end;
SetLength(ImageMap.ByteMap,BMP1.Width*BMP1.Height);
ImageMap.Size.Width:=BMP1.Width; ImageMap.Size.Height:=BMP1.Height;
RGBA := pointer(rdata);
for n := 0 to (Width*Height)-1 do
begin
ImageMap.ByteMap[n].R:=RGBA^.R;
ImageMap.ByteMap[n].G:=RGBA^.G;
ImageMap.ByteMap[n].B:=RGBA^.B;
ImageMap.ByteMap[n].A:=RGBA^.A;
inc(RGBA);
end;
//add code to flip image vert and horz
BMP1.EndUpdate();
finally
Freemem(Data);
end;
finally
BMP1.free;
Result:=True;
end;
end;