unit loadjpegorbmp;

interface

uses windows,jpeg,graphics,sysutils,extctrls,global;
procedure loadjpegbmp(fname : string;var resulted : tbitmap);
function errorwithbitmap(msg : string;artist, album : string) : tbitmap;
procedure initerrorbitmaps;


implementation
uses main;

var
  errorbitmap_noimage : tbitmap;
  errorbitmap_corrupt : tbitmap;

procedure initerrorbitmaps;
begin
errorbitmap_noimage := tbitmap.create;
errorbitmap_noimage:=errorwithbitmap('Image File Not Found','','');
errorbitmap_corrupt := tbitmap.create;
errorbitmap_noimage:=errorwithbitmap('Corrupt picture file','','');
end;

function errorwithbitmap(msg : string;artist, album : string) : tbitmap;
const
wt=170;
var
img : timage;
begin
result:=tbitmap.create;
img:=timage.create(nil);
img.width:=wt;
img.height:=wt;
loadbitmap(img,'NOIMAGE');
alphablendbitmapwithcolor(img.picture.bitmap,clappworkspace,img.picture.bitmap,0.3);
img.canvas.font.name:=JUKEBOXFONT;
img.canvas.font.size:=10;
img.canvas.font.color:=clWhite;
img.canvas.brush.style:=bsClear;
img.canvas.font.size:=8;
img.canvas.Textout((wt div 2)-((result.canvas.textwidth(msg)) div 2),wt div 2,msg);
if length(artist)>20 then begin artist:=copy(artist,1,25);artist:=artist+'...';end;
if length(album)>20 then begin album:=copy(album,1,25);album:=album+'...';end;
if artist<>'' then begin
img.canvas.Textout((wt div 2)-((result.canvas.textwidth(artist)) div 2),(wt div 2) + result.canvas.textheight(msg)*2,artist);
end;
if album<>'' then begin
img.canvas.Textout((wt div 2)-((result.canvas.textwidth(album)) div 2),(wt div 2) + result.canvas.textheight(album)*3,album);
end;

result.assign(img.picture.bitmap);
img.free;
end;


function maketempname(fn : string) : string;
var
i : integer;
begin
result:='';
for i:=1 to length(fn) do begin
if (fn[i]='\') or (fn[i]=':') then result:=result+'0' else result:=result+fn[i];
end;

end;


function docachedcheckandcopy(fn,ext : string) : string;
var
nfn : string;
a,b : tdatetime;
begin


if length(fn)>255 then nfn:=copy(nfn,length(nfn),255) else nfn:=fn;

nfn:=maketempname(nfn);


nfn:=cachepath+'\'+nfn+'.'+ext;
if DONTCHECKCACHE=false then begin

a:=FileAge(nfn);
b:=FileAge(fn+'\title.'+ext);

   if fileexists(nfn) then begin if (a<>b) then copyfile(pchar(fn+'\title.'+ext),pchar(nfn),false); end
                        else copyfile(pchar(fn+'\title.'+ext),pchar(nfn),false);
end;

result:=nfn;
end;



procedure loadjpegbmp(fname : string;var resulted : tbitmap);
var
JPEG : TJpegImage;
res : Tbitmap;
error : integer;
artist,album : string;
fn: string;
begin

error := 1;
res:=Tbitmap.create;
JPEG:=TJpegImage.Create;



if fileexists(fname+'\title.jpg') then begin
try
if CACHED=true then fn:=docachedcheckandcopy(fname,'jpg') else fn:=fname+'\title.jpg';
jpeg.loadfromfile(fn);
res.assign(jpeg);
error:=0;
except
error:=2;
end;
end;

if error>0 then if fileexists(fname+'\title.jpeg') then begin
try
if CACHED=true then fn:=docachedcheckandcopy(fname,'jpeg') else fn:=fname+'\title.jpeg';
jpeg.loadfromfile(fn);
res.assign(jpeg);
error:=0;
except
error:=2;
end;
end;


if fileexists(fname+'\folder.jpg') then begin
try
if CACHED=true then fn:=docachedcheckandcopy(fname,'jpg') else fn:=fname+'\folder.jpg';
jpeg.loadfromfile(fn);
res.assign(jpeg);
error:=0;
except
error:=2;
end;
end;

if error>0 then if fileexists(fname+'\title.bmp') then begin
try
if CACHED=true then fn:=docachedcheckandcopy(fname,'bmp') else fn:=fname+'\title.bmp';
res.Loadfromfile(fn);
//res.Assign(bmp);
//bmp.assign(res.graphic);
error:=0;
except
error:=2;
end;
end;

if error>0 then if fileexists(fname+'\title.bmp') then begin
try
if CACHED=true then fn:=docachedcheckandcopy(fname,'bmp') else fn:=fname+'\title.bmp';
res.Loadfromfile(fn);
//res.Assign(bmp);
//bmp.assign(res.graphic);
error:=0;
except
error:=2;
end;
end;

if error>0 then begin
extractalbumandaristfrompath(fname,album,artist);
generatecover(res,album+' '+artist);
//if error=1 then res.assign(errorbitmap_noimage);
//if error=2 then res.assign(errorbitmap_corrupt);
end;

if (resulted)=nil then resulted:=tbitmap.create;
resulted.assign(res);
res.SaveToFile(fname+'\title.bmp');
res.free;
jpeg.free;
end;

end.