Hallo Forum,
ich möchte euch gerne meine BMP Library vorstellen.
Ich habe sie geschrieben, damit man einfach BMP Daten in eine Bank einlesen,
manuell bearbeiten und speichern kann.
Ein weiterer Anlass war dieser Thread: https://www.blitzforum.de/foru...hp?t=36394
Den Großteil des Codes habe ich selber geschrieben, nur die Aufführung was wann kommt und einige Informationen zum BMP Dateiformat stammen aus der Software für den 3D Scanner von Benibaerenstark.
(Link: https://www.blitzforum.de/showcase/172/)
Funktionen:
Einlesen einer Bitmap in einen Type und einer Bank -- ~65ms
Anzeigen des Bildes, per WritePixelFast -- ~12ms
Speichern des Bildes aus dem Type/der Bank in eine Datei -- ~70ms
Löschen des Types/der Bank aus dem Speicher -- ~1ms
--Neue Version
Drehen von Bildern, bis jetzt nur 0,90,180,270° -- ~50ms
Spiegeln, horizontal, vertikal oder beides -- ~20ms
Farbinvertierung -- ~18ms
Code für die Lib:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN]
Type BMP_TImg Field bank Field breite,hoehe Field xpixm,ypixm Field bps Field FlaechenZahl Field Kompression Field usedColors Field importantColors End Type Dim BMP_Mirror(0,0,0)
Function BMP_Laden.BMP_TImg(Dateiname$) If Dateiname = "" Then Return Null Local stream = ReadFile(Dateiname) If Not stream Then RuntimeError "Bilddatei nicht gefunden!" If ReadByte(stream)<>66 Then RuntimeError "Keine BMP Datei!" If ReadByte(stream)<>77 Then RuntimeError "Keine BMP Datei!" Local BMP_Img.BMP_TImg=New BMP_TImg ReadInt(stream) ReadInt(stream) ReadInt(stream) ReadInt(stream) BMP_Img\breite=ReadInt(stream) BMP_Img\hoehe=ReadInt(stream) BMP_Img\FlaechenZahl=ReadShort(stream) BMP_Img\bps=ReadShort(stream) BMP_Img\Kompression=ReadInt(stream) ReadInt(stream) BMP_Img\xpixm=ReadInt(stream) BMP_Img\ypixm=ReadInt(stream) BMP_Img\usedColors=ReadInt(stream) BMP_Img\importantColors=ReadInt(stream) BMP_Img\bank=CreateBank(BMP_Img\breite*BMP_Img\hoehe*3) For i= 0 To (BMP_Img\breite*BMP_Img\hoehe*3)-1 PokeByte BMP_Img\bank,i, ReadByte (stream) Next Return BMP_Img.BMP_TImg End Function Function BMP_Zeichnen (BMP_Img.BMP_TImg,VerschiebungX=0,VerschiebungY=0) i=0 LockBuffer BackBuffer() For y =BMP_Img\hoehe-1 To 0 Step -1 For X=0 To BMP_Img\breite-1 b = PeekByte (BMP_Img\bank,i) i=i+1 g = PeekByte (BMP_Img\bank,i) i=i+1 r = PeekByte (BMP_Img\bank,i) i=i+1 rgb=a*$1000000 + r*$10000 + g*$100 + b WritePixelFast X+VerschiebungX,Y+VerschiebungY,rgb, BackBuffer() Next Next UnlockBuffer BackBuffer() End Function Function BMP_Speichern (BMP_Img.BMP_TImg,Pfad$="img.bmp") Stream = WriteFile (Pfad) WriteByte Stream,66 WriteByte Stream,77 WriteInt Stream,BMP_Img\breite*BMP_Img\hoehe*3+54 WriteInt Stream,0 WriteInt Stream,54 WriteInt Stream,40 WriteInt Stream,BMP_Img\breite WriteInt Stream,BMP_Img\hoehe WriteShort Stream,BMP_Img\FlaechenZahl WriteShort Stream,BMP_Img\bps WriteInt Stream,BMP_Img\Kompression WriteInt Stream,BMP_Img\breite*BMP_Img\hoehe*3 WriteInt Stream,BMP_Img\xpixm WriteInt Stream,BMP_Img\ypixm WriteInt Stream,BMP_Img\usedColors WriteInt Stream,BMP_Img\importantColors For i = 0 To (BMP_Img\breite*BMP_Img\hoehe*3)-1 WriteByte Stream,PeekByte(BMP_Img\bank,i) Next CloseFile Stream End Function Function BMP_Loeschen(BMP_Img.BMP_TImg) FreeBank BMP_Img\bank Delete BMP_Img.BMP_TImg Return True End Function
Function BMP_Drehen(BMP_Img.BMP_TImg,grad%=0) Dim BMP_Mirror(BMP_Img\breite,BMP_Img\hoehe,2) i=0 For y =BMP_Img\hoehe-1 To 0 Step -1 For X=0 To BMP_Img\breite-1 BMP_Mirror(X,Y,0)=PeekByte (BMP_Img\bank,i) i=i+1 BMP_Mirror(X,Y,1)=PeekByte (BMP_Img\bank,i) i=i+1 BMP_Mirror(X,Y,2)=PeekByte (BMP_Img\bank,i) i=i+1 Next Next Select grad Case 90,270 Local w,h w=BMP_Img\breite h=BMP_Img\hoehe BMP_Img\breite=h BMP_Img\hoehe=w i=0 For y =BMP_Img\hoehe-1 To 0 Step -1 For X=BMP_Img\breite-1 To 0 Step -1 PokeByte(BMP_Img\bank,i,BMP_Mirror(Y,X,0)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(Y,X,1)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(Y,X,2)) i=i+1 Next Next If grad=270 BMP_Spiegeln(BMP_Img.BMP_TImg,True,True) End If Case 180 BMP_Spiegeln(BMP_Img.BMP_TImg,True,True) Default Return End Select Dim BMP_Mirror(0,0,0) End Function Function BMP_Spiegeln(BMP_Img.BMP_TImg,horizontal=False,vertikal=False) If horizontal=False And vertikal=False Then Return 0 Local i=0 If horizontal=True And vertikal=True Local zwischenbank=CreateBank(BMP_Img\breite*BMP_Img\hoehe*3) For i = 0 To (BMP_Img\breite*BMP_Img\hoehe*3)-1 Step 3 PokeByte(zwischenbank,((BMP_Img\breite*BMP_Img\hoehe*3)-1)-i-2,PeekByte(BMP_Img\bank,i)) PokeByte(zwischenbank,((BMP_Img\breite*BMP_Img\hoehe*3)-1)-i-1,PeekByte(BMP_Img\bank,i+1)) PokeByte(zwischenbank,((BMP_Img\breite*BMP_Img\hoehe*3)-1)-i,PeekByte(BMP_Img\bank,i+2)) Next FreeBank BMP_Img\bank BMP_Img\bank=zwischenbank ElseIf horizontal=True Xor vertikal=True Dim BMP_Mirror(BMP_Img\breite,BMP_Img\hoehe,2) i=0 For y =BMP_Img\hoehe-1 To 0 Step -1 For X=0 To BMP_Img\breite-1 BMP_Mirror(X,Y,0)=PeekByte (BMP_Img\bank,i) i=i+1 BMP_Mirror(X,Y,1)=PeekByte (BMP_Img\bank,i) i=i+1 BMP_Mirror(X,Y,2)=PeekByte (BMP_Img\bank,i) i=i+1 Next Next If horizontal=True And vertikal=False i=0 For y =0 To BMP_Img\hoehe-1 For X=0 To BMP_Img\breite-1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,0)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,1)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,2)) i=i+1 Next Next ElseIf horizontal=False And vertikal=True i=0 For y =BMP_Img\hoehe-1 To 0 Step -1 For X=BMP_Img\breite-1 To 0 Step -1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,0)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,1)) i=i+1 PokeByte(BMP_Img\bank,i,BMP_Mirror(X,Y,2)) i=i+1 Next Next End If Dim BMP_Mirror(0,0,0) End If End Function Function BMP_Invertieren(BMP_Img.BMP_TImg) For i = 0 To (BMP_Img\breite*BMP_Img\hoehe*3)-1 PokeByte(BMP_Img\bank,i,255-PeekByte(BMP_Img\bank,i)) Next End Function
Ein Beispielcode:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Graphics 800,800,0,6 SetBuffer BackBuffer()
Include "BMP_Lib.bb"
Local testimg.BMP_TImg=BMP_Laden("Test.bmp") If testimg=Null Then RuntimeError "Konnte Bild nicht öffnen" BMP_Spiegeln(testimg,1,1) BMP_Invertieren(testimg) BMP_Drehen(testimg,270) BMP_Zeichnen(testimg)
Color 255,255,255 Text 1,1,"Fertig!" Text 1,30,testimg\breite Text 1,45,testimg\hoehe Text 1,60,testimg\bps Text 1,75,testimg\xpixm Text 1,90,testimg\ypixm Text 1,105,testimg\FlaechenZahl Text 1,120,testimg\Kompression Text 1,135,testimg\usedColors Text 1,150,testimg\importantColors Flip
BMP_Speichern(testimg,"Test2.bmp")
BMP_Loeschen(testimg) If testimg<>Null Then RuntimeError "!"
WaitKey
(Läuft natürlich nur mit einem Bild ^^)
Wer möchte kann sich auch noch diese Userlib integrieren,
dann werden die Befehle auch gehighlighted.
Code: [AUSKLAPPEN] [EINKLAPPEN] .lib " "
BMP_Laden%( Dateiname$ )
BMP_Zeichnen( BMP_Img, VerschiebungX%, VerschiebungY% )
BMP_Speichern( BMP_Img, Pfad$ )
BMP_Loeschen%( BMP_Img )
BMP_Drehen( BMP_Img, grad% )
BMP_Spiegeln( BMP_Img, horizontal%, vertikal% )
BMP_Invertieren( BMP_Img )
Und zu guter Letzt noch das Ganze als Download:
https://sites.google.com/site/...=0&d=1
Wer Fehler findet, bitte unbedingt sagen!
Der Code ist heute übern Tag enstanden,
ohne das ich mich vorher groß informiert habe
Viele Grüße,
count-doku
|