Problem to convert some images to JPG: Delphi code

Discussions on GFL SDK, the graphic library for reading and writing graphic files

Moderators: XnTriq, helmut, xnview

Post Reply
Nols Smit
Posts: 16
Joined: Mon Jun 27, 2005 6:38 pm

Problem to convert some images to JPG: Delphi code

Post by Nols Smit »

In the code shown below, I have a problem to convert some images to JPG. I get error 007 when executing the statement:
f := gflSaveBitmap(PChar(ThisFileName),gfl_bmp^, sp);

(Also note that the variable finfo.NumberOfImages give the file size).


procedure TForm1.btnRunClick(Sender: TObject);
var
i,j:integer;
NewFileName, ThisFileName, filename: string;
finfo: TGFL_FILE_INFORMATION;
lp: TGFL_LOAD_PARAMS;
sp: TGFL_SAVE_PARAMS;
gfl_bmp, gfm_bmp: PGFL_BITMAP;
e,f: GFL_ERROR;
format, file_extention: string;
width, new_width, height, new_height, size: cardinal;
Normal_Cursor: TCursor;

begin
Normal_Cursor := Screen.Cursor;
Screen.Cursor := crHourglass;

gflEnableLZW(GFL_TRUE);

try
if not DirectoryExists(edtSubFolder.text) then
MkDir(edtSubFolder.text);
if edtSubFolder.Text <>'' then
begin
For i := 0 to FileListBox1.Items.Count - 1 do
begin
if FileListBox1.Selected then
begin
filename := ExtractFileName(FileListBox1.Items.Strings);
file_extention := ExtractFileExt(filename);

If FileListBox1.FileName <> '' then
begin
gflGetDefaultLoadParams(lp);

e := gflLoadBitmap(Pchar(filename), gfl_bmp, lp, finfo);
if (e <> gfl_no_error) then
begin
MessageDlg('File not readable: ' + string(gflGetErrorString(e)), mtError, [mbOK], 0);
exit;
end;

width := finfo.Width;
height := finfo.Height;
size := finfo.NumberOfImages;

if width > height then
begin
new_width := StrToInt(rgrpSize.Items[rgrpSize.ItemIndex]);
new_height := Round((new_width / width) * height);
end
else
begin
new_height := StrToInt(rgrpSize.Items[rgrpSize.ItemIndex]);
new_width := Round((new_height / height) * width);
end;

gflResize(gfl_bmp, nil, new_width, new_height, GFL_RESIZE_BILINEAR, 0);

case rgrpFormat.ItemIndex of
0: j := 0; {jpg}
1: j := 8; {png}
2: j := 4; {bmp}
3: j := 2; {tif}
end;

format := gflGetDefaultFormatSuffixByIndex(j);
gflGetDefaultSaveParams(sp);
sp.FormatIndex := j;
if j = 0 then
sp.Quality := 100;
if j = 1 then
sp.Compression := 7;

if file_extention = '' then
j := 1000
else
j := Pos(file_extention, filename);

ThisFileName := DirectoryListBox1.Directory + '\' + edtSubFolder.Text + '\' +
Copy(filename,1,j-1) + '.' + format;

f := gflSaveBitmap(PChar(ThisFileName),gfl_bmp^, sp);
if (f <> gfl_no_error) then
begin
MessageDlg('File not saveable: ' + string(gflGetErrorString(f)), mtError, [mbOK], 0);
exit;
end;
gflFreeBitmap(gfl_bmp);
end;
end;
end;
end;
finally
Screen.Cursor := Normal_Cursor;
end;
User avatar
xnview
Author of XnView
Posts: 43357
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Problem to convert some images to JPG: Delphi code

Post by xnview »

Nols Smit wrote:In the code shown below, I have a problem to convert some images to JPG. I get error 007 when executing the statement:
f := gflSaveBitmap(PChar(ThisFileName),gfl_bmp^, sp);
Could you show me the picture with the problem of saving in jpeg??
Pierre.
Nols Smit
Posts: 16
Joined: Mon Jun 27, 2005 6:38 pm

Images unable to save as JPG files

Post by Nols Smit »

I've put four images on the following two ftp servers:

ftp://nols.ftp4me.com
ftp://196.33.85.17


They accep Anonymous logins


Regards,

Nols Smit
User avatar
xnview
Author of XnView
Posts: 43357
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Images unable to save as JPG files

Post by xnview »

Nols Smit wrote:I've put four images on the following two ftp servers:

ftp://nols.ftp4me.com
ftp://196.33.85.17


They accep Anonymous logins
It's normal, you must convert your 8bits pictures in 24bits.
Pierre.
Nols Smit
Posts: 16
Joined: Mon Jun 27, 2005 6:38 pm

Re: Images unable to save as JPG files

Post by Nols Smit »

xnview wrote:
Nols Smit wrote:I've put four images on the following two ftp servers:

ftp://nols.ftp4me.com
ftp://196.33.85.17


They accep Anonymous logins
It's normal, you must convert your 8bits pictures in 24bits.
If I change the coding:

if j = 0 then
begin
sp.Quality := 100;
sp.Flags := GFL_SAVE_ANYWAY; {<----- new line}
end;

Then it seems OK. Am I on the right track?


Regards,

Nols Smit
User avatar
xnview
Author of XnView
Posts: 43357
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Images unable to save as JPG files

Post by xnview »

Nols Smit wrote: sp.Flags := GFL_SAVE_ANYWAY; {<----- new line}
Yes, now the pciture is converted if depth can be saved with this format.
Pierre.
Post Reply