unit loadjpegorbmp; interface uses windows,jpeg,graphics,sysutils,extctrls,global; procedure loadjpegbmp(fname : string;var resulted : tbitmap);overload; procedure loadjpegbmp(fname : string;var resulted : tbitmap;ignore : boolean);overload; function errorwithbitmap(msg : string;artist, album : string) : tbitmap; procedure initerrorbitmaps; implementation uses main; type TRGBArray = array[Word] of TRGBTriple; pRGBArray = ^TRGBArray; 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; procedure ResizeOverSizeBitmap(SrcFn, DstFn,ext: string); var x, y: Integer; xP, yP: Integer; xP2, yP2: Integer; SrcLine1, SrcLine2: pRGBArray; t3: Integer; z, z2, iz2: Integer; DstLine: pRGBArray; DstGap: Integer; w1, w2, w3, w4: Integer; Src,Dst : TBitmap; JPG : TJpegImage; begin src:=Tbitmap.create(); loadjpegbmp(SrcFn,src,true); dst:=Tbitmap.create(); dst.width:=480; dst.height:=480; Src.PixelFormat := pf24Bit; Dst.PixelFormat := pf24Bit; DstLine := Dst.ScanLine[0]; DstGap := Integer(Dst.ScanLine[1]) - Integer(DstLine); xP2 := MulDiv(pred(Src.Width), 000, Dst.Width); yP2 := MulDiv(pred(Src.Height), 000, Dst.Height); yP := 0; for y := 0 to pred(Dst.Height) do begin xP := 0; SrcLine1 := Src.ScanLine[yP shr 16]; if (yP shr 16 < pred(Src.Height)) then SrcLine2 := Src.ScanLine[succ(yP shr 16)] else SrcLine2 := Src.ScanLine[yP shr 16]; z2 := succ(yP and $FFFF); iz2 := succ((not yp) and $FFFF); for x := 0 to pred(Dst.Width) do begin t3 := xP shr 16; z := xP and $FFFF; w2 := MulDiv(z, iz2, 000); w1 := iz2 - w2; w4 := MulDiv(z, z2, 000); w3 := z2 - w4; DstLine[x].rgbtRed := (SrcLine1[t3].rgbtRed * w1 + SrcLine1[t3 + 1].rgbtRed * w2 + SrcLine2[t3].rgbtRed * w3 + SrcLine2[t3 + 1].rgbtRed * w4) shr 16; DstLine[x].rgbtGreen := (SrcLine1[t3].rgbtGreen * w1 + SrcLine1[t3 + 1].rgbtGreen * w2 + SrcLine2[t3].rgbtGreen * w3 + SrcLine2[t3 + 1].rgbtGreen * w4) shr 16; DstLine[x].rgbtBlue := (SrcLine1[t3].rgbtBlue * w1 + SrcLine1[t3 + 1].rgbtBlue * w2 + SrcLine2[t3].rgbtBlue * w3 + SrcLine2[t3 + 1].rgbtBlue * w4) shr 16; Inc(xP, xP2); end; {for} Inc(yP, yP2); DstLine := pRGBArray(Integer(DstLine) + DstGap); end; {for} JPG:=TJpegImage.create(); JPG.assign(dst); JPG.CompressionQuality := 50; JPG.Compress; JPG.SaveToFile(DstFn+'.jpg'); JPG.Free; Src.Free; Dst.Free; end; procedure docachecopyresize(fn : string;nfn : string;ext : string); begin //if original is bigger than 40K resample it. //if (curSize>61440) or (ext='bmp') then ResizeOverSizeBitmap(fn,nfn,ext); //else copyfile(pchar(fn+'\title.'+ext),pchar(nfn+'\title.'+ext),false); end; function Get_File_Size(sFileToExamine: string): int64; var SearchRec: TSearchRec; sgPath: string; inRetval, I1: Integer; begin sgPath := ExpandFileName(sFileToExamine); try inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec); if inRetval = 0 then I1 := SearchRec.Size else I1 := -1; finally SysUtils.FindClose(SearchRec); end; Result := I1; end; function docachedcheckandcopy(fn,ext : string) : string; var nfn : string; begin if length(fn)>255 then nfn:=copy(nfn,length(nfn),255) else nfn:=fn; nfn:=maketempname(nfn); nfn:=cachepath+'\'+nfn; if DONTCHECKCACHE=false then begin //a:=Get_File_Size(fn+'\title.'+ext); //b:=Get_File_Size(nfn+'\title.'+ext); // if fileexists(nfn+'\title.'+ext) then if (a<>b) then docachecopyresize(fn,nfn,ext,a); if not(fileexists(nfn+'.jpg')) then docachecopyresize(fn,nfn,ext) else if (FileAge(fn)<FileAge(nfn)) then docachecopyresize(fn,nfn,ext); end else end; result:=nfn+'.jpg'; end; procedure loadjpegbmp_(fname : string;var resulted : tbitmap;ignoreCache : boolean); var JPEG : TJpegImage; res : Tbitmap; error : integer; artist,album : string; fn: string; begin error := 1; res:=Tbitmap.create; JPEG:=TJpegImage.Create; if ((CACHED=true)and(ignoreCache=false)) then begin fn:=docachedcheckandcopy(fname,'jpg'); try jpeg.loadfromfile(fn); res.assign(jpeg); error:=0; except error:=2; end; end; if error>0 then begin if fileexists(fname+'\title.jpg') then begin try fn:=fname+'\title.jpg'; if ((CACHED=true)and(ignoreCache=false)) then fn:=docachedcheckandcopy(fname,'jpg'); if fileexists(fname+'\folder.jpg') then deletefile(fname+'\folder.jpg'); copyfile(pchar(fname+'\title.jpg'),pchar(fname+'\folder.jpg'),false); 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 fn:=fname+'\title.jpeg'; if ((CACHED=true)and(ignoreCache=false)) then fn:=docachedcheckandcopy(fname,'jpeg'); jpeg.loadfromfile(fn); res.assign(jpeg); error:=0; except error:=2; end; end; if error>0 then if fileexists(fname+'\folder.jpg') then begin try fn:=fname+'\folder.jpg'; if ((CACHED=true)and(ignoreCache=false)) then fn:=docachedcheckandcopy(fname,'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 fn:=fname+'\title.bmp'; if ((CACHED=true)and(ignoreCache=false)) then fn:=docachedcheckandcopy(fname,'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 fn:=fname+'\title.bmp'; if ((CACHED=true)and(ignoreCache=false)) then fn:=docachedcheckandcopy(fname,'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; end; if (resulted)=nil then resulted:=tbitmap.create; resulted.assign(res); //res.SaveToFile(fname+'\title.bmp'); res.free; jpeg.free; end; procedure loadjpegbmp(fname : string;var resulted : tbitmap); begin loadjpegbmp_(fname,resulted,false); end; procedure loadjpegbmp(fname : string;var resulted : tbitmap;ignore : boolean); begin loadjpegbmp_(fname,resulted,ignore); end; end.