PDA

View Full Version : loading jpg/gif/bmp without VCL



virtual
27-04-2009, 08:01 PM
hi

i've tryed translating cpp function to pascal , but it dosen't work
the function is simply convert jpeg file to HBITMAP Object , using IPicture interface

the cpp code work fine , could any one correct my pascal translation !!

// CPP CODE


HBITMAP Ole2BITMAP(char *szFile)
{

HBITMAP hBitmap;
HBITMAP bmp;
DWORD dwFileSize;
DWORD dwBytesRead;
HANDLE hFile;
LPPICTURE gpPicture;
LPVOID lpPicData;
LPVOID pvData;
LPSTREAM pstm;
LPVOID hrlp;
HGLOBAL hGlobal;
BITMAP bm ;



hFile=CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_E XISTING,0,NULL);
dwFileSize=GetFileSize(hFile,NULL);


pvData=NULL;
hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
pvData=GlobalLock(hGlobal);
ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);


CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
OleLoadPicture(pstm,0,FALSE,IID_IPicture,(void**)&gpPicture);
pstm->Release();
gpPicture->get_Handle((OLE_HANDLE*)&hBitmap);

GetObject (hBitmap, sizeof (BITMAP), (PSTR) &bm) ;



bmp = (HBITMAP)CopyImage(hBitmap,IMAGE_BITMAP,bm.bmWidth , bm.bmHeight,LR_COPYRETURNORG);
return bmp;

}



// PASCAL TRANSLATION



uses activex
function ole2bitmap(name : string): HBITMAP;
var hBmp ,bmp : HBITMAP;
dwFileSize,dwBytesRead : DWORD;
hFile : integer;
gpPicture : IPicture;
pvData : pointer;
pstm : ISTREAM;
hGlob : hGlobal;
bm : BITMAP;
begin

hFile := CreateFile(Pchar(name),GENERIC_READ,0,NIL,OPEN_EXI STING,0,0);
dwFileSize :=GetFileSize(hFile,NIL);

pvData := NIL;
hGlob := GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
pvData:=GlobalLock(hGlob);
ReadFile(hFile,pvData,dwFileSize,dwBytesRead,NIL);
GlobalUnlock(hGlob);
CloseHandle(hFile);

CreateStreamOnHGlobal(hGlob,TRUE,pstm);
OleLoadPicture(pstm,0,FALSE,IPICTURE,gpPicture);
pstm._Release();
gpPicture.get_Handle(LongWord(hBmp));

GetObject (hBmp, sizeof (BITMAP), @bm) ;

bmp := CopyImage(hBmp,IMAGE_BITMAP,bm.bmWidth, bm.bmHeight,LR_COPYRETURNORG);

Result := bmp;

end;


thanks

noeska
27-04-2009, 08:33 PM
Try this one:


{------------------------------------------------------------------}
{ Load BMP Texture }
{------------------------------------------------------------------}
function LoadBMPTexture(Filename: Tstream; var Texture: GLuint): Boolean;
var
FileHeader: TBITMAPFILEHEADER;
InfoHeader: TBITMAPINFOHEADER;
Palette: array of RGBQUAD;
BitmapLength: LongWord;
PaletteLength: LongWord;
ReadBytes: LongWord;
TextureImage: TTextureImage;
begin
LoadBMPTexture:=False;
// Get header information
Filename.Read(FileHeader, SizeOf(FileHeader));
Filename.Read(InfoHeader, SizeOf(InfoHeader));
// Get palette
PaletteLength := InfoHeader.biClrUsed;
SetLength(Palette, PaletteLength);
ReadBytes:=Filename.Read(Palette,PaletteLength);
if (ReadBytes <> PaletteLength) then
begin
MessageBox(0, PChar('Error reading palette'), PChar('BMP Loader'), MB_OK);
Exit;
end;
textureimage.Width := InfoHeader.biWidth;
textureimage.Height := InfoHeader.biHeight;
BitmapLength := InfoHeader.biSizeImage;
if BitmapLength = 0 then
BitmapLength := Textureimage.Width * Textureimage.Height * InfoHeader.biBitCount Div 8;
// Get the actual pixel data
GetMem(textureimage.imageData, BitmapLength);
ReadBytes:=Filename.Read(textureimage.imagedata^, BitmapLength);
if (ReadBytes <> BitmapLength) then
begin
MessageBox(0, PChar('Error reading bitmap data'), PChar('BMP Unit'), MB_OK);
Exit;
end;
//BMP files are stored BGR so use GL_BGR
Texture:=CreateTexture(textureimage.width, textureimage.height, GL_BGR, textureimage.imagedata);
//clean up
FreeMem(textureimage.imageData)
end;


You may want to remove opengl specifcs like the call to CreateTexture.

virtual
28-04-2009, 10:32 AM
thanks noeska

but i am looking for JPG
because i belive that this is the easy way to load jpg using pure API
and this function keep my exe small ~ 20kb .

so any one have an idea how to fix this function ?!

arthurprs
28-04-2009, 12:52 PM
::)looks suspicious :D

anyway,
i don't have delphi to test,
but try removing "pstm._realease();", instead just set "pstm := nil;"

virtual
29-04-2009, 08:04 PM
the problem is in : gpPicture.get_Handle(LongWord(hBmp)); the program crash here

arthurprs
29-04-2009, 08:07 PM
the problem is in : gpPicture.get_Handle(LongWord(hBmp)); the program crash here

try
gpPicture.get_Handle(LongWord(@hBmp));

JernejL
30-04-2009, 10:20 PM
i't suggest you use vampyre imaging lib, it loads jpg, bmp and gif with no vcl.